rustc_ast_lowering/
item.rs

1use rustc_abi::ExternAbi;
2use rustc_ast::ptr::P;
3use rustc_ast::visit::AssocCtxt;
4use rustc_ast::*;
5use rustc_attr_data_structures::{AttributeKind, find_attr};
6use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
7use rustc_hir::def::{DefKind, PerNS, Res};
8use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
9use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin};
10use rustc_index::{IndexSlice, IndexVec};
11use rustc_middle::span_bug;
12use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
13use rustc_span::edit_distance::find_best_match_for_name;
14use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
15use smallvec::{SmallVec, smallvec};
16use thin_vec::ThinVec;
17use tracing::instrument;
18
19use super::errors::{InvalidAbi, InvalidAbiSuggestion, TupleStructWithDefault, UnionWithDefault};
20use super::stability::{enabled_names, gate_unstable_abi};
21use super::{
22    AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
23    RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt,
24};
25
26pub(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>>,
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.
36fn add_ty_alias_where_clause(
37    generics: &mut ast::Generics,
38    mut where_clauses: TyAliasWhereClauses,
39    prefer_first: bool,
40) {
41    if !prefer_first {
42        (where_clauses.before, where_clauses.after) = (where_clauses.after, where_clauses.before);
43    }
44    let where_clause =
45        if where_clauses.before.has_where_token || !where_clauses.after.has_where_token {
46            where_clauses.before
47        } else {
48            where_clauses.after
49        };
50    generics.where_clause.has_where_token = where_clause.has_where_token;
51    generics.where_clause.span = where_clause.span;
52}
53
54impl<'a, 'hir> ItemLowerer<'a, 'hir> {
55    fn with_lctx(
56        &mut self,
57        owner: NodeId,
58        f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
59    ) {
60        let mut lctx = LoweringContext::new(self.tcx, self.resolver);
61        lctx.with_hir_id_owner(owner, |lctx| f(lctx));
62
63        for (def_id, info) in lctx.children {
64            let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
65            assert!(
66                matches!(owner, hir::MaybeOwner::Phantom),
67                "duplicate copy of {def_id:?} in lctx.children"
68            );
69            *owner = info;
70        }
71    }
72
73    pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
74        let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
75        if let hir::MaybeOwner::Phantom = owner {
76            let node = self.ast_index[def_id];
77            match node {
78                AstOwner::NonOwner => {}
79                AstOwner::Crate(c) => {
80                    assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
81                    self.with_lctx(CRATE_NODE_ID, |lctx| {
82                        let module = lctx.lower_mod(&c.items, &c.spans);
83                        // FIXME(jdonszelman): is dummy span ever a problem here?
84                        lctx.lower_attrs(hir::CRATE_HIR_ID, &c.attrs, DUMMY_SP);
85                        hir::OwnerNode::Crate(module)
86                    })
87                }
88                AstOwner::Item(item) => {
89                    self.with_lctx(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)))
90                }
91                AstOwner::AssocItem(item, ctxt) => {
92                    self.with_lctx(item.id, |lctx| lctx.lower_assoc_item(item, ctxt))
93                }
94                AstOwner::ForeignItem(item) => self.with_lctx(item.id, |lctx| {
95                    hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item))
96                }),
97            }
98        }
99    }
100}
101
102impl<'hir> LoweringContext<'_, 'hir> {
103    pub(super) fn lower_mod(
104        &mut self,
105        items: &[P<Item>],
106        spans: &ModSpans,
107    ) -> &'hir hir::Mod<'hir> {
108        self.arena.alloc(hir::Mod {
109            spans: hir::ModSpans {
110                inner_span: self.lower_span(spans.inner_span),
111                inject_use_span: self.lower_span(spans.inject_use_span),
112            },
113            item_ids: self.arena.alloc_from_iter(items.iter().flat_map(|x| self.lower_item_ref(x))),
114        })
115    }
116
117    pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
118        let mut node_ids = smallvec![hir::ItemId { owner_id: self.owner_id(i.id) }];
119        if let ItemKind::Use(use_tree) = &i.kind {
120            self.lower_item_id_use_tree(use_tree, &mut node_ids);
121        }
122        node_ids
123    }
124
125    fn lower_item_id_use_tree(&mut self, tree: &UseTree, vec: &mut SmallVec<[hir::ItemId; 1]>) {
126        match &tree.kind {
127            UseTreeKind::Nested { items, .. } => {
128                for &(ref nested, id) in items {
129                    vec.push(hir::ItemId { owner_id: self.owner_id(id) });
130                    self.lower_item_id_use_tree(nested, vec);
131                }
132            }
133            UseTreeKind::Simple(..) | UseTreeKind::Glob => {}
134        }
135    }
136
137    fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
138        let vis_span = self.lower_span(i.vis.span);
139        let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
140        let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
141        let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
142        let item = hir::Item {
143            owner_id: hir_id.expect_owner(),
144            kind,
145            vis_span,
146            span: self.lower_span(i.span),
147            has_delayed_lints: !self.delayed_lints.is_empty(),
148        };
149        self.arena.alloc(item)
150    }
151
152    fn lower_item_kind(
153        &mut self,
154        span: Span,
155        id: NodeId,
156        hir_id: hir::HirId,
157        attrs: &'hir [hir::Attribute],
158        vis_span: Span,
159        i: &ItemKind,
160    ) -> hir::ItemKind<'hir> {
161        match i {
162            ItemKind::ExternCrate(orig_name, ident) => {
163                let ident = self.lower_ident(*ident);
164                hir::ItemKind::ExternCrate(*orig_name, ident)
165            }
166            ItemKind::Use(use_tree) => {
167                // Start with an empty prefix.
168                let prefix = Path { segments: ThinVec::new(), span: use_tree.span, tokens: None };
169
170                self.lower_use_tree(use_tree, &prefix, id, vis_span, attrs)
171            }
172            ItemKind::Static(box ast::StaticItem {
173                ident,
174                ty: t,
175                safety: _,
176                mutability: m,
177                expr: e,
178                define_opaque,
179            }) => {
180                let ident = self.lower_ident(*ident);
181                let (ty, body_id) =
182                    self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
183                self.lower_define_opaque(hir_id, define_opaque);
184                hir::ItemKind::Static(*m, ident, ty, body_id)
185            }
186            ItemKind::Const(box ast::ConstItem {
187                ident,
188                generics,
189                ty,
190                expr,
191                define_opaque,
192                ..
193            }) => {
194                let ident = self.lower_ident(*ident);
195                let (generics, (ty, body_id)) = self.lower_generics(
196                    generics,
197                    id,
198                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
199                    |this| {
200                        this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy)
201                    },
202                );
203                self.lower_define_opaque(hir_id, &define_opaque);
204                hir::ItemKind::Const(ident, generics, ty, body_id)
205            }
206            ItemKind::Fn(box Fn {
207                sig: FnSig { decl, header, span: fn_sig_span },
208                ident,
209                generics,
210                body,
211                contract,
212                define_opaque,
213                ..
214            }) => {
215                self.with_new_scopes(*fn_sig_span, |this| {
216                    // Note: we don't need to change the return type from `T` to
217                    // `impl Future<Output = T>` here because lower_body
218                    // only cares about the input argument patterns in the function
219                    // declaration (decl), not the return types.
220                    let coroutine_kind = header.coroutine_kind;
221                    let body_id = this.lower_maybe_coroutine_body(
222                        *fn_sig_span,
223                        span,
224                        hir_id,
225                        decl,
226                        coroutine_kind,
227                        body.as_deref(),
228                        attrs,
229                        contract.as_deref(),
230                    );
231
232                    let itctx = ImplTraitContext::Universal;
233                    let (generics, decl) = this.lower_generics(generics, id, itctx, |this| {
234                        this.lower_fn_decl(decl, id, *fn_sig_span, FnDeclKind::Fn, coroutine_kind)
235                    });
236                    let sig = hir::FnSig {
237                        decl,
238                        header: this.lower_fn_header(*header, hir::Safety::Safe, attrs),
239                        span: this.lower_span(*fn_sig_span),
240                    };
241                    this.lower_define_opaque(hir_id, define_opaque);
242                    let ident = this.lower_ident(*ident);
243                    hir::ItemKind::Fn {
244                        ident,
245                        sig,
246                        generics,
247                        body: body_id,
248                        has_body: body.is_some(),
249                    }
250                })
251            }
252            ItemKind::Mod(_, ident, mod_kind) => {
253                let ident = self.lower_ident(*ident);
254                match mod_kind {
255                    ModKind::Loaded(items, _, spans, _) => {
256                        hir::ItemKind::Mod(ident, self.lower_mod(items, spans))
257                    }
258                    ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
259                }
260            }
261            ItemKind::ForeignMod(fm) => hir::ItemKind::ForeignMod {
262                abi: fm.abi.map_or(ExternAbi::FALLBACK, |abi| self.lower_abi(abi)),
263                items: self
264                    .arena
265                    .alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
266            },
267            ItemKind::GlobalAsm(asm) => {
268                let asm = self.lower_inline_asm(span, asm);
269                let fake_body =
270                    self.lower_body(|this| (&[], this.expr(span, hir::ExprKind::InlineAsm(asm))));
271                hir::ItemKind::GlobalAsm { asm, fake_body }
272            }
273            ItemKind::TyAlias(box TyAlias { ident, generics, where_clauses, ty, .. }) => {
274                // We lower
275                //
276                // type Foo = impl Trait
277                //
278                // to
279                //
280                // type Foo = Foo1
281                // opaque type Foo1: Trait
282                let ident = self.lower_ident(*ident);
283                let mut generics = generics.clone();
284                add_ty_alias_where_clause(&mut generics, *where_clauses, true);
285                let (generics, ty) = self.lower_generics(
286                    &generics,
287                    id,
288                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
289                    |this| match ty {
290                        None => {
291                            let guar = this.dcx().span_delayed_bug(
292                                span,
293                                "expected to lower type alias type, but it was missing",
294                            );
295                            this.arena.alloc(this.ty(span, hir::TyKind::Err(guar)))
296                        }
297                        Some(ty) => this.lower_ty(
298                            ty,
299                            ImplTraitContext::OpaqueTy {
300                                origin: hir::OpaqueTyOrigin::TyAlias {
301                                    parent: this.local_def_id(id),
302                                    in_assoc_ty: false,
303                                },
304                            },
305                        ),
306                    },
307                );
308                hir::ItemKind::TyAlias(ident, generics, ty)
309            }
310            ItemKind::Enum(ident, generics, enum_definition) => {
311                let ident = self.lower_ident(*ident);
312                let (generics, variants) = self.lower_generics(
313                    generics,
314                    id,
315                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
316                    |this| {
317                        this.arena.alloc_from_iter(
318                            enum_definition.variants.iter().map(|x| this.lower_variant(i, x)),
319                        )
320                    },
321                );
322                hir::ItemKind::Enum(ident, generics, hir::EnumDef { variants })
323            }
324            ItemKind::Struct(ident, generics, struct_def) => {
325                let ident = self.lower_ident(*ident);
326                let (generics, struct_def) = self.lower_generics(
327                    generics,
328                    id,
329                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
330                    |this| this.lower_variant_data(hir_id, i, struct_def),
331                );
332                hir::ItemKind::Struct(ident, generics, struct_def)
333            }
334            ItemKind::Union(ident, generics, vdata) => {
335                let ident = self.lower_ident(*ident);
336                let (generics, vdata) = self.lower_generics(
337                    generics,
338                    id,
339                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
340                    |this| this.lower_variant_data(hir_id, i, vdata),
341                );
342                hir::ItemKind::Union(ident, generics, vdata)
343            }
344            ItemKind::Impl(box Impl {
345                safety,
346                polarity,
347                defaultness,
348                constness,
349                generics: ast_generics,
350                of_trait: trait_ref,
351                self_ty: ty,
352                items: impl_items,
353            }) => {
354                // Lower the "impl header" first. This ordering is important
355                // for in-band lifetimes! Consider `'a` here:
356                //
357                //     impl Foo<'a> for u32 {
358                //         fn method(&'a self) { .. }
359                //     }
360                //
361                // Because we start by lowering the `Foo<'a> for u32`
362                // part, we will add `'a` to the list of generics on
363                // the impl. When we then encounter it later in the
364                // method, it will not be considered an in-band
365                // lifetime to be added, but rather a reference to a
366                // parent lifetime.
367                let itctx = ImplTraitContext::Universal;
368                let (generics, (trait_ref, lowered_ty)) =
369                    self.lower_generics(ast_generics, id, itctx, |this| {
370                        let modifiers = TraitBoundModifiers {
371                            constness: BoundConstness::Never,
372                            asyncness: BoundAsyncness::Normal,
373                            // we don't use this in bound lowering
374                            polarity: BoundPolarity::Positive,
375                        };
376
377                        let trait_ref = trait_ref.as_ref().map(|trait_ref| {
378                            this.lower_trait_ref(
379                                modifiers,
380                                trait_ref,
381                                ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
382                            )
383                        });
384
385                        let lowered_ty = this.lower_ty(
386                            ty,
387                            ImplTraitContext::Disallowed(ImplTraitPosition::ImplSelf),
388                        );
389
390                        (trait_ref, lowered_ty)
391                    });
392
393                let new_impl_items = self
394                    .arena
395                    .alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
396
397                // `defaultness.has_value()` is never called for an `impl`, always `true` in order
398                // to not cause an assertion failure inside the `lower_defaultness` function.
399                let has_val = true;
400                let (defaultness, defaultness_span) = self.lower_defaultness(*defaultness, has_val);
401                let polarity = match polarity {
402                    ImplPolarity::Positive => ImplPolarity::Positive,
403                    ImplPolarity::Negative(s) => ImplPolarity::Negative(self.lower_span(*s)),
404                };
405                hir::ItemKind::Impl(self.arena.alloc(hir::Impl {
406                    constness: self.lower_constness(*constness),
407                    safety: self.lower_safety(*safety, hir::Safety::Safe),
408                    polarity,
409                    defaultness,
410                    defaultness_span,
411                    generics,
412                    of_trait: trait_ref,
413                    self_ty: lowered_ty,
414                    items: new_impl_items,
415                }))
416            }
417            ItemKind::Trait(box Trait {
418                constness,
419                is_auto,
420                safety,
421                ident,
422                generics,
423                bounds,
424                items,
425            }) => {
426                let constness = self.lower_constness(*constness);
427                let ident = self.lower_ident(*ident);
428                let (generics, (safety, items, bounds)) = self.lower_generics(
429                    generics,
430                    id,
431                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
432                    |this| {
433                        let bounds = this.lower_param_bounds(
434                            bounds,
435                            RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::SuperTrait),
436                            ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
437                        );
438                        let items = this.arena.alloc_from_iter(
439                            items.iter().map(|item| this.lower_trait_item_ref(item)),
440                        );
441                        let safety = this.lower_safety(*safety, hir::Safety::Safe);
442                        (safety, items, bounds)
443                    },
444                );
445                hir::ItemKind::Trait(constness, *is_auto, safety, ident, generics, bounds, items)
446            }
447            ItemKind::TraitAlias(ident, generics, bounds) => {
448                let ident = self.lower_ident(*ident);
449                let (generics, bounds) = self.lower_generics(
450                    generics,
451                    id,
452                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
453                    |this| {
454                        this.lower_param_bounds(
455                            bounds,
456                            RelaxedBoundPolicy::Allowed,
457                            ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
458                        )
459                    },
460                );
461                hir::ItemKind::TraitAlias(ident, generics, bounds)
462            }
463            ItemKind::MacroDef(ident, MacroDef { body, macro_rules }) => {
464                let ident = self.lower_ident(*ident);
465                let body = P(self.lower_delim_args(body));
466                let def_id = self.local_def_id(id);
467                let def_kind = self.tcx.def_kind(def_id);
468                let DefKind::Macro(macro_kind) = def_kind else {
469                    unreachable!(
470                        "expected DefKind::Macro for macro item, found {}",
471                        def_kind.descr(def_id.to_def_id())
472                    );
473                };
474                let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
475                hir::ItemKind::Macro(ident, macro_def, macro_kind)
476            }
477            ItemKind::Delegation(box delegation) => {
478                let delegation_results = self.lower_delegation(delegation, id, false);
479                hir::ItemKind::Fn {
480                    sig: delegation_results.sig,
481                    ident: delegation_results.ident,
482                    generics: delegation_results.generics,
483                    body: delegation_results.body_id,
484                    has_body: true,
485                }
486            }
487            ItemKind::MacCall(..) | ItemKind::DelegationMac(..) => {
488                panic!("macros should have been expanded by now")
489            }
490        }
491    }
492
493    fn lower_const_item(
494        &mut self,
495        ty: &Ty,
496        span: Span,
497        body: Option<&Expr>,
498        impl_trait_position: ImplTraitPosition,
499    ) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
500        let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(impl_trait_position));
501        (ty, self.lower_const_body(span, body))
502    }
503
504    #[instrument(level = "debug", skip(self))]
505    fn lower_use_tree(
506        &mut self,
507        tree: &UseTree,
508        prefix: &Path,
509        id: NodeId,
510        vis_span: Span,
511        attrs: &'hir [hir::Attribute],
512    ) -> hir::ItemKind<'hir> {
513        let path = &tree.prefix;
514        let segments = prefix.segments.iter().chain(path.segments.iter()).cloned().collect();
515
516        match tree.kind {
517            UseTreeKind::Simple(rename) => {
518                let mut ident = tree.ident();
519
520                // First, apply the prefix to the path.
521                let mut path = Path { segments, span: path.span, tokens: None };
522
523                // Correctly resolve `self` imports.
524                if path.segments.len() > 1
525                    && path.segments.last().unwrap().ident.name == kw::SelfLower
526                {
527                    let _ = path.segments.pop();
528                    if rename.is_none() {
529                        ident = path.segments.last().unwrap().ident;
530                    }
531                }
532
533                let res = self.lower_import_res(id, path.span);
534                let path = self.lower_use_path(res, &path, ParamMode::Explicit);
535                let ident = self.lower_ident(ident);
536                hir::ItemKind::Use(path, hir::UseKind::Single(ident))
537            }
538            UseTreeKind::Glob => {
539                let res = self.expect_full_res(id);
540                let res = self.lower_res(res);
541                // Put the result in the appropriate namespace.
542                let res = match res {
543                    Res::Def(DefKind::Mod | DefKind::Trait, _) => {
544                        PerNS { type_ns: Some(res), value_ns: None, macro_ns: None }
545                    }
546                    Res::Def(DefKind::Enum, _) => {
547                        PerNS { type_ns: None, value_ns: Some(res), macro_ns: None }
548                    }
549                    Res::Err => {
550                        // Propagate the error to all namespaces, just to be sure.
551                        let err = Some(Res::Err);
552                        PerNS { type_ns: err, value_ns: err, macro_ns: err }
553                    }
554                    _ => span_bug!(path.span, "bad glob res {:?}", res),
555                };
556                let path = Path { segments, span: path.span, tokens: None };
557                let path = self.lower_use_path(res, &path, ParamMode::Explicit);
558                hir::ItemKind::Use(path, hir::UseKind::Glob)
559            }
560            UseTreeKind::Nested { items: ref trees, .. } => {
561                // Nested imports are desugared into simple imports.
562                // So, if we start with
563                //
564                // ```
565                // pub(x) use foo::{a, b};
566                // ```
567                //
568                // we will create three items:
569                //
570                // ```
571                // pub(x) use foo::a;
572                // pub(x) use foo::b;
573                // pub(x) use foo::{}; // <-- this is called the `ListStem`
574                // ```
575                //
576                // The first two are produced by recursively invoking
577                // `lower_use_tree` (and indeed there may be things
578                // like `use foo::{a::{b, c}}` and so forth). They
579                // wind up being directly added to
580                // `self.items`. However, the structure of this
581                // function also requires us to return one item, and
582                // for that we return the `{}` import (called the
583                // `ListStem`).
584
585                let span = prefix.span.to(path.span);
586                let prefix = Path { segments, span, tokens: None };
587
588                // Add all the nested `PathListItem`s to the HIR.
589                for &(ref use_tree, id) in trees {
590                    let owner_id = self.owner_id(id);
591
592                    // Each `use` import is an item and thus are owners of the
593                    // names in the path. Up to this point the nested import is
594                    // the current owner, since we want each desugared import to
595                    // own its own names, we have to adjust the owner before
596                    // lowering the rest of the import.
597                    self.with_hir_id_owner(id, |this| {
598                        // `prefix` is lowered multiple times, but in different HIR owners.
599                        // So each segment gets renewed `HirId` with the same
600                        // `ItemLocalId` and the new owner. (See `lower_node_id`)
601                        let kind = this.lower_use_tree(use_tree, &prefix, id, vis_span, attrs);
602                        if !attrs.is_empty() {
603                            this.attrs.insert(hir::ItemLocalId::ZERO, attrs);
604                        }
605
606                        let item = hir::Item {
607                            owner_id,
608                            kind,
609                            vis_span,
610                            span: this.lower_span(use_tree.span),
611                            has_delayed_lints: !this.delayed_lints.is_empty(),
612                        };
613                        hir::OwnerNode::Item(this.arena.alloc(item))
614                    });
615                }
616
617                // Condition should match `build_reduced_graph_for_use_tree`.
618                let path = if trees.is_empty()
619                    && !(prefix.segments.is_empty()
620                        || prefix.segments.len() == 1
621                            && prefix.segments[0].ident.name == kw::PathRoot)
622                {
623                    // For empty lists we need to lower the prefix so it is checked for things
624                    // like stability later.
625                    let res = self.lower_import_res(id, span);
626                    self.lower_use_path(res, &prefix, ParamMode::Explicit)
627                } else {
628                    // For non-empty lists we can just drop all the data, the prefix is already
629                    // present in HIR as a part of nested imports.
630                    self.arena.alloc(hir::UsePath { res: PerNS::default(), segments: &[], span })
631                };
632                hir::ItemKind::Use(path, hir::UseKind::ListStem)
633            }
634        }
635    }
636
637    fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) -> hir::OwnerNode<'hir> {
638        // Evaluate with the lifetimes in `params` in-scope.
639        // This is used to track which lifetimes have already been defined,
640        // and which need to be replicated when lowering an async fn.
641        match ctxt {
642            AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item)),
643            AssocCtxt::Impl { of_trait } => {
644                hir::OwnerNode::ImplItem(self.lower_impl_item(item, of_trait))
645            }
646        }
647    }
648
649    fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
650        let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
651        let owner_id = hir_id.expect_owner();
652        let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
653        let (ident, kind) = match &i.kind {
654            ForeignItemKind::Fn(box Fn { sig, ident, generics, define_opaque, .. }) => {
655                let fdec = &sig.decl;
656                let itctx = ImplTraitContext::Universal;
657                let (generics, (decl, fn_args)) =
658                    self.lower_generics(generics, i.id, itctx, |this| {
659                        (
660                            // Disallow `impl Trait` in foreign items.
661                            this.lower_fn_decl(fdec, i.id, sig.span, FnDeclKind::ExternFn, None),
662                            this.lower_fn_params_to_idents(fdec),
663                        )
664                    });
665
666                // Unmarked safety in unsafe block defaults to unsafe.
667                let header = self.lower_fn_header(sig.header, hir::Safety::Unsafe, attrs);
668
669                if define_opaque.is_some() {
670                    self.dcx().span_err(i.span, "foreign functions cannot define opaque types");
671                }
672
673                (
674                    ident,
675                    hir::ForeignItemKind::Fn(
676                        hir::FnSig { header, decl, span: self.lower_span(sig.span) },
677                        fn_args,
678                        generics,
679                    ),
680                )
681            }
682            ForeignItemKind::Static(box StaticItem {
683                ident,
684                ty,
685                mutability,
686                expr: _,
687                safety,
688                define_opaque,
689            }) => {
690                let ty =
691                    self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
692                let safety = self.lower_safety(*safety, hir::Safety::Unsafe);
693                if define_opaque.is_some() {
694                    self.dcx().span_err(i.span, "foreign statics cannot define opaque types");
695                }
696                (ident, hir::ForeignItemKind::Static(ty, *mutability, safety))
697            }
698            ForeignItemKind::TyAlias(box TyAlias { ident, .. }) => {
699                (ident, hir::ForeignItemKind::Type)
700            }
701            ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),
702        };
703
704        let item = hir::ForeignItem {
705            owner_id,
706            ident: self.lower_ident(*ident),
707            kind,
708            vis_span: self.lower_span(i.vis.span),
709            span: self.lower_span(i.span),
710            has_delayed_lints: !self.delayed_lints.is_empty(),
711        };
712        self.arena.alloc(item)
713    }
714
715    fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemId {
716        hir::ForeignItemId { owner_id: self.owner_id(i.id) }
717    }
718
719    fn lower_variant(&mut self, item_kind: &ItemKind, v: &Variant) -> hir::Variant<'hir> {
720        let hir_id = self.lower_node_id(v.id);
721        self.lower_attrs(hir_id, &v.attrs, v.span);
722        hir::Variant {
723            hir_id,
724            def_id: self.local_def_id(v.id),
725            data: self.lower_variant_data(hir_id, item_kind, &v.data),
726            disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const_to_anon_const(e)),
727            ident: self.lower_ident(v.ident),
728            span: self.lower_span(v.span),
729        }
730    }
731
732    fn lower_variant_data(
733        &mut self,
734        parent_id: hir::HirId,
735        item_kind: &ItemKind,
736        vdata: &VariantData,
737    ) -> hir::VariantData<'hir> {
738        match vdata {
739            VariantData::Struct { fields, recovered } => {
740                let fields = self
741                    .arena
742                    .alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_field_def(f)));
743
744                if let ItemKind::Union(..) = item_kind {
745                    for field in &fields[..] {
746                        if let Some(default) = field.default {
747                            // Unions cannot derive `Default`, and it's not clear how to use default
748                            // field values of unions if that was supported. Therefore, blanket reject
749                            // trying to use field values with unions.
750                            if self.tcx.features().default_field_values() {
751                                self.dcx().emit_err(UnionWithDefault { span: default.span });
752                            } else {
753                                let _ = self.dcx().span_delayed_bug(
754                                default.span,
755                                "expected union default field values feature gate error but none \
756                                was produced",
757                            );
758                            }
759                        }
760                    }
761                }
762
763                hir::VariantData::Struct { fields, recovered: *recovered }
764            }
765            VariantData::Tuple(fields, id) => {
766                let ctor_id = self.lower_node_id(*id);
767                self.alias_attrs(ctor_id, parent_id);
768                let fields = self
769                    .arena
770                    .alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_field_def(f)));
771                for field in &fields[..] {
772                    if let Some(default) = field.default {
773                        // Default values in tuple struct and tuple variants are not allowed by the
774                        // RFC due to concerns about the syntax, both in the item definition and the
775                        // expression. We could in the future allow `struct S(i32 = 0);` and force
776                        // users to construct the value with `let _ = S { .. };`.
777                        if self.tcx.features().default_field_values() {
778                            self.dcx().emit_err(TupleStructWithDefault { span: default.span });
779                        } else {
780                            let _ = self.dcx().span_delayed_bug(
781                                default.span,
782                                "expected `default values on `struct` fields aren't supported` \
783                                 feature-gate error but none was produced",
784                            );
785                        }
786                    }
787                }
788                hir::VariantData::Tuple(fields, ctor_id, self.local_def_id(*id))
789            }
790            VariantData::Unit(id) => {
791                let ctor_id = self.lower_node_id(*id);
792                self.alias_attrs(ctor_id, parent_id);
793                hir::VariantData::Unit(ctor_id, self.local_def_id(*id))
794            }
795        }
796    }
797
798    pub(super) fn lower_field_def(
799        &mut self,
800        (index, f): (usize, &FieldDef),
801    ) -> hir::FieldDef<'hir> {
802        let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
803        let hir_id = self.lower_node_id(f.id);
804        self.lower_attrs(hir_id, &f.attrs, f.span);
805        hir::FieldDef {
806            span: self.lower_span(f.span),
807            hir_id,
808            def_id: self.local_def_id(f.id),
809            ident: match f.ident {
810                Some(ident) => self.lower_ident(ident),
811                // FIXME(jseyfried): positional field hygiene.
812                None => Ident::new(sym::integer(index), self.lower_span(f.span)),
813            },
814            vis_span: self.lower_span(f.vis.span),
815            default: f.default.as_ref().map(|v| self.lower_anon_const_to_anon_const(v)),
816            ty,
817            safety: self.lower_safety(f.safety, hir::Safety::Safe),
818        }
819    }
820
821    fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
822        let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
823        let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
824        let trait_item_def_id = hir_id.expect_owner();
825
826        let (ident, generics, kind, has_default) = match &i.kind {
827            AssocItemKind::Const(box ConstItem {
828                ident,
829                generics,
830                ty,
831                expr,
832                define_opaque,
833                ..
834            }) => {
835                let (generics, kind) = self.lower_generics(
836                    generics,
837                    i.id,
838                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
839                    |this| {
840                        let ty = this
841                            .lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
842                        let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
843
844                        hir::TraitItemKind::Const(ty, body)
845                    },
846                );
847
848                if define_opaque.is_some() {
849                    if expr.is_some() {
850                        self.lower_define_opaque(hir_id, &define_opaque);
851                    } else {
852                        self.dcx().span_err(
853                            i.span,
854                            "only trait consts with default bodies can define opaque types",
855                        );
856                    }
857                }
858
859                (*ident, generics, kind, expr.is_some())
860            }
861            AssocItemKind::Fn(box Fn {
862                sig, ident, generics, body: None, define_opaque, ..
863            }) => {
864                // FIXME(contracts): Deny contract here since it won't apply to
865                // any impl method or callees.
866                let idents = self.lower_fn_params_to_idents(&sig.decl);
867                let (generics, sig) = self.lower_method_sig(
868                    generics,
869                    sig,
870                    i.id,
871                    FnDeclKind::Trait,
872                    sig.header.coroutine_kind,
873                    attrs,
874                );
875                if define_opaque.is_some() {
876                    self.dcx().span_err(
877                        i.span,
878                        "only trait methods with default bodies can define opaque types",
879                    );
880                }
881                (
882                    *ident,
883                    generics,
884                    hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(idents)),
885                    false,
886                )
887            }
888            AssocItemKind::Fn(box Fn {
889                sig,
890                ident,
891                generics,
892                body: Some(body),
893                contract,
894                define_opaque,
895                ..
896            }) => {
897                let body_id = self.lower_maybe_coroutine_body(
898                    sig.span,
899                    i.span,
900                    hir_id,
901                    &sig.decl,
902                    sig.header.coroutine_kind,
903                    Some(body),
904                    attrs,
905                    contract.as_deref(),
906                );
907                let (generics, sig) = self.lower_method_sig(
908                    generics,
909                    sig,
910                    i.id,
911                    FnDeclKind::Trait,
912                    sig.header.coroutine_kind,
913                    attrs,
914                );
915                self.lower_define_opaque(hir_id, &define_opaque);
916                (
917                    *ident,
918                    generics,
919                    hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)),
920                    true,
921                )
922            }
923            AssocItemKind::Type(box TyAlias {
924                ident, generics, where_clauses, bounds, ty, ..
925            }) => {
926                let mut generics = generics.clone();
927                add_ty_alias_where_clause(&mut generics, *where_clauses, false);
928                let (generics, kind) = self.lower_generics(
929                    &generics,
930                    i.id,
931                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
932                    |this| {
933                        let ty = ty.as_ref().map(|x| {
934                            this.lower_ty(
935                                x,
936                                ImplTraitContext::Disallowed(ImplTraitPosition::AssocTy),
937                            )
938                        });
939                        hir::TraitItemKind::Type(
940                            this.lower_param_bounds(
941                                bounds,
942                                RelaxedBoundPolicy::Allowed,
943                                ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
944                            ),
945                            ty,
946                        )
947                    },
948                );
949                (*ident, generics, kind, ty.is_some())
950            }
951            AssocItemKind::Delegation(box delegation) => {
952                let delegation_results = self.lower_delegation(delegation, i.id, false);
953                let item_kind = hir::TraitItemKind::Fn(
954                    delegation_results.sig,
955                    hir::TraitFn::Provided(delegation_results.body_id),
956                );
957                (delegation.ident, delegation_results.generics, item_kind, true)
958            }
959            AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
960                panic!("macros should have been expanded by now")
961            }
962        };
963
964        let item = hir::TraitItem {
965            owner_id: trait_item_def_id,
966            ident: self.lower_ident(ident),
967            generics,
968            kind,
969            span: self.lower_span(i.span),
970            defaultness: hir::Defaultness::Default { has_value: has_default },
971            has_delayed_lints: !self.delayed_lints.is_empty(),
972        };
973        self.arena.alloc(item)
974    }
975
976    fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemId {
977        hir::TraitItemId { owner_id: self.owner_id(i.id) }
978    }
979
980    /// Construct `ExprKind::Err` for the given `span`.
981    pub(crate) fn expr_err(&mut self, span: Span, guar: ErrorGuaranteed) -> hir::Expr<'hir> {
982        self.expr(span, hir::ExprKind::Err(guar))
983    }
984
985    fn lower_impl_item(
986        &mut self,
987        i: &AssocItem,
988        is_in_trait_impl: bool,
989    ) -> &'hir hir::ImplItem<'hir> {
990        // Since `default impl` is not yet implemented, this is always true in impls.
991        let has_value = true;
992        let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
993        let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
994        let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
995
996        let (ident, (generics, kind)) = match &i.kind {
997            AssocItemKind::Const(box ConstItem {
998                ident,
999                generics,
1000                ty,
1001                expr,
1002                define_opaque,
1003                ..
1004            }) => (
1005                *ident,
1006                self.lower_generics(
1007                    generics,
1008                    i.id,
1009                    ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
1010                    |this| {
1011                        let ty = this
1012                            .lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
1013                        let body = this.lower_const_body(i.span, expr.as_deref());
1014                        this.lower_define_opaque(hir_id, &define_opaque);
1015                        hir::ImplItemKind::Const(ty, body)
1016                    },
1017                ),
1018            ),
1019            AssocItemKind::Fn(box Fn {
1020                sig,
1021                ident,
1022                generics,
1023                body,
1024                contract,
1025                define_opaque,
1026                ..
1027            }) => {
1028                let body_id = self.lower_maybe_coroutine_body(
1029                    sig.span,
1030                    i.span,
1031                    hir_id,
1032                    &sig.decl,
1033                    sig.header.coroutine_kind,
1034                    body.as_deref(),
1035                    attrs,
1036                    contract.as_deref(),
1037                );
1038                let (generics, sig) = self.lower_method_sig(
1039                    generics,
1040                    sig,
1041                    i.id,
1042                    if is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
1043                    sig.header.coroutine_kind,
1044                    attrs,
1045                );
1046                self.lower_define_opaque(hir_id, &define_opaque);
1047
1048                (*ident, (generics, hir::ImplItemKind::Fn(sig, body_id)))
1049            }
1050            AssocItemKind::Type(box TyAlias { ident, generics, where_clauses, ty, .. }) => {
1051                let mut generics = generics.clone();
1052                add_ty_alias_where_clause(&mut generics, *where_clauses, false);
1053                (
1054                    *ident,
1055                    self.lower_generics(
1056                        &generics,
1057                        i.id,
1058                        ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
1059                        |this| match ty {
1060                            None => {
1061                                let guar = this.dcx().span_delayed_bug(
1062                                    i.span,
1063                                    "expected to lower associated type, but it was missing",
1064                                );
1065                                let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err(guar)));
1066                                hir::ImplItemKind::Type(ty)
1067                            }
1068                            Some(ty) => {
1069                                let ty = this.lower_ty(
1070                                    ty,
1071                                    ImplTraitContext::OpaqueTy {
1072                                        origin: hir::OpaqueTyOrigin::TyAlias {
1073                                            parent: this.local_def_id(i.id),
1074                                            in_assoc_ty: true,
1075                                        },
1076                                    },
1077                                );
1078                                hir::ImplItemKind::Type(ty)
1079                            }
1080                        },
1081                    ),
1082                )
1083            }
1084            AssocItemKind::Delegation(box delegation) => {
1085                let delegation_results = self.lower_delegation(delegation, i.id, is_in_trait_impl);
1086                (
1087                    delegation.ident,
1088                    (
1089                        delegation_results.generics,
1090                        hir::ImplItemKind::Fn(delegation_results.sig, delegation_results.body_id),
1091                    ),
1092                )
1093            }
1094            AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
1095                panic!("macros should have been expanded by now")
1096            }
1097        };
1098
1099        let item = hir::ImplItem {
1100            owner_id: hir_id.expect_owner(),
1101            ident: self.lower_ident(ident),
1102            generics,
1103            kind,
1104            vis_span: self.lower_span(i.vis.span),
1105            span: self.lower_span(i.span),
1106            defaultness,
1107            has_delayed_lints: !self.delayed_lints.is_empty(),
1108            trait_item_def_id: self
1109                .resolver
1110                .get_partial_res(i.id)
1111                .map(|r| r.expect_full_res().opt_def_id())
1112                .unwrap_or(None),
1113        };
1114        self.arena.alloc(item)
1115    }
1116
1117    fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemId {
1118        hir::ImplItemId { owner_id: self.owner_id(i.id) }
1119    }
1120
1121    fn lower_defaultness(
1122        &self,
1123        d: Defaultness,
1124        has_value: bool,
1125    ) -> (hir::Defaultness, Option<Span>) {
1126        match d {
1127            Defaultness::Default(sp) => {
1128                (hir::Defaultness::Default { has_value }, Some(self.lower_span(sp)))
1129            }
1130            Defaultness::Final => {
1131                assert!(has_value);
1132                (hir::Defaultness::Final, None)
1133            }
1134        }
1135    }
1136
1137    fn record_body(
1138        &mut self,
1139        params: &'hir [hir::Param<'hir>],
1140        value: hir::Expr<'hir>,
1141    ) -> hir::BodyId {
1142        let body = hir::Body { params, value: self.arena.alloc(value) };
1143        let id = body.id();
1144        assert_eq!(id.hir_id.owner, self.current_hir_id_owner);
1145        self.bodies.push((id.hir_id.local_id, self.arena.alloc(body)));
1146        id
1147    }
1148
1149    pub(super) fn lower_body(
1150        &mut self,
1151        f: impl FnOnce(&mut Self) -> (&'hir [hir::Param<'hir>], hir::Expr<'hir>),
1152    ) -> hir::BodyId {
1153        let prev_coroutine_kind = self.coroutine_kind.take();
1154        let task_context = self.task_context.take();
1155        let (parameters, result) = f(self);
1156        let body_id = self.record_body(parameters, result);
1157        self.task_context = task_context;
1158        self.coroutine_kind = prev_coroutine_kind;
1159        body_id
1160    }
1161
1162    fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
1163        let hir_id = self.lower_node_id(param.id);
1164        self.lower_attrs(hir_id, &param.attrs, param.span);
1165        hir::Param {
1166            hir_id,
1167            pat: self.lower_pat(&param.pat),
1168            ty_span: self.lower_span(param.ty.span),
1169            span: self.lower_span(param.span),
1170        }
1171    }
1172
1173    pub(super) fn lower_fn_body(
1174        &mut self,
1175        decl: &FnDecl,
1176        contract: Option<&FnContract>,
1177        body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
1178    ) -> hir::BodyId {
1179        self.lower_body(|this| {
1180            let params =
1181                this.arena.alloc_from_iter(decl.inputs.iter().map(|x| this.lower_param(x)));
1182
1183            // Optionally lower the fn contract, which turns:
1184            //
1185            // { body }
1186            //
1187            // into:
1188            //
1189            // { contract_requires(PRECOND); let __postcond = |ret_val| POSTCOND; postcond({ body }) }
1190            if let Some(contract) = contract {
1191                let precond = if let Some(req) = &contract.requires {
1192                    // Lower the precondition check intrinsic.
1193                    let lowered_req = this.lower_expr_mut(&req);
1194                    let req_span = this.mark_span_with_reason(
1195                        DesugaringKind::Contract,
1196                        lowered_req.span,
1197                        None,
1198                    );
1199                    let precond = this.expr_call_lang_item_fn_mut(
1200                        req_span,
1201                        hir::LangItem::ContractCheckRequires,
1202                        &*arena_vec![this; lowered_req],
1203                    );
1204                    Some(this.stmt_expr(req.span, precond))
1205                } else {
1206                    None
1207                };
1208                let (postcond, body) = if let Some(ens) = &contract.ensures {
1209                    let ens_span = this.lower_span(ens.span);
1210                    let ens_span =
1211                        this.mark_span_with_reason(DesugaringKind::Contract, ens_span, None);
1212                    // Set up the postcondition `let` statement.
1213                    let check_ident: Ident =
1214                        Ident::from_str_and_span("__ensures_checker", ens_span);
1215                    let (checker_pat, check_hir_id) = this.pat_ident_binding_mode_mut(
1216                        ens_span,
1217                        check_ident,
1218                        hir::BindingMode::NONE,
1219                    );
1220                    let lowered_ens = this.lower_expr_mut(&ens);
1221                    let postcond_checker = this.expr_call_lang_item_fn(
1222                        ens_span,
1223                        hir::LangItem::ContractBuildCheckEnsures,
1224                        &*arena_vec![this; lowered_ens],
1225                    );
1226                    let postcond = this.stmt_let_pat(
1227                        None,
1228                        ens_span,
1229                        Some(postcond_checker),
1230                        this.arena.alloc(checker_pat),
1231                        hir::LocalSource::Contract,
1232                    );
1233
1234                    // Install contract_ensures so we will intercept `return` statements,
1235                    // then lower the body.
1236                    this.contract_ensures = Some((ens_span, check_ident, check_hir_id));
1237                    let body = this.arena.alloc(body(this));
1238
1239                    // Finally, inject an ensures check on the implicit return of the body.
1240                    let body = this.inject_ensures_check(body, ens_span, check_ident, check_hir_id);
1241                    (Some(postcond), body)
1242                } else {
1243                    let body = &*this.arena.alloc(body(this));
1244                    (None, body)
1245                };
1246                // Flatten the body into precond, then postcond, then wrapped body.
1247                let wrapped_body = this.block_all(
1248                    body.span,
1249                    this.arena.alloc_from_iter([precond, postcond].into_iter().flatten()),
1250                    Some(body),
1251                );
1252                (params, this.expr_block(wrapped_body))
1253            } else {
1254                (params, body(this))
1255            }
1256        })
1257    }
1258
1259    fn lower_fn_body_block(
1260        &mut self,
1261        decl: &FnDecl,
1262        body: &Block,
1263        contract: Option<&FnContract>,
1264    ) -> hir::BodyId {
1265        self.lower_fn_body(decl, contract, |this| this.lower_block_expr(body))
1266    }
1267
1268    pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId {
1269        self.lower_body(|this| {
1270            (
1271                &[],
1272                match expr {
1273                    Some(expr) => this.lower_expr_mut(expr),
1274                    None => this.expr_err(span, this.dcx().span_delayed_bug(span, "no block")),
1275                },
1276            )
1277        })
1278    }
1279
1280    /// Takes what may be the body of an `async fn` or a `gen fn` and wraps it in an `async {}` or
1281    /// `gen {}` block as appropriate.
1282    fn lower_maybe_coroutine_body(
1283        &mut self,
1284        fn_decl_span: Span,
1285        span: Span,
1286        fn_id: hir::HirId,
1287        decl: &FnDecl,
1288        coroutine_kind: Option<CoroutineKind>,
1289        body: Option<&Block>,
1290        attrs: &'hir [hir::Attribute],
1291        contract: Option<&FnContract>,
1292    ) -> hir::BodyId {
1293        let Some(body) = body else {
1294            // Functions without a body are an error, except if this is an intrinsic. For those we
1295            // create a fake body so that the entire rest of the compiler doesn't have to deal with
1296            // this as a special case.
1297            return self.lower_fn_body(decl, contract, |this| {
1298                if attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic))
1299                    || this.tcx.is_sdylib_interface_build()
1300                {
1301                    let span = this.lower_span(span);
1302                    let empty_block = hir::Block {
1303                        hir_id: this.next_id(),
1304                        stmts: &[],
1305                        expr: None,
1306                        rules: hir::BlockCheckMode::DefaultBlock,
1307                        span,
1308                        targeted_by_break: false,
1309                    };
1310                    let loop_ = hir::ExprKind::Loop(
1311                        this.arena.alloc(empty_block),
1312                        None,
1313                        hir::LoopSource::Loop,
1314                        span,
1315                    );
1316                    hir::Expr { hir_id: this.next_id(), kind: loop_, span }
1317                } else {
1318                    this.expr_err(span, this.dcx().has_errors().unwrap())
1319                }
1320            });
1321        };
1322        let Some(coroutine_kind) = coroutine_kind else {
1323            // Typical case: not a coroutine.
1324            return self.lower_fn_body_block(decl, body, contract);
1325        };
1326        // FIXME(contracts): Support contracts on async fn.
1327        self.lower_body(|this| {
1328            let (parameters, expr) = this.lower_coroutine_body_with_moved_arguments(
1329                decl,
1330                |this| this.lower_block_expr(body),
1331                fn_decl_span,
1332                body.span,
1333                coroutine_kind,
1334                hir::CoroutineSource::Fn,
1335            );
1336
1337            // FIXME(async_fn_track_caller): Can this be moved above?
1338            let hir_id = expr.hir_id;
1339            this.maybe_forward_track_caller(body.span, fn_id, hir_id);
1340
1341            (parameters, expr)
1342        })
1343    }
1344
1345    /// Lowers a desugared coroutine body after moving all of the arguments
1346    /// into the body. This is to make sure that the future actually owns the
1347    /// arguments that are passed to the function, and to ensure things like
1348    /// drop order are stable.
1349    pub(crate) fn lower_coroutine_body_with_moved_arguments(
1350        &mut self,
1351        decl: &FnDecl,
1352        lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::Expr<'hir>,
1353        fn_decl_span: Span,
1354        body_span: Span,
1355        coroutine_kind: CoroutineKind,
1356        coroutine_source: hir::CoroutineSource,
1357    ) -> (&'hir [hir::Param<'hir>], hir::Expr<'hir>) {
1358        let mut parameters: Vec<hir::Param<'_>> = Vec::new();
1359        let mut statements: Vec<hir::Stmt<'_>> = Vec::new();
1360
1361        // Async function parameters are lowered into the closure body so that they are
1362        // captured and so that the drop order matches the equivalent non-async functions.
1363        //
1364        // from:
1365        //
1366        //     async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {
1367        //         <body>
1368        //     }
1369        //
1370        // into:
1371        //
1372        //     fn foo(__arg0: <ty>, __arg1: <ty>, __arg2: <ty>) {
1373        //       async move {
1374        //         let __arg2 = __arg2;
1375        //         let <pattern> = __arg2;
1376        //         let __arg1 = __arg1;
1377        //         let <pattern> = __arg1;
1378        //         let __arg0 = __arg0;
1379        //         let <pattern> = __arg0;
1380        //         drop-temps { <body> } // see comments later in fn for details
1381        //       }
1382        //     }
1383        //
1384        // If `<pattern>` is a simple ident, then it is lowered to a single
1385        // `let <pattern> = <pattern>;` statement as an optimization.
1386        //
1387        // Note that the body is embedded in `drop-temps`; an
1388        // equivalent desugaring would be `return { <body>
1389        // };`. The key point is that we wish to drop all the
1390        // let-bound variables and temporaries created in the body
1391        // (and its tail expression!) before we drop the
1392        // parameters (c.f. rust-lang/rust#64512).
1393        for (index, parameter) in decl.inputs.iter().enumerate() {
1394            let parameter = self.lower_param(parameter);
1395            let span = parameter.pat.span;
1396
1397            // Check if this is a binding pattern, if so, we can optimize and avoid adding a
1398            // `let <pat> = __argN;` statement. In this case, we do not rename the parameter.
1399            let (ident, is_simple_parameter) = match parameter.pat.kind {
1400                hir::PatKind::Binding(hir::BindingMode(ByRef::No, _), _, ident, _) => (ident, true),
1401                // For `ref mut` or wildcard arguments, we can't reuse the binding, but
1402                // we can keep the same name for the parameter.
1403                // This lets rustdoc render it correctly in documentation.
1404                hir::PatKind::Binding(_, _, ident, _) => (ident, false),
1405                hir::PatKind::Wild => (Ident::with_dummy_span(rustc_span::kw::Underscore), false),
1406                _ => {
1407                    // Replace the ident for bindings that aren't simple.
1408                    let name = format!("__arg{index}");
1409                    let ident = Ident::from_str(&name);
1410
1411                    (ident, false)
1412                }
1413            };
1414
1415            let desugared_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);
1416
1417            // Construct a parameter representing `__argN: <ty>` to replace the parameter of the
1418            // async function.
1419            //
1420            // If this is the simple case, this parameter will end up being the same as the
1421            // original parameter, but with a different pattern id.
1422            let stmt_attrs = self.attrs.get(&parameter.hir_id.local_id).copied();
1423            let (new_parameter_pat, new_parameter_id) = self.pat_ident(desugared_span, ident);
1424            let new_parameter = hir::Param {
1425                hir_id: parameter.hir_id,
1426                pat: new_parameter_pat,
1427                ty_span: self.lower_span(parameter.ty_span),
1428                span: self.lower_span(parameter.span),
1429            };
1430
1431            if is_simple_parameter {
1432                // If this is the simple case, then we only insert one statement that is
1433                // `let <pat> = <pat>;`. We re-use the original argument's pattern so that
1434                // `HirId`s are densely assigned.
1435                let expr = self.expr_ident(desugared_span, ident, new_parameter_id);
1436                let stmt = self.stmt_let_pat(
1437                    stmt_attrs,
1438                    desugared_span,
1439                    Some(expr),
1440                    parameter.pat,
1441                    hir::LocalSource::AsyncFn,
1442                );
1443                statements.push(stmt);
1444            } else {
1445                // If this is not the simple case, then we construct two statements:
1446                //
1447                // ```
1448                // let __argN = __argN;
1449                // let <pat> = __argN;
1450                // ```
1451                //
1452                // The first statement moves the parameter into the closure and thus ensures
1453                // that the drop order is correct.
1454                //
1455                // The second statement creates the bindings that the user wrote.
1456
1457                // Construct the `let mut __argN = __argN;` statement. It must be a mut binding
1458                // because the user may have specified a `ref mut` binding in the next
1459                // statement.
1460                let (move_pat, move_id) =
1461                    self.pat_ident_binding_mode(desugared_span, ident, hir::BindingMode::MUT);
1462                let move_expr = self.expr_ident(desugared_span, ident, new_parameter_id);
1463                let move_stmt = self.stmt_let_pat(
1464                    None,
1465                    desugared_span,
1466                    Some(move_expr),
1467                    move_pat,
1468                    hir::LocalSource::AsyncFn,
1469                );
1470
1471                // Construct the `let <pat> = __argN;` statement. We re-use the original
1472                // parameter's pattern so that `HirId`s are densely assigned.
1473                let pattern_expr = self.expr_ident(desugared_span, ident, move_id);
1474                let pattern_stmt = self.stmt_let_pat(
1475                    stmt_attrs,
1476                    desugared_span,
1477                    Some(pattern_expr),
1478                    parameter.pat,
1479                    hir::LocalSource::AsyncFn,
1480                );
1481
1482                statements.push(move_stmt);
1483                statements.push(pattern_stmt);
1484            };
1485
1486            parameters.push(new_parameter);
1487        }
1488
1489        let mkbody = |this: &mut LoweringContext<'_, 'hir>| {
1490            // Create a block from the user's function body:
1491            let user_body = lower_body(this);
1492
1493            // Transform into `drop-temps { <user-body> }`, an expression:
1494            let desugared_span =
1495                this.mark_span_with_reason(DesugaringKind::Async, user_body.span, None);
1496            let user_body = this.expr_drop_temps(desugared_span, this.arena.alloc(user_body));
1497
1498            // As noted above, create the final block like
1499            //
1500            // ```
1501            // {
1502            //   let $param_pattern = $raw_param;
1503            //   ...
1504            //   drop-temps { <user-body> }
1505            // }
1506            // ```
1507            let body = this.block_all(
1508                desugared_span,
1509                this.arena.alloc_from_iter(statements),
1510                Some(user_body),
1511            );
1512
1513            this.expr_block(body)
1514        };
1515        let desugaring_kind = match coroutine_kind {
1516            CoroutineKind::Async { .. } => hir::CoroutineDesugaring::Async,
1517            CoroutineKind::Gen { .. } => hir::CoroutineDesugaring::Gen,
1518            CoroutineKind::AsyncGen { .. } => hir::CoroutineDesugaring::AsyncGen,
1519        };
1520        let closure_id = coroutine_kind.closure_id();
1521
1522        let coroutine_expr = self.make_desugared_coroutine_expr(
1523            // The default capture mode here is by-ref. Later on during upvar analysis,
1524            // we will force the captured arguments to by-move, but for async closures,
1525            // we want to make sure that we avoid unnecessarily moving captures, or else
1526            // all async closures would default to `FnOnce` as their calling mode.
1527            CaptureBy::Ref,
1528            closure_id,
1529            None,
1530            fn_decl_span,
1531            body_span,
1532            desugaring_kind,
1533            coroutine_source,
1534            mkbody,
1535        );
1536
1537        let expr = hir::Expr {
1538            hir_id: self.lower_node_id(closure_id),
1539            kind: coroutine_expr,
1540            span: self.lower_span(body_span),
1541        };
1542
1543        (self.arena.alloc_from_iter(parameters), expr)
1544    }
1545
1546    fn lower_method_sig(
1547        &mut self,
1548        generics: &Generics,
1549        sig: &FnSig,
1550        id: NodeId,
1551        kind: FnDeclKind,
1552        coroutine_kind: Option<CoroutineKind>,
1553        attrs: &[hir::Attribute],
1554    ) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
1555        let header = self.lower_fn_header(sig.header, hir::Safety::Safe, attrs);
1556        let itctx = ImplTraitContext::Universal;
1557        let (generics, decl) = self.lower_generics(generics, id, itctx, |this| {
1558            this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind)
1559        });
1560        (generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
1561    }
1562
1563    pub(super) fn lower_fn_header(
1564        &mut self,
1565        h: FnHeader,
1566        default_safety: hir::Safety,
1567        attrs: &[hir::Attribute],
1568    ) -> hir::FnHeader {
1569        let asyncness = if let Some(CoroutineKind::Async { span, .. }) = h.coroutine_kind {
1570            hir::IsAsync::Async(span)
1571        } else {
1572            hir::IsAsync::NotAsync
1573        };
1574
1575        let safety = self.lower_safety(h.safety, default_safety);
1576
1577        // Treat safe `#[target_feature]` functions as unsafe, but also remember that we did so.
1578        let safety = if find_attr!(attrs, AttributeKind::TargetFeature { .. })
1579            && safety.is_safe()
1580            && !self.tcx.sess.target.is_like_wasm
1581        {
1582            hir::HeaderSafety::SafeTargetFeatures
1583        } else {
1584            safety.into()
1585        };
1586
1587        hir::FnHeader {
1588            safety,
1589            asyncness,
1590            constness: self.lower_constness(h.constness),
1591            abi: self.lower_extern(h.ext),
1592        }
1593    }
1594
1595    pub(super) fn lower_abi(&mut self, abi_str: StrLit) -> ExternAbi {
1596        let ast::StrLit { symbol_unescaped, span, .. } = abi_str;
1597        let extern_abi = symbol_unescaped.as_str().parse().unwrap_or_else(|_| {
1598            self.error_on_invalid_abi(abi_str);
1599            ExternAbi::Rust
1600        });
1601        let tcx = self.tcx;
1602
1603        // we can't do codegen for unsupported ABIs, so error now so we won't get farther
1604        if !tcx.sess.target.is_abi_supported(extern_abi) {
1605            let mut err = struct_span_code_err!(
1606                tcx.dcx(),
1607                span,
1608                E0570,
1609                "{extern_abi} is not a supported ABI for the current target",
1610            );
1611
1612            if let ExternAbi::Stdcall { unwind } = extern_abi {
1613                let c_abi = ExternAbi::C { unwind };
1614                let system_abi = ExternAbi::System { unwind };
1615                err.help(format!("if you need `extern {extern_abi}` on win32 and `extern {c_abi}` everywhere else, \
1616                    use `extern {system_abi}`"
1617                ));
1618            }
1619            err.emit();
1620        }
1621        // Show required feature gate even if we already errored, as the user is likely to build the code
1622        // for the actually intended target next and then they will need the feature gate.
1623        gate_unstable_abi(tcx.sess, tcx.features(), span, extern_abi);
1624        extern_abi
1625    }
1626
1627    pub(super) fn lower_extern(&mut self, ext: Extern) -> ExternAbi {
1628        match ext {
1629            Extern::None => ExternAbi::Rust,
1630            Extern::Implicit(_) => ExternAbi::FALLBACK,
1631            Extern::Explicit(abi, _) => self.lower_abi(abi),
1632        }
1633    }
1634
1635    fn error_on_invalid_abi(&self, abi: StrLit) {
1636        let abi_names = enabled_names(self.tcx.features(), abi.span)
1637            .iter()
1638            .map(|s| Symbol::intern(s))
1639            .collect::<Vec<_>>();
1640        let suggested_name = find_best_match_for_name(&abi_names, abi.symbol_unescaped, None);
1641        self.dcx().emit_err(InvalidAbi {
1642            abi: abi.symbol_unescaped,
1643            span: abi.span,
1644            suggestion: suggested_name.map(|suggested_name| InvalidAbiSuggestion {
1645                span: abi.span,
1646                suggestion: suggested_name.to_string(),
1647            }),
1648            command: "rustc --print=calling-conventions".to_string(),
1649        });
1650    }
1651
1652    pub(super) fn lower_constness(&mut self, c: Const) -> hir::Constness {
1653        match c {
1654            Const::Yes(_) => hir::Constness::Const,
1655            Const::No => hir::Constness::NotConst,
1656        }
1657    }
1658
1659    pub(super) fn lower_safety(&self, s: Safety, default: hir::Safety) -> hir::Safety {
1660        match s {
1661            Safety::Unsafe(_) => hir::Safety::Unsafe,
1662            Safety::Default => default,
1663            Safety::Safe(_) => hir::Safety::Safe,
1664        }
1665    }
1666
1667    /// Return the pair of the lowered `generics` as `hir::Generics` and the evaluation of `f` with
1668    /// the carried impl trait definitions and bounds.
1669    #[instrument(level = "debug", skip(self, f))]
1670    fn lower_generics<T>(
1671        &mut self,
1672        generics: &Generics,
1673        parent_node_id: NodeId,
1674        itctx: ImplTraitContext,
1675        f: impl FnOnce(&mut Self) -> T,
1676    ) -> (&'hir hir::Generics<'hir>, T) {
1677        assert!(self.impl_trait_defs.is_empty());
1678        assert!(self.impl_trait_bounds.is_empty());
1679
1680        let mut predicates: SmallVec<[hir::WherePredicate<'hir>; 4]> = SmallVec::new();
1681        predicates.extend(generics.params.iter().filter_map(|param| {
1682            self.lower_generic_bound_predicate(
1683                param.ident,
1684                param.id,
1685                &param.kind,
1686                &param.bounds,
1687                param.colon_span,
1688                generics.span,
1689                RelaxedBoundPolicy::Allowed,
1690                itctx,
1691                PredicateOrigin::GenericParam,
1692            )
1693        }));
1694        predicates.extend(
1695            generics
1696                .where_clause
1697                .predicates
1698                .iter()
1699                .map(|predicate| self.lower_where_predicate(predicate, &generics.params)),
1700        );
1701
1702        let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = self
1703            .lower_generic_params_mut(&generics.params, hir::GenericParamSource::Generics)
1704            .collect();
1705
1706        // Introduce extra lifetimes if late resolution tells us to.
1707        let extra_lifetimes = self.resolver.extra_lifetime_params(parent_node_id);
1708        params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
1709            self.lifetime_res_to_generic_param(
1710                ident,
1711                node_id,
1712                res,
1713                hir::GenericParamSource::Generics,
1714            )
1715        }));
1716
1717        let has_where_clause_predicates = !generics.where_clause.predicates.is_empty();
1718        let where_clause_span = self.lower_span(generics.where_clause.span);
1719        let span = self.lower_span(generics.span);
1720        let res = f(self);
1721
1722        let impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
1723        params.extend(impl_trait_defs.into_iter());
1724
1725        let impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);
1726        predicates.extend(impl_trait_bounds.into_iter());
1727
1728        let lowered_generics = self.arena.alloc(hir::Generics {
1729            params: self.arena.alloc_from_iter(params),
1730            predicates: self.arena.alloc_from_iter(predicates),
1731            has_where_clause_predicates,
1732            where_clause_span,
1733            span,
1734        });
1735
1736        (lowered_generics, res)
1737    }
1738
1739    pub(super) fn lower_define_opaque(
1740        &mut self,
1741        hir_id: HirId,
1742        define_opaque: &Option<ThinVec<(NodeId, Path)>>,
1743    ) {
1744        assert_eq!(self.define_opaque, None);
1745        assert!(hir_id.is_owner());
1746        let Some(define_opaque) = define_opaque.as_ref() else {
1747            return;
1748        };
1749        let define_opaque = define_opaque.iter().filter_map(|(id, path)| {
1750            let res = self.resolver.get_partial_res(*id);
1751            let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) else {
1752                self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
1753                return None;
1754            };
1755            let Some(did) = did.as_local() else {
1756                self.dcx().span_err(
1757                    path.span,
1758                    "only opaque types defined in the local crate can be defined",
1759                );
1760                return None;
1761            };
1762            Some((self.lower_span(path.span), did))
1763        });
1764        let define_opaque = self.arena.alloc_from_iter(define_opaque);
1765        self.define_opaque = Some(define_opaque);
1766    }
1767
1768    pub(super) fn lower_generic_bound_predicate(
1769        &mut self,
1770        ident: Ident,
1771        id: NodeId,
1772        kind: &GenericParamKind,
1773        bounds: &[GenericBound],
1774        colon_span: Option<Span>,
1775        parent_span: Span,
1776        rbp: RelaxedBoundPolicy<'_>,
1777        itctx: ImplTraitContext,
1778        origin: PredicateOrigin,
1779    ) -> Option<hir::WherePredicate<'hir>> {
1780        // Do not create a clause if we do not have anything inside it.
1781        if bounds.is_empty() {
1782            return None;
1783        }
1784
1785        let bounds = self.lower_param_bounds(bounds, rbp, itctx);
1786
1787        let param_span = ident.span;
1788
1789        // Reconstruct the span of the entire predicate from the individual generic bounds.
1790        let span_start = colon_span.unwrap_or_else(|| param_span.shrink_to_hi());
1791        let span = bounds.iter().fold(span_start, |span_accum, bound| {
1792            match bound.span().find_ancestor_inside(parent_span) {
1793                Some(bound_span) => span_accum.to(bound_span),
1794                None => span_accum,
1795            }
1796        });
1797        let span = self.lower_span(span);
1798        let hir_id = self.next_id();
1799        let kind = self.arena.alloc(match kind {
1800            GenericParamKind::Const { .. } => return None,
1801            GenericParamKind::Type { .. } => {
1802                let def_id = self.local_def_id(id).to_def_id();
1803                let hir_id = self.next_id();
1804                let res = Res::Def(DefKind::TyParam, def_id);
1805                let ident = self.lower_ident(ident);
1806                let ty_path = self.arena.alloc(hir::Path {
1807                    span: param_span,
1808                    res,
1809                    segments: self
1810                        .arena
1811                        .alloc_from_iter([hir::PathSegment::new(ident, hir_id, res)]),
1812                });
1813                let ty_id = self.next_id();
1814                let bounded_ty =
1815                    self.ty_path(ty_id, param_span, hir::QPath::Resolved(None, ty_path));
1816                hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
1817                    bounded_ty: self.arena.alloc(bounded_ty),
1818                    bounds,
1819                    bound_generic_params: &[],
1820                    origin,
1821                })
1822            }
1823            GenericParamKind::Lifetime => {
1824                let lt_id = self.next_node_id();
1825                let lifetime =
1826                    self.new_named_lifetime(id, lt_id, ident, LifetimeSource::Other, ident.into());
1827                hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
1828                    lifetime,
1829                    bounds,
1830                    in_where_clause: false,
1831                })
1832            }
1833        });
1834        Some(hir::WherePredicate { hir_id, span, kind })
1835    }
1836
1837    fn lower_where_predicate(
1838        &mut self,
1839        pred: &WherePredicate,
1840        params: &[ast::GenericParam],
1841    ) -> hir::WherePredicate<'hir> {
1842        let hir_id = self.lower_node_id(pred.id);
1843        let span = self.lower_span(pred.span);
1844        self.lower_attrs(hir_id, &pred.attrs, span);
1845        let kind = self.arena.alloc(match &pred.kind {
1846            WherePredicateKind::BoundPredicate(WhereBoundPredicate {
1847                bound_generic_params,
1848                bounded_ty,
1849                bounds,
1850            }) => {
1851                let rbp = if bound_generic_params.is_empty() {
1852                    RelaxedBoundPolicy::AllowedIfOnTyParam(bounded_ty.id, params)
1853                } else {
1854                    RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::LateBoundVarsInScope)
1855                };
1856                hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
1857                    bound_generic_params: self.lower_generic_params(
1858                        bound_generic_params,
1859                        hir::GenericParamSource::Binder,
1860                    ),
1861                    bounded_ty: self.lower_ty(
1862                        bounded_ty,
1863                        ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1864                    ),
1865                    bounds: self.lower_param_bounds(
1866                        bounds,
1867                        rbp,
1868                        ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1869                    ),
1870                    origin: PredicateOrigin::WhereClause,
1871                })
1872            }
1873            WherePredicateKind::RegionPredicate(WhereRegionPredicate { lifetime, bounds }) => {
1874                hir::WherePredicateKind::RegionPredicate(hir::WhereRegionPredicate {
1875                    lifetime: self.lower_lifetime(
1876                        lifetime,
1877                        LifetimeSource::Other,
1878                        lifetime.ident.into(),
1879                    ),
1880                    bounds: self.lower_param_bounds(
1881                        bounds,
1882                        RelaxedBoundPolicy::Allowed,
1883                        ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1884                    ),
1885                    in_where_clause: true,
1886                })
1887            }
1888            WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty }) => {
1889                hir::WherePredicateKind::EqPredicate(hir::WhereEqPredicate {
1890                    lhs_ty: self
1891                        .lower_ty(lhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1892                    rhs_ty: self
1893                        .lower_ty(rhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1894                })
1895            }
1896        });
1897        hir::WherePredicate { hir_id, span, kind }
1898    }
1899}