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