]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_lowering/src/lib.rs
Deeply check that method signatures match, and allow for nested RPITITs
[rust.git] / compiler / rustc_ast_lowering / src / lib.rs
1 //! Lowers the AST to the HIR.
2 //!
3 //! Since the AST and HIR are fairly similar, this is mostly a simple procedure,
4 //! much like a fold. Where lowering involves a bit more work things get more
5 //! interesting and there are some invariants you should know about. These mostly
6 //! concern spans and IDs.
7 //!
8 //! Spans are assigned to AST nodes during parsing and then are modified during
9 //! expansion to indicate the origin of a node and the process it went through
10 //! being expanded. IDs are assigned to AST nodes just before lowering.
11 //!
12 //! For the simpler lowering steps, IDs and spans should be preserved. Unlike
13 //! expansion we do not preserve the process of lowering in the spans, so spans
14 //! should not be modified here. When creating a new node (as opposed to
15 //! "folding" an existing one), create a new ID using `next_id()`.
16 //!
17 //! You must ensure that IDs are unique. That means that you should only use the
18 //! ID from an AST node in a single HIR node (you can assume that AST node-IDs
19 //! are unique). Every new node must have a unique ID. Avoid cloning HIR nodes.
20 //! If you do, you must then set the new node's ID to a fresh one.
21 //!
22 //! Spans are used for error messages and for tools to map semantics back to
23 //! source code. It is therefore not as important with spans as IDs to be strict
24 //! about use (you can't break the compiler by screwing up a span). Obviously, a
25 //! HIR node can only have a single span. But multiple nodes can have the same
26 //! span and spans don't need to be kept in order, etc. Where code is preserved
27 //! by lowering, it should have the same span as in the AST. Where HIR nodes are
28 //! new it is probably best to give a span for the whole AST node being lowered.
29 //! All nodes should have real spans; don't use dummy spans. Tools are likely to
30 //! get confused if the spans from leaf AST nodes occur in multiple places
31 //! in the HIR, especially for multiple identifiers.
32
33 #![feature(box_patterns)]
34 #![feature(let_chains)]
35 #![feature(let_else)]
36 #![feature(never_type)]
37 #![recursion_limit = "256"]
38 #![allow(rustc::potential_query_instability)]
39 #![deny(rustc::untranslatable_diagnostic)]
40 #![deny(rustc::diagnostic_outside_of_impl)]
41
42 #[macro_use]
43 extern crate tracing;
44
45 use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait, TraitFnAsync};
46
47 use rustc_arena::declare_arena;
48 use rustc_ast::ptr::P;
49 use rustc_ast::visit;
50 use rustc_ast::{self as ast, *};
51 use rustc_ast_pretty::pprust;
52 use rustc_data_structures::captures::Captures;
53 use rustc_data_structures::fingerprint::Fingerprint;
54 use rustc_data_structures::fx::FxHashMap;
55 use rustc_data_structures::sorted_map::SortedMap;
56 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
57 use rustc_data_structures::sync::Lrc;
58 use rustc_errors::{DiagnosticArgFromDisplay, Handler, StashKey};
59 use rustc_hir as hir;
60 use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
61 use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
62 use rustc_hir::definitions::DefPathData;
63 use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate};
64 use rustc_index::vec::{Idx, IndexVec};
65 use rustc_middle::span_bug;
66 use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
67 use rustc_session::parse::feature_err;
68 use rustc_span::hygiene::MacroKind;
69 use rustc_span::source_map::DesugaringKind;
70 use rustc_span::symbol::{kw, sym, Ident, Symbol};
71 use rustc_span::{Span, DUMMY_SP};
72
73 use smallvec::SmallVec;
74 use std::collections::hash_map::Entry;
75
76 macro_rules! arena_vec {
77     ($this:expr; $($x:expr),*) => (
78         $this.arena.alloc_from_iter([$($x),*])
79     );
80 }
81
82 mod asm;
83 mod block;
84 mod errors;
85 mod expr;
86 mod index;
87 mod item;
88 mod lifetime_collector;
89 mod pat;
90 mod path;
91
92 struct LoweringContext<'a, 'hir> {
93     tcx: TyCtxt<'hir>,
94     resolver: &'a mut ResolverAstLowering,
95
96     /// Used to allocate HIR nodes.
97     arena: &'hir hir::Arena<'hir>,
98
99     /// Used to allocate temporary AST nodes for use during lowering.
100     /// This allows us to create "fake" AST -- these nodes can sometimes
101     /// be allocated on the stack, but other times we need them to live longer
102     /// than the current stack frame, so they can be collected into vectors
103     /// and things like that.
104     ast_arena: &'a Arena<'static>,
105
106     /// Bodies inside the owner being lowered.
107     bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
108     /// Attributes inside the owner being lowered.
109     attrs: SortedMap<hir::ItemLocalId, &'hir [Attribute]>,
110     /// Collect items that were created by lowering the current owner.
111     children: FxHashMap<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
112
113     generator_kind: Option<hir::GeneratorKind>,
114
115     /// When inside an `async` context, this is the `HirId` of the
116     /// `task_context` local bound to the resume argument of the generator.
117     task_context: Option<hir::HirId>,
118
119     /// Used to get the current `fn`'s def span to point to when using `await`
120     /// outside of an `async fn`.
121     current_item: Option<Span>,
122
123     catch_scope: Option<NodeId>,
124     loop_scope: Option<NodeId>,
125     is_in_loop_condition: bool,
126     is_in_trait_impl: bool,
127     is_in_dyn_type: bool,
128
129     current_hir_id_owner: LocalDefId,
130     item_local_id_counter: hir::ItemLocalId,
131     local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
132     trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
133
134     impl_trait_defs: Vec<hir::GenericParam<'hir>>,
135     impl_trait_bounds: Vec<hir::WherePredicate<'hir>>,
136
137     /// NodeIds that are lowered inside the current HIR owner.
138     node_id_to_local_id: FxHashMap<NodeId, hir::ItemLocalId>,
139
140     allow_try_trait: Option<Lrc<[Symbol]>>,
141     allow_gen_future: Option<Lrc<[Symbol]>>,
142     allow_into_future: Option<Lrc<[Symbol]>>,
143
144     /// Mapping from generics `def_id`s to TAIT generics `def_id`s.
145     /// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
146     /// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
147     /// field from the original parameter 'a to the new parameter 'a1.
148     generics_def_id_map: Vec<FxHashMap<LocalDefId, LocalDefId>>,
149 }
150
151 declare_arena!([
152     [] tys: rustc_ast::Ty,
153     [] aba: rustc_ast::AngleBracketedArgs,
154     [] ptr: rustc_ast::PolyTraitRef,
155     // This _marker field is needed because `declare_arena` creates `Arena<'tcx>` and we need to
156     // use `'tcx`. If we don't have this we get a compile error.
157     [] _marker: std::marker::PhantomData<&'tcx ()>,
158 ]);
159
160 trait ResolverAstLoweringExt {
161     fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>;
162     fn get_partial_res(&self, id: NodeId) -> Option<PartialRes>;
163     fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>>;
164     fn get_label_res(&self, id: NodeId) -> Option<NodeId>;
165     fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
166     fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
167     fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
168 }
169
170 impl ResolverAstLoweringExt for ResolverAstLowering {
171     fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
172         if let ExprKind::Path(None, path) = &expr.kind {
173             // Don't perform legacy const generics rewriting if the path already
174             // has generic arguments.
175             if path.segments.last().unwrap().args.is_some() {
176                 return None;
177             }
178
179             let partial_res = self.partial_res_map.get(&expr.id)?;
180             if partial_res.unresolved_segments() != 0 {
181                 return None;
182             }
183
184             if let Res::Def(DefKind::Fn, def_id) = partial_res.base_res() {
185                 // We only support cross-crate argument rewriting. Uses
186                 // within the same crate should be updated to use the new
187                 // const generics style.
188                 if def_id.is_local() {
189                     return None;
190                 }
191
192                 if let Some(v) = self.legacy_const_generic_args.get(&def_id) {
193                     return v.clone();
194                 }
195             }
196         }
197
198         None
199     }
200
201     /// Obtains resolution for a `NodeId` with a single resolution.
202     fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
203         self.partial_res_map.get(&id).copied()
204     }
205
206     /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
207     fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>> {
208         self.import_res_map.get(&id).copied().unwrap_or_default()
209     }
210
211     /// Obtains resolution for a label with the given `NodeId`.
212     fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
213         self.label_res_map.get(&id).copied()
214     }
215
216     /// Obtains resolution for a lifetime with the given `NodeId`.
217     fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
218         self.lifetimes_res_map.get(&id).copied()
219     }
220
221     /// Obtain the list of lifetimes parameters to add to an item.
222     ///
223     /// Extra lifetime parameters should only be added in places that can appear
224     /// as a `binder` in `LifetimeRes`.
225     ///
226     /// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
227     /// should appear at the enclosing `PolyTraitRef`.
228     fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
229         self.extra_lifetime_params_map.remove(&id).unwrap_or_default()
230     }
231
232     fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
233         self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
234     }
235 }
236
237 /// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
238 /// and if so, what meaning it has.
239 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
240 enum ImplTraitContext {
241     /// Treat `impl Trait` as shorthand for a new universal generic parameter.
242     /// Example: `fn foo(x: impl Debug)`, where `impl Debug` is conceptually
243     /// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
244     ///
245     /// Newly generated parameters should be inserted into the given `Vec`.
246     Universal,
247
248     /// Treat `impl Trait` as shorthand for a new opaque type.
249     /// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
250     /// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
251     ///
252     ReturnPositionOpaqueTy {
253         /// Origin: Either OpaqueTyOrigin::FnReturn or OpaqueTyOrigin::AsyncFn,
254         origin: hir::OpaqueTyOrigin,
255     },
256     /// Impl trait in type aliases.
257     TypeAliasesOpaqueTy,
258     /// Return-position `impl Trait` in trait definition
259     InTrait,
260     /// `impl Trait` is not accepted in this position.
261     Disallowed(ImplTraitPosition),
262 }
263
264 /// Position in which `impl Trait` is disallowed.
265 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
266 enum ImplTraitPosition {
267     Path,
268     Variable,
269     Type,
270     Trait,
271     AsyncBlock,
272     Bound,
273     Generic,
274     ExternFnParam,
275     ClosureParam,
276     PointerParam,
277     FnTraitParam,
278     TraitParam,
279     ImplParam,
280     ExternFnReturn,
281     ClosureReturn,
282     PointerReturn,
283     FnTraitReturn,
284     TraitReturn,
285     ImplReturn,
286 }
287
288 impl std::fmt::Display for ImplTraitPosition {
289     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
290         let name = match self {
291             ImplTraitPosition::Path => "path",
292             ImplTraitPosition::Variable => "variable binding",
293             ImplTraitPosition::Type => "type",
294             ImplTraitPosition::Trait => "trait",
295             ImplTraitPosition::AsyncBlock => "async block",
296             ImplTraitPosition::Bound => "bound",
297             ImplTraitPosition::Generic => "generic",
298             ImplTraitPosition::ExternFnParam => "`extern fn` param",
299             ImplTraitPosition::ClosureParam => "closure param",
300             ImplTraitPosition::PointerParam => "`fn` pointer param",
301             ImplTraitPosition::FnTraitParam => "`Fn` trait param",
302             ImplTraitPosition::TraitParam => "trait method param",
303             ImplTraitPosition::ImplParam => "`impl` method param",
304             ImplTraitPosition::ExternFnReturn => "`extern fn` return",
305             ImplTraitPosition::ClosureReturn => "closure return",
306             ImplTraitPosition::PointerReturn => "`fn` pointer return",
307             ImplTraitPosition::FnTraitReturn => "`Fn` trait return",
308             ImplTraitPosition::TraitReturn => "trait method return",
309             ImplTraitPosition::ImplReturn => "`impl` method return",
310         };
311
312         write!(f, "{}", name)
313     }
314 }
315
316 #[derive(Debug)]
317 enum FnDeclKind {
318     Fn,
319     Inherent,
320     ExternFn,
321     Closure,
322     Pointer,
323     Trait,
324     Impl,
325 }
326
327 impl FnDeclKind {
328     fn impl_trait_return_allowed(&self, tcx: TyCtxt<'_>) -> bool {
329         match self {
330             FnDeclKind::Fn | FnDeclKind::Inherent => true,
331             FnDeclKind::Impl if tcx.features().return_position_impl_trait_in_trait => true,
332             _ => false,
333         }
334     }
335
336     fn impl_trait_in_trait_allowed(&self, tcx: TyCtxt<'_>) -> bool {
337         match self {
338             FnDeclKind::Trait if tcx.features().return_position_impl_trait_in_trait => true,
339             _ => false,
340         }
341     }
342 }
343
344 #[derive(Copy, Clone)]
345 enum AstOwner<'a> {
346     NonOwner,
347     Crate(&'a ast::Crate),
348     Item(&'a ast::Item),
349     AssocItem(&'a ast::AssocItem, visit::AssocCtxt),
350     ForeignItem(&'a ast::ForeignItem),
351 }
352
353 fn index_crate<'a>(
354     node_id_to_def_id: &FxHashMap<NodeId, LocalDefId>,
355     krate: &'a Crate,
356 ) -> IndexVec<LocalDefId, AstOwner<'a>> {
357     let mut indexer = Indexer { node_id_to_def_id, index: IndexVec::new() };
358     indexer.index.ensure_contains_elem(CRATE_DEF_ID, || AstOwner::NonOwner);
359     indexer.index[CRATE_DEF_ID] = AstOwner::Crate(krate);
360     visit::walk_crate(&mut indexer, krate);
361     return indexer.index;
362
363     struct Indexer<'s, 'a> {
364         node_id_to_def_id: &'s FxHashMap<NodeId, LocalDefId>,
365         index: IndexVec<LocalDefId, AstOwner<'a>>,
366     }
367
368     impl<'a> visit::Visitor<'a> for Indexer<'_, 'a> {
369         fn visit_attribute(&mut self, _: &'a Attribute) {
370             // We do not want to lower expressions that appear in attributes,
371             // as they are not accessible to the rest of the HIR.
372         }
373
374         fn visit_item(&mut self, item: &'a ast::Item) {
375             let def_id = self.node_id_to_def_id[&item.id];
376             self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
377             self.index[def_id] = AstOwner::Item(item);
378             visit::walk_item(self, item)
379         }
380
381         fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: visit::AssocCtxt) {
382             let def_id = self.node_id_to_def_id[&item.id];
383             self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
384             self.index[def_id] = AstOwner::AssocItem(item, ctxt);
385             visit::walk_assoc_item(self, item, ctxt);
386         }
387
388         fn visit_foreign_item(&mut self, item: &'a ast::ForeignItem) {
389             let def_id = self.node_id_to_def_id[&item.id];
390             self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
391             self.index[def_id] = AstOwner::ForeignItem(item);
392             visit::walk_foreign_item(self, item);
393         }
394     }
395 }
396
397 /// Compute the hash for the HIR of the full crate.
398 /// This hash will then be part of the crate_hash which is stored in the metadata.
399 fn compute_hir_hash(
400     tcx: TyCtxt<'_>,
401     owners: &IndexVec<LocalDefId, hir::MaybeOwner<&hir::OwnerInfo<'_>>>,
402 ) -> Fingerprint {
403     let mut hir_body_nodes: Vec<_> = owners
404         .iter_enumerated()
405         .filter_map(|(def_id, info)| {
406             let info = info.as_owner()?;
407             let def_path_hash = tcx.hir().def_path_hash(def_id);
408             Some((def_path_hash, info))
409         })
410         .collect();
411     hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
412
413     tcx.with_stable_hashing_context(|mut hcx| {
414         let mut stable_hasher = StableHasher::new();
415         hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
416         stable_hasher.finish()
417     })
418 }
419
420 pub fn lower_to_hir<'hir>(tcx: TyCtxt<'hir>, (): ()) -> hir::Crate<'hir> {
421     let sess = tcx.sess;
422     let krate = tcx.untracked_crate.steal();
423     let mut resolver = tcx.resolver_for_lowering(()).steal();
424
425     let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);
426     let mut owners = IndexVec::from_fn_n(
427         |_| hir::MaybeOwner::Phantom,
428         tcx.definitions_untracked().def_index_count(),
429     );
430
431     let ast_arena = Arena::default();
432
433     for def_id in ast_index.indices() {
434         item::ItemLowerer {
435             tcx,
436             resolver: &mut resolver,
437             ast_arena: &ast_arena,
438             ast_index: &ast_index,
439             owners: &mut owners,
440         }
441         .lower_node(def_id);
442     }
443
444     // Drop AST to free memory
445     std::mem::drop(ast_index);
446     sess.time("drop_ast", || std::mem::drop(krate));
447
448     // Discard hygiene data, which isn't required after lowering to HIR.
449     if !sess.opts.unstable_opts.keep_hygiene_data {
450         rustc_span::hygiene::clear_syntax_context_map();
451     }
452
453     let hir_hash = compute_hir_hash(tcx, &owners);
454     hir::Crate { owners, hir_hash }
455 }
456
457 #[derive(Copy, Clone, PartialEq, Debug)]
458 enum ParamMode {
459     /// Any path in a type context.
460     Explicit,
461     /// Path in a type definition, where the anonymous lifetime `'_` is not allowed.
462     ExplicitNamed,
463     /// The `module::Type` in `module::Type::method` in an expression.
464     Optional,
465 }
466
467 enum ParenthesizedGenericArgs {
468     Ok,
469     Err,
470 }
471
472 impl<'a, 'hir> LoweringContext<'a, 'hir> {
473     fn create_def(
474         &mut self,
475         parent: LocalDefId,
476         node_id: ast::NodeId,
477         data: DefPathData,
478     ) -> LocalDefId {
479         debug_assert_ne!(node_id, ast::DUMMY_NODE_ID);
480         assert!(
481             self.opt_local_def_id(node_id).is_none(),
482             "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
483             node_id,
484             data,
485             self.tcx.hir().def_key(self.local_def_id(node_id)),
486         );
487
488         let def_id = self.tcx.create_def(parent, data);
489
490         debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
491         self.resolver.node_id_to_def_id.insert(node_id, def_id);
492
493         def_id
494     }
495
496     fn next_node_id(&mut self) -> NodeId {
497         let start = self.resolver.next_node_id;
498         let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
499         self.resolver.next_node_id = ast::NodeId::from_u32(next);
500         start
501     }
502
503     /// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
504     /// resolver (if any), after applying any remapping from `get_remapped_def_id`.
505     ///
506     /// For example, in a function like `fn foo<'a>(x: &'a u32)`,
507     /// invoking with the id from the `ast::Lifetime` node found inside
508     /// the `&'a u32` type would return the `LocalDefId` of the
509     /// `'a` parameter declared on `foo`.
510     ///
511     /// This function also applies remapping from `get_remapped_def_id`.
512     /// These are used when synthesizing opaque types from `-> impl Trait` return types and so forth.
513     /// For example, in a function like `fn foo<'a>() -> impl Debug + 'a`,
514     /// we would create an opaque type `type FooReturn<'a1> = impl Debug + 'a1`.
515     /// When lowering the `Debug + 'a` bounds, we add a remapping to map `'a` to `'a1`.
516     fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
517         self.resolver
518             .node_id_to_def_id
519             .get(&node)
520             .map(|local_def_id| self.get_remapped_def_id(*local_def_id))
521     }
522
523     fn local_def_id(&self, node: NodeId) -> LocalDefId {
524         self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
525     }
526
527     /// Get the previously recorded `to` local def id given the `from` local def id, obtained using
528     /// `generics_def_id_map` field.
529     fn get_remapped_def_id(&self, mut local_def_id: LocalDefId) -> LocalDefId {
530         // `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
531         // push new mappings so we need to try first the latest mappings, hence `iter().rev()`.
532         //
533         // Consider:
534         //
535         // `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}`
536         //
537         // We would end with a generics_def_id_map like:
538         //
539         // `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
540         //
541         // for the opaque type generated on `impl Sized + 'b`, We want the result to be:
542         // impl_sized#'b, so iterating forward is the wrong thing to do.
543         for map in self.generics_def_id_map.iter().rev() {
544             if let Some(r) = map.get(&local_def_id) {
545                 debug!("def_id_remapper: remapping from `{local_def_id:?}` to `{r:?}`");
546                 local_def_id = *r;
547             } else {
548                 debug!("def_id_remapper: no remapping for `{local_def_id:?}` found in map");
549             }
550         }
551
552         local_def_id
553     }
554
555     /// Freshen the `LoweringContext` and ready it to lower a nested item.
556     /// The lowered item is registered into `self.children`.
557     ///
558     /// This function sets up `HirId` lowering infrastructure,
559     /// and stashes the shared mutable state to avoid pollution by the closure.
560     #[instrument(level = "debug", skip(self, f))]
561     fn with_hir_id_owner(
562         &mut self,
563         owner: NodeId,
564         f: impl FnOnce(&mut Self) -> hir::OwnerNode<'hir>,
565     ) {
566         let def_id = self.local_def_id(owner);
567
568         let current_attrs = std::mem::take(&mut self.attrs);
569         let current_bodies = std::mem::take(&mut self.bodies);
570         let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
571         let current_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id);
572         let current_trait_map = std::mem::take(&mut self.trait_map);
573         let current_owner = std::mem::replace(&mut self.current_hir_id_owner, def_id);
574         let current_local_counter =
575             std::mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1));
576         let current_impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
577         let current_impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);
578
579         // Do not reset `next_node_id` and `node_id_to_def_id`:
580         // we want `f` to be able to refer to the `LocalDefId`s that the caller created.
581         // and the caller to refer to some of the subdefinitions' nodes' `LocalDefId`s.
582
583         // Always allocate the first `HirId` for the owner itself.
584         let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::new(0));
585         debug_assert_eq!(_old, None);
586
587         let item = f(self);
588         debug_assert_eq!(def_id, item.def_id());
589         // `f` should have consumed all the elements in these vectors when constructing `item`.
590         debug_assert!(self.impl_trait_defs.is_empty());
591         debug_assert!(self.impl_trait_bounds.is_empty());
592         let info = self.make_owner_info(item);
593
594         self.attrs = current_attrs;
595         self.bodies = current_bodies;
596         self.node_id_to_local_id = current_node_ids;
597         self.local_id_to_def_id = current_id_to_def_id;
598         self.trait_map = current_trait_map;
599         self.current_hir_id_owner = current_owner;
600         self.item_local_id_counter = current_local_counter;
601         self.impl_trait_defs = current_impl_trait_defs;
602         self.impl_trait_bounds = current_impl_trait_bounds;
603
604         let _old = self.children.insert(def_id, hir::MaybeOwner::Owner(info));
605         debug_assert!(_old.is_none())
606     }
607
608     /// Installs the remapping `remap` in scope while `f` is being executed.
609     /// This causes references to the `LocalDefId` keys to be changed to
610     /// refer to the values instead.
611     ///
612     /// The remapping is used when one piece of AST expands to multiple
613     /// pieces of HIR. For example, the function `fn foo<'a>(...) -> impl Debug + 'a`,
614     /// expands to both a function definition (`foo`) and a TAIT for the return value,
615     /// both of which have a lifetime parameter `'a`. The remapping allows us to
616     /// rewrite the `'a` in the return value to refer to the
617     /// `'a` declared on the TAIT, instead of the function.
618     fn with_remapping<R>(
619         &mut self,
620         remap: FxHashMap<LocalDefId, LocalDefId>,
621         f: impl FnOnce(&mut Self) -> R,
622     ) -> R {
623         self.generics_def_id_map.push(remap);
624         let res = f(self);
625         self.generics_def_id_map.pop();
626         res
627     }
628
629     fn make_owner_info(&mut self, node: hir::OwnerNode<'hir>) -> &'hir hir::OwnerInfo<'hir> {
630         let attrs = std::mem::take(&mut self.attrs);
631         let mut bodies = std::mem::take(&mut self.bodies);
632         let local_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id);
633         let trait_map = std::mem::take(&mut self.trait_map);
634
635         #[cfg(debug_assertions)]
636         for (id, attrs) in attrs.iter() {
637             // Verify that we do not store empty slices in the map.
638             if attrs.is_empty() {
639                 panic!("Stored empty attributes for {:?}", id);
640             }
641         }
642
643         bodies.sort_by_key(|(k, _)| *k);
644         let bodies = SortedMap::from_presorted_elements(bodies);
645         let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies);
646         let (nodes, parenting) =
647             index::index_hir(self.tcx.sess, &*self.tcx.definitions_untracked(), node, &bodies);
648         let nodes = hir::OwnerNodes {
649             hash_including_bodies,
650             hash_without_bodies,
651             nodes,
652             bodies,
653             local_id_to_def_id,
654         };
655         let attrs = {
656             let hash = self.tcx.with_stable_hashing_context(|mut hcx| {
657                 let mut stable_hasher = StableHasher::new();
658                 attrs.hash_stable(&mut hcx, &mut stable_hasher);
659                 stable_hasher.finish()
660             });
661             hir::AttributeMap { map: attrs, hash }
662         };
663
664         self.arena.alloc(hir::OwnerInfo { nodes, parenting, attrs, trait_map })
665     }
666
667     /// Hash the HIR node twice, one deep and one shallow hash.  This allows to differentiate
668     /// queries which depend on the full HIR tree and those which only depend on the item signature.
669     fn hash_owner(
670         &mut self,
671         node: hir::OwnerNode<'hir>,
672         bodies: &SortedMap<hir::ItemLocalId, &'hir hir::Body<'hir>>,
673     ) -> (Fingerprint, Fingerprint) {
674         self.tcx.with_stable_hashing_context(|mut hcx| {
675             let mut stable_hasher = StableHasher::new();
676             hcx.with_hir_bodies(node.def_id(), bodies, |hcx| {
677                 node.hash_stable(hcx, &mut stable_hasher)
678             });
679             let hash_including_bodies = stable_hasher.finish();
680             let mut stable_hasher = StableHasher::new();
681             hcx.without_hir_bodies(|hcx| node.hash_stable(hcx, &mut stable_hasher));
682             let hash_without_bodies = stable_hasher.finish();
683             (hash_including_bodies, hash_without_bodies)
684         })
685     }
686
687     /// This method allocates a new `HirId` for the given `NodeId` and stores it in
688     /// the `LoweringContext`'s `NodeId => HirId` map.
689     /// Take care not to call this method if the resulting `HirId` is then not
690     /// actually used in the HIR, as that would trigger an assertion in the
691     /// `HirIdValidator` later on, which makes sure that all `NodeId`s got mapped
692     /// properly. Calling the method twice with the same `NodeId` is fine though.
693     #[instrument(level = "debug", skip(self), ret)]
694     fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
695         assert_ne!(ast_node_id, DUMMY_NODE_ID);
696
697         match self.node_id_to_local_id.entry(ast_node_id) {
698             Entry::Occupied(o) => {
699                 hir::HirId { owner: self.current_hir_id_owner, local_id: *o.get() }
700             }
701             Entry::Vacant(v) => {
702                 // Generate a new `HirId`.
703                 let owner = self.current_hir_id_owner;
704                 let local_id = self.item_local_id_counter;
705                 let hir_id = hir::HirId { owner, local_id };
706
707                 v.insert(local_id);
708                 self.item_local_id_counter.increment_by(1);
709
710                 assert_ne!(local_id, hir::ItemLocalId::new(0));
711                 if let Some(def_id) = self.opt_local_def_id(ast_node_id) {
712                     // Do not override a `MaybeOwner::Owner` that may already here.
713                     self.children.entry(def_id).or_insert(hir::MaybeOwner::NonOwner(hir_id));
714                     self.local_id_to_def_id.insert(local_id, def_id);
715                 }
716
717                 if let Some(traits) = self.resolver.trait_map.remove(&ast_node_id) {
718                     self.trait_map.insert(hir_id.local_id, traits.into_boxed_slice());
719                 }
720
721                 hir_id
722             }
723         }
724     }
725
726     /// Generate a new `HirId` without a backing `NodeId`.
727     #[instrument(level = "debug", skip(self), ret)]
728     fn next_id(&mut self) -> hir::HirId {
729         let owner = self.current_hir_id_owner;
730         let local_id = self.item_local_id_counter;
731         assert_ne!(local_id, hir::ItemLocalId::new(0));
732         self.item_local_id_counter.increment_by(1);
733         hir::HirId { owner, local_id }
734     }
735
736     #[instrument(level = "trace", skip(self))]
737     fn lower_res(&mut self, res: Res<NodeId>) -> Res {
738         let res: Result<Res, ()> = res.apply_id(|id| {
739             let owner = self.current_hir_id_owner;
740             let local_id = self.node_id_to_local_id.get(&id).copied().ok_or(())?;
741             Ok(hir::HirId { owner, local_id })
742         });
743         trace!(?res);
744
745         // We may fail to find a HirId when the Res points to a Local from an enclosing HIR owner.
746         // This can happen when trying to lower the return type `x` in erroneous code like
747         //   async fn foo(x: u8) -> x {}
748         // In that case, `x` is lowered as a function parameter, and the return type is lowered as
749         // an opaque type as a synthesized HIR owner.
750         res.unwrap_or(Res::Err)
751     }
752
753     fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
754         self.resolver.get_partial_res(id).map_or(Res::Err, |pr| {
755             if pr.unresolved_segments() != 0 {
756                 panic!("path not fully resolved: {:?}", pr);
757             }
758             pr.base_res()
759         })
760     }
761
762     fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Res<NodeId>> {
763         self.resolver.get_import_res(id).present_items()
764     }
765
766     fn diagnostic(&self) -> &Handler {
767         self.tcx.sess.diagnostic()
768     }
769
770     /// Reuses the span but adds information like the kind of the desugaring and features that are
771     /// allowed inside this span.
772     fn mark_span_with_reason(
773         &self,
774         reason: DesugaringKind,
775         span: Span,
776         allow_internal_unstable: Option<Lrc<[Symbol]>>,
777     ) -> Span {
778         self.tcx.with_stable_hashing_context(|hcx| {
779             span.mark_with_reason(allow_internal_unstable, reason, self.tcx.sess.edition(), hcx)
780         })
781     }
782
783     /// Intercept all spans entering HIR.
784     /// Mark a span as relative to the current owning item.
785     fn lower_span(&self, span: Span) -> Span {
786         if self.tcx.sess.opts.unstable_opts.incremental_relative_spans {
787             span.with_parent(Some(self.current_hir_id_owner))
788         } else {
789             // Do not make spans relative when not using incremental compilation.
790             span
791         }
792     }
793
794     fn lower_ident(&self, ident: Ident) -> Ident {
795         Ident::new(ident.name, self.lower_span(ident.span))
796     }
797
798     /// Converts a lifetime into a new generic parameter.
799     #[instrument(level = "debug", skip(self))]
800     fn lifetime_res_to_generic_param(
801         &mut self,
802         ident: Ident,
803         node_id: NodeId,
804         res: LifetimeRes,
805     ) -> Option<hir::GenericParam<'hir>> {
806         let (name, kind) = match res {
807             LifetimeRes::Param { .. } => {
808                 (hir::ParamName::Plain(ident), hir::LifetimeParamKind::Explicit)
809             }
810             LifetimeRes::Fresh { param, .. } => {
811                 // Late resolution delegates to us the creation of the `LocalDefId`.
812                 let _def_id = self.create_def(
813                     self.current_hir_id_owner,
814                     param,
815                     DefPathData::LifetimeNs(kw::UnderscoreLifetime),
816                 );
817                 debug!(?_def_id);
818
819                 (hir::ParamName::Fresh, hir::LifetimeParamKind::Elided)
820             }
821             LifetimeRes::Static | LifetimeRes::Error => return None,
822             res => panic!(
823                 "Unexpected lifetime resolution {:?} for {:?} at {:?}",
824                 res, ident, ident.span
825             ),
826         };
827         let hir_id = self.lower_node_id(node_id);
828         Some(hir::GenericParam {
829             hir_id,
830             name,
831             span: self.lower_span(ident.span),
832             pure_wrt_drop: false,
833             kind: hir::GenericParamKind::Lifetime { kind },
834             colon_span: None,
835         })
836     }
837
838     /// Lowers a lifetime binder that defines `generic_params`, returning the corresponding HIR
839     /// nodes. The returned list includes any "extra" lifetime parameters that were added by the
840     /// name resolver owing to lifetime elision; this also populates the resolver's node-id->def-id
841     /// map, so that later calls to `opt_node_id_to_def_id` that refer to these extra lifetime
842     /// parameters will be successful.
843     #[instrument(level = "debug", skip(self, in_binder))]
844     #[inline]
845     fn lower_lifetime_binder<R>(
846         &mut self,
847         binder: NodeId,
848         generic_params: &[GenericParam],
849         in_binder: impl FnOnce(&mut Self, &'hir [hir::GenericParam<'hir>]) -> R,
850     ) -> R {
851         let extra_lifetimes = self.resolver.take_extra_lifetime_params(binder);
852         debug!(?extra_lifetimes);
853         let extra_lifetimes: Vec<_> = extra_lifetimes
854             .into_iter()
855             .filter_map(|(ident, node_id, res)| {
856                 self.lifetime_res_to_generic_param(ident, node_id, res)
857             })
858             .collect();
859
860         let generic_params: Vec<_> = self
861             .lower_generic_params_mut(generic_params)
862             .chain(extra_lifetimes.into_iter())
863             .collect();
864         let generic_params = self.arena.alloc_from_iter(generic_params);
865         debug!(?generic_params);
866
867         in_binder(self, generic_params)
868     }
869
870     fn with_dyn_type_scope<T>(&mut self, in_scope: bool, f: impl FnOnce(&mut Self) -> T) -> T {
871         let was_in_dyn_type = self.is_in_dyn_type;
872         self.is_in_dyn_type = in_scope;
873
874         let result = f(self);
875
876         self.is_in_dyn_type = was_in_dyn_type;
877
878         result
879     }
880
881     fn with_new_scopes<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
882         let was_in_loop_condition = self.is_in_loop_condition;
883         self.is_in_loop_condition = false;
884
885         let catch_scope = self.catch_scope.take();
886         let loop_scope = self.loop_scope.take();
887         let ret = f(self);
888         self.catch_scope = catch_scope;
889         self.loop_scope = loop_scope;
890
891         self.is_in_loop_condition = was_in_loop_condition;
892
893         ret
894     }
895
896     fn lower_attrs(&mut self, id: hir::HirId, attrs: &[Attribute]) -> Option<&'hir [Attribute]> {
897         if attrs.is_empty() {
898             None
899         } else {
900             debug_assert_eq!(id.owner, self.current_hir_id_owner);
901             let ret = self.arena.alloc_from_iter(attrs.iter().map(|a| self.lower_attr(a)));
902             debug_assert!(!ret.is_empty());
903             self.attrs.insert(id.local_id, ret);
904             Some(ret)
905         }
906     }
907
908     fn lower_attr(&self, attr: &Attribute) -> Attribute {
909         // Note that we explicitly do not walk the path. Since we don't really
910         // lower attributes (we use the AST version) there is nowhere to keep
911         // the `HirId`s. We don't actually need HIR version of attributes anyway.
912         // Tokens are also not needed after macro expansion and parsing.
913         let kind = match attr.kind {
914             AttrKind::Normal(ref normal) => AttrKind::Normal(P(NormalAttr {
915                 item: AttrItem {
916                     path: normal.item.path.clone(),
917                     args: self.lower_mac_args(&normal.item.args),
918                     tokens: None,
919                 },
920                 tokens: None,
921             })),
922             AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data),
923         };
924
925         Attribute { kind, id: attr.id, style: attr.style, span: self.lower_span(attr.span) }
926     }
927
928     fn alias_attrs(&mut self, id: hir::HirId, target_id: hir::HirId) {
929         debug_assert_eq!(id.owner, self.current_hir_id_owner);
930         debug_assert_eq!(target_id.owner, self.current_hir_id_owner);
931         if let Some(&a) = self.attrs.get(&target_id.local_id) {
932             debug_assert!(!a.is_empty());
933             self.attrs.insert(id.local_id, a);
934         }
935     }
936
937     fn lower_mac_args(&self, args: &MacArgs) -> MacArgs {
938         match *args {
939             MacArgs::Empty => MacArgs::Empty,
940             MacArgs::Delimited(dspan, delim, ref tokens) => {
941                 // This is either a non-key-value attribute, or a `macro_rules!` body.
942                 // We either not have any nonterminals present (in the case of an attribute),
943                 // or have tokens available for all nonterminals in the case of a nested
944                 // `macro_rules`: e.g:
945                 //
946                 // ```rust
947                 // macro_rules! outer {
948                 //     ($e:expr) => {
949                 //         macro_rules! inner {
950                 //             () => { $e }
951                 //         }
952                 //     }
953                 // }
954                 // ```
955                 //
956                 // In both cases, we don't want to synthesize any tokens
957                 MacArgs::Delimited(dspan, delim, tokens.flattened())
958             }
959             // This is an inert key-value attribute - it will never be visible to macros
960             // after it gets lowered to HIR. Therefore, we can extract literals to handle
961             // nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
962             MacArgs::Eq(eq_span, MacArgsEq::Ast(ref expr)) => {
963                 // In valid code the value always ends up as a single literal. Otherwise, a dummy
964                 // literal suffices because the error is handled elsewhere.
965                 let lit = if let ExprKind::Lit(lit) = &expr.kind {
966                     lit.clone()
967                 } else {
968                     Lit {
969                         token_lit: token::Lit::new(token::LitKind::Err, kw::Empty, None),
970                         kind: LitKind::Err,
971                         span: DUMMY_SP,
972                     }
973                 };
974                 MacArgs::Eq(eq_span, MacArgsEq::Hir(lit))
975             }
976             MacArgs::Eq(_, MacArgsEq::Hir(ref lit)) => {
977                 unreachable!("in literal form when lowering mac args eq: {:?}", lit)
978             }
979         }
980     }
981
982     /// Given an associated type constraint like one of these:
983     ///
984     /// ```ignore (illustrative)
985     /// T: Iterator<Item: Debug>
986     ///             ^^^^^^^^^^^
987     /// T: Iterator<Item = Debug>
988     ///             ^^^^^^^^^^^^
989     /// ```
990     ///
991     /// returns a `hir::TypeBinding` representing `Item`.
992     #[instrument(level = "debug", skip(self))]
993     fn lower_assoc_ty_constraint(
994         &mut self,
995         constraint: &AssocConstraint,
996         itctx: &mut ImplTraitContext,
997     ) -> hir::TypeBinding<'hir> {
998         debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
999         // lower generic arguments of identifier in constraint
1000         let gen_args = if let Some(ref gen_args) = constraint.gen_args {
1001             let gen_args_ctor = match gen_args {
1002                 GenericArgs::AngleBracketed(ref data) => {
1003                     self.lower_angle_bracketed_parameter_data(data, ParamMode::Explicit, itctx).0
1004                 }
1005                 GenericArgs::Parenthesized(ref data) => {
1006                     self.emit_bad_parenthesized_trait_in_assoc_ty(data);
1007                     let aba = self.ast_arena.aba.alloc(data.as_angle_bracketed_args());
1008                     self.lower_angle_bracketed_parameter_data(aba, ParamMode::Explicit, itctx).0
1009                 }
1010             };
1011             gen_args_ctor.into_generic_args(self)
1012         } else {
1013             self.arena.alloc(hir::GenericArgs::none())
1014         };
1015         let mut itctx_tait = ImplTraitContext::TypeAliasesOpaqueTy;
1016
1017         let kind = match constraint.kind {
1018             AssocConstraintKind::Equality { ref term } => {
1019                 let term = match term {
1020                     Term::Ty(ref ty) => self.lower_ty(ty, itctx).into(),
1021                     Term::Const(ref c) => self.lower_anon_const(c).into(),
1022                 };
1023                 hir::TypeBindingKind::Equality { term }
1024             }
1025             AssocConstraintKind::Bound { ref bounds } => {
1026                 // Piggy-back on the `impl Trait` context to figure out the correct behavior.
1027                 let (desugar_to_impl_trait, itctx) = match itctx {
1028                     // We are in the return position:
1029                     //
1030                     //     fn foo() -> impl Iterator<Item: Debug>
1031                     //
1032                     // so desugar to
1033                     //
1034                     //     fn foo() -> impl Iterator<Item = impl Debug>
1035                     ImplTraitContext::ReturnPositionOpaqueTy { .. }
1036                     | ImplTraitContext::TypeAliasesOpaqueTy { .. } => (true, itctx),
1037
1038                     // We are in the argument position, but within a dyn type:
1039                     //
1040                     //     fn foo(x: dyn Iterator<Item: Debug>)
1041                     //
1042                     // so desugar to
1043                     //
1044                     //     fn foo(x: dyn Iterator<Item = impl Debug>)
1045                     ImplTraitContext::Universal if self.is_in_dyn_type => (true, itctx),
1046
1047                     // In `type Foo = dyn Iterator<Item: Debug>` we desugar to
1048                     // `type Foo = dyn Iterator<Item = impl Debug>` but we have to override the
1049                     // "impl trait context" to permit `impl Debug` in this position (it desugars
1050                     // then to an opaque type).
1051                     //
1052                     // FIXME: this is only needed until `impl Trait` is allowed in type aliases.
1053                     ImplTraitContext::Disallowed(_) if self.is_in_dyn_type => {
1054                         (true, &mut itctx_tait)
1055                     }
1056
1057                     // We are in the parameter position, but not within a dyn type:
1058                     //
1059                     //     fn foo(x: impl Iterator<Item: Debug>)
1060                     //
1061                     // so we leave it as is and this gets expanded in astconv to a bound like
1062                     // `<T as Iterator>::Item: Debug` where `T` is the type parameter for the
1063                     // `impl Iterator`.
1064                     _ => (false, itctx),
1065                 };
1066
1067                 if desugar_to_impl_trait {
1068                     // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
1069                     // constructing the HIR for `impl bounds...` and then lowering that.
1070
1071                     let parent_def_id = self.current_hir_id_owner;
1072                     let impl_trait_node_id = self.next_node_id();
1073                     self.create_def(parent_def_id, impl_trait_node_id, DefPathData::ImplTrait);
1074
1075                     self.with_dyn_type_scope(false, |this| {
1076                         let node_id = this.next_node_id();
1077                         let ty = this.ast_arena.tys.alloc(Ty {
1078                             id: node_id,
1079                             kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
1080                             span: this.lower_span(constraint.span),
1081                             tokens: None,
1082                         });
1083                         let ty = this.lower_ty(ty, itctx);
1084
1085                         hir::TypeBindingKind::Equality { term: ty.into() }
1086                     })
1087                 } else {
1088                     // Desugar `AssocTy: Bounds` into a type binding where the
1089                     // later desugars into a trait predicate.
1090                     let bounds = self.lower_param_bounds(bounds, itctx);
1091
1092                     hir::TypeBindingKind::Constraint { bounds }
1093                 }
1094             }
1095         };
1096
1097         hir::TypeBinding {
1098             hir_id: self.lower_node_id(constraint.id),
1099             ident: self.lower_ident(constraint.ident),
1100             gen_args,
1101             kind,
1102             span: self.lower_span(constraint.span),
1103         }
1104     }
1105
1106     fn emit_bad_parenthesized_trait_in_assoc_ty(&self, data: &ParenthesizedArgs) {
1107         // Suggest removing empty parentheses: "Trait()" -> "Trait"
1108         let sub = if data.inputs.is_empty() {
1109             let parentheses_span =
1110                 data.inputs_span.shrink_to_lo().to(data.inputs_span.shrink_to_hi());
1111             AssocTyParenthesesSub::Empty { parentheses_span }
1112         }
1113         // Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
1114         else {
1115             // Start of parameters to the 1st argument
1116             let open_param = data.inputs_span.shrink_to_lo().to(data
1117                 .inputs
1118                 .first()
1119                 .unwrap()
1120                 .span
1121                 .shrink_to_lo());
1122             // End of last argument to end of parameters
1123             let close_param =
1124                 data.inputs.last().unwrap().span.shrink_to_hi().to(data.inputs_span.shrink_to_hi());
1125             AssocTyParenthesesSub::NotEmpty { open_param, close_param }
1126         };
1127         self.tcx.sess.emit_err(AssocTyParentheses { span: data.span, sub });
1128     }
1129
1130     #[instrument(level = "debug", skip(self))]
1131     fn lower_generic_arg(
1132         &mut self,
1133         arg: &ast::GenericArg,
1134         itctx: &mut ImplTraitContext,
1135     ) -> hir::GenericArg<'hir> {
1136         match arg {
1137             ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(&lt)),
1138             ast::GenericArg::Type(ty) => {
1139                 match ty.kind {
1140                     TyKind::Infer if self.tcx.features().generic_arg_infer => {
1141                         return GenericArg::Infer(hir::InferArg {
1142                             hir_id: self.lower_node_id(ty.id),
1143                             span: self.lower_span(ty.span),
1144                         });
1145                     }
1146                     // We parse const arguments as path types as we cannot distinguish them during
1147                     // parsing. We try to resolve that ambiguity by attempting resolution in both the
1148                     // type and value namespaces. If we resolved the path in the value namespace, we
1149                     // transform it into a generic const argument.
1150                     TyKind::Path(ref qself, ref path) => {
1151                         if let Some(partial_res) = self.resolver.get_partial_res(ty.id) {
1152                             let res = partial_res.base_res();
1153                             if !res.matches_ns(Namespace::TypeNS) {
1154                                 debug!(
1155                                     "lower_generic_arg: Lowering type argument as const argument: {:?}",
1156                                     ty,
1157                                 );
1158
1159                                 // Construct an AnonConst where the expr is the "ty"'s path.
1160
1161                                 let parent_def_id = self.current_hir_id_owner;
1162                                 let node_id = self.next_node_id();
1163
1164                                 // Add a definition for the in-band const def.
1165                                 self.create_def(parent_def_id, node_id, DefPathData::AnonConst);
1166
1167                                 let span = self.lower_span(ty.span);
1168                                 let path_expr = Expr {
1169                                     id: ty.id,
1170                                     kind: ExprKind::Path(qself.clone(), path.clone()),
1171                                     span,
1172                                     attrs: AttrVec::new(),
1173                                     tokens: None,
1174                                 };
1175
1176                                 let ct = self.with_new_scopes(|this| hir::AnonConst {
1177                                     hir_id: this.lower_node_id(node_id),
1178                                     body: this.lower_const_body(path_expr.span, Some(&path_expr)),
1179                                 });
1180                                 return GenericArg::Const(ConstArg { value: ct, span });
1181                             }
1182                         }
1183                     }
1184                     _ => {}
1185                 }
1186                 GenericArg::Type(self.lower_ty(&ty, itctx))
1187             }
1188             ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
1189                 value: self.lower_anon_const(&ct),
1190                 span: self.lower_span(ct.value.span),
1191             }),
1192         }
1193     }
1194
1195     #[instrument(level = "debug", skip(self))]
1196     fn lower_ty(&mut self, t: &Ty, itctx: &mut ImplTraitContext) -> &'hir hir::Ty<'hir> {
1197         self.arena.alloc(self.lower_ty_direct(t, itctx))
1198     }
1199
1200     fn lower_path_ty(
1201         &mut self,
1202         t: &Ty,
1203         qself: &Option<QSelf>,
1204         path: &Path,
1205         param_mode: ParamMode,
1206         itctx: &mut ImplTraitContext,
1207     ) -> hir::Ty<'hir> {
1208         // Check whether we should interpret this as a bare trait object.
1209         // This check mirrors the one in late resolution.  We only introduce this special case in
1210         // the rare occurrence we need to lower `Fresh` anonymous lifetimes.
1211         // The other cases when a qpath should be opportunistically made a trait object are handled
1212         // by `ty_path`.
1213         if qself.is_none()
1214             && let Some(partial_res) = self.resolver.get_partial_res(t.id)
1215             && partial_res.unresolved_segments() == 0
1216             && let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
1217         {
1218             let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
1219                 let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef {
1220                     bound_generic_params: vec![],
1221                     trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
1222                     span: t.span
1223                 });
1224                 let bound = this.lower_poly_trait_ref(
1225                     poly_trait_ref,
1226                     itctx,
1227                 );
1228                 let bounds = this.arena.alloc_from_iter([bound]);
1229                 let lifetime_bound = this.elided_dyn_bound(t.span);
1230                 (bounds, lifetime_bound)
1231             });
1232             let kind = hir::TyKind::TraitObject(bounds, &lifetime_bound, TraitObjectSyntax::None);
1233             return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
1234         }
1235
1236         let id = self.lower_node_id(t.id);
1237         let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx);
1238         self.ty_path(id, t.span, qpath)
1239     }
1240
1241     fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> {
1242         hir::Ty { hir_id: self.next_id(), kind, span: self.lower_span(span) }
1243     }
1244
1245     fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
1246         self.ty(span, hir::TyKind::Tup(tys))
1247     }
1248
1249     fn lower_ty_direct(&mut self, t: &Ty, itctx: &mut ImplTraitContext) -> hir::Ty<'hir> {
1250         let kind = match t.kind {
1251             TyKind::Infer => hir::TyKind::Infer,
1252             TyKind::Err => hir::TyKind::Err,
1253             TyKind::Slice(ref ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
1254             TyKind::Ptr(ref mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
1255             TyKind::Rptr(ref region, ref mt) => {
1256                 let region = region.unwrap_or_else(|| {
1257                     let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
1258                         self.resolver.get_lifetime_res(t.id)
1259                     {
1260                         debug_assert_eq!(start.plus(1), end);
1261                         start
1262                     } else {
1263                         self.next_node_id()
1264                     };
1265                     let span = self.tcx.sess.source_map().start_point(t.span);
1266                     Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id }
1267                 });
1268                 let lifetime = self.lower_lifetime(&region);
1269                 hir::TyKind::Rptr(lifetime, self.lower_mt(mt, itctx))
1270             }
1271             TyKind::BareFn(ref f) => {
1272                 self.lower_lifetime_binder(t.id, &f.generic_params, |lctx, generic_params| {
1273                     hir::TyKind::BareFn(lctx.arena.alloc(hir::BareFnTy {
1274                         generic_params,
1275                         unsafety: lctx.lower_unsafety(f.unsafety),
1276                         abi: lctx.lower_extern(f.ext),
1277                         decl: lctx.lower_fn_decl(&f.decl, None, t.span, FnDeclKind::Pointer, None),
1278                         param_names: lctx.lower_fn_params_to_names(&f.decl),
1279                     }))
1280                 })
1281             }
1282             TyKind::Never => hir::TyKind::Never,
1283             TyKind::Tup(ref tys) => hir::TyKind::Tup(
1284                 self.arena.alloc_from_iter(tys.iter().map(|ty| self.lower_ty_direct(ty, itctx))),
1285             ),
1286             TyKind::Paren(ref ty) => {
1287                 return self.lower_ty_direct(ty, itctx);
1288             }
1289             TyKind::Path(ref qself, ref path) => {
1290                 return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
1291             }
1292             TyKind::ImplicitSelf => {
1293                 let hir_id = self.lower_node_id(t.id);
1294                 let res = self.expect_full_res(t.id);
1295                 let res = self.lower_res(res);
1296                 hir::TyKind::Path(hir::QPath::Resolved(
1297                     None,
1298                     self.arena.alloc(hir::Path {
1299                         res,
1300                         segments: arena_vec![self; hir::PathSegment::new(
1301                             Ident::with_dummy_span(kw::SelfUpper),
1302                             hir_id,
1303                             res
1304                         )],
1305                         span: self.lower_span(t.span),
1306                     }),
1307                 ))
1308             }
1309             TyKind::Array(ref ty, ref length) => {
1310                 hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_array_length(length))
1311             }
1312             TyKind::Typeof(ref expr) => hir::TyKind::Typeof(self.lower_anon_const(expr)),
1313             TyKind::TraitObject(ref bounds, kind) => {
1314                 let mut lifetime_bound = None;
1315                 let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
1316                     let bounds =
1317                         this.arena.alloc_from_iter(bounds.iter().filter_map(
1318                             |bound| match *bound {
1319                                 GenericBound::Trait(
1320                                     ref ty,
1321                                     TraitBoundModifier::None | TraitBoundModifier::MaybeConst,
1322                                 ) => Some(this.lower_poly_trait_ref(ty, itctx)),
1323                                 // `~const ?Bound` will cause an error during AST validation
1324                                 // anyways, so treat it like `?Bound` as compilation proceeds.
1325                                 GenericBound::Trait(
1326                                     _,
1327                                     TraitBoundModifier::Maybe | TraitBoundModifier::MaybeConstMaybe,
1328                                 ) => None,
1329                                 GenericBound::Outlives(ref lifetime) => {
1330                                     if lifetime_bound.is_none() {
1331                                         lifetime_bound = Some(this.lower_lifetime(lifetime));
1332                                     }
1333                                     None
1334                                 }
1335                             },
1336                         ));
1337                     let lifetime_bound =
1338                         lifetime_bound.unwrap_or_else(|| this.elided_dyn_bound(t.span));
1339                     (bounds, lifetime_bound)
1340                 });
1341                 hir::TyKind::TraitObject(bounds, lifetime_bound, kind)
1342             }
1343             TyKind::ImplTrait(def_node_id, ref bounds) => {
1344                 let span = t.span;
1345                 match itctx {
1346                     ImplTraitContext::ReturnPositionOpaqueTy { origin } => {
1347                         self.lower_opaque_impl_trait(span, *origin, def_node_id, bounds, itctx)
1348                     }
1349                     ImplTraitContext::TypeAliasesOpaqueTy => {
1350                         let mut nested_itctx = ImplTraitContext::TypeAliasesOpaqueTy;
1351                         self.lower_opaque_impl_trait(
1352                             span,
1353                             hir::OpaqueTyOrigin::TyAlias,
1354                             def_node_id,
1355                             bounds,
1356                             &mut nested_itctx,
1357                         )
1358                     }
1359                     ImplTraitContext::InTrait => {
1360                         self.lower_impl_trait_in_trait(span, def_node_id, |lctx| {
1361                             lctx.lower_param_bounds(bounds, ImplTraitContext::InTrait)
1362                         })
1363                     }
1364                     ImplTraitContext::Universal => {
1365                         let span = t.span;
1366                         let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
1367                         let (param, bounds, path) =
1368                             self.lower_generic_and_bounds(def_node_id, span, ident, bounds);
1369                         self.impl_trait_defs.push(param);
1370                         if let Some(bounds) = bounds {
1371                             self.impl_trait_bounds.push(bounds);
1372                         }
1373                         path
1374                     }
1375                     ImplTraitContext::Disallowed(position) => {
1376                         self.tcx.sess.emit_err(MisplacedImplTrait {
1377                             span: t.span,
1378                             position: DiagnosticArgFromDisplay(&position),
1379                         });
1380                         hir::TyKind::Err
1381                     }
1382                 }
1383             }
1384             TyKind::MacCall(_) => panic!("`TyKind::MacCall` should have been expanded by now"),
1385             TyKind::CVarArgs => {
1386                 self.tcx.sess.delay_span_bug(
1387                     t.span,
1388                     "`TyKind::CVarArgs` should have been handled elsewhere",
1389                 );
1390                 hir::TyKind::Err
1391             }
1392         };
1393
1394         hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
1395     }
1396
1397     /// Lowers a `ReturnPositionOpaqueTy` (`-> impl Trait`) or a `TypeAliasesOpaqueTy` (`type F =
1398     /// impl Trait`): this creates the associated Opaque Type (TAIT) definition and then returns a
1399     /// HIR type that references the TAIT.
1400     ///
1401     /// Given a function definition like:
1402     ///
1403     /// ```rust
1404     /// fn test<'a, T: Debug>(x: &'a T) -> impl Debug + 'a {
1405     ///     x
1406     /// }
1407     /// ```
1408     ///
1409     /// we will create a TAIT definition in the HIR like
1410     ///
1411     /// ```
1412     /// type TestReturn<'a, T, 'x> = impl Debug + 'x
1413     /// ```
1414     ///
1415     /// and return a type like `TestReturn<'static, T, 'a>`, so that the function looks like:
1416     ///
1417     /// ```rust
1418     /// fn test<'a, T: Debug>(x: &'a T) -> TestReturn<'static, T, 'a>
1419     /// ```
1420     ///
1421     /// Note the subtlety around type parameters! The new TAIT, `TestReturn`, inherits all the
1422     /// type parameters from the function `test` (this is implemented in the query layer, they aren't
1423     /// added explicitly in the HIR). But this includes all the lifetimes, and we only want to
1424     /// capture the lifetimes that are referenced in the bounds. Therefore, we add *extra* lifetime parameters
1425     /// for the lifetimes that get captured (`'x`, in our example above) and reference those.
1426     #[instrument(level = "debug", skip(self), ret)]
1427     fn lower_opaque_impl_trait(
1428         &mut self,
1429         span: Span,
1430         origin: hir::OpaqueTyOrigin,
1431         opaque_ty_node_id: NodeId,
1432         bounds: &GenericBounds,
1433         itctx: &mut ImplTraitContext,
1434     ) -> hir::TyKind<'hir> {
1435         // Make sure we know that some funky desugaring has been going on here.
1436         // This is a first: there is code in other places like for loop
1437         // desugaring that explicitly states that we don't want to track that.
1438         // Not tracking it makes lints in rustc and clippy very fragile, as
1439         // frequently opened issues show.
1440         let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
1441
1442         let opaque_ty_def_id = self.local_def_id(opaque_ty_node_id);
1443         debug!(?opaque_ty_def_id);
1444
1445         // Contains the new lifetime definitions created for the TAIT (if any).
1446         let mut collected_lifetimes = Vec::new();
1447
1448         // If this came from a TAIT (as opposed to a function that returns an RPIT), we only want
1449         // to capture the lifetimes that appear in the bounds. So visit the bounds to find out
1450         // exactly which ones those are.
1451         let lifetimes_to_remap = if origin == hir::OpaqueTyOrigin::TyAlias {
1452             // in a TAIT like `type Foo<'a> = impl Foo<'a>`, we don't keep all the lifetime parameters
1453             Vec::new()
1454         } else {
1455             // in fn return position, like the `fn test<'a>() -> impl Debug + 'a` example,
1456             // we only keep the lifetimes that appear in the `impl Debug` itself:
1457             lifetime_collector::lifetimes_in_bounds(&self.resolver, bounds)
1458         };
1459         debug!(?lifetimes_to_remap);
1460
1461         self.with_hir_id_owner(opaque_ty_node_id, |lctx| {
1462             let mut new_remapping = FxHashMap::default();
1463
1464             // If this opaque type is only capturing a subset of the lifetimes (those that appear
1465             // in bounds), then create the new lifetime parameters required and create a mapping
1466             // from the old `'a` (on the function) to the new `'a` (on the opaque type).
1467             collected_lifetimes = lctx.create_lifetime_defs(
1468                 opaque_ty_def_id,
1469                 &lifetimes_to_remap,
1470                 &mut new_remapping,
1471             );
1472             debug!(?collected_lifetimes);
1473             debug!(?new_remapping);
1474
1475             // Install the remapping from old to new (if any):
1476             lctx.with_remapping(new_remapping, |lctx| {
1477                 // This creates HIR lifetime definitions as `hir::GenericParam`, in the given
1478                 // example `type TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection
1479                 // containing `&['x]`.
1480                 let lifetime_defs = lctx.arena.alloc_from_iter(collected_lifetimes.iter().map(
1481                     |&(new_node_id, lifetime)| {
1482                         let hir_id = lctx.lower_node_id(new_node_id);
1483                         debug_assert_ne!(lctx.opt_local_def_id(new_node_id), None);
1484
1485                         let (name, kind) = if lifetime.ident.name == kw::UnderscoreLifetime {
1486                             (hir::ParamName::Fresh, hir::LifetimeParamKind::Elided)
1487                         } else {
1488                             (
1489                                 hir::ParamName::Plain(lifetime.ident),
1490                                 hir::LifetimeParamKind::Explicit,
1491                             )
1492                         };
1493
1494                         hir::GenericParam {
1495                             hir_id,
1496                             name,
1497                             span: lifetime.ident.span,
1498                             pure_wrt_drop: false,
1499                             kind: hir::GenericParamKind::Lifetime { kind },
1500                             colon_span: None,
1501                         }
1502                     },
1503                 ));
1504                 debug!(?lifetime_defs);
1505
1506                 // Then when we lower the param bounds, references to 'a are remapped to 'a1, so we
1507                 // get back Debug + 'a1, which is suitable for use on the TAIT.
1508                 let hir_bounds = lctx.lower_param_bounds(bounds, itctx);
1509                 debug!(?hir_bounds);
1510
1511                 let opaque_ty_item = hir::OpaqueTy {
1512                     generics: self.arena.alloc(hir::Generics {
1513                         params: lifetime_defs,
1514                         predicates: &[],
1515                         has_where_clause_predicates: false,
1516                         where_clause_span: lctx.lower_span(span),
1517                         span: lctx.lower_span(span),
1518                     }),
1519                     bounds: hir_bounds,
1520                     origin,
1521                 };
1522                 debug!(?opaque_ty_item);
1523
1524                 lctx.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span)
1525             })
1526         });
1527
1528         // This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type
1529         // TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`.
1530         let lifetimes =
1531             self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(|(_, lifetime)| {
1532                 let id = self.next_node_id();
1533                 let span = lifetime.ident.span;
1534
1535                 let ident = if lifetime.ident.name == kw::UnderscoreLifetime {
1536                     Ident::with_dummy_span(kw::UnderscoreLifetime)
1537                 } else {
1538                     lifetime.ident
1539                 };
1540
1541                 let l = self.new_named_lifetime(lifetime.id, id, span, ident);
1542                 hir::GenericArg::Lifetime(l)
1543             }));
1544         debug!(?lifetimes);
1545
1546         // `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1547         hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, lifetimes)
1548     }
1549
1550     #[instrument(level = "debug", skip(self, lower_bounds))]
1551     fn lower_impl_trait_in_trait(
1552         &mut self,
1553         span: Span,
1554         opaque_ty_node_id: NodeId,
1555         lower_bounds: impl FnOnce(&mut Self) -> hir::GenericBounds<'hir>,
1556     ) -> hir::TyKind<'hir> {
1557         let opaque_ty_def_id = self.local_def_id(opaque_ty_node_id);
1558         self.with_hir_id_owner(opaque_ty_node_id, |lctx| {
1559             let hir_bounds = lower_bounds(lctx);
1560             let rpitit_placeholder = hir::ImplTraitPlaceholder { bounds: hir_bounds };
1561             let rpitit_item = hir::Item {
1562                 def_id: opaque_ty_def_id,
1563                 ident: Ident::empty(),
1564                 kind: hir::ItemKind::ImplTraitPlaceholder(rpitit_placeholder),
1565                 span: lctx.lower_span(span),
1566                 vis_span: lctx.lower_span(span.shrink_to_lo()),
1567             };
1568             hir::OwnerNode::Item(lctx.arena.alloc(rpitit_item))
1569         });
1570         hir::TyKind::ImplTraitInTrait(hir::ItemId { def_id: opaque_ty_def_id })
1571     }
1572
1573     /// Registers a new opaque type with the proper `NodeId`s and
1574     /// returns the lowered node-ID for the opaque type.
1575     fn generate_opaque_type(
1576         &mut self,
1577         opaque_ty_id: LocalDefId,
1578         opaque_ty_item: hir::OpaqueTy<'hir>,
1579         span: Span,
1580         opaque_ty_span: Span,
1581     ) -> hir::OwnerNode<'hir> {
1582         let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(opaque_ty_item);
1583         // Generate an `type Foo = impl Trait;` declaration.
1584         trace!("registering opaque type with id {:#?}", opaque_ty_id);
1585         let opaque_ty_item = hir::Item {
1586             def_id: opaque_ty_id,
1587             ident: Ident::empty(),
1588             kind: opaque_ty_item_kind,
1589             vis_span: self.lower_span(span.shrink_to_lo()),
1590             span: self.lower_span(opaque_ty_span),
1591         };
1592         hir::OwnerNode::Item(self.arena.alloc(opaque_ty_item))
1593     }
1594
1595     /// Given a `parent_def_id`, a list of `lifetimes_in_bounds and a `remapping` hash to be
1596     /// filled, this function creates new definitions for `Param` and `Fresh` lifetimes, inserts the
1597     /// new definition, adds it to the remapping with the definition of the given lifetime and
1598     /// returns a list of lifetimes to be lowered afterwards.
1599     fn create_lifetime_defs(
1600         &mut self,
1601         parent_def_id: LocalDefId,
1602         lifetimes_in_bounds: &[Lifetime],
1603         remapping: &mut FxHashMap<LocalDefId, LocalDefId>,
1604     ) -> Vec<(NodeId, Lifetime)> {
1605         let mut result = Vec::new();
1606
1607         for lifetime in lifetimes_in_bounds {
1608             let res = self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error);
1609             debug!(?res);
1610
1611             match res {
1612                 LifetimeRes::Param { param: old_def_id, binder: _ } => {
1613                     if remapping.get(&old_def_id).is_none() {
1614                         let node_id = self.next_node_id();
1615
1616                         let new_def_id = self.create_def(
1617                             parent_def_id,
1618                             node_id,
1619                             DefPathData::LifetimeNs(lifetime.ident.name),
1620                         );
1621                         remapping.insert(old_def_id, new_def_id);
1622
1623                         result.push((node_id, *lifetime));
1624                     }
1625                 }
1626
1627                 LifetimeRes::Fresh { param, binder: _ } => {
1628                     debug_assert_eq!(lifetime.ident.name, kw::UnderscoreLifetime);
1629                     if let Some(old_def_id) = self.opt_local_def_id(param) && remapping.get(&old_def_id).is_none() {
1630                         let node_id = self.next_node_id();
1631
1632                         let new_def_id = self.create_def(
1633                             parent_def_id,
1634                             node_id,
1635                             DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1636                         );
1637                         remapping.insert(old_def_id, new_def_id);
1638
1639                         result.push((node_id, *lifetime));
1640                     }
1641                 }
1642
1643                 LifetimeRes::Static | LifetimeRes::Error => {}
1644
1645                 res => {
1646                     let bug_msg = format!(
1647                         "Unexpected lifetime resolution {:?} for {:?} at {:?}",
1648                         res, lifetime.ident, lifetime.ident.span
1649                     );
1650                     span_bug!(lifetime.ident.span, "{}", bug_msg);
1651                 }
1652             }
1653         }
1654
1655         result
1656     }
1657
1658     fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] {
1659         // Skip the `...` (`CVarArgs`) trailing arguments from the AST,
1660         // as they are not explicit in HIR/Ty function signatures.
1661         // (instead, the `c_variadic` flag is set to `true`)
1662         let mut inputs = &decl.inputs[..];
1663         if decl.c_variadic() {
1664             inputs = &inputs[..inputs.len() - 1];
1665         }
1666         self.arena.alloc_from_iter(inputs.iter().map(|param| match param.pat.kind {
1667             PatKind::Ident(_, ident, _) => self.lower_ident(ident),
1668             _ => Ident::new(kw::Empty, self.lower_span(param.pat.span)),
1669         }))
1670     }
1671
1672     // Lowers a function declaration.
1673     //
1674     // `decl`: the unlowered (AST) function declaration.
1675     // `fn_def_id`: if `Some`, impl Trait arguments are lowered into generic parameters on the
1676     //      given DefId, otherwise impl Trait is disallowed. Must be `Some` if
1677     //      `make_ret_async` is also `Some`.
1678     // `make_ret_async`: if `Some`, converts `-> T` into `-> impl Future<Output = T>` in the
1679     //      return type. This is used for `async fn` declarations. The `NodeId` is the ID of the
1680     //      return type `impl Trait` item, and the `Span` points to the `async` keyword.
1681     #[instrument(level = "debug", skip(self))]
1682     fn lower_fn_decl(
1683         &mut self,
1684         decl: &FnDecl,
1685         fn_node_id: Option<NodeId>,
1686         fn_span: Span,
1687         kind: FnDeclKind,
1688         make_ret_async: Option<(NodeId, Span)>,
1689     ) -> &'hir hir::FnDecl<'hir> {
1690         let c_variadic = decl.c_variadic();
1691
1692         // Skip the `...` (`CVarArgs`) trailing arguments from the AST,
1693         // as they are not explicit in HIR/Ty function signatures.
1694         // (instead, the `c_variadic` flag is set to `true`)
1695         let mut inputs = &decl.inputs[..];
1696         if c_variadic {
1697             inputs = &inputs[..inputs.len() - 1];
1698         }
1699         let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| {
1700             if fn_node_id.is_some() {
1701                 self.lower_ty_direct(&param.ty, &mut ImplTraitContext::Universal)
1702             } else {
1703                 self.lower_ty_direct(
1704                     &param.ty,
1705                     &mut ImplTraitContext::Disallowed(match kind {
1706                         FnDeclKind::Fn | FnDeclKind::Inherent => {
1707                             unreachable!("fn should allow in-band lifetimes")
1708                         }
1709                         FnDeclKind::ExternFn => ImplTraitPosition::ExternFnParam,
1710                         FnDeclKind::Closure => ImplTraitPosition::ClosureParam,
1711                         FnDeclKind::Pointer => ImplTraitPosition::PointerParam,
1712                         FnDeclKind::Trait => ImplTraitPosition::TraitParam,
1713                         FnDeclKind::Impl => ImplTraitPosition::ImplParam,
1714                     }),
1715                 )
1716             }
1717         }));
1718
1719         let output = if let Some((ret_id, span)) = make_ret_async {
1720             match kind {
1721                 FnDeclKind::Trait => {
1722                     if !kind.impl_trait_in_trait_allowed(self.tcx) {
1723                         self.tcx
1724                             .sess
1725                             .create_feature_err(
1726                                 TraitFnAsync { fn_span, span },
1727                                 sym::return_position_impl_trait_in_trait,
1728                             )
1729                             .emit();
1730                     }
1731                     self.lower_async_fn_ret_ty_in_trait(
1732                         &decl.output,
1733                         fn_node_id.expect("`make_ret_async` but no `fn_def_id`"),
1734                         ret_id,
1735                     )
1736                 }
1737                 _ => {
1738                     if !kind.impl_trait_return_allowed(self.tcx) {
1739                         if kind == FnDeclKind::Impl {
1740                             self.tcx
1741                                 .sess
1742                                 .create_feature_err(
1743                                     TraitFnAsync { fn_span, span },
1744                                     sym::return_position_impl_trait_in_trait,
1745                                 )
1746                                 .emit();
1747                         } else {
1748                             self.tcx.sess.emit_err(TraitFnAsync { fn_span, span });
1749                         }
1750                     }
1751                     self.lower_async_fn_ret_ty(
1752                         &decl.output,
1753                         fn_node_id.expect("`make_ret_async` but no `fn_def_id`"),
1754                         ret_id,
1755                     )
1756                 }
1757             }
1758         } else {
1759             match decl.output {
1760                 FnRetTy::Ty(ref ty) => {
1761                     let mut context = match fn_node_id {
1762                         Some(fn_node_id) if kind.impl_trait_return_allowed(self.tcx) => {
1763                             let fn_def_id = self.local_def_id(fn_node_id);
1764                             ImplTraitContext::ReturnPositionOpaqueTy {
1765                                 origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
1766                             }
1767                         }
1768                         Some(_) if kind.impl_trait_in_trait_allowed(self.tcx) => {
1769                             ImplTraitContext::InTrait
1770                         }
1771                         _ => ImplTraitContext::Disallowed(match kind {
1772                             FnDeclKind::Fn | FnDeclKind::Inherent => {
1773                                 unreachable!("fn should allow in-band lifetimes")
1774                             }
1775                             FnDeclKind::ExternFn => ImplTraitPosition::ExternFnReturn,
1776                             FnDeclKind::Closure => ImplTraitPosition::ClosureReturn,
1777                             FnDeclKind::Pointer => ImplTraitPosition::PointerReturn,
1778                             FnDeclKind::Trait => ImplTraitPosition::TraitReturn,
1779                             FnDeclKind::Impl => ImplTraitPosition::ImplReturn,
1780                         }),
1781                     };
1782                     hir::FnRetTy::Return(self.lower_ty(ty, &mut context))
1783                 }
1784                 FnRetTy::Default(span) => hir::FnRetTy::DefaultReturn(self.lower_span(span)),
1785             }
1786         };
1787
1788         self.arena.alloc(hir::FnDecl {
1789             inputs,
1790             output,
1791             c_variadic,
1792             implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
1793                 let is_mutable_pat = matches!(
1794                     arg.pat.kind,
1795                     PatKind::Ident(hir::BindingAnnotation(_, Mutability::Mut), ..)
1796                 );
1797
1798                 match arg.ty.kind {
1799                     TyKind::ImplicitSelf if is_mutable_pat => hir::ImplicitSelfKind::Mut,
1800                     TyKind::ImplicitSelf => hir::ImplicitSelfKind::Imm,
1801                     // Given we are only considering `ImplicitSelf` types, we needn't consider
1802                     // the case where we have a mutable pattern to a reference as that would
1803                     // no longer be an `ImplicitSelf`.
1804                     TyKind::Rptr(_, ref mt)
1805                         if mt.ty.kind.is_implicit_self() && mt.mutbl == ast::Mutability::Mut =>
1806                     {
1807                         hir::ImplicitSelfKind::MutRef
1808                     }
1809                     TyKind::Rptr(_, ref mt) if mt.ty.kind.is_implicit_self() => {
1810                         hir::ImplicitSelfKind::ImmRef
1811                     }
1812                     _ => hir::ImplicitSelfKind::None,
1813                 }
1814             }),
1815         })
1816     }
1817
1818     // Transforms `-> T` for `async fn` into `-> OpaqueTy { .. }`
1819     // combined with the following definition of `OpaqueTy`:
1820     //
1821     //     type OpaqueTy<generics_from_parent_fn> = impl Future<Output = T>;
1822     //
1823     // `output`: unlowered output type (`T` in `-> T`)
1824     // `fn_def_id`: `DefId` of the parent function (used to create child impl trait definition)
1825     // `opaque_ty_node_id`: `NodeId` of the opaque `impl Trait` type that should be created
1826     #[instrument(level = "debug", skip(self))]
1827     fn lower_async_fn_ret_ty(
1828         &mut self,
1829         output: &FnRetTy,
1830         fn_node_id: NodeId,
1831         opaque_ty_node_id: NodeId,
1832     ) -> hir::FnRetTy<'hir> {
1833         let span = output.span();
1834
1835         let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);
1836
1837         let opaque_ty_def_id = self.local_def_id(opaque_ty_node_id);
1838         let fn_def_id = self.local_def_id(fn_node_id);
1839
1840         // When we create the opaque type for this async fn, it is going to have
1841         // to capture all the lifetimes involved in the signature (including in the
1842         // return type). This is done by introducing lifetime parameters for:
1843         //
1844         // - all the explicitly declared lifetimes from the impl and function itself;
1845         // - all the elided lifetimes in the fn arguments;
1846         // - all the elided lifetimes in the return type.
1847         //
1848         // So for example in this snippet:
1849         //
1850         // ```rust
1851         // impl<'a> Foo<'a> {
1852         //   async fn bar<'b>(&self, x: &'b Vec<f64>, y: &str) -> &u32 {
1853         //   //               ^ '0                       ^ '1     ^ '2
1854         //   // elided lifetimes used below
1855         //   }
1856         // }
1857         // ```
1858         //
1859         // we would create an opaque type like:
1860         //
1861         // ```
1862         // type Bar<'a, 'b, '0, '1, '2> = impl Future<Output = &'2 u32>;
1863         // ```
1864         //
1865         // and we would then desugar `bar` to the equivalent of:
1866         //
1867         // ```rust
1868         // impl<'a> Foo<'a> {
1869         //   fn bar<'b, '0, '1>(&'0 self, x: &'b Vec<f64>, y: &'1 str) -> Bar<'a, 'b, '0, '1, '_>
1870         // }
1871         // ```
1872         //
1873         // Note that the final parameter to `Bar` is `'_`, not `'2` --
1874         // this is because the elided lifetimes from the return type
1875         // should be figured out using the ordinary elision rules, and
1876         // this desugaring achieves that.
1877
1878         // Calculate all the lifetimes that should be captured
1879         // by the opaque type. This should include all in-scope
1880         // lifetime parameters, including those defined in-band.
1881
1882         // Contains the new lifetime definitions created for the TAIT (if any) generated for the
1883         // return type.
1884         let mut collected_lifetimes = Vec::new();
1885         let mut new_remapping = FxHashMap::default();
1886
1887         let extra_lifetime_params = self.resolver.take_extra_lifetime_params(opaque_ty_node_id);
1888         debug!(?extra_lifetime_params);
1889         for (ident, outer_node_id, outer_res) in extra_lifetime_params {
1890             let outer_def_id = self.local_def_id(outer_node_id);
1891             let inner_node_id = self.next_node_id();
1892
1893             // Add a definition for the in scope lifetime def.
1894             let inner_def_id = self.create_def(
1895                 opaque_ty_def_id,
1896                 inner_node_id,
1897                 DefPathData::LifetimeNs(ident.name),
1898             );
1899             new_remapping.insert(outer_def_id, inner_def_id);
1900
1901             let inner_res = match outer_res {
1902                 // Input lifetime like `'a`:
1903                 LifetimeRes::Param { param, .. } => {
1904                     LifetimeRes::Param { param, binder: fn_node_id }
1905                 }
1906                 // Input lifetime like `'1`:
1907                 LifetimeRes::Fresh { param, .. } => {
1908                     LifetimeRes::Fresh { param, binder: fn_node_id }
1909                 }
1910                 LifetimeRes::Static | LifetimeRes::Error => continue,
1911                 res => {
1912                     panic!(
1913                         "Unexpected lifetime resolution {:?} for {:?} at {:?}",
1914                         res, ident, ident.span
1915                     )
1916                 }
1917             };
1918
1919             let lifetime = Lifetime { id: outer_node_id, ident };
1920             collected_lifetimes.push((inner_node_id, lifetime, Some(inner_res)));
1921         }
1922
1923         debug!(?collected_lifetimes);
1924
1925         // We only want to capture the lifetimes that appear in the bounds. So visit the bounds to
1926         // find out exactly which ones those are.
1927         // in fn return position, like the `fn test<'a>() -> impl Debug + 'a` example,
1928         // we only keep the lifetimes that appear in the `impl Debug` itself:
1929         let lifetimes_to_remap = lifetime_collector::lifetimes_in_ret_ty(&self.resolver, output);
1930         debug!(?lifetimes_to_remap);
1931
1932         self.with_hir_id_owner(opaque_ty_node_id, |this| {
1933             // If this opaque type is only capturing a subset of the lifetimes (those that appear
1934             // in bounds), then create the new lifetime parameters required and create a mapping
1935             // from the old `'a` (on the function) to the new `'a` (on the opaque type).
1936             collected_lifetimes.extend(
1937                 this.create_lifetime_defs(
1938                     opaque_ty_def_id,
1939                     &lifetimes_to_remap,
1940                     &mut new_remapping,
1941                 )
1942                 .into_iter()
1943                 .map(|(new_node_id, lifetime)| (new_node_id, lifetime, None)),
1944             );
1945             debug!(?collected_lifetimes);
1946             debug!(?new_remapping);
1947
1948             // Install the remapping from old to new (if any):
1949             this.with_remapping(new_remapping, |this| {
1950                 // We have to be careful to get elision right here. The
1951                 // idea is that we create a lifetime parameter for each
1952                 // lifetime in the return type.  So, given a return type
1953                 // like `async fn foo(..) -> &[&u32]`, we lower to `impl
1954                 // Future<Output = &'1 [ &'2 u32 ]>`.
1955                 //
1956                 // Then, we will create `fn foo(..) -> Foo<'_, '_>`, and
1957                 // hence the elision takes place at the fn site.
1958                 let future_bound = this.lower_async_fn_output_type_to_future_bound(
1959                     output,
1960                     span,
1961                     ImplTraitContext::ReturnPositionOpaqueTy {
1962                         origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
1963                     },
1964                 );
1965
1966                 let generic_params = this.arena.alloc_from_iter(collected_lifetimes.iter().map(
1967                     |&(new_node_id, lifetime, _)| {
1968                         let hir_id = this.lower_node_id(new_node_id);
1969                         debug_assert_ne!(this.opt_local_def_id(new_node_id), None);
1970
1971                         let (name, kind) = if lifetime.ident.name == kw::UnderscoreLifetime {
1972                             (hir::ParamName::Fresh, hir::LifetimeParamKind::Elided)
1973                         } else {
1974                             (
1975                                 hir::ParamName::Plain(lifetime.ident),
1976                                 hir::LifetimeParamKind::Explicit,
1977                             )
1978                         };
1979
1980                         hir::GenericParam {
1981                             hir_id,
1982                             name,
1983                             span: lifetime.ident.span,
1984                             pure_wrt_drop: false,
1985                             kind: hir::GenericParamKind::Lifetime { kind },
1986                             colon_span: None,
1987                         }
1988                     },
1989                 ));
1990                 debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params);
1991
1992                 let opaque_ty_item = hir::OpaqueTy {
1993                     generics: this.arena.alloc(hir::Generics {
1994                         params: generic_params,
1995                         predicates: &[],
1996                         has_where_clause_predicates: false,
1997                         where_clause_span: this.lower_span(span),
1998                         span: this.lower_span(span),
1999                     }),
2000                     bounds: arena_vec![this; future_bound],
2001                     origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
2002                 };
2003
2004                 trace!("exist ty from async fn def id: {:#?}", opaque_ty_def_id);
2005                 this.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span)
2006             })
2007         });
2008
2009         // As documented above, we need to create the lifetime
2010         // arguments to our opaque type. Continuing with our example,
2011         // we're creating the type arguments for the return type:
2012         //
2013         // ```
2014         // Bar<'a, 'b, '0, '1, '_>
2015         // ```
2016         //
2017         // For the "input" lifetime parameters, we wish to create
2018         // references to the parameters themselves, including the
2019         // "implicit" ones created from parameter types (`'a`, `'b`,
2020         // '`0`, `'1`).
2021         //
2022         // For the "output" lifetime parameters, we just want to
2023         // generate `'_`.
2024         let generic_args = self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(
2025             |(_, lifetime, res)| {
2026                 let id = self.next_node_id();
2027                 let span = lifetime.ident.span;
2028
2029                 let ident = if lifetime.ident.name == kw::UnderscoreLifetime {
2030                     Ident::with_dummy_span(kw::UnderscoreLifetime)
2031                 } else {
2032                     lifetime.ident
2033                 };
2034
2035                 let res = res.unwrap_or(
2036                     self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error),
2037                 );
2038                 hir::GenericArg::Lifetime(self.new_named_lifetime_with_res(id, span, ident, res))
2039             },
2040         ));
2041
2042         // Create the `Foo<...>` reference itself. Note that the `type
2043         // Foo = impl Trait` is, internally, created as a child of the
2044         // async fn, so the *type parameters* are inherited.  It's
2045         // only the lifetime parameters that we must supply.
2046         let opaque_ty_ref =
2047             hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, generic_args);
2048         let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
2049         hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
2050     }
2051
2052     // Transforms `-> T` for `async fn` into `-> OpaqueTy { .. }`
2053     // combined with the following definition of `OpaqueTy`:
2054     //
2055     //     type OpaqueTy<generics_from_parent_fn> = impl Future<Output = T>;
2056     //
2057     // `output`: unlowered output type (`T` in `-> T`)
2058     // `fn_def_id`: `DefId` of the parent function (used to create child impl trait definition)
2059     // `opaque_ty_node_id`: `NodeId` of the opaque `impl Trait` type that should be created
2060     #[instrument(level = "debug", skip(self))]
2061     fn lower_async_fn_ret_ty_in_trait(
2062         &mut self,
2063         output: &FnRetTy,
2064         fn_node_id: NodeId,
2065         opaque_ty_node_id: NodeId,
2066     ) -> hir::FnRetTy<'hir> {
2067         let kind = self.lower_impl_trait_in_trait(output.span(), opaque_ty_node_id, |lctx| {
2068             let bound = lctx.lower_async_fn_output_type_to_future_bound(
2069                 output,
2070                 output.span(),
2071                 ImplTraitContext::InTrait,
2072             );
2073             arena_vec![lctx; bound]
2074         });
2075
2076         let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, output.span(), None);
2077         let opaque_ty = self.ty(opaque_ty_span, kind);
2078         hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
2079     }
2080
2081     /// Transforms `-> T` into `Future<Output = T>`.
2082     fn lower_async_fn_output_type_to_future_bound(
2083         &mut self,
2084         output: &FnRetTy,
2085         span: Span,
2086         mut nested_impl_trait_context: ImplTraitContext,
2087     ) -> hir::GenericBound<'hir> {
2088         // Compute the `T` in `Future<Output = T>` from the return type.
2089         let output_ty = match output {
2090             FnRetTy::Ty(ty) => {
2091                 // Not `OpaqueTyOrigin::AsyncFn`: that's only used for the
2092                 // `impl Future` opaque type that `async fn` implicitly
2093                 // generates.
2094                 self.lower_ty(ty, &mut nested_impl_trait_context)
2095             }
2096             FnRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
2097         };
2098
2099         // "<Output = T>"
2100         let future_args = self.arena.alloc(hir::GenericArgs {
2101             args: &[],
2102             bindings: arena_vec![self; self.output_ty_binding(span, output_ty)],
2103             parenthesized: false,
2104             span_ext: DUMMY_SP,
2105         });
2106
2107         hir::GenericBound::LangItemTrait(
2108             // ::std::future::Future<future_params>
2109             hir::LangItem::Future,
2110             self.lower_span(span),
2111             self.next_id(),
2112             future_args,
2113         )
2114     }
2115
2116     #[instrument(level = "trace", skip(self))]
2117     fn lower_param_bound(
2118         &mut self,
2119         tpb: &GenericBound,
2120         itctx: &mut ImplTraitContext,
2121     ) -> hir::GenericBound<'hir> {
2122         match tpb {
2123             GenericBound::Trait(p, modifier) => hir::GenericBound::Trait(
2124                 self.lower_poly_trait_ref(p, itctx),
2125                 self.lower_trait_bound_modifier(*modifier),
2126             ),
2127             GenericBound::Outlives(lifetime) => {
2128                 hir::GenericBound::Outlives(self.lower_lifetime(lifetime))
2129             }
2130         }
2131     }
2132
2133     fn lower_lifetime(&mut self, l: &Lifetime) -> &'hir hir::Lifetime {
2134         let span = self.lower_span(l.ident.span);
2135         let ident = self.lower_ident(l.ident);
2136         self.new_named_lifetime(l.id, l.id, span, ident)
2137     }
2138
2139     #[instrument(level = "debug", skip(self))]
2140     fn new_named_lifetime_with_res(
2141         &mut self,
2142         id: NodeId,
2143         span: Span,
2144         ident: Ident,
2145         res: LifetimeRes,
2146     ) -> &'hir hir::Lifetime {
2147         let name = match res {
2148             LifetimeRes::Param { param, .. } => {
2149                 let p_name = ParamName::Plain(ident);
2150                 let param = self.get_remapped_def_id(param);
2151
2152                 hir::LifetimeName::Param(param, p_name)
2153             }
2154             LifetimeRes::Fresh { param, .. } => {
2155                 debug_assert_eq!(ident.name, kw::UnderscoreLifetime);
2156                 let param = self.local_def_id(param);
2157
2158                 hir::LifetimeName::Param(param, ParamName::Fresh)
2159             }
2160             LifetimeRes::Infer => hir::LifetimeName::Infer,
2161             LifetimeRes::Static => hir::LifetimeName::Static,
2162             LifetimeRes::Error => hir::LifetimeName::Error,
2163             res => panic!("Unexpected lifetime resolution {:?} for {:?} at {:?}", res, ident, span),
2164         };
2165
2166         debug!(?name);
2167         self.arena.alloc(hir::Lifetime {
2168             hir_id: self.lower_node_id(id),
2169             span: self.lower_span(span),
2170             name,
2171         })
2172     }
2173
2174     #[instrument(level = "debug", skip(self))]
2175     fn new_named_lifetime(
2176         &mut self,
2177         id: NodeId,
2178         new_id: NodeId,
2179         span: Span,
2180         ident: Ident,
2181     ) -> &'hir hir::Lifetime {
2182         let res = self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error);
2183         self.new_named_lifetime_with_res(new_id, span, ident, res)
2184     }
2185
2186     fn lower_generic_params_mut<'s>(
2187         &'s mut self,
2188         params: &'s [GenericParam],
2189     ) -> impl Iterator<Item = hir::GenericParam<'hir>> + Captures<'a> + Captures<'s> {
2190         params.iter().map(move |param| self.lower_generic_param(param))
2191     }
2192
2193     fn lower_generic_params(&mut self, params: &[GenericParam]) -> &'hir [hir::GenericParam<'hir>] {
2194         self.arena.alloc_from_iter(self.lower_generic_params_mut(params))
2195     }
2196
2197     #[instrument(level = "trace", skip(self))]
2198     fn lower_generic_param(&mut self, param: &GenericParam) -> hir::GenericParam<'hir> {
2199         let (name, kind) = self.lower_generic_param_kind(param);
2200
2201         let hir_id = self.lower_node_id(param.id);
2202         self.lower_attrs(hir_id, &param.attrs);
2203         hir::GenericParam {
2204             hir_id,
2205             name,
2206             span: self.lower_span(param.span()),
2207             pure_wrt_drop: self.tcx.sess.contains_name(&param.attrs, sym::may_dangle),
2208             kind,
2209             colon_span: param.colon_span.map(|s| self.lower_span(s)),
2210         }
2211     }
2212
2213     fn lower_generic_param_kind(
2214         &mut self,
2215         param: &GenericParam,
2216     ) -> (hir::ParamName, hir::GenericParamKind<'hir>) {
2217         match param.kind {
2218             GenericParamKind::Lifetime => {
2219                 // AST resolution emitted an error on those parameters, so we lower them using
2220                 // `ParamName::Error`.
2221                 let param_name =
2222                     if let Some(LifetimeRes::Error) = self.resolver.get_lifetime_res(param.id) {
2223                         ParamName::Error
2224                     } else {
2225                         let ident = self.lower_ident(param.ident);
2226                         ParamName::Plain(ident)
2227                     };
2228                 let kind =
2229                     hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
2230
2231                 (param_name, kind)
2232             }
2233             GenericParamKind::Type { ref default, .. } => {
2234                 let kind = hir::GenericParamKind::Type {
2235                     default: default.as_ref().map(|x| {
2236                         self.lower_ty(x, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type))
2237                     }),
2238                     synthetic: false,
2239                 };
2240
2241                 (hir::ParamName::Plain(self.lower_ident(param.ident)), kind)
2242             }
2243             GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
2244                 let ty =
2245                     self.lower_ty(&ty, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type));
2246                 let default = default.as_ref().map(|def| self.lower_anon_const(def));
2247                 (
2248                     hir::ParamName::Plain(self.lower_ident(param.ident)),
2249                     hir::GenericParamKind::Const { ty, default },
2250                 )
2251             }
2252         }
2253     }
2254
2255     fn lower_trait_ref(
2256         &mut self,
2257         p: &TraitRef,
2258         itctx: &mut ImplTraitContext,
2259     ) -> hir::TraitRef<'hir> {
2260         let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
2261             hir::QPath::Resolved(None, path) => path,
2262             qpath => panic!("lower_trait_ref: unexpected QPath `{:?}`", qpath),
2263         };
2264         hir::TraitRef { path, hir_ref_id: self.lower_node_id(p.ref_id) }
2265     }
2266
2267     #[instrument(level = "debug", skip(self))]
2268     fn lower_poly_trait_ref(
2269         &mut self,
2270         p: &PolyTraitRef,
2271         itctx: &mut ImplTraitContext,
2272     ) -> hir::PolyTraitRef<'hir> {
2273         self.lower_lifetime_binder(
2274             p.trait_ref.ref_id,
2275             &p.bound_generic_params,
2276             |lctx, bound_generic_params| {
2277                 let trait_ref = lctx.lower_trait_ref(&p.trait_ref, itctx);
2278                 hir::PolyTraitRef { bound_generic_params, trait_ref, span: lctx.lower_span(p.span) }
2279             },
2280         )
2281     }
2282
2283     fn lower_mt(&mut self, mt: &MutTy, itctx: &mut ImplTraitContext) -> hir::MutTy<'hir> {
2284         hir::MutTy { ty: self.lower_ty(&mt.ty, itctx), mutbl: mt.mutbl }
2285     }
2286
2287     #[instrument(level = "debug", skip(self), ret)]
2288     fn lower_param_bounds(
2289         &mut self,
2290         bounds: &[GenericBound],
2291         itctx: &mut ImplTraitContext,
2292     ) -> hir::GenericBounds<'hir> {
2293         self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, itctx))
2294     }
2295
2296     fn lower_param_bounds_mut<'s, 'b>(
2297         &'s mut self,
2298         bounds: &'s [GenericBound],
2299         itctx: &'b mut ImplTraitContext,
2300     ) -> impl Iterator<Item = hir::GenericBound<'hir>> + Captures<'s> + Captures<'a> + Captures<'b>
2301     {
2302         bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx))
2303     }
2304
2305     #[instrument(level = "debug", skip(self), ret)]
2306     fn lower_generic_and_bounds(
2307         &mut self,
2308         node_id: NodeId,
2309         span: Span,
2310         ident: Ident,
2311         bounds: &[GenericBound],
2312     ) -> (hir::GenericParam<'hir>, Option<hir::WherePredicate<'hir>>, hir::TyKind<'hir>) {
2313         // Add a definition for the in-band `Param`.
2314         let def_id = self.local_def_id(node_id);
2315
2316         // Set the name to `impl Bound1 + Bound2`.
2317         let param = hir::GenericParam {
2318             hir_id: self.lower_node_id(node_id),
2319             name: ParamName::Plain(self.lower_ident(ident)),
2320             pure_wrt_drop: false,
2321             span: self.lower_span(span),
2322             kind: hir::GenericParamKind::Type { default: None, synthetic: true },
2323             colon_span: None,
2324         };
2325
2326         let preds = self.lower_generic_bound_predicate(
2327             ident,
2328             node_id,
2329             &GenericParamKind::Type { default: None },
2330             bounds,
2331             &mut ImplTraitContext::Universal,
2332             hir::PredicateOrigin::ImplTrait,
2333         );
2334
2335         let hir_id = self.next_id();
2336         let res = Res::Def(DefKind::TyParam, def_id.to_def_id());
2337         let ty = hir::TyKind::Path(hir::QPath::Resolved(
2338             None,
2339             self.arena.alloc(hir::Path {
2340                 span: self.lower_span(span),
2341                 res,
2342                 segments:
2343                     arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
2344             }),
2345         ));
2346
2347         (param, preds, ty)
2348     }
2349
2350     /// Lowers a block directly to an expression, presuming that it
2351     /// has no attributes and is not targeted by a `break`.
2352     fn lower_block_expr(&mut self, b: &Block) -> hir::Expr<'hir> {
2353         let block = self.lower_block(b, false);
2354         self.expr_block(block, AttrVec::new())
2355     }
2356
2357     fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {
2358         match c.value.kind {
2359             ExprKind::Underscore => {
2360                 if self.tcx.features().generic_arg_infer {
2361                     hir::ArrayLen::Infer(self.lower_node_id(c.id), c.value.span)
2362                 } else {
2363                     feature_err(
2364                         &self.tcx.sess.parse_sess,
2365                         sym::generic_arg_infer,
2366                         c.value.span,
2367                         "using `_` for array lengths is unstable",
2368                     )
2369                     .stash(c.value.span, StashKey::UnderscoreForArrayLengths);
2370                     hir::ArrayLen::Body(self.lower_anon_const(c))
2371                 }
2372             }
2373             _ => hir::ArrayLen::Body(self.lower_anon_const(c)),
2374         }
2375     }
2376
2377     fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
2378         self.with_new_scopes(|this| hir::AnonConst {
2379             hir_id: this.lower_node_id(c.id),
2380             body: this.lower_const_body(c.value.span, Some(&c.value)),
2381         })
2382     }
2383
2384     fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
2385         match u {
2386             CompilerGenerated => hir::UnsafeSource::CompilerGenerated,
2387             UserProvided => hir::UnsafeSource::UserProvided,
2388         }
2389     }
2390
2391     fn lower_trait_bound_modifier(&mut self, f: TraitBoundModifier) -> hir::TraitBoundModifier {
2392         match f {
2393             TraitBoundModifier::None => hir::TraitBoundModifier::None,
2394             TraitBoundModifier::MaybeConst => hir::TraitBoundModifier::MaybeConst,
2395
2396             // `MaybeConstMaybe` will cause an error during AST validation, but we need to pick a
2397             // placeholder for compilation to proceed.
2398             TraitBoundModifier::MaybeConstMaybe | TraitBoundModifier::Maybe => {
2399                 hir::TraitBoundModifier::Maybe
2400             }
2401         }
2402     }
2403
2404     // Helper methods for building HIR.
2405
2406     fn stmt(&mut self, span: Span, kind: hir::StmtKind<'hir>) -> hir::Stmt<'hir> {
2407         hir::Stmt { span: self.lower_span(span), kind, hir_id: self.next_id() }
2408     }
2409
2410     fn stmt_expr(&mut self, span: Span, expr: hir::Expr<'hir>) -> hir::Stmt<'hir> {
2411         self.stmt(span, hir::StmtKind::Expr(self.arena.alloc(expr)))
2412     }
2413
2414     fn stmt_let_pat(
2415         &mut self,
2416         attrs: Option<&'hir [Attribute]>,
2417         span: Span,
2418         init: Option<&'hir hir::Expr<'hir>>,
2419         pat: &'hir hir::Pat<'hir>,
2420         source: hir::LocalSource,
2421     ) -> hir::Stmt<'hir> {
2422         let hir_id = self.next_id();
2423         if let Some(a) = attrs {
2424             debug_assert!(!a.is_empty());
2425             self.attrs.insert(hir_id.local_id, a);
2426         }
2427         let local = hir::Local {
2428             hir_id,
2429             init,
2430             pat,
2431             els: None,
2432             source,
2433             span: self.lower_span(span),
2434             ty: None,
2435         };
2436         self.stmt(span, hir::StmtKind::Local(self.arena.alloc(local)))
2437     }
2438
2439     fn block_expr(&mut self, expr: &'hir hir::Expr<'hir>) -> &'hir hir::Block<'hir> {
2440         self.block_all(expr.span, &[], Some(expr))
2441     }
2442
2443     fn block_all(
2444         &mut self,
2445         span: Span,
2446         stmts: &'hir [hir::Stmt<'hir>],
2447         expr: Option<&'hir hir::Expr<'hir>>,
2448     ) -> &'hir hir::Block<'hir> {
2449         let blk = hir::Block {
2450             stmts,
2451             expr,
2452             hir_id: self.next_id(),
2453             rules: hir::BlockCheckMode::DefaultBlock,
2454             span: self.lower_span(span),
2455             targeted_by_break: false,
2456         };
2457         self.arena.alloc(blk)
2458     }
2459
2460     fn pat_cf_continue(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
2461         let field = self.single_pat_field(span, pat);
2462         self.pat_lang_item_variant(span, hir::LangItem::ControlFlowContinue, field, None)
2463     }
2464
2465     fn pat_cf_break(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
2466         let field = self.single_pat_field(span, pat);
2467         self.pat_lang_item_variant(span, hir::LangItem::ControlFlowBreak, field, None)
2468     }
2469
2470     fn pat_some(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
2471         let field = self.single_pat_field(span, pat);
2472         self.pat_lang_item_variant(span, hir::LangItem::OptionSome, field, None)
2473     }
2474
2475     fn pat_none(&mut self, span: Span) -> &'hir hir::Pat<'hir> {
2476         self.pat_lang_item_variant(span, hir::LangItem::OptionNone, &[], None)
2477     }
2478
2479     fn single_pat_field(
2480         &mut self,
2481         span: Span,
2482         pat: &'hir hir::Pat<'hir>,
2483     ) -> &'hir [hir::PatField<'hir>] {
2484         let field = hir::PatField {
2485             hir_id: self.next_id(),
2486             ident: Ident::new(sym::integer(0), self.lower_span(span)),
2487             is_shorthand: false,
2488             pat,
2489             span: self.lower_span(span),
2490         };
2491         arena_vec![self; field]
2492     }
2493
2494     fn pat_lang_item_variant(
2495         &mut self,
2496         span: Span,
2497         lang_item: hir::LangItem,
2498         fields: &'hir [hir::PatField<'hir>],
2499         hir_id: Option<hir::HirId>,
2500     ) -> &'hir hir::Pat<'hir> {
2501         let qpath = hir::QPath::LangItem(lang_item, self.lower_span(span), hir_id);
2502         self.pat(span, hir::PatKind::Struct(qpath, fields, false))
2503     }
2504
2505     fn pat_ident(&mut self, span: Span, ident: Ident) -> (&'hir hir::Pat<'hir>, hir::HirId) {
2506         self.pat_ident_binding_mode(span, ident, hir::BindingAnnotation::NONE)
2507     }
2508
2509     fn pat_ident_mut(&mut self, span: Span, ident: Ident) -> (hir::Pat<'hir>, hir::HirId) {
2510         self.pat_ident_binding_mode_mut(span, ident, hir::BindingAnnotation::NONE)
2511     }
2512
2513     fn pat_ident_binding_mode(
2514         &mut self,
2515         span: Span,
2516         ident: Ident,
2517         bm: hir::BindingAnnotation,
2518     ) -> (&'hir hir::Pat<'hir>, hir::HirId) {
2519         let (pat, hir_id) = self.pat_ident_binding_mode_mut(span, ident, bm);
2520         (self.arena.alloc(pat), hir_id)
2521     }
2522
2523     fn pat_ident_binding_mode_mut(
2524         &mut self,
2525         span: Span,
2526         ident: Ident,
2527         bm: hir::BindingAnnotation,
2528     ) -> (hir::Pat<'hir>, hir::HirId) {
2529         let hir_id = self.next_id();
2530
2531         (
2532             hir::Pat {
2533                 hir_id,
2534                 kind: hir::PatKind::Binding(bm, hir_id, self.lower_ident(ident), None),
2535                 span: self.lower_span(span),
2536                 default_binding_modes: true,
2537             },
2538             hir_id,
2539         )
2540     }
2541
2542     fn pat(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
2543         self.arena.alloc(hir::Pat {
2544             hir_id: self.next_id(),
2545             kind,
2546             span: self.lower_span(span),
2547             default_binding_modes: true,
2548         })
2549     }
2550
2551     fn pat_without_dbm(&mut self, span: Span, kind: hir::PatKind<'hir>) -> hir::Pat<'hir> {
2552         hir::Pat {
2553             hir_id: self.next_id(),
2554             kind,
2555             span: self.lower_span(span),
2556             default_binding_modes: false,
2557         }
2558     }
2559
2560     fn ty_path(
2561         &mut self,
2562         mut hir_id: hir::HirId,
2563         span: Span,
2564         qpath: hir::QPath<'hir>,
2565     ) -> hir::Ty<'hir> {
2566         let kind = match qpath {
2567             hir::QPath::Resolved(None, path) => {
2568                 // Turn trait object paths into `TyKind::TraitObject` instead.
2569                 match path.res {
2570                     Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
2571                         let principal = hir::PolyTraitRef {
2572                             bound_generic_params: &[],
2573                             trait_ref: hir::TraitRef { path, hir_ref_id: hir_id },
2574                             span: self.lower_span(span),
2575                         };
2576
2577                         // The original ID is taken by the `PolyTraitRef`,
2578                         // so the `Ty` itself needs a different one.
2579                         hir_id = self.next_id();
2580                         hir::TyKind::TraitObject(
2581                             arena_vec![self; principal],
2582                             self.elided_dyn_bound(span),
2583                             TraitObjectSyntax::None,
2584                         )
2585                     }
2586                     _ => hir::TyKind::Path(hir::QPath::Resolved(None, path)),
2587                 }
2588             }
2589             _ => hir::TyKind::Path(qpath),
2590         };
2591
2592         hir::Ty { hir_id, kind, span: self.lower_span(span) }
2593     }
2594
2595     /// Invoked to create the lifetime argument(s) for an elided trait object
2596     /// bound, like the bound in `Box<dyn Debug>`. This method is not invoked
2597     /// when the bound is written, even if it is written with `'_` like in
2598     /// `Box<dyn Debug + '_>`. In those cases, `lower_lifetime` is invoked.
2599     fn elided_dyn_bound(&mut self, span: Span) -> &'hir hir::Lifetime {
2600         let r = hir::Lifetime {
2601             hir_id: self.next_id(),
2602             span: self.lower_span(span),
2603             name: hir::LifetimeName::ImplicitObjectLifetimeDefault,
2604         };
2605         debug!("elided_dyn_bound: r={:?}", r);
2606         self.arena.alloc(r)
2607     }
2608 }
2609
2610 /// Helper struct for delayed construction of GenericArgs.
2611 struct GenericArgsCtor<'hir> {
2612     args: SmallVec<[hir::GenericArg<'hir>; 4]>,
2613     bindings: &'hir [hir::TypeBinding<'hir>],
2614     parenthesized: bool,
2615     span: Span,
2616 }
2617
2618 impl<'hir> GenericArgsCtor<'hir> {
2619     fn is_empty(&self) -> bool {
2620         self.args.is_empty() && self.bindings.is_empty() && !self.parenthesized
2621     }
2622
2623     fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {
2624         let ga = hir::GenericArgs {
2625             args: this.arena.alloc_from_iter(self.args),
2626             bindings: self.bindings,
2627             parenthesized: self.parenthesized,
2628             span_ext: this.lower_span(self.span),
2629         };
2630         this.arena.alloc(ga)
2631     }
2632 }