• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use super::errors::{InvalidAbi, InvalidAbiSuggestion, MisplacedRelaxTraitBound};
2 use super::ResolverAstLoweringExt;
3 use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
4 use super::{FnDeclKind, LoweringContext, ParamMode};
5 
6 use hir::definitions::DefPathData;
7 use rustc_ast::ptr::P;
8 use rustc_ast::visit::AssocCtxt;
9 use rustc_ast::*;
10 use rustc_data_structures::sorted_map::SortedMap;
11 use rustc_errors::ErrorGuaranteed;
12 use rustc_hir as hir;
13 use rustc_hir::def::{DefKind, Res};
14 use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
15 use rustc_hir::PredicateOrigin;
16 use rustc_index::{Idx, IndexSlice, IndexVec};
17 use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
18 use rustc_span::edit_distance::find_best_match_for_name;
19 use rustc_span::source_map::DesugaringKind;
20 use rustc_span::symbol::{kw, sym, Ident};
21 use rustc_span::{Span, Symbol};
22 use rustc_target::spec::abi;
23 use smallvec::{smallvec, SmallVec};
24 use thin_vec::ThinVec;
25 
26 pub(super) struct ItemLowerer<'a, 'hir> {
27     pub(super) tcx: TyCtxt<'hir>,
28     pub(super) resolver: &'a mut ResolverAstLowering,
29     pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
30     pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
31 }
32 
33 /// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
34 /// to the where clause that is preferred, if it exists. Otherwise, it sets the span to the other where
35 /// clause if it exists.
add_ty_alias_where_clause( generics: &mut ast::Generics, mut where_clauses: (TyAliasWhereClause, TyAliasWhereClause), prefer_first: bool, )36 fn add_ty_alias_where_clause(
37     generics: &mut ast::Generics,
38     mut where_clauses: (TyAliasWhereClause, TyAliasWhereClause),
39     prefer_first: bool,
40 ) {
41     if !prefer_first {
42         where_clauses = (where_clauses.1, where_clauses.0);
43     }
44     if where_clauses.0.0 || !where_clauses.1.0 {
45         generics.where_clause.has_where_token = where_clauses.0.0;
46         generics.where_clause.span = where_clauses.0.1;
47     } else {
48         generics.where_clause.has_where_token = where_clauses.1.0;
49         generics.where_clause.span = where_clauses.1.1;
50     }
51 }
52 
53 impl<'a, 'hir> ItemLowerer<'a, 'hir> {
with_lctx( &mut self, owner: NodeId, f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>, )54     fn with_lctx(
55         &mut self,
56         owner: NodeId,
57         f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
58     ) {
59         let mut lctx = LoweringContext {
60             // Pseudo-globals.
61             tcx: self.tcx,
62             resolver: self.resolver,
63             arena: self.tcx.hir_arena,
64 
65             // HirId handling.
66             bodies: Vec::new(),
67             attrs: SortedMap::default(),
68             children: Vec::default(),
69             current_hir_id_owner: hir::CRATE_OWNER_ID,
70             item_local_id_counter: hir::ItemLocalId::new(0),
71             node_id_to_local_id: Default::default(),
72             trait_map: Default::default(),
73 
74             // Lowering state.
75             catch_scope: None,
76             loop_scope: None,
77             is_in_loop_condition: false,
78             is_in_trait_impl: false,
79             is_in_dyn_type: false,
80             generator_kind: None,
81             task_context: None,
82             current_item: None,
83             impl_trait_defs: Vec::new(),
84             impl_trait_bounds: Vec::new(),
85             allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
86             allow_gen_future: Some([sym::gen_future, sym::closure_track_caller][..].into()),
87             generics_def_id_map: Default::default(),
88         };
89         lctx.with_hir_id_owner(owner, |lctx| f(lctx));
90 
91         for (def_id, info) in lctx.children {
92             let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
93             debug_assert!(matches!(owner, hir::MaybeOwner::Phantom));
94             *owner = info;
95         }
96     }
97 
lower_node( &mut self, def_id: LocalDefId, ) -> hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>98     pub(super) fn lower_node(
99         &mut self,
100         def_id: LocalDefId,
101     ) -> hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>> {
102         let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
103         if let hir::MaybeOwner::Phantom = owner {
104             let node = self.ast_index[def_id];
105             match node {
106                 AstOwner::NonOwner => {}
107                 AstOwner::Crate(c) => self.lower_crate(c),
108                 AstOwner::Item(item) => self.lower_item(item),
109                 AstOwner::AssocItem(item, ctxt) => self.lower_assoc_item(item, ctxt),
110                 AstOwner::ForeignItem(item) => self.lower_foreign_item(item),
111             }
112         }
113 
114         self.owners[def_id]
115     }
116 
117     #[instrument(level = "debug", skip(self, c))]
lower_crate(&mut self, c: &Crate)118     fn lower_crate(&mut self, c: &Crate) {
119         debug_assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
120         self.with_lctx(CRATE_NODE_ID, |lctx| {
121             let module = lctx.lower_mod(&c.items, &c.spans);
122             lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs);
123             hir::OwnerNode::Crate(module)
124         })
125     }
126 
127     #[instrument(level = "debug", skip(self))]
lower_item(&mut self, item: &Item)128     fn lower_item(&mut self, item: &Item) {
129         self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
130     }
131 
lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt)132     fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) {
133         let def_id = self.resolver.node_id_to_def_id[&item.id];
134 
135         let parent_id = self.tcx.local_parent(def_id);
136         let parent_hir = self.lower_node(parent_id).unwrap();
137         self.with_lctx(item.id, |lctx| {
138             // Evaluate with the lifetimes in `params` in-scope.
139             // This is used to track which lifetimes have already been defined,
140             // and which need to be replicated when lowering an async fn.
141 
142             if let hir::ItemKind::Impl(impl_) = parent_hir.node().expect_item().kind {
143                 lctx.is_in_trait_impl = impl_.of_trait.is_some();
144             }
145 
146             match ctxt {
147                 AssocCtxt::Trait => hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)),
148                 AssocCtxt::Impl => hir::OwnerNode::ImplItem(lctx.lower_impl_item(item)),
149             }
150         })
151     }
152 
lower_foreign_item(&mut self, item: &ForeignItem)153     fn lower_foreign_item(&mut self, item: &ForeignItem) {
154         self.with_lctx(item.id, |lctx| hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item)))
155     }
156 }
157 
158 impl<'hir> LoweringContext<'_, 'hir> {
lower_mod( &mut self, items: &[P<Item>], spans: &ModSpans, ) -> &'hir hir::Mod<'hir>159     pub(super) fn lower_mod(
160         &mut self,
161         items: &[P<Item>],
162         spans: &ModSpans,
163     ) -> &'hir hir::Mod<'hir> {
164         self.arena.alloc(hir::Mod {
165             spans: hir::ModSpans {
166                 inner_span: self.lower_span(spans.inner_span),
167                 inject_use_span: self.lower_span(spans.inject_use_span),
168             },
169             item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_ref(x))),
170         })
171     }
172 
lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]>173     pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
174         let mut node_ids =
175             smallvec![hir::ItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }];
176         if let ItemKind::Use(use_tree) = &i.kind {
177             self.lower_item_id_use_tree(use_tree, &mut node_ids);
178         }
179         node_ids
180     }
181 
lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>)182     fn lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>) {
183         match &tree.kind {
184             UseTreeKind::Nested(nested_vec) => {
185                 for &(ref nested, id) in nested_vec {
186                     vec.push(hir::ItemId {
187                         owner_id: hir::OwnerId { def_id: self.local_def_id(id) },
188                     });
189                     self.lower_item_id_use_tree(nested, vec);
190                 }
191             }
192             UseTreeKind::Simple(..) | UseTreeKind::Glob => {}
193         }
194     }
195 
lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir>196     fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
197         let mut ident = i.ident;
198         let vis_span = self.lower_span(i.vis.span);
199         let hir_id = self.lower_node_id(i.id);
200         let attrs = self.lower_attrs(hir_id, &i.attrs);
201         let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
202         let item = hir::Item {
203             owner_id: hir_id.expect_owner(),
204             ident: self.lower_ident(ident),
205             kind,
206             vis_span,
207             span: self.lower_span(i.span),
208         };
209         self.arena.alloc(item)
210     }
211 
lower_item_kind( &mut self, span: Span, id: NodeId, hir_id: hir::HirId, ident: &mut Ident, attrs: Option<&'hir [Attribute]>, vis_span: Span, i: &ItemKind, ) -> hir::ItemKind<'hir>212     fn lower_item_kind(
213         &mut self,
214         span: Span,
215         id: NodeId,
216         hir_id: hir::HirId,
217         ident: &mut Ident,
218         attrs: Option<&'hir [Attribute]>,
219         vis_span: Span,
220         i: &ItemKind,
221     ) -> hir::ItemKind<'hir> {
222         match i {
223             ItemKind::ExternCrate(orig_name) => hir::ItemKind::ExternCrate(*orig_name),
224             ItemKind::Use(use_tree) => {
225                 // Start with an empty prefix.
226                 let prefix = Path { segments: ThinVec::new(), span: use_tree.span, tokens: None };
227 
228                 self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
229             }
230             ItemKind::Static(box ast::StaticItem { ty: t, mutability: m, expr: e }) => {
231                 let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
232                 hir::ItemKind::Static(ty, *m, body_id)
233             }
234             ItemKind::Const(box ast::ConstItem { ty, expr, .. }) => {
235                 let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
236                 hir::ItemKind::Const(ty, body_id)
237             }
238             ItemKind::Fn(box Fn {
239                 sig: FnSig { decl, header, span: fn_sig_span },
240                 generics,
241                 body,
242                 ..
243             }) => {
244                 self.with_new_scopes(|this| {
245                     this.current_item = Some(ident.span);
246 
247                     // Note: we don't need to change the return type from `T` to
248                     // `impl Future<Output = T>` here because lower_body
249                     // only cares about the input argument patterns in the function
250                     // declaration (decl), not the return types.
251                     let asyncness = header.asyncness;
252                     let body_id = this.lower_maybe_async_body(
253                         span,
254                         hir_id,
255                         &decl,
256                         asyncness,
257                         body.as_deref(),
258                     );
259 
260                     let itctx = ImplTraitContext::Universal;
261                     let (generics, decl) =
262                         this.lower_generics(generics, header.constness, id, &itctx, |this| {
263                             let ret_id = asyncness.opt_return_id();
264                             this.lower_fn_decl(&decl, id, *fn_sig_span, FnDeclKind::Fn, ret_id)
265                         });
266                     let sig = hir::FnSig {
267                         decl,
268                         header: this.lower_fn_header(*header),
269                         span: this.lower_span(*fn_sig_span),
270                     };
271                     hir::ItemKind::Fn(sig, generics, body_id)
272                 })
273             }
274             ItemKind::Mod(_, mod_kind) => match mod_kind {
275                 ModKind::Loaded(items, _, spans) => {
276                     hir::ItemKind::Mod(self.lower_mod(items, spans))
277                 }
278                 ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
279             },
280             ItemKind::ForeignMod(fm) => hir::ItemKind::ForeignMod {
281                 abi: fm.abi.map_or(abi::Abi::FALLBACK, |abi| self.lower_abi(abi)),
282                 items: self
283                     .arena
284                     .alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
285             },
286             ItemKind::GlobalAsm(asm) => hir::ItemKind::GlobalAsm(self.lower_inline_asm(span, asm)),
287             ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty, .. }) => {
288                 // We lower
289                 //
290                 // type Foo = impl Trait
291                 //
292                 // to
293                 //
294                 // type Foo = Foo1
295                 // opaque type Foo1: Trait
296                 let mut generics = generics.clone();
297                 add_ty_alias_where_clause(&mut generics, *where_clauses, true);
298                 let (generics, ty) = self.lower_generics(
299                     &generics,
300                     Const::No,
301                     id,
302                     &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
303                     |this| match ty {
304                         None => {
305                             let guar = this.tcx.sess.delay_span_bug(
306                                 span,
307                                 "expected to lower type alias type, but it was missing",
308                             );
309                             this.arena.alloc(this.ty(span, hir::TyKind::Err(guar)))
310                         }
311                         Some(ty) => this.lower_ty(
312                             ty,
313                             &ImplTraitContext::TypeAliasesOpaqueTy { in_assoc_ty: false },
314                         ),
315                     },
316                 );
317                 hir::ItemKind::TyAlias(ty, generics)
318             }
319             ItemKind::Enum(enum_definition, generics) => {
320                 let (generics, variants) = self.lower_generics(
321                     generics,
322                     Const::No,
323                     id,
324                     &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
325                     |this| {
326                         this.arena.alloc_from_iter(
327                             enum_definition.variants.iter().map(|x| this.lower_variant(x)),
328                         )
329                     },
330                 );
331                 hir::ItemKind::Enum(hir::EnumDef { variants }, generics)
332             }
333             ItemKind::Struct(struct_def, generics) => {
334                 let (generics, struct_def) = self.lower_generics(
335                     generics,
336                     Const::No,
337                     id,
338                     &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
339                     |this| this.lower_variant_data(hir_id, struct_def),
340                 );
341                 hir::ItemKind::Struct(struct_def, generics)
342             }
343             ItemKind::Union(vdata, generics) => {
344                 let (generics, vdata) = self.lower_generics(
345                     generics,
346                     Const::No,
347                     id,
348                     &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
349                     |this| this.lower_variant_data(hir_id, vdata),
350                 );
351                 hir::ItemKind::Union(vdata, generics)
352             }
353             ItemKind::Impl(box Impl {
354                 unsafety,
355                 polarity,
356                 defaultness,
357                 constness,
358                 generics: ast_generics,
359                 of_trait: trait_ref,
360                 self_ty: ty,
361                 items: impl_items,
362             }) => {
363                 // Lower the "impl header" first. This ordering is important
364                 // for in-band lifetimes! Consider `'a` here:
365                 //
366                 //     impl Foo<'a> for u32 {
367                 //         fn method(&'a self) { .. }
368                 //     }
369                 //
370                 // Because we start by lowering the `Foo<'a> for u32`
371                 // part, we will add `'a` to the list of generics on
372                 // the impl. When we then encounter it later in the
373                 // method, it will not be considered an in-band
374                 // lifetime to be added, but rather a reference to a
375                 // parent lifetime.
376                 let itctx = ImplTraitContext::Universal;
377                 let (generics, (trait_ref, lowered_ty)) =
378                     self.lower_generics(ast_generics, *constness, id, &itctx, |this| {
379                         let trait_ref = trait_ref.as_ref().map(|trait_ref| {
380                             this.lower_trait_ref(
381                                 trait_ref,
382                                 &ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
383                             )
384                         });
385 
386                         let lowered_ty = this.lower_ty(
387                             ty,
388                             &ImplTraitContext::Disallowed(ImplTraitPosition::ImplSelf),
389                         );
390 
391                         (trait_ref, lowered_ty)
392                     });
393 
394                 let new_impl_items = self
395                     .arena
396                     .alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
397 
398                 // `defaultness.has_value()` is never called for an `impl`, always `true` in order
399                 // to not cause an assertion failure inside the `lower_defaultness` function.
400                 let has_val = true;
401                 let (defaultness, defaultness_span) = self.lower_defaultness(*defaultness, has_val);
402                 let polarity = match polarity {
403                     ImplPolarity::Positive => ImplPolarity::Positive,
404                     ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(*s)),
405                 };
406                 hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
407                     unsafety: self.lower_unsafety(*unsafety),
408                     polarity,
409                     defaultness,
410                     defaultness_span,
411                     constness: self.lower_constness(*constness),
412                     generics,
413                     of_trait: trait_ref,
414                     self_ty: lowered_ty,
415                     items: new_impl_items,
416                 }))
417             }
418             ItemKind::Trait(box Trait { is_auto, unsafety, generics, bounds, items }) => {
419                 // FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible
420                 let constness = attrs
421                     .unwrap_or(&[])
422                     .iter()
423                     .find(|x| x.has_name(sym::const_trait))
424                     .map_or(Const::No, |x| Const::Yes(x.span));
425                 let (generics, (unsafety, items, bounds)) = self.lower_generics(
426                     generics,
427                     constness,
428                     id,
429                     &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
430                     |this| {
431                         let bounds = this.lower_param_bounds(
432                             bounds,
433                             &ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
434                         );
435                         let items = this.arena.alloc_from_iter(
436                             items.iter().map(|item| this.lower_trait_item_ref(item)),
437                         );
438                         let unsafety = this.lower_unsafety(*unsafety);
439                         (unsafety, items, bounds)
440                     },
441                 );
442                 hir::ItemKind::Trait(*is_auto, unsafety, generics, bounds, items)
443             }
444             ItemKind::TraitAlias(generics, bounds) => {
445                 let (generics, bounds) = self.lower_generics(
446                     generics,
447                     Const::No,
448                     id,
449                     &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
450                     |this| {
451                         this.lower_param_bounds(
452                             bounds,
453                             &ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
454                         )
455                     },
456                 );
457                 hir::ItemKind::TraitAlias(generics, bounds)
458             }
459             ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
460                 let body = P(self.lower_delim_args(body));
461                 let macro_kind = self.resolver.decl_macro_kind(self.local_def_id(id));
462                 let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
463                 hir::ItemKind::Macro(macro_def, macro_kind)
464             }
465             ItemKind::MacCall(..) => {
466                 panic!("`TyMac` should have been expanded by now")
467             }
468         }
469     }
470 
lower_const_item( &mut self, ty: &Ty, span: Span, body: Option<&Expr>, ) -> (&'hir hir::Ty<'hir>, hir::BodyId)471     fn lower_const_item(
472         &mut self,
473         ty: &Ty,
474         span: Span,
475         body: Option<&Expr>,
476     ) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
477         let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
478         (ty, self.lower_const_body(span, body))
479     }
480 
481     #[instrument(level = "debug", skip(self))]
lower_use_tree( &mut self, tree: &UseTree, prefix: &Path, id: NodeId, vis_span: Span, ident: &mut Ident, attrs: Option<&'hir [Attribute]>, ) -> hir::ItemKind<'hir>482     fn lower_use_tree(
483         &mut self,
484         tree: &UseTree,
485         prefix: &Path,
486         id: NodeId,
487         vis_span: Span,
488         ident: &mut Ident,
489         attrs: Option<&'hir [Attribute]>,
490     ) -> hir::ItemKind<'hir> {
491         let path = &tree.prefix;
492         let segments = prefix.segments.iter().chain(path.segments.iter()).cloned().collect();
493 
494         match tree.kind {
495             UseTreeKind::Simple(rename) => {
496                 *ident = tree.ident();
497 
498                 // First, apply the prefix to the path.
499                 let mut path = Path { segments, span: path.span, tokens: None };
500 
501                 // Correctly resolve `self` imports.
502                 if path.segments.len() > 1
503                     && path.segments.last().unwrap().ident.name == kw::SelfLower
504                 {
505                     let _ = path.segments.pop();
506                     if rename.is_none() {
507                         *ident = path.segments.last().unwrap().ident;
508                     }
509                 }
510 
511                 let res =
512                     self.expect_full_res_from_use(id).map(|res| self.lower_res(res)).collect();
513                 let path = self.lower_use_path(res, &path, ParamMode::Explicit);
514                 hir::ItemKind::Use(path, hir::UseKind::Single)
515             }
516             UseTreeKind::Glob => {
517                 let res = self.expect_full_res(id);
518                 let res = smallvec![self.lower_res(res)];
519                 let path = Path { segments, span: path.span, tokens: None };
520                 let path = self.lower_use_path(res, &path, ParamMode::Explicit);
521                 hir::ItemKind::Use(path, hir::UseKind::Glob)
522             }
523             UseTreeKind::Nested(ref trees) => {
524                 // Nested imports are desugared into simple imports.
525                 // So, if we start with
526                 //
527                 // ```
528                 // pub(x) use foo::{a, b};
529                 // ```
530                 //
531                 // we will create three items:
532                 //
533                 // ```
534                 // pub(x) use foo::a;
535                 // pub(x) use foo::b;
536                 // pub(x) use foo::{}; // <-- this is called the `ListStem`
537                 // ```
538                 //
539                 // The first two are produced by recursively invoking
540                 // `lower_use_tree` (and indeed there may be things
541                 // like `use foo::{a::{b, c}}` and so forth). They
542                 // wind up being directly added to
543                 // `self.items`. However, the structure of this
544                 // function also requires us to return one item, and
545                 // for that we return the `{}` import (called the
546                 // `ListStem`).
547 
548                 let prefix = Path { segments, span: prefix.span.to(path.span), tokens: None };
549 
550                 // Add all the nested `PathListItem`s to the HIR.
551                 for &(ref use_tree, id) in trees {
552                     let new_hir_id = self.local_def_id(id);
553 
554                     let mut prefix = prefix.clone();
555 
556                     // Give the segments new node-ids since they are being cloned.
557                     for seg in &mut prefix.segments {
558                         // Give the cloned segment the same resolution information
559                         // as the old one (this is needed for stability checking).
560                         let new_id = self.next_node_id();
561                         self.resolver.clone_res(seg.id, new_id);
562                         seg.id = new_id;
563                     }
564 
565                     // Each `use` import is an item and thus are owners of the
566                     // names in the path. Up to this point the nested import is
567                     // the current owner, since we want each desugared import to
568                     // own its own names, we have to adjust the owner before
569                     // lowering the rest of the import.
570                     self.with_hir_id_owner(id, |this| {
571                         let mut ident = *ident;
572 
573                         let kind =
574                             this.lower_use_tree(use_tree, &prefix, id, vis_span, &mut ident, attrs);
575                         if let Some(attrs) = attrs {
576                             this.attrs.insert(hir::ItemLocalId::new(0), attrs);
577                         }
578 
579                         let item = hir::Item {
580                             owner_id: hir::OwnerId { def_id: new_hir_id },
581                             ident: this.lower_ident(ident),
582                             kind,
583                             vis_span,
584                             span: this.lower_span(use_tree.span),
585                         };
586                         hir::OwnerNode::Item(this.arena.alloc(item))
587                     });
588                 }
589 
590                 let res =
591                     self.expect_full_res_from_use(id).map(|res| self.lower_res(res)).collect();
592                 let path = self.lower_use_path(res, &prefix, ParamMode::Explicit);
593                 hir::ItemKind::Use(path, hir::UseKind::ListStem)
594             }
595         }
596     }
597 
lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir>598     fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
599         let hir_id = self.lower_node_id(i.id);
600         let owner_id = hir_id.expect_owner();
601         self.lower_attrs(hir_id, &i.attrs);
602         let item = hir::ForeignItem {
603             owner_id,
604             ident: self.lower_ident(i.ident),
605             kind: match &i.kind {
606                 ForeignItemKind::Fn(box Fn { sig, generics, .. }) => {
607                     let fdec = &sig.decl;
608                     let itctx = ImplTraitContext::Universal;
609                     let (generics, (fn_dec, fn_args)) =
610                         self.lower_generics(generics, Const::No, i.id, &itctx, |this| {
611                             (
612                                 // Disallow `impl Trait` in foreign items.
613                                 this.lower_fn_decl(
614                                     fdec,
615                                     i.id,
616                                     sig.span,
617                                     FnDeclKind::ExternFn,
618                                     None,
619                                 ),
620                                 this.lower_fn_params_to_names(fdec),
621                             )
622                         });
623 
624                     hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
625                 }
626                 ForeignItemKind::Static(t, m, _) => {
627                     let ty = self
628                         .lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
629                     hir::ForeignItemKind::Static(ty, *m)
630                 }
631                 ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
632                 ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),
633             },
634             vis_span: self.lower_span(i.vis.span),
635             span: self.lower_span(i.span),
636         };
637         self.arena.alloc(item)
638     }
639 
lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef640     fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
641         hir::ForeignItemRef {
642             id: hir::ForeignItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
643             ident: self.lower_ident(i.ident),
644             span: self.lower_span(i.span),
645         }
646     }
647 
lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir>648     fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> {
649         let hir_id = self.lower_node_id(v.id);
650         self.lower_attrs(hir_id, &v.attrs);
651         hir::Variant {
652             hir_id,
653             def_id: self.local_def_id(v.id),
654             data: self.lower_variant_data(hir_id, &v.data),
655             disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
656             ident: self.lower_ident(v.ident),
657             span: self.lower_span(v.span),
658         }
659     }
660 
lower_variant_data( &mut self, parent_id: hir::HirId, vdata: &VariantData, ) -> hir::VariantData<'hir>661     fn lower_variant_data(
662         &mut self,
663         parent_id: hir::HirId,
664         vdata: &VariantData,
665     ) -> hir::VariantData<'hir> {
666         match vdata {
667             VariantData::Struct(fields, recovered) => hir::VariantData::Struct(
668                 self.arena
669                     .alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_field_def(f))),
670                 *recovered,
671             ),
672             VariantData::Tuple(fields, id) => {
673                 let ctor_id = self.lower_node_id(*id);
674                 self.alias_attrs(ctor_id, parent_id);
675                 hir::VariantData::Tuple(
676                     self.arena.alloc_from_iter(
677                         fields.iter().enumerate().map(|f| self.lower_field_def(f)),
678                     ),
679                     ctor_id,
680                     self.local_def_id(*id),
681                 )
682             }
683             VariantData::Unit(id) => {
684                 let ctor_id = self.lower_node_id(*id);
685                 self.alias_attrs(ctor_id, parent_id);
686                 hir::VariantData::Unit(ctor_id, self.local_def_id(*id))
687             }
688         }
689     }
690 
lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir>691     fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir> {
692         let ty = if let TyKind::Path(qself, path) = &f.ty.kind {
693             let t = self.lower_path_ty(
694                 &f.ty,
695                 qself,
696                 path,
697                 ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124)
698                 &ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy),
699             );
700             self.arena.alloc(t)
701         } else {
702             self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy))
703         };
704         let hir_id = self.lower_node_id(f.id);
705         self.lower_attrs(hir_id, &f.attrs);
706         hir::FieldDef {
707             span: self.lower_span(f.span),
708             hir_id,
709             def_id: self.local_def_id(f.id),
710             ident: match f.ident {
711                 Some(ident) => self.lower_ident(ident),
712                 // FIXME(jseyfried): positional field hygiene.
713                 None => Ident::new(sym::integer(index), self.lower_span(f.span)),
714             },
715             vis_span: self.lower_span(f.vis.span),
716             ty,
717         }
718     }
719 
lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir>720     fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
721         let hir_id = self.lower_node_id(i.id);
722         self.lower_attrs(hir_id, &i.attrs);
723         let trait_item_def_id = hir_id.expect_owner();
724 
725         let (generics, kind, has_default) = match &i.kind {
726             AssocItemKind::Const(box ConstItem { ty, expr, .. }) => {
727                 let ty =
728                     self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
729                 let body = expr.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
730                 (hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
731             }
732             AssocItemKind::Fn(box Fn { sig, generics, body: None, .. }) => {
733                 let asyncness = sig.header.asyncness;
734                 let names = self.lower_fn_params_to_names(&sig.decl);
735                 let (generics, sig) = self.lower_method_sig(
736                     generics,
737                     sig,
738                     i.id,
739                     FnDeclKind::Trait,
740                     asyncness.opt_return_id(),
741                 );
742                 (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)), false)
743             }
744             AssocItemKind::Fn(box Fn { sig, generics, body: Some(body), .. }) => {
745                 let asyncness = sig.header.asyncness;
746                 let body_id =
747                     self.lower_maybe_async_body(i.span, hir_id, &sig.decl, asyncness, Some(&body));
748                 let (generics, sig) = self.lower_method_sig(
749                     generics,
750                     sig,
751                     i.id,
752                     FnDeclKind::Trait,
753                     asyncness.opt_return_id(),
754                 );
755                 (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)), true)
756             }
757             AssocItemKind::Type(box TyAlias { generics, where_clauses, bounds, ty, .. }) => {
758                 let mut generics = generics.clone();
759                 add_ty_alias_where_clause(&mut generics, *where_clauses, false);
760                 let (generics, kind) = self.lower_generics(
761                     &generics,
762                     Const::No,
763                     i.id,
764                     &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
765                     |this| {
766                         let ty = ty.as_ref().map(|x| {
767                             this.lower_ty(
768                                 x,
769                                 &ImplTraitContext::Disallowed(ImplTraitPosition::AssocTy),
770                             )
771                         });
772                         hir::TraitItemKind::Type(
773                             this.lower_param_bounds(
774                                 bounds,
775                                 &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
776                             ),
777                             ty,
778                         )
779                     },
780                 );
781                 (generics, kind, ty.is_some())
782             }
783             AssocItemKind::MacCall(..) => panic!("macro item shouldn't exist at this point"),
784         };
785 
786         let item = hir::TraitItem {
787             owner_id: trait_item_def_id,
788             ident: self.lower_ident(i.ident),
789             generics,
790             kind,
791             span: self.lower_span(i.span),
792             defaultness: hir::Defaultness::Default { has_value: has_default },
793         };
794         self.arena.alloc(item)
795     }
796 
lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef797     fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
798         let kind = match &i.kind {
799             AssocItemKind::Const(..) => hir::AssocItemKind::Const,
800             AssocItemKind::Type(..) => hir::AssocItemKind::Type,
801             AssocItemKind::Fn(box Fn { sig, .. }) => {
802                 hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
803             }
804             AssocItemKind::MacCall(..) => unimplemented!(),
805         };
806         let id = hir::TraitItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } };
807         hir::TraitItemRef {
808             id,
809             ident: self.lower_ident(i.ident),
810             span: self.lower_span(i.span),
811             kind,
812         }
813     }
814 
815     /// Construct `ExprKind::Err` for the given `span`.
expr_err(&mut self, span: Span, guar: ErrorGuaranteed) -> hir::Expr<'hir>816     pub(crate) fn expr_err(&mut self, span: Span, guar: ErrorGuaranteed) -> hir::Expr<'hir> {
817         self.expr(span, hir::ExprKind::Err(guar))
818     }
819 
lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir>820     fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
821         // Since `default impl` is not yet implemented, this is always true in impls.
822         let has_value = true;
823         let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
824         let hir_id = self.lower_node_id(i.id);
825         self.lower_attrs(hir_id, &i.attrs);
826 
827         let (generics, kind) = match &i.kind {
828             AssocItemKind::Const(box ConstItem { ty, expr, .. }) => {
829                 let ty =
830                     self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
831                 (
832                     hir::Generics::empty(),
833                     hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
834                 )
835             }
836             AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
837                 self.current_item = Some(i.span);
838                 let asyncness = sig.header.asyncness;
839                 let body_id = self.lower_maybe_async_body(
840                     i.span,
841                     hir_id,
842                     &sig.decl,
843                     asyncness,
844                     body.as_deref(),
845                 );
846                 let (generics, sig) = self.lower_method_sig(
847                     generics,
848                     sig,
849                     i.id,
850                     if self.is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
851                     asyncness.opt_return_id(),
852                 );
853 
854                 (generics, hir::ImplItemKind::Fn(sig, body_id))
855             }
856             AssocItemKind::Type(box TyAlias { generics, where_clauses, ty, .. }) => {
857                 let mut generics = generics.clone();
858                 add_ty_alias_where_clause(&mut generics, *where_clauses, false);
859                 self.lower_generics(
860                     &generics,
861                     Const::No,
862                     i.id,
863                     &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
864                     |this| match ty {
865                         None => {
866                             let guar = this.tcx.sess.delay_span_bug(
867                                 i.span,
868                                 "expected to lower associated type, but it was missing",
869                             );
870                             let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err(guar)));
871                             hir::ImplItemKind::Type(ty)
872                         }
873                         Some(ty) => {
874                             let ty = this.lower_ty(
875                                 ty,
876                                 &ImplTraitContext::TypeAliasesOpaqueTy { in_assoc_ty: true },
877                             );
878                             hir::ImplItemKind::Type(ty)
879                         }
880                     },
881                 )
882             }
883             AssocItemKind::MacCall(..) => panic!("`TyMac` should have been expanded by now"),
884         };
885 
886         let item = hir::ImplItem {
887             owner_id: hir_id.expect_owner(),
888             ident: self.lower_ident(i.ident),
889             generics,
890             kind,
891             vis_span: self.lower_span(i.vis.span),
892             span: self.lower_span(i.span),
893             defaultness,
894         };
895         self.arena.alloc(item)
896     }
897 
lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef898     fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
899         hir::ImplItemRef {
900             id: hir::ImplItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
901             ident: self.lower_ident(i.ident),
902             span: self.lower_span(i.span),
903             kind: match &i.kind {
904                 AssocItemKind::Const(..) => hir::AssocItemKind::Const,
905                 AssocItemKind::Type(..) => hir::AssocItemKind::Type,
906                 AssocItemKind::Fn(box Fn { sig, .. }) => {
907                     hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
908                 }
909                 AssocItemKind::MacCall(..) => unimplemented!(),
910             },
911             trait_item_def_id: self
912                 .resolver
913                 .get_partial_res(i.id)
914                 .map(|r| r.expect_full_res().def_id()),
915         }
916     }
917 
lower_defaultness( &self, d: Defaultness, has_value: bool, ) -> (hir::Defaultness, Option<Span>)918     fn lower_defaultness(
919         &self,
920         d: Defaultness,
921         has_value: bool,
922     ) -> (hir::Defaultness, Option<Span>) {
923         match d {
924             Defaultness::Default(sp) => {
925                 (hir::Defaultness::Default { has_value }, Some(self.lower_span(sp)))
926             }
927             Defaultness::Final => {
928                 assert!(has_value);
929                 (hir::Defaultness::Final, None)
930             }
931         }
932     }
933 
record_body( &mut self, params: &'hir [hir::Param<'hir>], value: hir::Expr<'hir>, ) -> hir::BodyId934     fn record_body(
935         &mut self,
936         params: &'hir [hir::Param<'hir>],
937         value: hir::Expr<'hir>,
938     ) -> hir::BodyId {
939         let body = hir::Body {
940             generator_kind: self.generator_kind,
941             params,
942             value: self.arena.alloc(value),
943         };
944         let id = body.id();
945         debug_assert_eq!(id.hir_id.owner, self.current_hir_id_owner);
946         self.bodies.push((id.hir_id.local_id, self.arena.alloc(body)));
947         id
948     }
949 
lower_body( &mut self, f: impl FnOnce(&mut Self) -> (&'hir [hir::Param<'hir>], hir::Expr<'hir>), ) -> hir::BodyId950     pub(super) fn lower_body(
951         &mut self,
952         f: impl FnOnce(&mut Self) -> (&'hir [hir::Param<'hir>], hir::Expr<'hir>),
953     ) -> hir::BodyId {
954         let prev_gen_kind = self.generator_kind.take();
955         let task_context = self.task_context.take();
956         let (parameters, result) = f(self);
957         let body_id = self.record_body(parameters, result);
958         self.task_context = task_context;
959         self.generator_kind = prev_gen_kind;
960         body_id
961     }
962 
lower_param(&mut self, param: &Param) -> hir::Param<'hir>963     fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
964         let hir_id = self.lower_node_id(param.id);
965         self.lower_attrs(hir_id, &param.attrs);
966         hir::Param {
967             hir_id,
968             pat: self.lower_pat(&param.pat),
969             ty_span: self.lower_span(param.ty.span),
970             span: self.lower_span(param.span),
971         }
972     }
973 
lower_fn_body( &mut self, decl: &FnDecl, body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, ) -> hir::BodyId974     pub(super) fn lower_fn_body(
975         &mut self,
976         decl: &FnDecl,
977         body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
978     ) -> hir::BodyId {
979         self.lower_body(|this| {
980             (
981                 this.arena.alloc_from_iter(decl.inputs.iter().map(|x| this.lower_param(x))),
982                 body(this),
983             )
984         })
985     }
986 
lower_fn_body_block( &mut self, span: Span, decl: &FnDecl, body: Option<&Block>, ) -> hir::BodyId987     fn lower_fn_body_block(
988         &mut self,
989         span: Span,
990         decl: &FnDecl,
991         body: Option<&Block>,
992     ) -> hir::BodyId {
993         self.lower_fn_body(decl, |this| this.lower_block_expr_opt(span, body))
994     }
995 
lower_block_expr_opt(&mut self, span: Span, block: Option<&Block>) -> hir::Expr<'hir>996     fn lower_block_expr_opt(&mut self, span: Span, block: Option<&Block>) -> hir::Expr<'hir> {
997         match block {
998             Some(block) => self.lower_block_expr(block),
999             None => self.expr_err(span, self.tcx.sess.delay_span_bug(span, "no block")),
1000         }
1001     }
1002 
lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId1003     pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId {
1004         self.lower_body(|this| {
1005             (
1006                 &[],
1007                 match expr {
1008                     Some(expr) => this.lower_expr_mut(expr),
1009                     None => this.expr_err(span, this.tcx.sess.delay_span_bug(span, "no block")),
1010                 },
1011             )
1012         })
1013     }
1014 
lower_maybe_async_body( &mut self, span: Span, fn_id: hir::HirId, decl: &FnDecl, asyncness: Async, body: Option<&Block>, ) -> hir::BodyId1015     fn lower_maybe_async_body(
1016         &mut self,
1017         span: Span,
1018         fn_id: hir::HirId,
1019         decl: &FnDecl,
1020         asyncness: Async,
1021         body: Option<&Block>,
1022     ) -> hir::BodyId {
1023         let (closure_id, body) = match (asyncness, body) {
1024             (Async::Yes { closure_id, .. }, Some(body)) => (closure_id, body),
1025             _ => return self.lower_fn_body_block(span, decl, body),
1026         };
1027 
1028         self.lower_body(|this| {
1029             let mut parameters: Vec<hir::Param<'_>> = Vec::new();
1030             let mut statements: Vec<hir::Stmt<'_>> = Vec::new();
1031 
1032             // Async function parameters are lowered into the closure body so that they are
1033             // captured and so that the drop order matches the equivalent non-async functions.
1034             //
1035             // from:
1036             //
1037             //     async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
1038             //         <body>
1039             //     }
1040             //
1041             // into:
1042             //
1043             //     fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
1044             //       async move {
1045             //         let __arg2 = __arg2;
1046             //         let <pattern> = __arg2;
1047             //         let __arg1 = __arg1;
1048             //         let <pattern> = __arg1;
1049             //         let __arg0 = __arg0;
1050             //         let <pattern> = __arg0;
1051             //         drop-temps { <body> } // see comments later in fn for details
1052             //       }
1053             //     }
1054             //
1055             // If `<pattern>` is a simple ident, then it is lowered to a single
1056             // `let <pattern> = <pattern>;` statement as an optimization.
1057             //
1058             // Note that the body is embedded in `drop-temps`; an
1059             // equivalent desugaring would be `return { <body>
1060             // };`. The key point is that we wish to drop all the
1061             // let-bound variables and temporaries created in the body
1062             // (and its tail expression!) before we drop the
1063             // parameters (c.f. rust-lang/rust#64512).
1064             for (index, parameter) in decl.inputs.iter().enumerate() {
1065                 let parameter = this.lower_param(parameter);
1066                 let span = parameter.pat.span;
1067 
1068                 // Check if this is a binding pattern, if so, we can optimize and avoid adding a
1069                 // `let <pat> = __argN;` statement. In this case, we do not rename the parameter.
1070                 let (ident, is_simple_parameter) = match parameter.pat.kind {
1071                     hir::PatKind::Binding(hir::BindingAnnotation(ByRef::No, _), _, ident, _) => {
1072                         (ident, true)
1073                     }
1074                     // For `ref mut` or wildcard arguments, we can't reuse the binding, but
1075                     // we can keep the same name for the parameter.
1076                     // This lets rustdoc render it correctly in documentation.
1077                     hir::PatKind::Binding(_, _, ident, _) => (ident, false),
1078                     hir::PatKind::Wild => {
1079                         (Ident::with_dummy_span(rustc_span::symbol::kw::Underscore), false)
1080                     }
1081                     _ => {
1082                         // Replace the ident for bindings that aren't simple.
1083                         let name = format!("__arg{index}");
1084                         let ident = Ident::from_str(&name);
1085 
1086                         (ident, false)
1087                     }
1088                 };
1089 
1090                 let desugared_span = this.mark_span_with_reason(DesugaringKind::Async, span, None);
1091 
1092                 // Construct a parameter representing `__argN: <ty>` to replace the parameter of the
1093                 // async function.
1094                 //
1095                 // If this is the simple case, this parameter will end up being the same as the
1096                 // original parameter, but with a different pattern id.
1097                 let stmt_attrs = this.attrs.get(&parameter.hir_id.local_id).copied();
1098                 let (new_parameter_pat, new_parameter_id) = this.pat_ident(desugared_span, ident);
1099                 let new_parameter = hir::Param {
1100                     hir_id: parameter.hir_id,
1101                     pat: new_parameter_pat,
1102                     ty_span: this.lower_span(parameter.ty_span),
1103                     span: this.lower_span(parameter.span),
1104                 };
1105 
1106                 if is_simple_parameter {
1107                     // If this is the simple case, then we only insert one statement that is
1108                     // `let <pat> = <pat>;`. We re-use the original argument's pattern so that
1109                     // `HirId`s are densely assigned.
1110                     let expr = this.expr_ident(desugared_span, ident, new_parameter_id);
1111                     let stmt = this.stmt_let_pat(
1112                         stmt_attrs,
1113                         desugared_span,
1114                         Some(expr),
1115                         parameter.pat,
1116                         hir::LocalSource::AsyncFn,
1117                     );
1118                     statements.push(stmt);
1119                 } else {
1120                     // If this is not the simple case, then we construct two statements:
1121                     //
1122                     // ```
1123                     // let __argN = __argN;
1124                     // let <pat> = __argN;
1125                     // ```
1126                     //
1127                     // The first statement moves the parameter into the closure and thus ensures
1128                     // that the drop order is correct.
1129                     //
1130                     // The second statement creates the bindings that the user wrote.
1131 
1132                     // Construct the `let mut __argN = __argN;` statement. It must be a mut binding
1133                     // because the user may have specified a `ref mut` binding in the next
1134                     // statement.
1135                     let (move_pat, move_id) = this.pat_ident_binding_mode(
1136                         desugared_span,
1137                         ident,
1138                         hir::BindingAnnotation::MUT,
1139                     );
1140                     let move_expr = this.expr_ident(desugared_span, ident, new_parameter_id);
1141                     let move_stmt = this.stmt_let_pat(
1142                         None,
1143                         desugared_span,
1144                         Some(move_expr),
1145                         move_pat,
1146                         hir::LocalSource::AsyncFn,
1147                     );
1148 
1149                     // Construct the `let <pat> = __argN;` statement. We re-use the original
1150                     // parameter's pattern so that `HirId`s are densely assigned.
1151                     let pattern_expr = this.expr_ident(desugared_span, ident, move_id);
1152                     let pattern_stmt = this.stmt_let_pat(
1153                         stmt_attrs,
1154                         desugared_span,
1155                         Some(pattern_expr),
1156                         parameter.pat,
1157                         hir::LocalSource::AsyncFn,
1158                     );
1159 
1160                     statements.push(move_stmt);
1161                     statements.push(pattern_stmt);
1162                 };
1163 
1164                 parameters.push(new_parameter);
1165             }
1166 
1167             let async_expr = this.make_async_expr(
1168                 CaptureBy::Value,
1169                 closure_id,
1170                 None,
1171                 body.span,
1172                 hir::AsyncGeneratorKind::Fn,
1173                 |this| {
1174                     // Create a block from the user's function body:
1175                     let user_body = this.lower_block_expr(body);
1176 
1177                     // Transform into `drop-temps { <user-body> }`, an expression:
1178                     let desugared_span =
1179                         this.mark_span_with_reason(DesugaringKind::Async, user_body.span, None);
1180                     let user_body =
1181                         this.expr_drop_temps(desugared_span, this.arena.alloc(user_body));
1182 
1183                     // As noted above, create the final block like
1184                     //
1185                     // ```
1186                     // {
1187                     //   let $param_pattern = $raw_param;
1188                     //   ...
1189                     //   drop-temps { <user-body> }
1190                     // }
1191                     // ```
1192                     let body = this.block_all(
1193                         desugared_span,
1194                         this.arena.alloc_from_iter(statements),
1195                         Some(user_body),
1196                     );
1197 
1198                     this.expr_block(body)
1199                 },
1200             );
1201 
1202             let hir_id = this.lower_node_id(closure_id);
1203             this.maybe_forward_track_caller(body.span, fn_id, hir_id);
1204             let expr = hir::Expr { hir_id, kind: async_expr, span: this.lower_span(body.span) };
1205 
1206             (this.arena.alloc_from_iter(parameters), expr)
1207         })
1208     }
1209 
lower_method_sig( &mut self, generics: &Generics, sig: &FnSig, id: NodeId, kind: FnDeclKind, is_async: Option<(NodeId, Span)>, ) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>)1210     fn lower_method_sig(
1211         &mut self,
1212         generics: &Generics,
1213         sig: &FnSig,
1214         id: NodeId,
1215         kind: FnDeclKind,
1216         is_async: Option<(NodeId, Span)>,
1217     ) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
1218         let header = self.lower_fn_header(sig.header);
1219         let itctx = ImplTraitContext::Universal;
1220         let (generics, decl) =
1221             self.lower_generics(generics, sig.header.constness, id, &itctx, |this| {
1222                 this.lower_fn_decl(&sig.decl, id, sig.span, kind, is_async)
1223             });
1224         (generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
1225     }
1226 
lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader1227     fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
1228         hir::FnHeader {
1229             unsafety: self.lower_unsafety(h.unsafety),
1230             asyncness: self.lower_asyncness(h.asyncness),
1231             constness: self.lower_constness(h.constness),
1232             abi: self.lower_extern(h.ext),
1233         }
1234     }
1235 
lower_abi(&mut self, abi: StrLit) -> abi::Abi1236     pub(super) fn lower_abi(&mut self, abi: StrLit) -> abi::Abi {
1237         abi::lookup(abi.symbol_unescaped.as_str()).unwrap_or_else(|| {
1238             self.error_on_invalid_abi(abi);
1239             abi::Abi::Rust
1240         })
1241     }
1242 
lower_extern(&mut self, ext: Extern) -> abi::Abi1243     pub(super) fn lower_extern(&mut self, ext: Extern) -> abi::Abi {
1244         match ext {
1245             Extern::None => abi::Abi::Rust,
1246             Extern::Implicit(_) => abi::Abi::FALLBACK,
1247             Extern::Explicit(abi, _) => self.lower_abi(abi),
1248         }
1249     }
1250 
error_on_invalid_abi(&self, abi: StrLit)1251     fn error_on_invalid_abi(&self, abi: StrLit) {
1252         let abi_names = abi::enabled_names(self.tcx.features(), abi.span)
1253             .iter()
1254             .map(|s| Symbol::intern(s))
1255             .collect::<Vec<_>>();
1256         let suggested_name = find_best_match_for_name(&abi_names, abi.symbol_unescaped, None);
1257         self.tcx.sess.emit_err(InvalidAbi {
1258             abi: abi.symbol_unescaped,
1259             span: abi.span,
1260             suggestion: suggested_name.map(|suggested_name| InvalidAbiSuggestion {
1261                 span: abi.span,
1262                 suggestion: format!("\"{suggested_name}\""),
1263             }),
1264             command: "rustc --print=calling-conventions".to_string(),
1265         });
1266     }
1267 
lower_asyncness(&mut self, a: Async) -> hir::IsAsync1268     fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync {
1269         match a {
1270             Async::Yes { .. } => hir::IsAsync::Async,
1271             Async::No => hir::IsAsync::NotAsync,
1272         }
1273     }
1274 
lower_constness(&mut self, c: Const) -> hir::Constness1275     pub(super) fn lower_constness(&mut self, c: Const) -> hir::Constness {
1276         match c {
1277             Const::Yes(_) => hir::Constness::Const,
1278             Const::No => hir::Constness::NotConst,
1279         }
1280     }
1281 
lower_unsafety(&mut self, u: Unsafe) -> hir::Unsafety1282     pub(super) fn lower_unsafety(&mut self, u: Unsafe) -> hir::Unsafety {
1283         match u {
1284             Unsafe::Yes(_) => hir::Unsafety::Unsafe,
1285             Unsafe::No => hir::Unsafety::Normal,
1286         }
1287     }
1288 
1289     /// Return the pair of the lowered `generics` as `hir::Generics` and the evaluation of `f` with
1290     /// the carried impl trait definitions and bounds.
1291     #[instrument(level = "debug", skip(self, f))]
lower_generics<T>( &mut self, generics: &Generics, constness: Const, parent_node_id: NodeId, itctx: &ImplTraitContext, f: impl FnOnce(&mut Self) -> T, ) -> (&'hir hir::Generics<'hir>, T)1292     fn lower_generics<T>(
1293         &mut self,
1294         generics: &Generics,
1295         constness: Const,
1296         parent_node_id: NodeId,
1297         itctx: &ImplTraitContext,
1298         f: impl FnOnce(&mut Self) -> T,
1299     ) -> (&'hir hir::Generics<'hir>, T) {
1300         debug_assert!(self.impl_trait_defs.is_empty());
1301         debug_assert!(self.impl_trait_bounds.is_empty());
1302 
1303         // Error if `?Trait` bounds in where clauses don't refer directly to type parameters.
1304         // Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
1305         // these into hir when we lower thee where clauses), but this makes it quite difficult to
1306         // keep track of the Span info. Now, `add_implicitly_sized` in `AstConv` checks both param bounds and
1307         // where clauses for `?Sized`.
1308         for pred in &generics.where_clause.predicates {
1309             let WherePredicate::BoundPredicate(bound_pred) = pred else {
1310                 continue;
1311             };
1312             let compute_is_param = || {
1313                 // Check if the where clause type is a plain type parameter.
1314                 match self
1315                     .resolver
1316                     .get_partial_res(bound_pred.bounded_ty.id)
1317                     .and_then(|r| r.full_res())
1318                 {
1319                     Some(Res::Def(DefKind::TyParam, def_id))
1320                         if bound_pred.bound_generic_params.is_empty() =>
1321                     {
1322                         generics
1323                             .params
1324                             .iter()
1325                             .any(|p| def_id == self.local_def_id(p.id).to_def_id())
1326                     }
1327                     // Either the `bounded_ty` is not a plain type parameter, or
1328                     // it's not found in the generic type parameters list.
1329                     _ => false,
1330                 }
1331             };
1332             // We only need to compute this once per `WherePredicate`, but don't
1333             // need to compute this at all unless there is a Maybe bound.
1334             let mut is_param: Option<bool> = None;
1335             for bound in &bound_pred.bounds {
1336                 if !matches!(*bound, GenericBound::Trait(_, TraitBoundModifier::Maybe)) {
1337                     continue;
1338                 }
1339                 let is_param = *is_param.get_or_insert_with(compute_is_param);
1340                 if !is_param {
1341                     self.tcx.sess.emit_err(MisplacedRelaxTraitBound { span: bound.span() });
1342                 }
1343             }
1344         }
1345 
1346         let mut predicates: SmallVec<[hir::WherePredicate<'hir>; 4]> = SmallVec::new();
1347         predicates.extend(generics.params.iter().filter_map(|param| {
1348             self.lower_generic_bound_predicate(
1349                 param.ident,
1350                 param.id,
1351                 &param.kind,
1352                 &param.bounds,
1353                 param.colon_span,
1354                 generics.span,
1355                 itctx,
1356                 PredicateOrigin::GenericParam,
1357             )
1358         }));
1359         predicates.extend(
1360             generics
1361                 .where_clause
1362                 .predicates
1363                 .iter()
1364                 .map(|predicate| self.lower_where_predicate(predicate)),
1365         );
1366 
1367         let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = self
1368             .lower_generic_params_mut(&generics.params, hir::GenericParamSource::Generics)
1369             .collect();
1370 
1371         // Introduce extra lifetimes if late resolution tells us to.
1372         let extra_lifetimes = self.resolver.take_extra_lifetime_params(parent_node_id);
1373         params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
1374             self.lifetime_res_to_generic_param(
1375                 ident,
1376                 node_id,
1377                 res,
1378                 hir::GenericParamSource::Generics,
1379             )
1380         }));
1381 
1382         let has_where_clause_predicates = !generics.where_clause.predicates.is_empty();
1383         let where_clause_span = self.lower_span(generics.where_clause.span);
1384         let span = self.lower_span(generics.span);
1385         let res = f(self);
1386 
1387         let impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
1388         params.extend(impl_trait_defs.into_iter());
1389 
1390         let impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);
1391         predicates.extend(impl_trait_bounds.into_iter());
1392 
1393         // Desugar `~const` bound in generics into an additional `const host: bool` param
1394         // if the effects feature is enabled.
1395         if let Const::Yes(span) = constness && self.tcx.features().effects
1396             // Do not add host param if it already has it (manually specified)
1397             && !params.iter().any(|x| {
1398                 self.attrs.get(&x.hir_id.local_id).map_or(false, |attrs| {
1399                     attrs.iter().any(|x| x.has_name(sym::rustc_host))
1400                 })
1401             })
1402         {
1403             let param_node_id = self.next_node_id();
1404             let const_node_id = self.next_node_id();
1405             let def_id = self.create_def(self.local_def_id(parent_node_id), param_node_id, DefPathData::TypeNs(sym::host), span);
1406             let anon_const: LocalDefId = self.create_def(def_id, const_node_id, DefPathData::AnonConst, span);
1407 
1408             let hir_id = self.next_id();
1409             let const_id = self.next_id();
1410             let const_expr_id = self.next_id();
1411             let bool_id = self.next_id();
1412 
1413             self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
1414             self.children.push((anon_const, hir::MaybeOwner::NonOwner(const_id)));
1415 
1416             let attr_id = self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id();
1417 
1418             let attrs = self.arena.alloc_from_iter([
1419                 Attribute {
1420                     kind: AttrKind::Normal(P(NormalAttr::from_ident(Ident::new(sym::rustc_host, span)))),
1421                     span,
1422                     id: attr_id,
1423                     style: AttrStyle::Outer,
1424                 },
1425             ]);
1426             self.attrs.insert(hir_id.local_id, attrs);
1427 
1428             let const_body = self.lower_body(|this| {
1429                 (
1430                     &[],
1431                     hir::Expr {
1432                         hir_id: const_expr_id,
1433                         kind: hir::ExprKind::Lit(
1434                             this.arena.alloc(hir::Lit { node: LitKind::Bool(true), span }),
1435                         ),
1436                         span,
1437                     },
1438                 )
1439             });
1440 
1441             let param = hir::GenericParam {
1442                 def_id,
1443                 hir_id,
1444                 name: hir::ParamName::Plain(Ident { name: sym::host, span }),
1445                 span,
1446                 kind: hir::GenericParamKind::Const {
1447                     ty: self.arena.alloc(self.ty(
1448                         span,
1449                         hir::TyKind::Path(hir::QPath::Resolved(
1450                             None,
1451                             self.arena.alloc(hir::Path {
1452                                 res: Res::PrimTy(hir::PrimTy::Bool),
1453                                 span,
1454                                 segments: self.arena.alloc_from_iter([hir::PathSegment {
1455                                     ident: Ident { name: sym::bool, span },
1456                                     hir_id: bool_id,
1457                                     res: Res::PrimTy(hir::PrimTy::Bool),
1458                                     args: None,
1459                                     infer_args: false,
1460                                 }]),
1461                             }),
1462                         )),
1463                     )),
1464                     default: Some(hir::AnonConst { def_id: anon_const, hir_id: const_id, body: const_body }),
1465                 },
1466                 colon_span: None,
1467                 pure_wrt_drop: false,
1468                 source: hir::GenericParamSource::Generics,
1469             };
1470 
1471             params.push(param);
1472         }
1473 
1474         let lowered_generics = self.arena.alloc(hir::Generics {
1475             params: self.arena.alloc_from_iter(params),
1476             predicates: self.arena.alloc_from_iter(predicates),
1477             has_where_clause_predicates,
1478             where_clause_span,
1479             span,
1480         });
1481 
1482         (lowered_generics, res)
1483     }
1484 
lower_generic_bound_predicate( &mut self, ident: Ident, id: NodeId, kind: &GenericParamKind, bounds: &[GenericBound], colon_span: Option<Span>, parent_span: Span, itctx: &ImplTraitContext, origin: PredicateOrigin, ) -> Option<hir::WherePredicate<'hir>>1485     pub(super) fn lower_generic_bound_predicate(
1486         &mut self,
1487         ident: Ident,
1488         id: NodeId,
1489         kind: &GenericParamKind,
1490         bounds: &[GenericBound],
1491         colon_span: Option<Span>,
1492         parent_span: Span,
1493         itctx: &ImplTraitContext,
1494         origin: PredicateOrigin,
1495     ) -> Option<hir::WherePredicate<'hir>> {
1496         // Do not create a clause if we do not have anything inside it.
1497         if bounds.is_empty() {
1498             return None;
1499         }
1500 
1501         let bounds = self.lower_param_bounds(bounds, itctx);
1502 
1503         let ident = self.lower_ident(ident);
1504         let param_span = ident.span;
1505 
1506         // Reconstruct the span of the entire predicate from the individual generic bounds.
1507         let span_start = colon_span.unwrap_or_else(|| param_span.shrink_to_hi());
1508         let span = bounds.iter().fold(span_start, |span_accum, bound| {
1509             match bound.span().find_ancestor_inside(parent_span) {
1510                 Some(bound_span) => span_accum.to(bound_span),
1511                 None => span_accum,
1512             }
1513         });
1514         let span = self.lower_span(span);
1515 
1516         match kind {
1517             GenericParamKind::Const { .. } => None,
1518             GenericParamKind::Type { .. } => {
1519                 let def_id = self.local_def_id(id).to_def_id();
1520                 let hir_id = self.next_id();
1521                 let res = Res::Def(DefKind::TyParam, def_id);
1522                 let ty_path = self.arena.alloc(hir::Path {
1523                     span: param_span,
1524                     res,
1525                     segments: self
1526                         .arena
1527                         .alloc_from_iter([hir::PathSegment::new(ident, hir_id, res)]),
1528                 });
1529                 let ty_id = self.next_id();
1530                 let bounded_ty =
1531                     self.ty_path(ty_id, param_span, hir::QPath::Resolved(None, ty_path));
1532                 Some(hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1533                     hir_id: self.next_id(),
1534                     bounded_ty: self.arena.alloc(bounded_ty),
1535                     bounds,
1536                     span,
1537                     bound_generic_params: &[],
1538                     origin,
1539                 }))
1540             }
1541             GenericParamKind::Lifetime => {
1542                 let ident = self.lower_ident(ident);
1543                 let lt_id = self.next_node_id();
1544                 let lifetime = self.new_named_lifetime(id, lt_id, ident);
1545                 Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
1546                     lifetime,
1547                     span,
1548                     bounds,
1549                     in_where_clause: false,
1550                 }))
1551             }
1552         }
1553     }
1554 
lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir>1555     fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {
1556         match pred {
1557             WherePredicate::BoundPredicate(WhereBoundPredicate {
1558                 bound_generic_params,
1559                 bounded_ty,
1560                 bounds,
1561                 span,
1562             }) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1563                 hir_id: self.next_id(),
1564                 bound_generic_params: self
1565                     .lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder),
1566                 bounded_ty: self
1567                     .lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1568                 bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
1569                     self.lower_param_bound(
1570                         bound,
1571                         &ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1572                     )
1573                 })),
1574                 span: self.lower_span(*span),
1575                 origin: PredicateOrigin::WhereClause,
1576             }),
1577             WherePredicate::RegionPredicate(WhereRegionPredicate { lifetime, bounds, span }) => {
1578                 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
1579                     span: self.lower_span(*span),
1580                     lifetime: self.lower_lifetime(lifetime),
1581                     bounds: self.lower_param_bounds(
1582                         bounds,
1583                         &ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1584                     ),
1585                     in_where_clause: true,
1586                 })
1587             }
1588             WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span }) => {
1589                 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
1590                     lhs_ty: self
1591                         .lower_ty(lhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1592                     rhs_ty: self
1593                         .lower_ty(rhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1594                     span: self.lower_span(*span),
1595                 })
1596             }
1597         }
1598     }
1599 }
1600