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