1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Lowers the AST to the HIR.
13 //! Since the AST and HIR are fairly similar, this is mostly a simple procedure,
14 //! much like a fold. Where lowering involves a bit more work things get more
15 //! interesting and there are some invariants you should know about. These mostly
16 //! concern spans and ids.
18 //! Spans are assigned to AST nodes during parsing and then are modified during
19 //! expansion to indicate the origin of a node and the process it went through
20 //! being expanded. Ids are assigned to AST nodes just before lowering.
22 //! For the simpler lowering steps, ids and spans should be preserved. Unlike
23 //! expansion we do not preserve the process of lowering in the spans, so spans
24 //! should not be modified here. When creating a new node (as opposed to
25 //! 'folding' an existing one), then you create a new id using `next_id()`.
27 //! You must ensure that ids are unique. That means that you should only use the
28 //! id from an AST node in a single HIR node (you can assume that AST node ids
29 //! are unique). Every new node must have a unique id. Avoid cloning HIR nodes.
30 //! If you do, you must then set the new node's id to a fresh one.
32 //! Spans are used for error messages and for tools to map semantics back to
33 //! source code. It is therefore not as important with spans as ids to be strict
34 //! about use (you can't break the compiler by screwing up a span). Obviously, a
35 //! HIR node can only have a single span. But multiple nodes can have the same
36 //! span and spans don't need to be kept in order, etc. Where code is preserved
37 //! by lowering, it should have the same span as in the AST. Where HIR nodes are
38 //! new it is probably best to give a span for the whole AST node being lowered.
39 //! All nodes should have real spans, don't use dummy spans. Tools are likely to
40 //! get confused if the spans from leaf AST nodes occur in multiple places
41 //! in the HIR, especially for multiple identifiers.
43 use dep_graph::DepGraph;
46 use hir::map::{Definitions, DefKey, DefPathData};
47 use hir::def_id::{DefIndex, DefId, CRATE_DEF_INDEX, DefIndexAddressSpace};
48 use hir::def::{Def, PathResolution};
49 use lint::builtin::PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES;
50 use middle::cstore::CrateStore;
51 use rustc_data_structures::indexed_vec::IndexVec;
53 use util::common::FN_OUTPUT_NAME;
54 use util::nodemap::{DefIdMap, FxHashMap, NodeMap};
56 use std::collections::{BTreeMap, HashSet};
63 use syntax::ext::hygiene::{Mark, SyntaxContext};
65 use syntax::codemap::{self, respan, Spanned, CompilerDesugaringKind};
66 use syntax::std_inject;
67 use syntax::symbol::{Symbol, keywords};
68 use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
69 use syntax::parse::token::Token;
70 use syntax::util::small_vector::SmallVector;
71 use syntax::visit::{self, Visitor};
74 const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
76 pub struct LoweringContext<'a> {
77 crate_root: Option<&'static str>,
79 // Use to assign ids to hir nodes that do not directly correspond to an ast node
82 cstore: &'a CrateStore,
84 // As we walk the AST we must keep track of the current 'parent' def id (in
85 // the form of a DefIndex) so that if we create a new node which introduces
86 // a definition, then we can properly create the def id.
87 parent_def: Option<DefIndex>,
88 resolver: &'a mut Resolver,
89 name_map: FxHashMap<Ident, Name>,
91 /// The items being lowered are collected here.
92 items: BTreeMap<NodeId, hir::Item>,
94 trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
95 impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
96 bodies: BTreeMap<hir::BodyId, hir::Body>,
97 exported_macros: Vec<hir::MacroDef>,
99 trait_impls: BTreeMap<DefId, Vec<NodeId>>,
100 trait_auto_impl: BTreeMap<DefId, NodeId>,
104 catch_scopes: Vec<NodeId>,
105 loop_scopes: Vec<NodeId>,
106 is_in_loop_condition: bool,
107 is_in_trait_impl: bool,
109 type_def_lifetime_params: DefIdMap<usize>,
111 current_hir_id_owner: Vec<(DefIndex, u32)>,
112 item_local_id_counters: NodeMap<u32>,
113 node_id_to_hir_id: IndexVec<NodeId, hir::HirId>,
117 /// Resolve a hir path generated by the lowerer when expanding `for`, `if let`, etc.
118 fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool);
120 /// Obtain the resolution for a node id
121 fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
123 /// We must keep the set of definitions up to date as we add nodes that weren't in the AST.
124 /// This should only return `None` during testing.
125 fn definitions(&mut self) -> &mut Definitions;
128 #[derive(Clone, Copy, Debug)]
129 enum ImplTraitContext {
130 /// Treat `impl Trait` as shorthand for a new universal generic parameter.
131 /// Example: `fn foo(x: impl Debug)`, where `impl Debug` is conceptually
132 /// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
134 /// We store a DefId here so we can look up necessary information later
137 /// Treat `impl Trait` as shorthand for a new universal existential parameter.
138 /// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
139 /// equivalent to a fresh existential parameter like `abstract type T; fn foo() -> T`.
142 /// `impl Trait` is not accepted in this position.
146 pub fn lower_crate(sess: &Session,
148 dep_graph: &DepGraph,
150 resolver: &mut Resolver)
152 // We're constructing the HIR here; we don't care what we will
153 // read, since we haven't even constructed the *input* to
155 let _ignore = dep_graph.in_ignore();
158 crate_root: std_inject::injected_crate_name(krate),
163 name_map: FxHashMap(),
164 items: BTreeMap::new(),
165 trait_items: BTreeMap::new(),
166 impl_items: BTreeMap::new(),
167 bodies: BTreeMap::new(),
168 trait_impls: BTreeMap::new(),
169 trait_auto_impl: BTreeMap::new(),
170 exported_macros: Vec::new(),
171 catch_scopes: Vec::new(),
172 loop_scopes: Vec::new(),
173 is_in_loop_condition: false,
174 type_def_lifetime_params: DefIdMap(),
175 current_hir_id_owner: vec![(CRATE_DEF_INDEX, 0)],
176 item_local_id_counters: NodeMap(),
177 node_id_to_hir_id: IndexVec::new(),
179 is_in_trait_impl: false,
183 #[derive(Copy, Clone, PartialEq, Eq)]
185 /// Any path in a type context.
187 /// The `module::Type` in `module::Type::method` in an expression.
191 struct LoweredNodeId {
196 enum ParenthesizedGenericArgs {
202 impl<'a> LoweringContext<'a> {
203 fn lower_crate(mut self, c: &Crate) -> hir::Crate {
204 /// Full-crate AST visitor that inserts into a fresh
205 /// `LoweringContext` any information that may be
206 /// needed from arbitrary locations in the crate.
207 /// E.g. The number of lifetime generic parameters
208 /// declared for every type and trait definition.
209 struct MiscCollector<'lcx, 'interner: 'lcx> {
210 lctx: &'lcx mut LoweringContext<'interner>,
213 impl<'lcx, 'interner> Visitor<'lcx> for MiscCollector<'lcx, 'interner> {
214 fn visit_item(&mut self, item: &'lcx Item) {
215 self.lctx.allocate_hir_id_counter(item.id, item);
218 ItemKind::Struct(_, ref generics) |
219 ItemKind::Union(_, ref generics) |
220 ItemKind::Enum(_, ref generics) |
221 ItemKind::Ty(_, ref generics) |
222 ItemKind::Trait(_, _, ref generics, ..) => {
223 let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
224 let count = generics.lifetimes.len();
225 self.lctx.type_def_lifetime_params.insert(def_id, count);
229 visit::walk_item(self, item);
232 fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
233 self.lctx.allocate_hir_id_counter(item.id, item);
234 visit::walk_trait_item(self, item);
237 fn visit_impl_item(&mut self, item: &'lcx ImplItem) {
238 self.lctx.allocate_hir_id_counter(item.id, item);
239 visit::walk_impl_item(self, item);
243 struct ItemLowerer<'lcx, 'interner: 'lcx> {
244 lctx: &'lcx mut LoweringContext<'interner>,
247 impl<'lcx, 'interner> ItemLowerer<'lcx, 'interner> {
248 fn with_trait_impl_ref<F>(&mut self, trait_impl_ref: &Option<TraitRef>, f: F)
249 where F: FnOnce(&mut Self)
251 let old = self.lctx.is_in_trait_impl;
252 self.lctx.is_in_trait_impl = if let &None = trait_impl_ref {
258 self.lctx.is_in_trait_impl = old;
262 impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
263 fn visit_item(&mut self, item: &'lcx Item) {
264 let mut item_lowered = true;
265 self.lctx.with_hir_id_owner(item.id, |lctx| {
266 if let Some(hir_item) = lctx.lower_item(item) {
267 lctx.items.insert(item.id, hir_item);
269 item_lowered = false;
274 if let ItemKind::Impl(_,_,_,_,ref opt_trait_ref,_,_) = item.node {
275 self.with_trait_impl_ref(opt_trait_ref, |this| {
276 visit::walk_item(this, item)
279 visit::walk_item(self, item);
284 fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
285 self.lctx.with_hir_id_owner(item.id, |lctx| {
286 let id = hir::TraitItemId { node_id: item.id };
287 let hir_item = lctx.lower_trait_item(item);
288 lctx.trait_items.insert(id, hir_item);
291 visit::walk_trait_item(self, item);
294 fn visit_impl_item(&mut self, item: &'lcx ImplItem) {
295 self.lctx.with_hir_id_owner(item.id, |lctx| {
296 let id = hir::ImplItemId { node_id: item.id };
297 let hir_item = lctx.lower_impl_item(item);
298 lctx.impl_items.insert(id, hir_item);
300 visit::walk_impl_item(self, item);
304 self.lower_node_id(CRATE_NODE_ID);
305 debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == hir::CRATE_HIR_ID);
307 visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c);
308 visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c);
310 let module = self.lower_mod(&c.module);
311 let attrs = self.lower_attrs(&c.attrs);
312 let body_ids = body_ids(&self.bodies);
316 .init_node_id_to_hir_id_mapping(self.node_id_to_hir_id);
322 exported_macros: hir::HirVec::from(self.exported_macros),
324 trait_items: self.trait_items,
325 impl_items: self.impl_items,
328 trait_impls: self.trait_impls,
329 trait_auto_impl: self.trait_auto_impl,
333 fn allocate_hir_id_counter<T: Debug>(&mut self,
336 if self.item_local_id_counters.insert(owner, 0).is_some() {
337 bug!("Tried to allocate item_local_id_counter for {:?} twice", debug);
339 // Always allocate the first HirId for the owner itself
340 self.lower_node_id_with_owner(owner, owner);
343 fn lower_node_id_generic<F>(&mut self,
347 where F: FnOnce(&mut Self) -> hir::HirId
349 if ast_node_id == DUMMY_NODE_ID {
350 return LoweredNodeId {
351 node_id: DUMMY_NODE_ID,
352 hir_id: hir::DUMMY_HIR_ID,
356 let min_size = ast_node_id.as_usize() + 1;
358 if min_size > self.node_id_to_hir_id.len() {
359 self.node_id_to_hir_id.resize(min_size, hir::DUMMY_HIR_ID);
362 let existing_hir_id = self.node_id_to_hir_id[ast_node_id];
364 if existing_hir_id == hir::DUMMY_HIR_ID {
365 // Generate a new HirId
366 let hir_id = alloc_hir_id(self);
367 self.node_id_to_hir_id[ast_node_id] = hir_id;
369 node_id: ast_node_id,
374 node_id: ast_node_id,
375 hir_id: existing_hir_id,
380 fn with_hir_id_owner<F>(&mut self, owner: NodeId, f: F)
381 where F: FnOnce(&mut Self)
383 let counter = self.item_local_id_counters
384 .insert(owner, HIR_ID_COUNTER_LOCKED)
386 let def_index = self.resolver.definitions().opt_def_index(owner).unwrap();
387 self.current_hir_id_owner.push((def_index, counter));
389 let (new_def_index, new_counter) = self.current_hir_id_owner.pop().unwrap();
391 debug_assert!(def_index == new_def_index);
392 debug_assert!(new_counter >= counter);
394 let prev = self.item_local_id_counters.insert(owner, new_counter).unwrap();
395 debug_assert!(prev == HIR_ID_COUNTER_LOCKED);
398 /// This method allocates a new HirId for the given NodeId and stores it in
399 /// the LoweringContext's NodeId => HirId map.
400 /// Take care not to call this method if the resulting HirId is then not
401 /// actually used in the HIR, as that would trigger an assertion in the
402 /// HirIdValidator later on, which makes sure that all NodeIds got mapped
403 /// properly. Calling the method twice with the same NodeId is fine though.
404 fn lower_node_id(&mut self, ast_node_id: NodeId) -> LoweredNodeId {
405 self.lower_node_id_generic(ast_node_id, |this| {
406 let &mut (def_index, ref mut local_id_counter) = this.current_hir_id_owner
409 let local_id = *local_id_counter;
410 *local_id_counter += 1;
413 local_id: hir::ItemLocalId(local_id),
418 fn lower_node_id_with_owner(&mut self,
422 self.lower_node_id_generic(ast_node_id, |this| {
423 let local_id_counter = this.item_local_id_counters
426 let local_id = *local_id_counter;
428 // We want to be sure not to modify the counter in the map while it
429 // is also on the stack. Otherwise we'll get lost updates when writing
430 // back from the stack to the map.
431 debug_assert!(local_id != HIR_ID_COUNTER_LOCKED);
433 *local_id_counter += 1;
434 let def_index = this.resolver.definitions().opt_def_index(owner).unwrap();
438 local_id: hir::ItemLocalId(local_id),
443 fn record_body(&mut self, value: hir::Expr, decl: Option<&FnDecl>)
445 let body = hir::Body {
446 arguments: decl.map_or(hir_vec![], |decl| {
447 decl.inputs.iter().map(|x| self.lower_arg(x)).collect()
449 is_generator: self.is_generator,
453 self.bodies.insert(id, body);
457 fn next_id(&mut self) -> LoweredNodeId {
458 self.lower_node_id(self.sess.next_node_id())
461 fn expect_full_def(&mut self, id: NodeId) -> Def {
462 self.resolver.get_resolution(id).map_or(Def::Err, |pr| {
463 if pr.unresolved_segments() != 0 {
464 bug!("path not fully resolved: {:?}", pr);
470 fn diagnostic(&self) -> &errors::Handler {
471 self.sess.diagnostic()
474 fn str_to_ident(&self, s: &'static str) -> Name {
478 fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, span: Span) -> Span
480 let mark = Mark::fresh(Mark::root());
481 mark.set_expn_info(codemap::ExpnInfo {
483 callee: codemap::NameAndSpan {
484 format: codemap::CompilerDesugaring(reason),
486 allow_internal_unstable: true,
487 allow_internal_unsafe: false,
490 span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
493 fn with_catch_scope<T, F>(&mut self, catch_id: NodeId, f: F) -> T
494 where F: FnOnce(&mut LoweringContext) -> T
496 let len = self.catch_scopes.len();
497 self.catch_scopes.push(catch_id);
499 let result = f(self);
500 assert_eq!(len + 1, self.catch_scopes.len(),
501 "catch scopes should be added and removed in stack order");
503 self.catch_scopes.pop().unwrap();
508 fn lower_body<F>(&mut self, decl: Option<&FnDecl>, f: F) -> hir::BodyId
509 where F: FnOnce(&mut LoweringContext) -> hir::Expr
511 let prev = mem::replace(&mut self.is_generator, false);
512 let result = f(self);
513 let r = self.record_body(result, decl);
514 self.is_generator = prev;
518 fn with_loop_scope<T, F>(&mut self, loop_id: NodeId, f: F) -> T
519 where F: FnOnce(&mut LoweringContext) -> T
521 // We're no longer in the base loop's condition; we're in another loop.
522 let was_in_loop_condition = self.is_in_loop_condition;
523 self.is_in_loop_condition = false;
525 let len = self.loop_scopes.len();
526 self.loop_scopes.push(loop_id);
528 let result = f(self);
529 assert_eq!(len + 1, self.loop_scopes.len(),
530 "Loop scopes should be added and removed in stack order");
532 self.loop_scopes.pop().unwrap();
534 self.is_in_loop_condition = was_in_loop_condition;
539 fn with_loop_condition_scope<T, F>(&mut self, f: F) -> T
540 where F: FnOnce(&mut LoweringContext) -> T
542 let was_in_loop_condition = self.is_in_loop_condition;
543 self.is_in_loop_condition = true;
545 let result = f(self);
547 self.is_in_loop_condition = was_in_loop_condition;
552 fn with_new_scopes<T, F>(&mut self, f: F) -> T
553 where F: FnOnce(&mut LoweringContext) -> T
555 let was_in_loop_condition = self.is_in_loop_condition;
556 self.is_in_loop_condition = false;
558 let catch_scopes = mem::replace(&mut self.catch_scopes, Vec::new());
559 let loop_scopes = mem::replace(&mut self.loop_scopes, Vec::new());
560 let result = f(self);
561 self.catch_scopes = catch_scopes;
562 self.loop_scopes = loop_scopes;
564 self.is_in_loop_condition = was_in_loop_condition;
569 fn with_parent_def<T, F>(&mut self, parent_id: NodeId, f: F) -> T
570 where F: FnOnce(&mut LoweringContext) -> T
572 let old_def = self.parent_def;
574 let defs = self.resolver.definitions();
575 Some(defs.opt_def_index(parent_id).unwrap())
578 let result = f(self);
580 self.parent_def = old_def;
584 fn def_key(&mut self, id: DefId) -> DefKey {
586 self.resolver.definitions().def_key(id.index)
588 self.cstore.def_key(id)
592 fn lower_ident(&mut self, ident: Ident) -> Name {
593 let ident = ident.modern();
594 if ident.ctxt == SyntaxContext::empty() {
597 *self.name_map.entry(ident).or_insert_with(|| Symbol::from_ident(ident))
600 fn lower_opt_sp_ident(&mut self, o_id: Option<Spanned<Ident>>) -> Option<Spanned<Name>> {
601 o_id.map(|sp_ident| respan(sp_ident.span, sp_ident.node.name))
604 fn lower_loop_destination(&mut self, destination: Option<(NodeId, Spanned<Ident>)>)
608 Some((id, label_ident)) => {
609 let target = if let Def::Label(loop_id) = self.expect_full_def(id) {
610 hir::LoopIdResult::Ok(self.lower_node_id(loop_id).node_id)
612 hir::LoopIdResult::Err(hir::LoopIdError::UnresolvedLabel)
615 ident: Some(label_ident),
616 target_id: hir::ScopeTarget::Loop(target),
620 let loop_id = self.loop_scopes
622 .map(|innermost_loop_id| *innermost_loop_id);
626 target_id: hir::ScopeTarget::Loop(
627 loop_id.map(|id| Ok(self.lower_node_id(id).node_id))
628 .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
635 fn lower_attrs(&mut self, attrs: &Vec<Attribute>) -> hir::HirVec<Attribute> {
636 attrs.iter().map(|a| self.lower_attr(a)).collect::<Vec<_>>().into()
639 fn lower_attr(&mut self, attr: &Attribute) -> Attribute {
643 path: attr.path.clone(),
644 tokens: self.lower_token_stream(attr.tokens.clone()),
645 is_sugared_doc: attr.is_sugared_doc,
650 fn lower_token_stream(&mut self, tokens: TokenStream) -> TokenStream {
652 .flat_map(|tree| self.lower_token_tree(tree).into_trees())
656 fn lower_token_tree(&mut self, tree: TokenTree) -> TokenStream {
658 TokenTree::Token(span, token) => {
659 self.lower_token(token, span)
661 TokenTree::Delimited(span, delimited) => {
662 TokenTree::Delimited(span, Delimited {
663 delim: delimited.delim,
664 tts: self.lower_token_stream(delimited.tts.into()).into(),
670 fn lower_token(&mut self, token: Token, span: Span) -> TokenStream {
672 Token::Interpolated(_) => {}
673 other => return TokenTree::Token(span, other).into(),
676 let tts = token.interpolated_to_tokenstream(&self.sess.parse_sess, span);
677 self.lower_token_stream(tts)
680 fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
682 attrs: self.lower_attrs(&arm.attrs),
683 pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
684 guard: arm.guard.as_ref().map(|ref x| P(self.lower_expr(x))),
685 body: P(self.lower_expr(&arm.body)),
689 fn lower_ty_binding(&mut self, b: &TypeBinding, itctx: ImplTraitContext) -> hir::TypeBinding {
691 id: self.lower_node_id(b.id).node_id,
692 name: self.lower_ident(b.ident),
693 ty: self.lower_ty(&b.ty, itctx),
698 fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext) -> P<hir::Ty> {
699 let kind = match t.node {
700 TyKind::Infer => hir::TyInfer,
701 TyKind::Err => hir::TyErr,
702 TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty, itctx)),
703 TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt, itctx)),
704 TyKind::Rptr(ref region, ref mt) => {
705 let span = t.span.with_hi(t.span.lo());
706 let lifetime = match *region {
707 Some(ref lt) => self.lower_lifetime(lt),
708 None => self.elided_lifetime(span)
710 hir::TyRptr(lifetime, self.lower_mt(mt, itctx))
712 TyKind::BareFn(ref f) => {
713 hir::TyBareFn(P(hir::BareFnTy {
714 lifetimes: self.lower_lifetime_defs(&f.lifetimes),
715 unsafety: self.lower_unsafety(f.unsafety),
717 decl: self.lower_fn_decl(&f.decl, None, false),
718 arg_names: self.lower_fn_args_to_names(&f.decl),
721 TyKind::Never => hir::TyNever,
722 TyKind::Tup(ref tys) => {
723 hir::TyTup(tys.iter().map(|ty| self.lower_ty(ty, itctx)).collect())
725 TyKind::Paren(ref ty) => {
726 return self.lower_ty(ty, itctx);
728 TyKind::Path(ref qself, ref path) => {
729 let id = self.lower_node_id(t.id);
730 let qpath = self.lower_qpath(t.id, qself, path, ParamMode::Explicit, itctx);
731 return self.ty_path(id, t.span, qpath);
733 TyKind::ImplicitSelf => {
734 hir::TyPath(hir::QPath::Resolved(None, P(hir::Path {
735 def: self.expect_full_def(t.id),
737 hir::PathSegment::from_name(keywords::SelfType.name())
742 TyKind::Array(ref ty, ref length) => {
743 let length = self.lower_body(None, |this| this.lower_expr(length));
744 hir::TyArray(self.lower_ty(ty, itctx), length)
746 TyKind::Typeof(ref expr) => {
747 let expr = self.lower_body(None, |this| this.lower_expr(expr));
750 TyKind::TraitObject(ref bounds, ..) => {
751 let mut lifetime_bound = None;
752 let bounds = bounds.iter().filter_map(|bound| {
754 TraitTyParamBound(ref ty, TraitBoundModifier::None) => {
755 Some(self.lower_poly_trait_ref(ty, itctx))
757 TraitTyParamBound(_, TraitBoundModifier::Maybe) => None,
758 RegionTyParamBound(ref lifetime) => {
759 if lifetime_bound.is_none() {
760 lifetime_bound = Some(self.lower_lifetime(lifetime));
766 let lifetime_bound = lifetime_bound.unwrap_or_else(|| {
767 self.elided_lifetime(t.span)
769 hir::TyTraitObject(bounds, lifetime_bound)
771 TyKind::ImplTrait(ref bounds) => {
772 use syntax::feature_gate::{emit_feature_err, GateIssue};
774 ImplTraitContext::Existential => {
775 let has_feature = self.sess.features.borrow().conservative_impl_trait;
776 if !t.span.allows_unstable() && !has_feature {
777 emit_feature_err(&self.sess.parse_sess, "conservative_impl_trait",
778 t.span, GateIssue::Language,
779 "`impl Trait` in return position is experimental");
781 let def_index = self.resolver.definitions().opt_def_index(t.id).unwrap();
782 let hir_bounds = self.lower_bounds(bounds, itctx);
783 let (lifetimes, lifetime_defs) =
784 self.lifetimes_from_impl_trait_bounds(def_index, &hir_bounds);
786 hir::TyImplTraitExistential(hir::ExistTy {
787 generics: hir::Generics {
788 lifetimes: lifetime_defs,
789 // Type parameters are taken from environment:
790 ty_params: Vec::new().into(),
791 where_clause: hir::WhereClause {
792 id: self.next_id().node_id,
793 predicates: Vec::new().into(),
800 ImplTraitContext::Universal(def_id) => {
801 let has_feature = self.sess.features.borrow().universal_impl_trait;
802 if !t.span.allows_unstable() && !has_feature {
803 emit_feature_err(&self.sess.parse_sess, "universal_impl_trait",
804 t.span, GateIssue::Language,
805 "`impl Trait` in argument position is experimental");
807 hir::TyImplTraitUniversal(def_id, self.lower_bounds(bounds, itctx))
809 ImplTraitContext::Disallowed => {
810 span_err!(self.sess, t.span, E0562,
811 "`impl Trait` not allowed outside of function \
812 and inherent method return types");
817 TyKind::Mac(_) => panic!("TyMac should have been expanded by now."),
820 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(t.id);
829 fn lifetimes_from_impl_trait_bounds(
831 parent_index: DefIndex,
832 bounds: &hir::TyParamBounds
833 ) -> (HirVec<hir::Lifetime>, HirVec<hir::LifetimeDef>) {
835 // This visitor walks over impl trait bounds and creates defs for all lifetimes which
836 // appear in the bounds, excluding lifetimes that are created within the bounds.
837 // e.g. 'a, 'b, but not 'c in `impl for<'c> SomeTrait<'a, 'b, 'c>`
838 struct ImplTraitLifetimeCollector<'r, 'a: 'r> {
839 context: &'r mut LoweringContext<'a>,
841 currently_bound_lifetimes: Vec<Name>,
842 already_defined_lifetimes: HashSet<Name>,
843 output_lifetimes: Vec<hir::Lifetime>,
844 output_lifetime_defs: Vec<hir::LifetimeDef>,
847 impl<'r, 'a: 'r, 'v> hir::intravisit::Visitor<'v> for ImplTraitLifetimeCollector<'r, 'a> {
848 fn nested_visit_map<'this>(&'this mut self)
849 -> hir::intravisit::NestedVisitorMap<'this, 'v> {
850 hir::intravisit::NestedVisitorMap::None
853 fn visit_poly_trait_ref(&mut self,
854 polytr: &'v hir::PolyTraitRef,
855 _: hir::TraitBoundModifier) {
856 let old_len = self.currently_bound_lifetimes.len();
858 // Record the introduction of 'a in `for<'a> ...`
859 for lt_def in &polytr.bound_lifetimes {
860 // Introduce lifetimes one at a time so that we can handle
861 // cases like `fn foo<'d>() -> impl for<'a, 'b: 'a, 'c: 'b + 'd> ...`
862 if let hir::LifetimeName::Name(name) = lt_def.lifetime.name {
863 self.currently_bound_lifetimes.push(name);
866 // Visit the lifetime bounds
867 for lt_bound in <_def.bounds {
868 self.visit_lifetime(<_bound);
872 hir::intravisit::walk_trait_ref(self, &polytr.trait_ref);
874 self.currently_bound_lifetimes.truncate(old_len);
877 fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
878 // Exclude '_, 'static, and elided lifetimes (there should be no elided lifetimes)
879 if let hir::LifetimeName::Name(lifetime_name) = lifetime.name {
880 if !self.currently_bound_lifetimes.contains(&lifetime_name) &&
881 !self.already_defined_lifetimes.contains(&lifetime_name)
883 self.already_defined_lifetimes.insert(lifetime_name);
884 let name = hir::LifetimeName::Name(lifetime_name);
886 self.output_lifetimes.push(hir::Lifetime {
887 id: self.context.next_id().node_id,
892 let def_node_id = self.context.next_id().node_id;
893 self.context.resolver.definitions().create_def_with_parent(
896 DefPathData::LifetimeDef(lifetime_name.as_str()),
897 DefIndexAddressSpace::High,
900 let def_lifetime = hir::Lifetime {
905 self.output_lifetime_defs.push(hir::LifetimeDef {
906 lifetime: def_lifetime,
907 bounds: Vec::new().into(),
908 pure_wrt_drop: false,
915 let mut lifetime_collector = ImplTraitLifetimeCollector {
917 parent: parent_index,
918 currently_bound_lifetimes: Vec::new(),
919 already_defined_lifetimes: HashSet::new(),
920 output_lifetimes: Vec::new(),
921 output_lifetime_defs: Vec::new(),
924 for bound in bounds {
925 hir::intravisit::walk_ty_param_bound(&mut lifetime_collector, &bound);
929 lifetime_collector.output_lifetimes.into(),
930 lifetime_collector.output_lifetime_defs.into()
934 fn lower_foreign_mod(&mut self, fm: &ForeignMod) -> hir::ForeignMod {
937 items: fm.items.iter().map(|x| self.lower_foreign_item(x)).collect(),
941 fn lower_global_asm(&mut self, ga: &GlobalAsm) -> P<hir::GlobalAsm> {
948 fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
950 node: hir::Variant_ {
951 name: v.node.name.name,
952 attrs: self.lower_attrs(&v.node.attrs),
953 data: self.lower_variant_data(&v.node.data),
954 disr_expr: v.node.disr_expr.as_ref().map(|e| {
955 self.lower_body(None, |this| this.lower_expr(e))
962 fn lower_qpath(&mut self,
964 qself: &Option<QSelf>,
966 param_mode: ParamMode,
967 itctx: ImplTraitContext)
969 let qself_position = qself.as_ref().map(|q| q.position);
970 let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx));
972 let resolution = self.resolver.get_resolution(id)
973 .unwrap_or(PathResolution::new(Def::Err));
975 let proj_start = p.segments.len() - resolution.unresolved_segments();
976 let path = P(hir::Path {
977 def: resolution.base_def(),
978 segments: p.segments[..proj_start].iter().enumerate().map(|(i, segment)| {
979 let param_mode = match (qself_position, param_mode) {
980 (Some(j), ParamMode::Optional) if i < j => {
981 // This segment is part of the trait path in a
982 // qualified path - one of `a`, `b` or `Trait`
983 // in `<X as a::b::Trait>::T::U::method`.
989 // Figure out if this is a type/trait segment,
990 // which may need lifetime elision performed.
991 let parent_def_id = |this: &mut Self, def_id: DefId| {
994 index: this.def_key(def_id).parent.expect("missing parent")
997 let type_def_id = match resolution.base_def() {
998 Def::AssociatedTy(def_id) if i + 2 == proj_start => {
999 Some(parent_def_id(self, def_id))
1001 Def::Variant(def_id) if i + 1 == proj_start => {
1002 Some(parent_def_id(self, def_id))
1004 Def::Struct(def_id) |
1005 Def::Union(def_id) |
1007 Def::TyAlias(def_id) |
1008 Def::Trait(def_id) if i + 1 == proj_start => Some(def_id),
1011 let parenthesized_generic_args = match resolution.base_def() {
1012 // `a::b::Trait(Args)`
1013 Def::Trait(..) if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
1014 // `a::b::Trait(Args)::TraitItem`
1016 Def::AssociatedConst(..) |
1017 Def::AssociatedTy(..) if i + 2 == proj_start => ParenthesizedGenericArgs::Ok,
1018 // Avoid duplicated errors
1019 Def::Err => ParenthesizedGenericArgs::Ok,
1021 Def::Struct(..) | Def::Enum(..) | Def::Union(..) | Def::TyAlias(..) |
1022 Def::Variant(..) if i + 1 == proj_start => ParenthesizedGenericArgs::Err,
1023 // A warning for now, for compatibility reasons
1024 _ => ParenthesizedGenericArgs::Warn,
1027 let num_lifetimes = type_def_id.map_or(0, |def_id| {
1028 if let Some(&n) = self.type_def_lifetime_params.get(&def_id) {
1031 assert!(!def_id.is_local());
1033 .item_generics_cloned_untracked(def_id, self.sess)
1036 self.type_def_lifetime_params.insert(def_id, n);
1039 self.lower_path_segment(p.span, segment, param_mode, num_lifetimes,
1040 parenthesized_generic_args, itctx)
1045 // Simple case, either no projections, or only fully-qualified.
1046 // E.g. `std::mem::size_of` or `<I as Iterator>::Item`.
1047 if resolution.unresolved_segments() == 0 {
1048 return hir::QPath::Resolved(qself, path);
1051 // Create the innermost type that we're projecting from.
1052 let mut ty = if path.segments.is_empty() {
1053 // If the base path is empty that means there exists a
1054 // syntactical `Self`, e.g. `&i32` in `<&i32>::clone`.
1055 qself.expect("missing QSelf for <T>::...")
1057 // Otherwise, the base path is an implicit `Self` type path,
1058 // e.g. `Vec` in `Vec::new` or `<I as Iterator>::Item` in
1059 // `<I as Iterator>::Item::default`.
1060 let new_id = self.next_id();
1061 self.ty_path(new_id, p.span, hir::QPath::Resolved(qself, path))
1064 // Anything after the base path are associated "extensions",
1065 // out of which all but the last one are associated types,
1066 // e.g. for `std::vec::Vec::<T>::IntoIter::Item::clone`:
1067 // * base path is `std::vec::Vec<T>`
1068 // * "extensions" are `IntoIter`, `Item` and `clone`
1069 // * type nodes are:
1070 // 1. `std::vec::Vec<T>` (created above)
1071 // 2. `<std::vec::Vec<T>>::IntoIter`
1072 // 3. `<<std::vec::Vec<T>>::IntoIter>::Item`
1073 // * final path is `<<<std::vec::Vec<T>>::IntoIter>::Item>::clone`
1074 for (i, segment) in p.segments.iter().enumerate().skip(proj_start) {
1075 let segment = P(self.lower_path_segment(p.span, segment, param_mode, 0,
1076 ParenthesizedGenericArgs::Warn,
1078 let qpath = hir::QPath::TypeRelative(ty, segment);
1080 // It's finished, return the extension of the right node type.
1081 if i == p.segments.len() - 1 {
1085 // Wrap the associated extension in another type node.
1086 let new_id = self.next_id();
1087 ty = self.ty_path(new_id, p.span, qpath);
1090 // Should've returned in the for loop above.
1091 span_bug!(p.span, "lower_qpath: no final extension segment in {}..{}",
1092 proj_start, p.segments.len())
1095 fn lower_path_extra(&mut self,
1099 param_mode: ParamMode,
1100 defaults_to_global: bool)
1102 let mut segments = p.segments.iter();
1103 if defaults_to_global && p.is_global() {
1108 def: self.expect_full_def(id),
1109 segments: segments.map(|segment| {
1110 self.lower_path_segment(p.span, segment, param_mode, 0,
1111 ParenthesizedGenericArgs::Err,
1112 ImplTraitContext::Disallowed)
1113 }).chain(name.map(|name| hir::PathSegment::from_name(name)))
1119 fn lower_path(&mut self,
1122 param_mode: ParamMode,
1123 defaults_to_global: bool)
1125 self.lower_path_extra(id, p, None, param_mode, defaults_to_global)
1128 fn lower_path_segment(&mut self,
1130 segment: &PathSegment,
1131 param_mode: ParamMode,
1132 expected_lifetimes: usize,
1133 parenthesized_generic_args: ParenthesizedGenericArgs,
1134 itctx: ImplTraitContext)
1135 -> hir::PathSegment {
1136 let (mut parameters, infer_types) = if let Some(ref parameters) = segment.parameters {
1137 let msg = "parenthesized parameters may only be used with a trait";
1138 match **parameters {
1139 PathParameters::AngleBracketed(ref data) => {
1140 self.lower_angle_bracketed_parameter_data(data, param_mode, itctx)
1142 PathParameters::Parenthesized(ref data) => match parenthesized_generic_args {
1143 ParenthesizedGenericArgs::Ok =>
1144 self.lower_parenthesized_parameter_data(data),
1145 ParenthesizedGenericArgs::Warn => {
1146 self.sess.buffer_lint(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
1147 CRATE_NODE_ID, data.span, msg.into());
1148 (hir::PathParameters::none(), true)
1150 ParenthesizedGenericArgs::Err => {
1151 struct_span_err!(self.sess, data.span, E0214, "{}", msg)
1152 .span_label(data.span, "only traits may use parentheses").emit();
1153 (hir::PathParameters::none(), true)
1158 self.lower_angle_bracketed_parameter_data(&Default::default(), param_mode, itctx)
1161 if !parameters.parenthesized && parameters.lifetimes.is_empty() {
1162 parameters.lifetimes = (0..expected_lifetimes).map(|_| {
1163 self.elided_lifetime(path_span)
1167 hir::PathSegment::new(
1168 self.lower_ident(segment.identifier),
1174 fn lower_angle_bracketed_parameter_data(&mut self,
1175 data: &AngleBracketedParameterData,
1176 param_mode: ParamMode,
1177 itctx: ImplTraitContext)
1178 -> (hir::PathParameters, bool) {
1179 let &AngleBracketedParameterData { ref lifetimes, ref types, ref bindings, .. } = data;
1180 (hir::PathParameters {
1181 lifetimes: self.lower_lifetimes(lifetimes),
1182 types: types.iter().map(|ty| self.lower_ty(ty, itctx)).collect(),
1183 bindings: bindings.iter().map(|b| self.lower_ty_binding(b, itctx)).collect(),
1184 parenthesized: false,
1185 }, types.is_empty() && param_mode == ParamMode::Optional)
1188 fn lower_parenthesized_parameter_data(&mut self,
1189 data: &ParenthesizedParameterData)
1190 -> (hir::PathParameters, bool) {
1191 const DISALLOWED: ImplTraitContext = ImplTraitContext::Disallowed;
1192 let &ParenthesizedParameterData { ref inputs, ref output, span } = data;
1193 let inputs = inputs.iter().map(|ty| self.lower_ty(ty, DISALLOWED)).collect();
1194 let mk_tup = |this: &mut Self, tys, span| {
1195 let LoweredNodeId { node_id, hir_id } = this.next_id();
1196 P(hir::Ty { node: hir::TyTup(tys), id: node_id, hir_id, span })
1199 (hir::PathParameters {
1200 lifetimes: hir::HirVec::new(),
1201 types: hir_vec![mk_tup(self, inputs, span)],
1202 bindings: hir_vec![hir::TypeBinding {
1203 id: self.next_id().node_id,
1204 name: Symbol::intern(FN_OUTPUT_NAME),
1205 ty: output.as_ref().map(|ty| self.lower_ty(&ty, DISALLOWED))
1206 .unwrap_or_else(|| mk_tup(self, hir::HirVec::new(), span)),
1207 span: output.as_ref().map_or(span, |ty| ty.span),
1209 parenthesized: true,
1213 fn lower_local(&mut self, l: &Local) -> P<hir::Local> {
1214 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(l.id);
1218 ty: l.ty.as_ref().map(|t| self.lower_ty(t, ImplTraitContext::Disallowed)),
1219 pat: self.lower_pat(&l.pat),
1220 init: l.init.as_ref().map(|e| P(self.lower_expr(e))),
1222 attrs: l.attrs.clone(),
1223 source: hir::LocalSource::Normal,
1227 fn lower_mutability(&mut self, m: Mutability) -> hir::Mutability {
1229 Mutability::Mutable => hir::MutMutable,
1230 Mutability::Immutable => hir::MutImmutable,
1234 fn lower_arg(&mut self, arg: &Arg) -> hir::Arg {
1235 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(arg.id);
1239 pat: self.lower_pat(&arg.pat),
1243 fn lower_fn_args_to_names(&mut self, decl: &FnDecl)
1244 -> hir::HirVec<Spanned<Name>> {
1245 decl.inputs.iter().map(|arg| {
1246 match arg.pat.node {
1247 PatKind::Ident(_, ident, None) => {
1248 respan(ident.span, ident.node.name)
1250 _ => respan(arg.pat.span, keywords::Invalid.name()),
1256 fn lower_fn_decl(&mut self,
1258 fn_def_id: Option<DefId>,
1259 impl_trait_return_allow: bool)
1261 // NOTE: The two last paramters here have to do with impl Trait. If fn_def_id is Some,
1262 // then impl Trait arguments are lowered into generic paramters on the given
1263 // fn_def_id, otherwise impl Trait is disallowed. (for now)
1265 // Furthermore, if impl_trait_return_allow is true, then impl Trait may be used in
1266 // return positions as well. This guards against trait declarations and their impls
1267 // where impl Trait is disallowed. (again for now)
1269 inputs: decl.inputs.iter()
1270 .map(|arg| if let Some(def_id) = fn_def_id {
1271 self.lower_ty(&arg.ty, ImplTraitContext::Universal(def_id))
1273 self.lower_ty(&arg.ty, ImplTraitContext::Disallowed)
1275 output: match decl.output {
1276 FunctionRetTy::Ty(ref ty) => match fn_def_id {
1277 Some(_) if impl_trait_return_allow =>
1278 hir::Return(self.lower_ty(ty, ImplTraitContext::Existential)),
1279 _ => hir::Return(self.lower_ty(ty, ImplTraitContext::Disallowed)),
1281 FunctionRetTy::Default(span) => hir::DefaultReturn(span),
1283 variadic: decl.variadic,
1284 has_implicit_self: decl.inputs.get(0).map_or(false, |arg| {
1286 TyKind::ImplicitSelf => true,
1287 TyKind::Rptr(_, ref mt) => mt.ty.node == TyKind::ImplicitSelf,
1294 fn lower_ty_param_bound(&mut self, tpb: &TyParamBound, itctx: ImplTraitContext)
1295 -> hir::TyParamBound {
1297 TraitTyParamBound(ref ty, modifier) => {
1298 hir::TraitTyParamBound(self.lower_poly_trait_ref(ty, itctx),
1299 self.lower_trait_bound_modifier(modifier))
1301 RegionTyParamBound(ref lifetime) => {
1302 hir::RegionTyParamBound(self.lower_lifetime(lifetime))
1307 fn lower_ty_param(&mut self, tp: &TyParam, add_bounds: &[TyParamBound]) -> hir::TyParam {
1308 let mut name = self.lower_ident(tp.ident);
1310 // Don't expose `Self` (recovered "keyword used as ident" parse error).
1311 // `rustc::ty` expects `Self` to be only used for a trait's `Self`.
1312 // Instead, use gensym("Self") to create a distinct name that looks the same.
1313 if name == keywords::SelfType.name() {
1314 name = Symbol::gensym("Self");
1317 let itctx = ImplTraitContext::Universal(self.resolver.definitions().local_def_id(tp.id));
1318 let mut bounds = self.lower_bounds(&tp.bounds, itctx);
1319 if !add_bounds.is_empty() {
1320 bounds = bounds.into_iter().chain(
1321 self.lower_bounds(add_bounds, itctx).into_iter()
1326 id: self.lower_node_id(tp.id).node_id,
1329 default: tp.default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
1331 pure_wrt_drop: tp.attrs.iter().any(|attr| attr.check_name("may_dangle")),
1332 synthetic: tp.attrs.iter()
1333 .filter(|attr| attr.check_name("rustc_synthetic"))
1334 .map(|_| hir::SyntheticTyParamKind::ImplTrait)
1339 fn lower_ty_params(&mut self, tps: &Vec<TyParam>, add_bounds: &NodeMap<Vec<TyParamBound>>)
1340 -> hir::HirVec<hir::TyParam> {
1341 tps.iter().map(|tp| {
1342 self.lower_ty_param(tp, add_bounds.get(&tp.id).map_or(&[][..], |x| &x))
1346 fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
1348 id: self.lower_node_id(l.id).node_id,
1349 name: match self.lower_ident(l.ident) {
1350 x if x == "'_" => hir::LifetimeName::Underscore,
1351 x if x == "'static" => hir::LifetimeName::Static,
1352 name => hir::LifetimeName::Name(name),
1358 fn lower_lifetime_def(&mut self, l: &LifetimeDef) -> hir::LifetimeDef {
1360 lifetime: self.lower_lifetime(&l.lifetime),
1361 bounds: self.lower_lifetimes(&l.bounds),
1362 pure_wrt_drop: l.attrs.iter().any(|attr| attr.check_name("may_dangle")),
1366 fn lower_lifetimes(&mut self, lts: &Vec<Lifetime>) -> hir::HirVec<hir::Lifetime> {
1367 lts.iter().map(|l| self.lower_lifetime(l)).collect()
1370 fn lower_lifetime_defs(&mut self, lts: &Vec<LifetimeDef>) -> hir::HirVec<hir::LifetimeDef> {
1371 lts.iter().map(|l| self.lower_lifetime_def(l)).collect()
1374 fn lower_generics(&mut self, g: &Generics) -> hir::Generics {
1375 // Collect `?Trait` bounds in where clause and move them to parameter definitions.
1376 let mut add_bounds = NodeMap();
1377 for pred in &g.where_clause.predicates {
1378 if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {
1379 'next_bound: for bound in &bound_pred.bounds {
1380 if let TraitTyParamBound(_, TraitBoundModifier::Maybe) = *bound {
1381 let report_error = |this: &mut Self| {
1382 this.diagnostic().span_err(bound_pred.bounded_ty.span,
1383 "`?Trait` bounds are only permitted at the \
1384 point where a type parameter is declared");
1386 // Check if the where clause type is a plain type parameter.
1387 match bound_pred.bounded_ty.node {
1388 TyKind::Path(None, ref path)
1389 if path.segments.len() == 1 &&
1390 bound_pred.bound_lifetimes.is_empty() => {
1391 if let Some(Def::TyParam(def_id)) =
1392 self.resolver.get_resolution(bound_pred.bounded_ty.id)
1393 .map(|d| d.base_def()) {
1394 if let Some(node_id) =
1395 self.resolver.definitions().as_local_node_id(def_id) {
1396 for ty_param in &g.ty_params {
1397 if node_id == ty_param.id {
1398 add_bounds.entry(ty_param.id).or_insert(Vec::new())
1399 .push(bound.clone());
1400 continue 'next_bound;
1407 _ => report_error(self)
1415 ty_params: self.lower_ty_params(&g.ty_params, &add_bounds),
1416 lifetimes: self.lower_lifetime_defs(&g.lifetimes),
1417 where_clause: self.lower_where_clause(&g.where_clause),
1422 fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause {
1424 id: self.lower_node_id(wc.id).node_id,
1425 predicates: wc.predicates
1427 .map(|predicate| self.lower_where_predicate(predicate))
1432 fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate {
1434 WherePredicate::BoundPredicate(WhereBoundPredicate{ ref bound_lifetimes,
1438 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1439 bound_lifetimes: self.lower_lifetime_defs(bound_lifetimes),
1440 bounded_ty: self.lower_ty(bounded_ty, ImplTraitContext::Disallowed),
1441 bounds: bounds.iter().filter_map(|bound| match *bound {
1442 // Ignore `?Trait` bounds, they were copied into type parameters already.
1443 TraitTyParamBound(_, TraitBoundModifier::Maybe) => None,
1444 _ => Some(self.lower_ty_param_bound(bound, ImplTraitContext::Disallowed))
1449 WherePredicate::RegionPredicate(WhereRegionPredicate{ ref lifetime,
1452 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
1454 lifetime: self.lower_lifetime(lifetime),
1455 bounds: bounds.iter().map(|bound| self.lower_lifetime(bound)).collect(),
1458 WherePredicate::EqPredicate(WhereEqPredicate{ id,
1462 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
1463 id: self.lower_node_id(id).node_id,
1464 lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::Disallowed),
1465 rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::Disallowed),
1472 fn lower_variant_data(&mut self, vdata: &VariantData) -> hir::VariantData {
1474 VariantData::Struct(ref fields, id) => {
1475 hir::VariantData::Struct(fields.iter()
1477 .map(|f| self.lower_struct_field(f))
1479 self.lower_node_id(id).node_id)
1481 VariantData::Tuple(ref fields, id) => {
1482 hir::VariantData::Tuple(fields.iter()
1484 .map(|f| self.lower_struct_field(f))
1486 self.lower_node_id(id).node_id)
1488 VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id).node_id),
1492 fn lower_trait_ref(&mut self, p: &TraitRef, itctx: ImplTraitContext) -> hir::TraitRef {
1493 let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
1494 hir::QPath::Resolved(None, path) => path.and_then(|path| path),
1495 qpath => bug!("lower_trait_ref: unexpected QPath `{:?}`", qpath)
1499 ref_id: self.lower_node_id(p.ref_id).node_id,
1503 fn lower_poly_trait_ref(&mut self,
1505 itctx: ImplTraitContext)
1506 -> hir::PolyTraitRef {
1508 bound_lifetimes: self.lower_lifetime_defs(&p.bound_lifetimes),
1509 trait_ref: self.lower_trait_ref(&p.trait_ref, itctx),
1514 fn lower_struct_field(&mut self, (index, f): (usize, &StructField)) -> hir::StructField {
1517 id: self.lower_node_id(f.id).node_id,
1518 name: self.lower_ident(match f.ident {
1519 Some(ident) => ident,
1520 // FIXME(jseyfried) positional field hygiene
1521 None => Ident { name: Symbol::intern(&index.to_string()), ctxt: f.span.ctxt() },
1523 vis: self.lower_visibility(&f.vis, None),
1524 ty: self.lower_ty(&f.ty, ImplTraitContext::Disallowed),
1525 attrs: self.lower_attrs(&f.attrs),
1529 fn lower_field(&mut self, f: &Field) -> hir::Field {
1531 name: respan(f.ident.span, self.lower_ident(f.ident.node)),
1532 expr: P(self.lower_expr(&f.expr)),
1534 is_shorthand: f.is_shorthand,
1538 fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext) -> hir::MutTy {
1540 ty: self.lower_ty(&mt.ty, itctx),
1541 mutbl: self.lower_mutability(mt.mutbl),
1545 fn lower_bounds(&mut self, bounds: &[TyParamBound], itctx: ImplTraitContext)
1546 -> hir::TyParamBounds {
1547 bounds.iter().map(|bound| self.lower_ty_param_bound(bound, itctx)).collect()
1550 fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> P<hir::Block> {
1551 let mut expr = None;
1553 let mut stmts = vec![];
1555 for (index, stmt) in b.stmts.iter().enumerate() {
1556 if index == b.stmts.len() - 1 {
1557 if let StmtKind::Expr(ref e) = stmt.node {
1558 expr = Some(P(self.lower_expr(e)));
1560 stmts.extend(self.lower_stmt(stmt));
1563 stmts.extend(self.lower_stmt(stmt));
1567 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(b.id);
1572 stmts: stmts.into(),
1574 rules: self.lower_block_check_mode(&b.rules),
1580 fn lower_item_kind(&mut self,
1583 attrs: &hir::HirVec<Attribute>,
1584 vis: &mut hir::Visibility,
1588 ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
1589 ItemKind::Use(ref view_path) => {
1590 let path = match view_path.node {
1591 ViewPathSimple(_, ref path) => path,
1592 ViewPathGlob(ref path) => path,
1593 ViewPathList(ref path, ref path_list_idents) => {
1594 for &Spanned { node: ref import, span } in path_list_idents {
1595 // `use a::{self as x, b as y};` lowers to
1596 // `use a as x; use a::b as y;`
1597 let mut ident = import.name;
1598 let suffix = if ident.name == keywords::SelfValue.name() {
1599 if let Some(last) = path.segments.last() {
1600 ident = last.identifier;
1607 let mut path = self.lower_path_extra(import.id, path, suffix,
1608 ParamMode::Explicit, true);
1611 self.allocate_hir_id_counter(import.id, import);
1613 node_id: import_node_id,
1614 hir_id: import_hir_id,
1615 } = self.lower_node_id(import.id);
1617 self.with_hir_id_owner(import_node_id, |this| {
1618 let vis = match *vis {
1619 hir::Visibility::Public => hir::Visibility::Public,
1620 hir::Visibility::Crate => hir::Visibility::Crate,
1621 hir::Visibility::Inherited => hir::Visibility::Inherited,
1622 hir::Visibility::Restricted { ref path, id: _ } => {
1623 hir::Visibility::Restricted {
1625 // We are allocating a new NodeId here
1626 id: this.next_id().node_id,
1631 this.items.insert(import_node_id, hir::Item {
1633 hir_id: import_hir_id,
1634 name: import.rename.unwrap_or(ident).name,
1635 attrs: attrs.clone(),
1636 node: hir::ItemUse(P(path), hir::UseKind::Single),
1645 let path = P(self.lower_path(id, path, ParamMode::Explicit, true));
1646 let kind = match view_path.node {
1647 ViewPathSimple(ident, _) => {
1649 hir::UseKind::Single
1651 ViewPathGlob(_) => {
1654 ViewPathList(..) => {
1655 // Privatize the degenerate import base, used only to check
1656 // the stability of `use a::{};`, to avoid it showing up as
1657 // a reexport by accident when `pub`, e.g. in documentation.
1658 *vis = hir::Inherited;
1659 hir::UseKind::ListStem
1662 hir::ItemUse(path, kind)
1664 ItemKind::Static(ref t, m, ref e) => {
1665 let value = self.lower_body(None, |this| this.lower_expr(e));
1666 hir::ItemStatic(self.lower_ty(t, ImplTraitContext::Disallowed),
1667 self.lower_mutability(m),
1670 ItemKind::Const(ref t, ref e) => {
1671 let value = self.lower_body(None, |this| this.lower_expr(e));
1672 hir::ItemConst(self.lower_ty(t, ImplTraitContext::Disallowed), value)
1674 ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
1675 let fn_def_id = self.resolver.definitions().opt_local_def_id(id);
1676 self.with_new_scopes(|this| {
1677 let body_id = this.lower_body(Some(decl), |this| {
1678 let body = this.lower_block(body, false);
1679 this.expr_block(body, ThinVec::new())
1681 hir::ItemFn(this.lower_fn_decl(decl, fn_def_id, true),
1682 this.lower_unsafety(unsafety),
1683 this.lower_constness(constness),
1685 this.lower_generics(generics),
1689 ItemKind::Mod(ref m) => hir::ItemMod(self.lower_mod(m)),
1690 ItemKind::ForeignMod(ref nm) => hir::ItemForeignMod(self.lower_foreign_mod(nm)),
1691 ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
1692 ItemKind::Ty(ref t, ref generics) => {
1693 hir::ItemTy(self.lower_ty(t, ImplTraitContext::Disallowed),
1694 self.lower_generics(generics))
1696 ItemKind::Enum(ref enum_definition, ref generics) => {
1697 hir::ItemEnum(hir::EnumDef {
1698 variants: enum_definition.variants
1700 .map(|x| self.lower_variant(x))
1703 self.lower_generics(generics))
1705 ItemKind::Struct(ref struct_def, ref generics) => {
1706 let struct_def = self.lower_variant_data(struct_def);
1707 hir::ItemStruct(struct_def, self.lower_generics(generics))
1709 ItemKind::Union(ref vdata, ref generics) => {
1710 let vdata = self.lower_variant_data(vdata);
1711 hir::ItemUnion(vdata, self.lower_generics(generics))
1713 ItemKind::AutoImpl(unsafety, ref trait_ref) => {
1714 let trait_ref = self.lower_trait_ref(trait_ref, ImplTraitContext::Disallowed);
1716 if let Def::Trait(def_id) = trait_ref.path.def {
1717 self.trait_auto_impl.insert(def_id, id);
1720 hir::ItemAutoImpl(self.lower_unsafety(unsafety),
1723 ItemKind::Impl(unsafety,
1729 ref impl_items) => {
1730 let new_impl_items = impl_items.iter()
1731 .map(|item| self.lower_impl_item_ref(item))
1733 let ifce = ifce.as_ref().map(|trait_ref| {
1734 self.lower_trait_ref(trait_ref, ImplTraitContext::Disallowed)
1737 if let Some(ref trait_ref) = ifce {
1738 if let Def::Trait(def_id) = trait_ref.path.def {
1739 self.trait_impls.entry(def_id).or_insert(vec![]).push(id);
1743 hir::ItemImpl(self.lower_unsafety(unsafety),
1744 self.lower_impl_polarity(polarity),
1745 self.lower_defaultness(defaultness, true /* [1] */),
1746 self.lower_generics(generics),
1748 self.lower_ty(ty, ImplTraitContext::Disallowed),
1751 ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => {
1752 let bounds = self.lower_bounds(bounds, ImplTraitContext::Disallowed);
1753 let items = items.iter().map(|item| self.lower_trait_item_ref(item)).collect();
1754 hir::ItemTrait(self.lower_is_auto(is_auto),
1755 self.lower_unsafety(unsafety),
1756 self.lower_generics(generics),
1760 ItemKind::MacroDef(..) | ItemKind::Mac(..) => panic!("Shouldn't still be around"),
1763 // [1] `defaultness.has_value()` is never called for an `impl`, always `true` in order to
1764 // not cause an assertion failure inside the `lower_defaultness` function
1767 fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem {
1768 self.with_parent_def(i.id, |this| {
1769 let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
1770 let fn_def_id = this.resolver.definitions().opt_local_def_id(node_id);
1775 name: this.lower_ident(i.ident),
1776 attrs: this.lower_attrs(&i.attrs),
1777 generics: this.lower_generics(&i.generics),
1778 node: match i.node {
1779 TraitItemKind::Const(ref ty, ref default) => {
1780 hir::TraitItemKind::Const(this.lower_ty(ty, ImplTraitContext::Disallowed),
1781 default.as_ref().map(|x| {
1782 this.lower_body(None, |this| this.lower_expr(x))
1785 TraitItemKind::Method(ref sig, None) => {
1786 let names = this.lower_fn_args_to_names(&sig.decl);
1787 hir::TraitItemKind::Method(this.lower_method_sig(sig, fn_def_id, false),
1788 hir::TraitMethod::Required(names))
1790 TraitItemKind::Method(ref sig, Some(ref body)) => {
1791 let body_id = this.lower_body(Some(&sig.decl), |this| {
1792 let body = this.lower_block(body, false);
1793 this.expr_block(body, ThinVec::new())
1795 hir::TraitItemKind::Method(this.lower_method_sig(sig, fn_def_id, false),
1796 hir::TraitMethod::Provided(body_id))
1798 TraitItemKind::Type(ref bounds, ref default) => {
1799 hir::TraitItemKind::Type(this.lower_bounds(bounds,
1800 ImplTraitContext::Disallowed),
1801 default.as_ref().map(|x| {
1802 this.lower_ty(x, ImplTraitContext::Disallowed)
1805 TraitItemKind::Macro(..) => panic!("Shouldn't exist any more"),
1812 fn lower_trait_item_ref(&mut self, i: &TraitItem) -> hir::TraitItemRef {
1813 let (kind, has_default) = match i.node {
1814 TraitItemKind::Const(_, ref default) => {
1815 (hir::AssociatedItemKind::Const, default.is_some())
1817 TraitItemKind::Type(_, ref default) => {
1818 (hir::AssociatedItemKind::Type, default.is_some())
1820 TraitItemKind::Method(ref sig, ref default) => {
1821 (hir::AssociatedItemKind::Method {
1822 has_self: sig.decl.has_self(),
1823 }, default.is_some())
1825 TraitItemKind::Macro(..) => unimplemented!(),
1828 id: hir::TraitItemId { node_id: i.id },
1829 name: self.lower_ident(i.ident),
1831 defaultness: self.lower_defaultness(Defaultness::Default, has_default),
1836 fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem {
1837 self.with_parent_def(i.id, |this| {
1838 let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
1839 let fn_def_id = this.resolver.definitions().opt_local_def_id(node_id);
1844 name: this.lower_ident(i.ident),
1845 attrs: this.lower_attrs(&i.attrs),
1846 generics: this.lower_generics(&i.generics),
1847 vis: this.lower_visibility(&i.vis, None),
1848 defaultness: this.lower_defaultness(i.defaultness, true /* [1] */),
1849 node: match i.node {
1850 ImplItemKind::Const(ref ty, ref expr) => {
1851 let body_id = this.lower_body(None, |this| this.lower_expr(expr));
1852 hir::ImplItemKind::Const(
1853 this.lower_ty(ty, ImplTraitContext::Disallowed),
1857 ImplItemKind::Method(ref sig, ref body) => {
1858 let body_id = this.lower_body(Some(&sig.decl), |this| {
1859 let body = this.lower_block(body, false);
1860 this.expr_block(body, ThinVec::new())
1862 let impl_trait_return_allow = !this.is_in_trait_impl;
1863 hir::ImplItemKind::Method(this.lower_method_sig(sig,
1865 impl_trait_return_allow),
1868 ImplItemKind::Type(ref ty) =>
1869 hir::ImplItemKind::Type(this.lower_ty(ty, ImplTraitContext::Disallowed)),
1870 ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
1876 // [1] since `default impl` is not yet implemented, this is always true in impls
1879 fn lower_impl_item_ref(&mut self, i: &ImplItem) -> hir::ImplItemRef {
1881 id: hir::ImplItemId { node_id: i.id },
1882 name: self.lower_ident(i.ident),
1884 vis: self.lower_visibility(&i.vis, Some(i.id)),
1885 defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
1886 kind: match i.node {
1887 ImplItemKind::Const(..) => hir::AssociatedItemKind::Const,
1888 ImplItemKind::Type(..) => hir::AssociatedItemKind::Type,
1889 ImplItemKind::Method(ref sig, _) => {
1890 hir::AssociatedItemKind::Method {
1891 has_self: sig.decl.has_self(),
1894 ImplItemKind::Macro(..) => unimplemented!(),
1898 // [1] since `default impl` is not yet implemented, this is always true in impls
1901 fn lower_mod(&mut self, m: &Mod) -> hir::Mod {
1904 item_ids: m.items.iter().flat_map(|x| self.lower_item_id(x)).collect(),
1908 fn lower_item_id(&mut self, i: &Item) -> SmallVector<hir::ItemId> {
1910 ItemKind::Use(ref view_path) => {
1911 if let ViewPathList(_, ref imports) = view_path.node {
1912 return iter::once(i.id).chain(imports.iter().map(|import| import.node.id))
1913 .map(|id| hir::ItemId { id: id }).collect();
1916 ItemKind::MacroDef(..) => return SmallVector::new(),
1919 SmallVector::one(hir::ItemId { id: i.id })
1922 pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
1923 let mut name = i.ident.name;
1924 let mut vis = self.lower_visibility(&i.vis, None);
1925 let attrs = self.lower_attrs(&i.attrs);
1926 if let ItemKind::MacroDef(ref def) = i.node {
1927 if !def.legacy || i.attrs.iter().any(|attr| attr.path == "macro_export") {
1928 let body = self.lower_token_stream(def.stream());
1929 self.exported_macros.push(hir::MacroDef {
1942 let node = self.with_parent_def(i.id, |this| {
1943 this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
1946 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(i.id);
1959 fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem {
1960 self.with_parent_def(i.id, |this| {
1962 id: this.lower_node_id(i.id).node_id,
1964 attrs: this.lower_attrs(&i.attrs),
1965 node: match i.node {
1966 ForeignItemKind::Fn(ref fdec, ref generics) => {
1967 // Disallow impl Trait in foreign items
1968 hir::ForeignItemFn(this.lower_fn_decl(fdec, None, false),
1969 this.lower_fn_args_to_names(fdec),
1970 this.lower_generics(generics))
1972 ForeignItemKind::Static(ref t, m) => {
1973 hir::ForeignItemStatic(this.lower_ty(t, ImplTraitContext::Disallowed), m)
1975 ForeignItemKind::Ty => {
1976 hir::ForeignItemType
1979 vis: this.lower_visibility(&i.vis, None),
1985 fn lower_method_sig(&mut self,
1987 fn_def_id: Option<DefId>,
1988 impl_trait_return_allow: bool)
1992 unsafety: self.lower_unsafety(sig.unsafety),
1993 constness: self.lower_constness(sig.constness),
1994 decl: self.lower_fn_decl(&sig.decl, fn_def_id, impl_trait_return_allow),
1998 fn lower_is_auto(&mut self, a: IsAuto) -> hir::IsAuto {
2000 IsAuto::Yes => hir::IsAuto::Yes,
2001 IsAuto::No => hir::IsAuto::No,
2005 fn lower_unsafety(&mut self, u: Unsafety) -> hir::Unsafety {
2007 Unsafety::Unsafe => hir::Unsafety::Unsafe,
2008 Unsafety::Normal => hir::Unsafety::Normal,
2012 fn lower_constness(&mut self, c: Spanned<Constness>) -> hir::Constness {
2014 Constness::Const => hir::Constness::Const,
2015 Constness::NotConst => hir::Constness::NotConst,
2019 fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
2021 UnOp::Deref => hir::UnDeref,
2022 UnOp::Not => hir::UnNot,
2023 UnOp::Neg => hir::UnNeg,
2027 fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
2029 node: match b.node {
2030 BinOpKind::Add => hir::BiAdd,
2031 BinOpKind::Sub => hir::BiSub,
2032 BinOpKind::Mul => hir::BiMul,
2033 BinOpKind::Div => hir::BiDiv,
2034 BinOpKind::Rem => hir::BiRem,
2035 BinOpKind::And => hir::BiAnd,
2036 BinOpKind::Or => hir::BiOr,
2037 BinOpKind::BitXor => hir::BiBitXor,
2038 BinOpKind::BitAnd => hir::BiBitAnd,
2039 BinOpKind::BitOr => hir::BiBitOr,
2040 BinOpKind::Shl => hir::BiShl,
2041 BinOpKind::Shr => hir::BiShr,
2042 BinOpKind::Eq => hir::BiEq,
2043 BinOpKind::Lt => hir::BiLt,
2044 BinOpKind::Le => hir::BiLe,
2045 BinOpKind::Ne => hir::BiNe,
2046 BinOpKind::Ge => hir::BiGe,
2047 BinOpKind::Gt => hir::BiGt,
2053 fn lower_pat(&mut self, p: &Pat) -> P<hir::Pat> {
2054 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(p.id);
2059 node: match p.node {
2060 PatKind::Wild => hir::PatKind::Wild,
2061 PatKind::Ident(ref binding_mode, pth1, ref sub) => {
2062 match self.resolver.get_resolution(p.id).map(|d| d.base_def()) {
2063 // `None` can occur in body-less function signatures
2064 def @ None | def @ Some(Def::Local(_)) => {
2065 let canonical_id = match def {
2066 Some(Def::Local(id)) => id,
2069 hir::PatKind::Binding(self.lower_binding_mode(binding_mode),
2071 respan(pth1.span, pth1.node.name),
2072 sub.as_ref().map(|x| self.lower_pat(x)))
2075 hir::PatKind::Path(hir::QPath::Resolved(None, P(hir::Path {
2079 hir::PathSegment::from_name(pth1.node.name)
2085 PatKind::Lit(ref e) => hir::PatKind::Lit(P(self.lower_expr(e))),
2086 PatKind::TupleStruct(ref path, ref pats, ddpos) => {
2087 let qpath = self.lower_qpath(p.id, &None, path, ParamMode::Optional,
2088 ImplTraitContext::Disallowed);
2089 hir::PatKind::TupleStruct(qpath,
2090 pats.iter().map(|x| self.lower_pat(x)).collect(),
2093 PatKind::Path(ref qself, ref path) => {
2094 hir::PatKind::Path(self.lower_qpath(p.id, qself, path, ParamMode::Optional,
2095 ImplTraitContext::Disallowed))
2097 PatKind::Struct(ref path, ref fields, etc) => {
2098 let qpath = self.lower_qpath(p.id, &None, path, ParamMode::Optional,
2099 ImplTraitContext::Disallowed);
2101 let fs = fields.iter()
2105 node: hir::FieldPat {
2106 name: self.lower_ident(f.node.ident),
2107 pat: self.lower_pat(&f.node.pat),
2108 is_shorthand: f.node.is_shorthand,
2113 hir::PatKind::Struct(qpath, fs, etc)
2115 PatKind::Tuple(ref elts, ddpos) => {
2116 hir::PatKind::Tuple(elts.iter().map(|x| self.lower_pat(x)).collect(), ddpos)
2118 PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)),
2119 PatKind::Ref(ref inner, mutbl) => {
2120 hir::PatKind::Ref(self.lower_pat(inner), self.lower_mutability(mutbl))
2122 PatKind::Range(ref e1, ref e2, ref end) => {
2123 hir::PatKind::Range(P(self.lower_expr(e1)),
2124 P(self.lower_expr(e2)),
2125 self.lower_range_end(end))
2127 PatKind::Slice(ref before, ref slice, ref after) => {
2128 hir::PatKind::Slice(before.iter().map(|x| self.lower_pat(x)).collect(),
2129 slice.as_ref().map(|x| self.lower_pat(x)),
2130 after.iter().map(|x| self.lower_pat(x)).collect())
2132 PatKind::Mac(_) => panic!("Shouldn't exist here"),
2138 fn lower_range_end(&mut self, e: &RangeEnd) -> hir::RangeEnd {
2140 RangeEnd::Included(_) => hir::RangeEnd::Included,
2141 RangeEnd::Excluded => hir::RangeEnd::Excluded,
2145 fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
2146 let kind = match e.node {
2148 // Eventually a desugaring for `box EXPR`
2149 // (similar to the desugaring above for `in PLACE BLOCK`)
2150 // should go here, desugaring
2154 // let mut place = BoxPlace::make_place();
2155 // let raw_place = Place::pointer(&mut place);
2156 // let value = $value;
2158 // ::std::ptr::write(raw_place, value);
2159 // Boxed::finalize(place)
2162 // But for now there are type-inference issues doing that.
2163 ExprKind::Box(ref inner) => {
2164 hir::ExprBox(P(self.lower_expr(inner)))
2167 // Desugar ExprBox: `in (PLACE) EXPR`
2168 ExprKind::InPlace(ref placer, ref value_expr) => {
2172 // let mut place = Placer::make_place(p);
2173 // let raw_place = Place::pointer(&mut place);
2175 // std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR ));
2176 // InPlace::finalize(place)
2178 let placer_expr = P(self.lower_expr(placer));
2179 let value_expr = P(self.lower_expr(value_expr));
2181 let placer_ident = self.str_to_ident("placer");
2182 let place_ident = self.str_to_ident("place");
2183 let p_ptr_ident = self.str_to_ident("p_ptr");
2185 let make_place = ["ops", "Placer", "make_place"];
2186 let place_pointer = ["ops", "Place", "pointer"];
2187 let move_val_init = ["intrinsics", "move_val_init"];
2188 let inplace_finalize = ["ops", "InPlace", "finalize"];
2191 self.allow_internal_unstable(CompilerDesugaringKind::BackArrow, e.span);
2192 let make_call = |this: &mut LoweringContext, p, args| {
2193 let path = P(this.expr_std_path(unstable_span, p, ThinVec::new()));
2194 P(this.expr_call(e.span, path, args))
2197 let mk_stmt_let = |this: &mut LoweringContext, bind, expr| {
2198 this.stmt_let(e.span, false, bind, expr)
2201 let mk_stmt_let_mut = |this: &mut LoweringContext, bind, expr| {
2202 this.stmt_let(e.span, true, bind, expr)
2205 // let placer = <placer_expr> ;
2206 let (s1, placer_binding) = {
2207 mk_stmt_let(self, placer_ident, placer_expr)
2210 // let mut place = Placer::make_place(placer);
2211 let (s2, place_binding) = {
2212 let placer = self.expr_ident(e.span, placer_ident, placer_binding);
2213 let call = make_call(self, &make_place, hir_vec![placer]);
2214 mk_stmt_let_mut(self, place_ident, call)
2217 // let p_ptr = Place::pointer(&mut place);
2218 let (s3, p_ptr_binding) = {
2219 let agent = P(self.expr_ident(e.span, place_ident, place_binding));
2220 let args = hir_vec![self.expr_mut_addr_of(e.span, agent)];
2221 let call = make_call(self, &place_pointer, args);
2222 mk_stmt_let(self, p_ptr_ident, call)
2225 // pop_unsafe!(EXPR));
2226 let pop_unsafe_expr = {
2227 self.signal_block_expr(hir_vec![],
2230 hir::PopUnsafeBlock(hir::CompilerGenerated),
2235 // std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR ));
2236 // InPlace::finalize(place)
2239 let ptr = self.expr_ident(e.span, p_ptr_ident, p_ptr_binding);
2240 let call_move_val_init =
2242 make_call(self, &move_val_init, hir_vec![ptr, pop_unsafe_expr]),
2243 self.next_id().node_id);
2244 let call_move_val_init = respan(e.span, call_move_val_init);
2246 let place = self.expr_ident(e.span, place_ident, place_binding);
2247 let call = make_call(self, &inplace_finalize, hir_vec![place]);
2248 P(self.signal_block_expr(hir_vec![call_move_val_init],
2251 hir::PushUnsafeBlock(hir::CompilerGenerated),
2255 let block = self.block_all(e.span, hir_vec![s1, s2, s3], Some(expr));
2256 hir::ExprBlock(P(block))
2259 ExprKind::Array(ref exprs) => {
2260 hir::ExprArray(exprs.iter().map(|x| self.lower_expr(x)).collect())
2262 ExprKind::Repeat(ref expr, ref count) => {
2263 let expr = P(self.lower_expr(expr));
2264 let count = self.lower_body(None, |this| this.lower_expr(count));
2265 hir::ExprRepeat(expr, count)
2267 ExprKind::Tup(ref elts) => {
2268 hir::ExprTup(elts.iter().map(|x| self.lower_expr(x)).collect())
2270 ExprKind::Call(ref f, ref args) => {
2271 let f = P(self.lower_expr(f));
2272 hir::ExprCall(f, args.iter().map(|x| self.lower_expr(x)).collect())
2274 ExprKind::MethodCall(ref seg, ref args) => {
2275 let hir_seg = self.lower_path_segment(e.span, seg, ParamMode::Optional, 0,
2276 ParenthesizedGenericArgs::Err,
2277 ImplTraitContext::Disallowed);
2278 let args = args.iter().map(|x| self.lower_expr(x)).collect();
2279 hir::ExprMethodCall(hir_seg, seg.span, args)
2281 ExprKind::Binary(binop, ref lhs, ref rhs) => {
2282 let binop = self.lower_binop(binop);
2283 let lhs = P(self.lower_expr(lhs));
2284 let rhs = P(self.lower_expr(rhs));
2285 hir::ExprBinary(binop, lhs, rhs)
2287 ExprKind::Unary(op, ref ohs) => {
2288 let op = self.lower_unop(op);
2289 let ohs = P(self.lower_expr(ohs));
2290 hir::ExprUnary(op, ohs)
2292 ExprKind::Lit(ref l) => hir::ExprLit(P((**l).clone())),
2293 ExprKind::Cast(ref expr, ref ty) => {
2294 let expr = P(self.lower_expr(expr));
2295 hir::ExprCast(expr, self.lower_ty(ty, ImplTraitContext::Disallowed))
2297 ExprKind::Type(ref expr, ref ty) => {
2298 let expr = P(self.lower_expr(expr));
2299 hir::ExprType(expr, self.lower_ty(ty, ImplTraitContext::Disallowed))
2301 ExprKind::AddrOf(m, ref ohs) => {
2302 let m = self.lower_mutability(m);
2303 let ohs = P(self.lower_expr(ohs));
2304 hir::ExprAddrOf(m, ohs)
2306 // More complicated than you might expect because the else branch
2307 // might be `if let`.
2308 ExprKind::If(ref cond, ref blk, ref else_opt) => {
2309 let else_opt = else_opt.as_ref().map(|els| {
2311 ExprKind::IfLet(..) => {
2312 // wrap the if-let expr in a block
2313 let span = els.span;
2314 let els = P(self.lower_expr(els));
2319 let blk = P(hir::Block {
2324 rules: hir::DefaultBlock,
2326 targeted_by_break: false,
2328 P(self.expr_block(blk, ThinVec::new()))
2330 _ => P(self.lower_expr(els)),
2334 let then_blk = self.lower_block(blk, false);
2335 let then_expr = self.expr_block(then_blk, ThinVec::new());
2337 hir::ExprIf(P(self.lower_expr(cond)), P(then_expr), else_opt)
2339 ExprKind::While(ref cond, ref body, opt_ident) => {
2340 self.with_loop_scope(e.id, |this|
2342 this.with_loop_condition_scope(|this| P(this.lower_expr(cond))),
2343 this.lower_block(body, false),
2344 this.lower_opt_sp_ident(opt_ident)))
2346 ExprKind::Loop(ref body, opt_ident) => {
2347 self.with_loop_scope(e.id, |this|
2348 hir::ExprLoop(this.lower_block(body, false),
2349 this.lower_opt_sp_ident(opt_ident),
2350 hir::LoopSource::Loop))
2352 ExprKind::Catch(ref body) => {
2353 self.with_catch_scope(body.id, |this|
2354 hir::ExprBlock(this.lower_block(body, true)))
2356 ExprKind::Match(ref expr, ref arms) => {
2357 hir::ExprMatch(P(self.lower_expr(expr)),
2358 arms.iter().map(|x| self.lower_arm(x)).collect(),
2359 hir::MatchSource::Normal)
2361 ExprKind::Closure(capture_clause, ref decl, ref body, fn_decl_span) => {
2362 self.with_new_scopes(|this| {
2363 this.with_parent_def(e.id, |this| {
2364 let mut is_generator = false;
2365 let body_id = this.lower_body(Some(decl), |this| {
2366 let e = this.lower_expr(body);
2367 is_generator = this.is_generator;
2370 if is_generator && !decl.inputs.is_empty() {
2371 span_err!(this.sess, fn_decl_span, E0628,
2372 "generators cannot have explicit arguments");
2373 this.sess.abort_if_errors();
2375 hir::ExprClosure(this.lower_capture_clause(capture_clause),
2376 this.lower_fn_decl(decl, None, false),
2383 ExprKind::Block(ref blk) => hir::ExprBlock(self.lower_block(blk, false)),
2384 ExprKind::Assign(ref el, ref er) => {
2385 hir::ExprAssign(P(self.lower_expr(el)), P(self.lower_expr(er)))
2387 ExprKind::AssignOp(op, ref el, ref er) => {
2388 hir::ExprAssignOp(self.lower_binop(op),
2389 P(self.lower_expr(el)),
2390 P(self.lower_expr(er)))
2392 ExprKind::Field(ref el, ident) => {
2393 hir::ExprField(P(self.lower_expr(el)),
2394 respan(ident.span, self.lower_ident(ident.node)))
2396 ExprKind::TupField(ref el, ident) => {
2397 hir::ExprTupField(P(self.lower_expr(el)), ident)
2399 ExprKind::Index(ref el, ref er) => {
2400 hir::ExprIndex(P(self.lower_expr(el)), P(self.lower_expr(er)))
2402 ExprKind::Range(ref e1, ref e2, lims) => {
2403 use syntax::ast::RangeLimits::*;
2405 let path = match (e1, e2, lims) {
2406 (&None, &None, HalfOpen) => "RangeFull",
2407 (&Some(..), &None, HalfOpen) => "RangeFrom",
2408 (&None, &Some(..), HalfOpen) => "RangeTo",
2409 (&Some(..), &Some(..), HalfOpen) => "Range",
2410 (&None, &Some(..), Closed) => "RangeToInclusive",
2411 (&Some(..), &Some(..), Closed) => "RangeInclusive",
2412 (_, &None, Closed) =>
2413 panic!(self.diagnostic().span_fatal(
2414 e.span, "inclusive range with no end")),
2418 e1.iter().map(|e| ("start", e)).chain(e2.iter().map(|e| ("end", e)))
2420 let expr = P(self.lower_expr(&e));
2422 self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
2423 self.field(Symbol::intern(s), expr, unstable_span)
2424 }).collect::<P<[hir::Field]>>();
2426 let is_unit = fields.is_empty();
2428 self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
2430 iter::once("ops").chain(iter::once(path))
2431 .collect::<Vec<_>>();
2432 let struct_path = self.std_path(unstable_span, &struct_path, is_unit);
2433 let struct_path = hir::QPath::Resolved(None, P(struct_path));
2435 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(e.id);
2441 hir::ExprPath(struct_path)
2443 hir::ExprStruct(struct_path, fields, None)
2445 span: unstable_span,
2446 attrs: e.attrs.clone(),
2449 ExprKind::Path(ref qself, ref path) => {
2450 hir::ExprPath(self.lower_qpath(e.id, qself, path, ParamMode::Optional,
2451 ImplTraitContext::Disallowed))
2453 ExprKind::Break(opt_ident, ref opt_expr) => {
2454 let label_result = if self.is_in_loop_condition && opt_ident.is_none() {
2457 target_id: hir::ScopeTarget::Loop(
2458 Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into()),
2461 self.lower_loop_destination(opt_ident.map(|ident| (e.id, ident)))
2465 opt_expr.as_ref().map(|x| P(self.lower_expr(x))))
2467 ExprKind::Continue(opt_ident) =>
2469 if self.is_in_loop_condition && opt_ident.is_none() {
2472 target_id: hir::ScopeTarget::Loop(Err(
2473 hir::LoopIdError::UnlabeledCfInWhileCondition).into()),
2476 self.lower_loop_destination(opt_ident.map( |ident| (e.id, ident)))
2478 ExprKind::Ret(ref e) => hir::ExprRet(e.as_ref().map(|x| P(self.lower_expr(x)))),
2479 ExprKind::InlineAsm(ref asm) => {
2480 let hir_asm = hir::InlineAsm {
2481 inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(),
2482 outputs: asm.outputs.iter().map(|out| {
2483 hir::InlineAsmOutput {
2484 constraint: out.constraint.clone(),
2486 is_indirect: out.is_indirect,
2489 asm: asm.asm.clone(),
2490 asm_str_style: asm.asm_str_style,
2491 clobbers: asm.clobbers.clone().into(),
2492 volatile: asm.volatile,
2493 alignstack: asm.alignstack,
2494 dialect: asm.dialect,
2498 asm.outputs.iter().map(|out| self.lower_expr(&out.expr)).collect();
2500 asm.inputs.iter().map(|&(_, ref input)| self.lower_expr(input)).collect();
2501 hir::ExprInlineAsm(P(hir_asm), outputs, inputs)
2503 ExprKind::Struct(ref path, ref fields, ref maybe_expr) => {
2504 hir::ExprStruct(self.lower_qpath(e.id, &None, path, ParamMode::Optional,
2505 ImplTraitContext::Disallowed),
2506 fields.iter().map(|x| self.lower_field(x)).collect(),
2507 maybe_expr.as_ref().map(|x| P(self.lower_expr(x))))
2509 ExprKind::Paren(ref ex) => {
2510 let mut ex = self.lower_expr(ex);
2511 // include parens in span, but only if it is a super-span.
2512 if e.span.contains(ex.span) {
2515 // merge attributes into the inner expression.
2516 let mut attrs = e.attrs.clone();
2517 attrs.extend::<Vec<_>>(ex.attrs.into());
2522 ExprKind::Yield(ref opt_expr) => {
2523 self.is_generator = true;
2524 let expr = opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| {
2525 self.expr(e.span, hir::ExprTup(hir_vec![]), ThinVec::new())
2527 hir::ExprYield(P(expr))
2530 // Desugar ExprIfLet
2531 // From: `if let <pat> = <sub_expr> <body> [<else_opt>]`
2532 ExprKind::IfLet(ref pat, ref sub_expr, ref body, ref else_opt) => {
2535 // match <sub_expr> {
2537 // _ => [<else_opt> | ()]
2540 let mut arms = vec![];
2542 // `<pat> => <body>`
2544 let body = self.lower_block(body, false);
2545 let body_expr = P(self.expr_block(body, ThinVec::new()));
2546 let pat = self.lower_pat(pat);
2547 arms.push(self.arm(hir_vec![pat], body_expr));
2550 // _ => [<else_opt>|()]
2552 let wildcard_arm: Option<&Expr> = else_opt.as_ref().map(|p| &**p);
2553 let wildcard_pattern = self.pat_wild(e.span);
2554 let body = if let Some(else_expr) = wildcard_arm {
2555 P(self.lower_expr(else_expr))
2557 self.expr_tuple(e.span, hir_vec![])
2559 arms.push(self.arm(hir_vec![wildcard_pattern], body));
2562 let contains_else_clause = else_opt.is_some();
2564 let sub_expr = P(self.lower_expr(sub_expr));
2569 hir::MatchSource::IfLetDesugar {
2570 contains_else_clause,
2574 // Desugar ExprWhileLet
2575 // From: `[opt_ident]: while let <pat> = <sub_expr> <body>`
2576 ExprKind::WhileLet(ref pat, ref sub_expr, ref body, opt_ident) => {
2579 // [opt_ident]: loop {
2580 // match <sub_expr> {
2586 // Note that the block AND the condition are evaluated in the loop scope.
2587 // This is done to allow `break` from inside the condition of the loop.
2588 let (body, break_expr, sub_expr) = self.with_loop_scope(e.id, |this| (
2589 this.lower_block(body, false),
2590 this.expr_break(e.span, ThinVec::new()),
2591 this.with_loop_condition_scope(|this| P(this.lower_expr(sub_expr))),
2594 // `<pat> => <body>`
2596 let body_expr = P(self.expr_block(body, ThinVec::new()));
2597 let pat = self.lower_pat(pat);
2598 self.arm(hir_vec![pat], body_expr)
2603 let pat_under = self.pat_wild(e.span);
2604 self.arm(hir_vec![pat_under], break_expr)
2607 // `match <sub_expr> { ... }`
2608 let arms = hir_vec![pat_arm, break_arm];
2609 let match_expr = self.expr(e.span,
2610 hir::ExprMatch(sub_expr,
2612 hir::MatchSource::WhileLetDesugar),
2615 // `[opt_ident]: loop { ... }`
2616 let loop_block = P(self.block_expr(P(match_expr)));
2617 let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident),
2618 hir::LoopSource::WhileLet);
2619 // add attributes to the outer returned expr node
2623 // Desugar ExprForLoop
2624 // From: `[opt_ident]: for <pat> in <head> <body>`
2625 ExprKind::ForLoop(ref pat, ref head, ref body, opt_ident) => {
2629 // let result = match ::std::iter::IntoIterator::into_iter(<head>) {
2631 // [opt_ident]: loop {
2633 // match ::std::iter::Iterator::next(&mut iter) {
2634 // ::std::option::Option::Some(val) => __next = val,
2635 // ::std::option::Option::None => break
2637 // let <pat> = __next;
2638 // StmtExpr(<body>);
2646 let head = self.lower_expr(head);
2648 let iter = self.str_to_ident("iter");
2650 let next_ident = self.str_to_ident("__next");
2651 let next_pat = self.pat_ident_binding_mode(e.span,
2653 hir::BindingAnnotation::Mutable);
2655 // `::std::option::Option::Some(val) => next = val`
2657 let val_ident = self.str_to_ident("val");
2658 let val_pat = self.pat_ident(e.span, val_ident);
2659 let val_expr = P(self.expr_ident(e.span, val_ident, val_pat.id));
2660 let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
2661 let assign = P(self.expr(e.span,
2662 hir::ExprAssign(next_expr, val_expr),
2664 let some_pat = self.pat_some(e.span, val_pat);
2665 self.arm(hir_vec![some_pat], assign)
2668 // `::std::option::Option::None => break`
2670 let break_expr = self.with_loop_scope(e.id, |this|
2671 this.expr_break(e.span, ThinVec::new()));
2672 let pat = self.pat_none(e.span);
2673 self.arm(hir_vec![pat], break_expr)
2677 let iter_pat = self.pat_ident_binding_mode(e.span,
2679 hir::BindingAnnotation::Mutable);
2681 // `match ::std::iter::Iterator::next(&mut iter) { ... }`
2683 let iter = P(self.expr_ident(e.span, iter, iter_pat.id));
2684 let ref_mut_iter = self.expr_mut_addr_of(e.span, iter);
2685 let next_path = &["iter", "Iterator", "next"];
2686 let next_path = P(self.expr_std_path(e.span, next_path, ThinVec::new()));
2687 let next_expr = P(self.expr_call(e.span, next_path,
2688 hir_vec![ref_mut_iter]));
2689 let arms = hir_vec![pat_arm, break_arm];
2692 hir::ExprMatch(next_expr, arms,
2693 hir::MatchSource::ForLoopDesugar),
2696 let match_stmt = respan(e.span, hir::StmtExpr(match_expr, self.next_id().node_id));
2698 let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
2701 let next_let = self.stmt_let_pat(e.span,
2704 hir::LocalSource::ForLoopDesugar);
2706 // `let <pat> = __next`
2707 let pat = self.lower_pat(pat);
2708 let pat_let = self.stmt_let_pat(e.span,
2711 hir::LocalSource::ForLoopDesugar);
2713 let body_block = self.with_loop_scope(e.id,
2714 |this| this.lower_block(body, false));
2715 let body_expr = P(self.expr_block(body_block, ThinVec::new()));
2716 let body_stmt = respan(e.span, hir::StmtExpr(body_expr, self.next_id().node_id));
2718 let loop_block = P(self.block_all(e.span,
2725 // `[opt_ident]: loop { ... }`
2726 let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident),
2727 hir::LoopSource::ForLoop);
2728 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(e.id);
2729 let loop_expr = P(hir::Expr {
2734 attrs: ThinVec::new(),
2737 // `mut iter => { ... }`
2738 let iter_arm = self.arm(hir_vec![iter_pat], loop_expr);
2740 // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
2741 let into_iter_expr = {
2742 let into_iter_path = &["iter", "IntoIterator", "into_iter"];
2743 let into_iter = P(self.expr_std_path(e.span, into_iter_path,
2745 P(self.expr_call(e.span, into_iter, hir_vec![head]))
2748 let match_expr = P(self.expr_match(e.span,
2751 hir::MatchSource::ForLoopDesugar));
2753 // `{ let _result = ...; _result }`
2754 // underscore prevents an unused_variables lint if the head diverges
2755 let result_ident = self.str_to_ident("_result");
2756 let (let_stmt, let_stmt_binding) =
2757 self.stmt_let(e.span, false, result_ident, match_expr);
2759 let result = P(self.expr_ident(e.span, result_ident, let_stmt_binding));
2760 let block = P(self.block_all(e.span, hir_vec![let_stmt], Some(result)));
2761 // add the attributes to the outer returned expr node
2762 return self.expr_block(block, e.attrs.clone());
2765 // Desugar ExprKind::Try
2767 ExprKind::Try(ref sub_expr) => {
2770 // match Try::into_result(<expr>) {
2771 // Ok(val) => #[allow(unreachable_code)] val,
2772 // Err(err) => #[allow(unreachable_code)]
2773 // // If there is an enclosing `catch {...}`
2774 // break 'catch_target Try::from_error(From::from(err)),
2776 // return Try::from_error(From::from(err)),
2780 self.allow_internal_unstable(CompilerDesugaringKind::QuestionMark, e.span);
2782 // Try::into_result(<expr>)
2785 let sub_expr = self.lower_expr(sub_expr);
2787 let path = &["ops", "Try", "into_result"];
2788 let path = P(self.expr_std_path(unstable_span, path, ThinVec::new()));
2789 P(self.expr_call(e.span, path, hir_vec![sub_expr]))
2792 // #[allow(unreachable_code)]
2794 // allow(unreachable_code)
2796 let allow_ident = self.str_to_ident("allow");
2797 let uc_ident = self.str_to_ident("unreachable_code");
2798 let uc_meta_item = attr::mk_spanned_word_item(e.span, uc_ident);
2799 let uc_nested = NestedMetaItemKind::MetaItem(uc_meta_item);
2800 let uc_spanned = respan(e.span, uc_nested);
2801 attr::mk_spanned_list_item(e.span, allow_ident, vec![uc_spanned])
2803 attr::mk_spanned_attr_outer(e.span, attr::mk_attr_id(), allow)
2805 let attrs = vec![attr];
2807 // Ok(val) => #[allow(unreachable_code)] val,
2809 let val_ident = self.str_to_ident("val");
2810 let val_pat = self.pat_ident(e.span, val_ident);
2811 let val_expr = P(self.expr_ident_with_attrs(e.span,
2814 ThinVec::from(attrs.clone())));
2815 let ok_pat = self.pat_ok(e.span, val_pat);
2817 self.arm(hir_vec![ok_pat], val_expr)
2820 // Err(err) => #[allow(unreachable_code)]
2821 // return Try::from_error(From::from(err)),
2823 let err_ident = self.str_to_ident("err");
2824 let err_local = self.pat_ident(e.span, err_ident);
2826 let path = &["convert", "From", "from"];
2827 let from = P(self.expr_std_path(e.span, path, ThinVec::new()));
2828 let err_expr = self.expr_ident(e.span, err_ident, err_local.id);
2830 self.expr_call(e.span, from, hir_vec![err_expr])
2832 let from_err_expr = {
2833 let path = &["ops", "Try", "from_error"];
2834 let from_err = P(self.expr_std_path(unstable_span, path,
2836 P(self.expr_call(e.span, from_err, hir_vec![from_expr]))
2839 let thin_attrs = ThinVec::from(attrs);
2840 let catch_scope = self.catch_scopes.last().map(|x| *x);
2841 let ret_expr = if let Some(catch_node) = catch_scope {
2847 target_id: hir::ScopeTarget::Block(catch_node),
2854 hir::Expr_::ExprRet(Some(from_err_expr)),
2859 let err_pat = self.pat_err(e.span, err_local);
2860 self.arm(hir_vec![err_pat], ret_expr)
2863 hir::ExprMatch(discr,
2864 hir_vec![err_arm, ok_arm],
2865 hir::MatchSource::TryDesugar)
2868 ExprKind::Mac(_) => panic!("Shouldn't exist here"),
2871 let LoweredNodeId { node_id, hir_id } = self.lower_node_id(e.id);
2878 attrs: e.attrs.clone(),
2882 fn lower_stmt(&mut self, s: &Stmt) -> SmallVector<hir::Stmt> {
2883 SmallVector::one(match s.node {
2884 StmtKind::Local(ref l) => Spanned {
2885 node: hir::StmtDecl(P(Spanned {
2886 node: hir::DeclLocal(self.lower_local(l)),
2888 }), self.lower_node_id(s.id).node_id),
2891 StmtKind::Item(ref it) => {
2892 // Can only use the ID once.
2893 let mut id = Some(s.id);
2894 return self.lower_item_id(it).into_iter().map(|item_id| Spanned {
2895 node: hir::StmtDecl(P(Spanned {
2896 node: hir::DeclItem(item_id),
2899 .map(|id| self.lower_node_id(id).node_id)
2900 .unwrap_or_else(|| self.next_id().node_id)),
2904 StmtKind::Expr(ref e) => {
2906 node: hir::StmtExpr(P(self.lower_expr(e)),
2907 self.lower_node_id(s.id).node_id),
2911 StmtKind::Semi(ref e) => {
2913 node: hir::StmtSemi(P(self.lower_expr(e)),
2914 self.lower_node_id(s.id).node_id),
2918 StmtKind::Mac(..) => panic!("Shouldn't exist here"),
2922 fn lower_capture_clause(&mut self, c: CaptureBy) -> hir::CaptureClause {
2924 CaptureBy::Value => hir::CaptureByValue,
2925 CaptureBy::Ref => hir::CaptureByRef,
2929 /// If an `explicit_owner` is given, this method allocates the `HirId` in
2930 /// the address space of that item instead of the item currently being
2931 /// lowered. This can happen during `lower_impl_item_ref()` where we need to
2932 /// lower a `Visibility` value although we haven't lowered the owning
2933 /// `ImplItem` in question yet.
2934 fn lower_visibility(&mut self,
2936 explicit_owner: Option<NodeId>)
2937 -> hir::Visibility {
2939 Visibility::Public => hir::Public,
2940 Visibility::Crate(..) => hir::Visibility::Crate,
2941 Visibility::Restricted { ref path, id } => {
2942 hir::Visibility::Restricted {
2943 path: P(self.lower_path(id, path, ParamMode::Explicit, true)),
2944 id: if let Some(owner) = explicit_owner {
2945 self.lower_node_id_with_owner(id, owner).node_id
2947 self.lower_node_id(id).node_id
2951 Visibility::Inherited => hir::Inherited,
2955 fn lower_defaultness(&mut self, d: Defaultness, has_value: bool) -> hir::Defaultness {
2957 Defaultness::Default => hir::Defaultness::Default { has_value: has_value },
2958 Defaultness::Final => {
2960 hir::Defaultness::Final
2965 fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
2967 BlockCheckMode::Default => hir::DefaultBlock,
2968 BlockCheckMode::Unsafe(u) => hir::UnsafeBlock(self.lower_unsafe_source(u)),
2972 fn lower_binding_mode(&mut self, b: &BindingMode) -> hir::BindingAnnotation {
2974 BindingMode::ByValue(Mutability::Immutable) =>
2975 hir::BindingAnnotation::Unannotated,
2976 BindingMode::ByRef(Mutability::Immutable) => hir::BindingAnnotation::Ref,
2977 BindingMode::ByValue(Mutability::Mutable) => hir::BindingAnnotation::Mutable,
2978 BindingMode::ByRef(Mutability::Mutable) => hir::BindingAnnotation::RefMut,
2982 fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
2984 CompilerGenerated => hir::CompilerGenerated,
2985 UserProvided => hir::UserProvided,
2989 fn lower_impl_polarity(&mut self, i: ImplPolarity) -> hir::ImplPolarity {
2991 ImplPolarity::Positive => hir::ImplPolarity::Positive,
2992 ImplPolarity::Negative => hir::ImplPolarity::Negative,
2996 fn lower_trait_bound_modifier(&mut self, f: TraitBoundModifier) -> hir::TraitBoundModifier {
2998 TraitBoundModifier::None => hir::TraitBoundModifier::None,
2999 TraitBoundModifier::Maybe => hir::TraitBoundModifier::Maybe,
3003 // Helper methods for building HIR.
3005 fn arm(&mut self, pats: hir::HirVec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
3014 fn field(&mut self, name: Name, expr: P<hir::Expr>, span: Span) -> hir::Field {
3022 is_shorthand: false,
3026 fn expr_break(&mut self, span: Span, attrs: ThinVec<Attribute>) -> P<hir::Expr> {
3027 let expr_break = hir::ExprBreak(self.lower_loop_destination(None), None);
3028 P(self.expr(span, expr_break, attrs))
3031 fn expr_call(&mut self, span: Span, e: P<hir::Expr>, args: hir::HirVec<hir::Expr>)
3033 self.expr(span, hir::ExprCall(e, args), ThinVec::new())
3036 fn expr_ident(&mut self, span: Span, id: Name, binding: NodeId) -> hir::Expr {
3037 self.expr_ident_with_attrs(span, id, binding, ThinVec::new())
3040 fn expr_ident_with_attrs(&mut self, span: Span,
3043 attrs: ThinVec<Attribute>) -> hir::Expr {
3044 let expr_path = hir::ExprPath(hir::QPath::Resolved(None, P(hir::Path {
3046 def: Def::Local(binding),
3047 segments: hir_vec![hir::PathSegment::from_name(id)],
3050 self.expr(span, expr_path, attrs)
3053 fn expr_mut_addr_of(&mut self, span: Span, e: P<hir::Expr>) -> hir::Expr {
3054 self.expr(span, hir::ExprAddrOf(hir::MutMutable, e), ThinVec::new())
3057 fn expr_std_path(&mut self,
3059 components: &[&str],
3060 attrs: ThinVec<Attribute>)
3062 let path = self.std_path(span, components, true);
3063 self.expr(span, hir::ExprPath(hir::QPath::Resolved(None, P(path))), attrs)
3066 fn expr_match(&mut self,
3069 arms: hir::HirVec<hir::Arm>,
3070 source: hir::MatchSource)
3072 self.expr(span, hir::ExprMatch(arg, arms, source), ThinVec::new())
3075 fn expr_block(&mut self, b: P<hir::Block>, attrs: ThinVec<Attribute>) -> hir::Expr {
3076 self.expr(b.span, hir::ExprBlock(b), attrs)
3079 fn expr_tuple(&mut self, sp: Span, exprs: hir::HirVec<hir::Expr>) -> P<hir::Expr> {
3080 P(self.expr(sp, hir::ExprTup(exprs), ThinVec::new()))
3083 fn expr(&mut self, span: Span, node: hir::Expr_, attrs: ThinVec<Attribute>) -> hir::Expr {
3084 let LoweredNodeId { node_id, hir_id } = self.next_id();
3094 fn stmt_let_pat(&mut self,
3096 ex: Option<P<hir::Expr>>,
3098 source: hir::LocalSource)
3100 let LoweredNodeId { node_id, hir_id } = self.next_id();
3102 let local = P(hir::Local {
3109 attrs: ThinVec::new(),
3112 let decl = respan(sp, hir::DeclLocal(local));
3113 respan(sp, hir::StmtDecl(P(decl), self.next_id().node_id))
3116 fn stmt_let(&mut self, sp: Span, mutbl: bool, ident: Name, ex: P<hir::Expr>)
3117 -> (hir::Stmt, NodeId) {
3118 let pat = if mutbl {
3119 self.pat_ident_binding_mode(sp, ident, hir::BindingAnnotation::Mutable)
3121 self.pat_ident(sp, ident)
3123 let pat_id = pat.id;
3124 (self.stmt_let_pat(sp, Some(ex), pat, hir::LocalSource::Normal), pat_id)
3127 fn block_expr(&mut self, expr: P<hir::Expr>) -> hir::Block {
3128 self.block_all(expr.span, hir::HirVec::new(), Some(expr))
3131 fn block_all(&mut self, span: Span, stmts: hir::HirVec<hir::Stmt>, expr: Option<P<hir::Expr>>)
3133 let LoweredNodeId { node_id, hir_id } = self.next_id();
3140 rules: hir::DefaultBlock,
3142 targeted_by_break: false,
3146 fn pat_ok(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
3147 self.pat_std_enum(span, &["result", "Result", "Ok"], hir_vec![pat])
3150 fn pat_err(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
3151 self.pat_std_enum(span, &["result", "Result", "Err"], hir_vec![pat])
3154 fn pat_some(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
3155 self.pat_std_enum(span, &["option", "Option", "Some"], hir_vec![pat])
3158 fn pat_none(&mut self, span: Span) -> P<hir::Pat> {
3159 self.pat_std_enum(span, &["option", "Option", "None"], hir_vec![])
3162 fn pat_std_enum(&mut self,
3164 components: &[&str],
3165 subpats: hir::HirVec<P<hir::Pat>>)
3167 let path = self.std_path(span, components, true);
3168 let qpath = hir::QPath::Resolved(None, P(path));
3169 let pt = if subpats.is_empty() {
3170 hir::PatKind::Path(qpath)
3172 hir::PatKind::TupleStruct(qpath, subpats, None)
3177 fn pat_ident(&mut self, span: Span, name: Name) -> P<hir::Pat> {
3178 self.pat_ident_binding_mode(span, name, hir::BindingAnnotation::Unannotated)
3181 fn pat_ident_binding_mode(&mut self, span: Span, name: Name, bm: hir::BindingAnnotation)
3183 let LoweredNodeId { node_id, hir_id } = self.next_id();
3188 node: hir::PatKind::Binding(bm,
3199 fn pat_wild(&mut self, span: Span) -> P<hir::Pat> {
3200 self.pat(span, hir::PatKind::Wild)
3203 fn pat(&mut self, span: Span, pat: hir::PatKind) -> P<hir::Pat> {
3204 let LoweredNodeId { node_id, hir_id } = self.next_id();
3213 /// Given suffix ["b","c","d"], returns path `::std::b::c::d` when
3214 /// `fld.cx.use_std`, and `::core::b::c::d` otherwise.
3215 /// The path is also resolved according to `is_value`.
3216 fn std_path(&mut self, span: Span, components: &[&str], is_value: bool) -> hir::Path {
3217 let mut path = hir::Path {
3220 segments: iter::once(keywords::CrateRoot.name()).chain({
3221 self.crate_root.into_iter().chain(components.iter().cloned()).map(Symbol::intern)
3222 }).map(hir::PathSegment::from_name).collect(),
3225 self.resolver.resolve_hir_path(&mut path, is_value);
3229 fn signal_block_expr(&mut self,
3230 stmts: hir::HirVec<hir::Stmt>,
3233 rule: hir::BlockCheckMode,
3234 attrs: ThinVec<Attribute>)
3236 let LoweredNodeId { node_id, hir_id } = self.next_id();
3238 let block = P(hir::Block {
3245 targeted_by_break: false,
3247 self.expr_block(block, attrs)
3250 fn ty_path(&mut self, id: LoweredNodeId, span: Span, qpath: hir::QPath) -> P<hir::Ty> {
3252 let node = match qpath {
3253 hir::QPath::Resolved(None, path) => {
3254 // Turn trait object paths into `TyTraitObject` instead.
3255 if let Def::Trait(_) = path.def {
3256 let principal = hir::PolyTraitRef {
3257 bound_lifetimes: hir_vec![],
3258 trait_ref: hir::TraitRef {
3259 path: path.and_then(|path| path),
3265 // The original ID is taken by the `PolyTraitRef`,
3266 // so the `Ty` itself needs a different one.
3267 id = self.next_id();
3269 hir::TyTraitObject(hir_vec![principal], self.elided_lifetime(span))
3271 hir::TyPath(hir::QPath::Resolved(None, path))
3274 _ => hir::TyPath(qpath)
3276 P(hir::Ty { id: id.node_id, hir_id: id.hir_id, node, span })
3279 fn elided_lifetime(&mut self, span: Span) -> hir::Lifetime {
3281 id: self.next_id().node_id,
3283 name: hir::LifetimeName::Implicit,
3288 fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
3289 // Sorting by span ensures that we get things in order within a
3290 // file, and also puts the files in a sensible order.
3291 let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
3292 body_ids.sort_by_key(|b| bodies[b].value.span);