Skip to main content

rustc_hir_analysis/hir_ty_lowering/
mod.rs

1//! HIR ty lowering: Lowers type-system entities[^1] from the [HIR][hir] to
2//! the [`rustc_middle::ty`] representation.
3//!
4//! Not to be confused with *AST lowering* which lowers AST constructs to HIR ones
5//! or with *THIR* / *MIR* *lowering* / *building* which lowers HIR *bodies*
6//! (i.e., “executable code”) to THIR / MIR.
7//!
8//! Most lowering routines are defined on [`dyn HirTyLowerer`](HirTyLowerer) directly,
9//! like the main routine of this module, `lower_ty`.
10//!
11//! This module used to be called `astconv`.
12//!
13//! [^1]: This includes types, lifetimes / regions, constants in type positions,
14//! trait references and bounds.
15
16mod bounds;
17mod cmse;
18mod dyn_trait;
19pub mod errors;
20pub mod generics;
21
22use std::slice;
23
24use rustc_ast::LitKind;
25use rustc_data_structures::assert_matches;
26use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
27use rustc_errors::codes::*;
28use rustc_errors::{
29    Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, struct_span_code_err,
30};
31use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
32use rustc_hir::def_id::{DefId, LocalDefId};
33use rustc_hir::{self as hir, AnonConst, GenericArg, GenericArgs, HirId};
34use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
35use rustc_infer::traits::DynCompatibilityViolation;
36use rustc_macros::{TypeFoldable, TypeVisitable};
37use rustc_middle::middle::stability::AllowUnstable;
38use rustc_middle::ty::print::PrintPolyTraitRefExt as _;
39use rustc_middle::ty::{
40    self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, LitToConstInput, Ty, TyCtxt,
41    TypeSuperFoldable, TypeVisitableExt, TypingMode, Upcast, const_lit_matches_ty, fold_regions,
42};
43use rustc_middle::{bug, span_bug};
44use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
45use rustc_session::parse::feature_err;
46use rustc_span::{DUMMY_SP, Ident, Span, kw, sym};
47use rustc_trait_selection::infer::InferCtxtExt;
48use rustc_trait_selection::traits::wf::object_region_bounds;
49use rustc_trait_selection::traits::{self, FulfillmentError};
50use tracing::{debug, instrument};
51
52use crate::check::check_abi;
53use crate::check_c_variadic_abi;
54use crate::errors::{AmbiguousLifetimeBound, BadReturnTypeNotation};
55use crate::hir_ty_lowering::errors::{GenericsArgsErrExtend, prohibit_assoc_item_constraint};
56use crate::hir_ty_lowering::generics::{check_generic_arg_count, lower_generic_args};
57use crate::middle::resolve_bound_vars as rbv;
58
59/// The context in which an implied bound is being added to a item being lowered (i.e. a sizedness
60/// trait or a default trait)
61#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for ImpliedBoundsContext<'tcx> {
    #[inline]
    fn clone(&self) -> ImpliedBoundsContext<'tcx> {
        let _: ::core::clone::AssertParamIsClone<LocalDefId>;
        let _:
                ::core::clone::AssertParamIsClone<&'tcx [hir::WherePredicate<'tcx>]>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for ImpliedBoundsContext<'tcx> { }Copy)]
62pub(crate) enum ImpliedBoundsContext<'tcx> {
63    /// An implied bound is added to a trait definition (i.e. a new supertrait), used when adding
64    /// a default `MetaSized` supertrait
65    TraitDef(LocalDefId),
66    /// An implied bound is added to a type parameter
67    TyParam(LocalDefId, &'tcx [hir::WherePredicate<'tcx>]),
68    /// An implied bound being added in any other context
69    AssociatedTypeOrImplTrait,
70}
71
72/// A path segment that is semantically allowed to have generic arguments.
73#[derive(#[automatically_derived]
impl ::core::fmt::Debug for GenericPathSegment {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field2_finish(f,
            "GenericPathSegment", &self.0, &&self.1)
    }
}Debug)]
74pub struct GenericPathSegment(pub DefId, pub usize);
75
76#[derive(#[automatically_derived]
impl ::core::marker::Copy for PredicateFilter { }Copy, #[automatically_derived]
impl ::core::clone::Clone for PredicateFilter {
    #[inline]
    fn clone(&self) -> PredicateFilter {
        let _: ::core::clone::AssertParamIsClone<Ident>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for PredicateFilter {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            PredicateFilter::All =>
                ::core::fmt::Formatter::write_str(f, "All"),
            PredicateFilter::SelfOnly =>
                ::core::fmt::Formatter::write_str(f, "SelfOnly"),
            PredicateFilter::SelfTraitThatDefines(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "SelfTraitThatDefines", &__self_0),
            PredicateFilter::SelfAndAssociatedTypeBounds =>
                ::core::fmt::Formatter::write_str(f,
                    "SelfAndAssociatedTypeBounds"),
            PredicateFilter::ConstIfConst =>
                ::core::fmt::Formatter::write_str(f, "ConstIfConst"),
            PredicateFilter::SelfConstIfConst =>
                ::core::fmt::Formatter::write_str(f, "SelfConstIfConst"),
        }
    }
}Debug)]
77pub enum PredicateFilter {
78    /// All predicates may be implied by the trait.
79    All,
80
81    /// Only traits that reference `Self: ..` are implied by the trait.
82    SelfOnly,
83
84    /// Only traits that reference `Self: ..` and define an associated type
85    /// with the given ident are implied by the trait. This mode exists to
86    /// side-step query cycles when lowering associated types.
87    SelfTraitThatDefines(Ident),
88
89    /// Only traits that reference `Self: ..` and their associated type bounds.
90    /// For example, given `Self: Tr<A: B>`, this would expand to `Self: Tr`
91    /// and `<Self as Tr>::A: B`.
92    SelfAndAssociatedTypeBounds,
93
94    /// Filter only the `[const]` bounds, which are lowered into `HostEffect` clauses.
95    ConstIfConst,
96
97    /// Filter only the `[const]` bounds which are *also* in the supertrait position.
98    SelfConstIfConst,
99}
100
101#[derive(#[automatically_derived]
impl<'a> ::core::fmt::Debug for RegionInferReason<'a> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            RegionInferReason::ExplicitObjectLifetime =>
                ::core::fmt::Formatter::write_str(f,
                    "ExplicitObjectLifetime"),
            RegionInferReason::ObjectLifetimeDefault(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "ObjectLifetimeDefault", &__self_0),
            RegionInferReason::Param(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Param",
                    &__self_0),
            RegionInferReason::RegionPredicate =>
                ::core::fmt::Formatter::write_str(f, "RegionPredicate"),
            RegionInferReason::Reference =>
                ::core::fmt::Formatter::write_str(f, "Reference"),
            RegionInferReason::OutlivesBound =>
                ::core::fmt::Formatter::write_str(f, "OutlivesBound"),
        }
    }
}Debug)]
102pub enum RegionInferReason<'a> {
103    /// Lifetime on a trait object that is spelled explicitly, e.g. `+ 'a` or `+ '_`.
104    ExplicitObjectLifetime,
105    /// A trait object's lifetime when it is elided, e.g. `dyn Any`.
106    ObjectLifetimeDefault(Span),
107    /// Generic lifetime parameter
108    Param(&'a ty::GenericParamDef),
109    RegionPredicate,
110    Reference,
111    OutlivesBound,
112}
113
114#[derive(#[automatically_derived]
impl ::core::marker::Copy for InherentAssocCandidate { }Copy, #[automatically_derived]
impl ::core::clone::Clone for InherentAssocCandidate {
    #[inline]
    fn clone(&self) -> InherentAssocCandidate {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        *self
    }
}Clone, const _: () =
    {
        impl<'tcx>
            ::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
            for InherentAssocCandidate {
            fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
                __folder: &mut __F) -> Result<Self, __F::Error> {
                Ok(match self {
                        InherentAssocCandidate {
                            impl_: __binding_0,
                            assoc_item: __binding_1,
                            scope: __binding_2 } => {
                            InherentAssocCandidate {
                                impl_: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
                                        __folder)?,
                                assoc_item: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
                                        __folder)?,
                                scope: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_2,
                                        __folder)?,
                            }
                        }
                    })
            }
            fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
                __folder: &mut __F) -> Self {
                match self {
                    InherentAssocCandidate {
                        impl_: __binding_0,
                        assoc_item: __binding_1,
                        scope: __binding_2 } => {
                        InherentAssocCandidate {
                            impl_: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
                                __folder),
                            assoc_item: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
                                __folder),
                            scope: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_2,
                                __folder),
                        }
                    }
                }
            }
        }
    };TypeFoldable, const _: () =
    {
        impl<'tcx>
            ::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
            for InherentAssocCandidate {
            fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
                __visitor: &mut __V) -> __V::Result {
                match *self {
                    InherentAssocCandidate {
                        impl_: ref __binding_0,
                        assoc_item: ref __binding_1,
                        scope: ref __binding_2 } => {
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                        {
                            match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_2,
                                        __visitor)) {
                                ::core::ops::ControlFlow::Continue(()) => {}
                                ::core::ops::ControlFlow::Break(r) => {
                                    return ::rustc_middle::ty::VisitorResult::from_residual(r);
                                }
                            }
                        }
                    }
                }
                <__V::Result as ::rustc_middle::ty::VisitorResult>::output()
            }
        }
    };TypeVisitable, #[automatically_derived]
impl ::core::fmt::Debug for InherentAssocCandidate {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f,
            "InherentAssocCandidate", "impl_", &self.impl_, "assoc_item",
            &self.assoc_item, "scope", &&self.scope)
    }
}Debug)]
115pub struct InherentAssocCandidate {
116    pub impl_: DefId,
117    pub assoc_item: DefId,
118    pub scope: DefId,
119}
120
121/// A context which can lower type-system entities from the [HIR][hir] to
122/// the [`rustc_middle::ty`] representation.
123///
124/// This trait used to be called `AstConv`.
125pub trait HirTyLowerer<'tcx> {
126    fn tcx(&self) -> TyCtxt<'tcx>;
127
128    fn dcx(&self) -> DiagCtxtHandle<'_>;
129
130    /// Returns the [`LocalDefId`] of the overarching item whose constituents get lowered.
131    fn item_def_id(&self) -> LocalDefId;
132
133    /// Returns the region to use when a lifetime is omitted (and not elided).
134    fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx>;
135
136    /// Returns the type to use when a type is omitted.
137    fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
138
139    /// Returns the const to use when a const is omitted.
140    fn ct_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx>;
141
142    fn register_trait_ascription_bounds(
143        &self,
144        bounds: Vec<(ty::Clause<'tcx>, Span)>,
145        hir_id: HirId,
146        span: Span,
147    );
148
149    /// Probe bounds in scope where the bounded type coincides with the given type parameter.
150    ///
151    /// Rephrased, this returns bounds of the form `T: Trait`, where `T` is a type parameter
152    /// with the given `def_id`. This is a subset of the full set of bounds.
153    ///
154    /// This method may use the given `assoc_name` to disregard bounds whose trait reference
155    /// doesn't define an associated item with the provided name.
156    ///
157    /// This is used for one specific purpose: Resolving “short-hand” associated type references
158    /// like `T::Item` where `T` is a type parameter. In principle, we would do that by first
159    /// getting the full set of predicates in scope and then filtering down to find those that
160    /// apply to `T`, but this can lead to cycle errors. The problem is that we have to do this
161    /// resolution *in order to create the predicates in the first place*.
162    /// Hence, we have this “special pass”.
163    fn probe_ty_param_bounds(
164        &self,
165        span: Span,
166        def_id: LocalDefId,
167        assoc_ident: Ident,
168    ) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]>;
169
170    fn select_inherent_assoc_candidates(
171        &self,
172        span: Span,
173        self_ty: Ty<'tcx>,
174        candidates: Vec<InherentAssocCandidate>,
175    ) -> (Vec<InherentAssocCandidate>, Vec<FulfillmentError<'tcx>>);
176
177    /// Lower a path to an associated item (of a trait) to a projection.
178    ///
179    /// This method has to be defined by the concrete lowering context because
180    /// dealing with higher-ranked trait references depends on its capabilities:
181    ///
182    /// If the context can make use of type inference, it can simply instantiate
183    /// any late-bound vars bound by the trait reference with inference variables.
184    /// If it doesn't support type inference, there is nothing reasonable it can
185    /// do except reject the associated type.
186    ///
187    /// The canonical example of this is associated type `T::P` where `T` is a type
188    /// param constrained by `T: for<'a> Trait<'a>` and where `Trait` defines `P`.
189    fn lower_assoc_item_path(
190        &self,
191        span: Span,
192        item_def_id: DefId,
193        item_segment: &hir::PathSegment<'tcx>,
194        poly_trait_ref: ty::PolyTraitRef<'tcx>,
195    ) -> Result<(DefId, GenericArgsRef<'tcx>), ErrorGuaranteed>;
196
197    fn lower_fn_sig(
198        &self,
199        decl: &hir::FnDecl<'tcx>,
200        generics: Option<&hir::Generics<'_>>,
201        hir_id: HirId,
202        hir_ty: Option<&hir::Ty<'_>>,
203    ) -> (Vec<Ty<'tcx>>, Ty<'tcx>);
204
205    /// Returns `AdtDef` if `ty` is an ADT.
206    ///
207    /// Note that `ty` might be a alias type that needs normalization.
208    /// This used to get the enum variants in scope of the type.
209    /// For example, `Self::A` could refer to an associated type
210    /// or to an enum variant depending on the result of this function.
211    fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>>;
212
213    /// Record the lowered type of a HIR node in this context.
214    fn record_ty(&self, hir_id: HirId, ty: Ty<'tcx>, span: Span);
215
216    /// The inference context of the lowering context if applicable.
217    fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
218
219    /// Convenience method for coercing the lowering context into a trait object type.
220    ///
221    /// Most lowering routines are defined on the trait object type directly
222    /// necessitating a coercion step from the concrete lowering context.
223    fn lowerer(&self) -> &dyn HirTyLowerer<'tcx>
224    where
225        Self: Sized,
226    {
227        self
228    }
229
230    /// Performs minimalistic dyn compat checks outside of bodies, but full within bodies.
231    /// Outside of bodies we could end up in cycles, so we delay most checks to later phases.
232    fn dyn_compatibility_violations(&self, trait_def_id: DefId) -> Vec<DynCompatibilityViolation>;
233}
234
235/// The "qualified self" of an associated item path.
236///
237/// For diagnostic purposes only.
238enum AssocItemQSelf {
239    Trait(DefId),
240    TyParam(LocalDefId, Span),
241    SelfTyAlias,
242}
243
244impl AssocItemQSelf {
245    fn to_string(&self, tcx: TyCtxt<'_>) -> String {
246        match *self {
247            Self::Trait(def_id) => tcx.def_path_str(def_id),
248            Self::TyParam(def_id, _) => tcx.hir_ty_param_name(def_id).to_string(),
249            Self::SelfTyAlias => kw::SelfUpper.to_string(),
250        }
251    }
252}
253
254#[derive(#[automatically_derived]
impl ::core::fmt::Debug for LowerTypeRelativePathMode {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            LowerTypeRelativePathMode::Type(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Type",
                    &__self_0),
            LowerTypeRelativePathMode::Const =>
                ::core::fmt::Formatter::write_str(f, "Const"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for LowerTypeRelativePathMode {
    #[inline]
    fn clone(&self) -> LowerTypeRelativePathMode {
        let _: ::core::clone::AssertParamIsClone<PermitVariants>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LowerTypeRelativePathMode { }Copy)]
255enum LowerTypeRelativePathMode {
256    Type(PermitVariants),
257    Const,
258}
259
260impl LowerTypeRelativePathMode {
261    fn assoc_tag(self) -> ty::AssocTag {
262        match self {
263            Self::Type(_) => ty::AssocTag::Type,
264            Self::Const => ty::AssocTag::Const,
265        }
266    }
267
268    fn def_kind(self) -> DefKind {
269        match self {
270            Self::Type(_) => DefKind::AssocTy,
271            Self::Const => DefKind::AssocConst,
272        }
273    }
274
275    fn permit_variants(self) -> PermitVariants {
276        match self {
277            Self::Type(permit_variants) => permit_variants,
278            // FIXME(mgca): Support paths like `Option::<T>::None` or `Option::<T>::Some` which
279            // resolve to const ctors/fn items respectively.
280            Self::Const => PermitVariants::No,
281        }
282    }
283}
284
285/// Whether to permit a path to resolve to an enum variant.
286#[derive(#[automatically_derived]
impl ::core::fmt::Debug for PermitVariants {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                PermitVariants::Yes => "Yes",
                PermitVariants::No => "No",
            })
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for PermitVariants {
    #[inline]
    fn clone(&self) -> PermitVariants { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PermitVariants { }Copy)]
287pub enum PermitVariants {
288    Yes,
289    No,
290}
291
292#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for TypeRelativePath<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            TypeRelativePath::AssocItem(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f,
                    "AssocItem", __self_0, &__self_1),
            TypeRelativePath::Variant { adt: __self_0, variant_did: __self_1 }
                =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "Variant", "adt", __self_0, "variant_did", &__self_1),
            TypeRelativePath::Ctor { ctor_def_id: __self_0, args: __self_1 }
                =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f, "Ctor",
                    "ctor_def_id", __self_0, "args", &__self_1),
        }
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for TypeRelativePath<'tcx> {
    #[inline]
    fn clone(&self) -> TypeRelativePath<'tcx> {
        let _: ::core::clone::AssertParamIsClone<DefId>;
        let _: ::core::clone::AssertParamIsClone<GenericArgsRef<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<GenericArgsRef<'tcx>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for TypeRelativePath<'tcx> { }Copy)]
293enum TypeRelativePath<'tcx> {
294    AssocItem(DefId, GenericArgsRef<'tcx>),
295    Variant { adt: Ty<'tcx>, variant_did: DefId },
296    Ctor { ctor_def_id: DefId, args: GenericArgsRef<'tcx> },
297}
298
299/// New-typed boolean indicating whether explicit late-bound lifetimes
300/// are present in a set of generic arguments.
301///
302/// For example if we have some method `fn f<'a>(&'a self)` implemented
303/// for some type `T`, although `f` is generic in the lifetime `'a`, `'a`
304/// is late-bound so should not be provided explicitly. Thus, if `f` is
305/// instantiated with some generic arguments providing `'a` explicitly,
306/// we taint those arguments with `ExplicitLateBound::Yes` so that we
307/// can provide an appropriate diagnostic later.
308#[derive(#[automatically_derived]
impl ::core::marker::Copy for ExplicitLateBound { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ExplicitLateBound {
    #[inline]
    fn clone(&self) -> ExplicitLateBound { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for ExplicitLateBound {
    #[inline]
    fn eq(&self, other: &ExplicitLateBound) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for ExplicitLateBound {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                ExplicitLateBound::Yes => "Yes",
                ExplicitLateBound::No => "No",
            })
    }
}Debug)]
309pub enum ExplicitLateBound {
310    Yes,
311    No,
312}
313
314#[derive(#[automatically_derived]
impl ::core::marker::Copy for IsMethodCall { }Copy, #[automatically_derived]
impl ::core::clone::Clone for IsMethodCall {
    #[inline]
    fn clone(&self) -> IsMethodCall { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for IsMethodCall {
    #[inline]
    fn eq(&self, other: &IsMethodCall) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq)]
315pub enum IsMethodCall {
316    Yes,
317    No,
318}
319
320/// Denotes the "position" of a generic argument, indicating if it is a generic type,
321/// generic function or generic method call.
322#[derive(#[automatically_derived]
impl ::core::marker::Copy for GenericArgPosition { }Copy, #[automatically_derived]
impl ::core::clone::Clone for GenericArgPosition {
    #[inline]
    fn clone(&self) -> GenericArgPosition { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for GenericArgPosition {
    #[inline]
    fn eq(&self, other: &GenericArgPosition) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq)]
323pub(crate) enum GenericArgPosition {
324    Type,
325    Value, // e.g., functions
326    MethodCall,
327}
328
329/// Whether to allow duplicate associated iten constraints in a trait ref, e.g.
330/// `Trait<Assoc = Ty, Assoc = Ty>`. This is forbidden in `dyn Trait<...>`
331/// but allowed everywhere else.
332#[derive(#[automatically_derived]
impl ::core::clone::Clone for OverlappingAsssocItemConstraints {
    #[inline]
    fn clone(&self) -> OverlappingAsssocItemConstraints { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for OverlappingAsssocItemConstraints { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for OverlappingAsssocItemConstraints {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                OverlappingAsssocItemConstraints::Allowed => "Allowed",
                OverlappingAsssocItemConstraints::Forbidden => "Forbidden",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for OverlappingAsssocItemConstraints {
    #[inline]
    fn eq(&self, other: &OverlappingAsssocItemConstraints) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq)]
333pub(crate) enum OverlappingAsssocItemConstraints {
334    Allowed,
335    Forbidden,
336}
337
338/// A marker denoting that the generic arguments that were
339/// provided did not match the respective generic parameters.
340#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericArgCountMismatch {
    #[inline]
    fn clone(&self) -> GenericArgCountMismatch {
        GenericArgCountMismatch {
            reported: ::core::clone::Clone::clone(&self.reported),
            invalid_args: ::core::clone::Clone::clone(&self.invalid_args),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericArgCountMismatch {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "GenericArgCountMismatch", "reported", &self.reported,
            "invalid_args", &&self.invalid_args)
    }
}Debug)]
341pub struct GenericArgCountMismatch {
342    pub reported: ErrorGuaranteed,
343    /// A list of indices of arguments provided that were not valid.
344    pub invalid_args: Vec<usize>,
345}
346
347/// Decorates the result of a generic argument count mismatch
348/// check with whether explicit late bounds were provided.
349#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericArgCountResult {
    #[inline]
    fn clone(&self) -> GenericArgCountResult {
        GenericArgCountResult {
            explicit_late_bound: ::core::clone::Clone::clone(&self.explicit_late_bound),
            correct: ::core::clone::Clone::clone(&self.correct),
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericArgCountResult {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "GenericArgCountResult", "explicit_late_bound",
            &self.explicit_late_bound, "correct", &&self.correct)
    }
}Debug)]
350pub struct GenericArgCountResult {
351    pub explicit_late_bound: ExplicitLateBound,
352    pub correct: Result<(), GenericArgCountMismatch>,
353}
354
355/// A context which can lower HIR's [`GenericArg`] to `rustc_middle`'s [`ty::GenericArg`].
356///
357/// Its only consumer is [`generics::lower_generic_args`].
358/// Read its documentation to learn more.
359pub trait GenericArgsLowerer<'a, 'tcx> {
360    fn args_for_def_id(&mut self, def_id: DefId) -> (Option<&'a GenericArgs<'tcx>>, bool);
361
362    fn provided_kind(
363        &mut self,
364        preceding_args: &[ty::GenericArg<'tcx>],
365        param: &ty::GenericParamDef,
366        arg: &GenericArg<'tcx>,
367    ) -> ty::GenericArg<'tcx>;
368
369    fn inferred_kind(
370        &mut self,
371        preceding_args: &[ty::GenericArg<'tcx>],
372        param: &ty::GenericParamDef,
373        infer_args: bool,
374    ) -> ty::GenericArg<'tcx>;
375}
376
377struct ForbidMCGParamUsesFolder<'tcx> {
378    tcx: TyCtxt<'tcx>,
379    anon_const_def_id: LocalDefId,
380    span: Span,
381    is_self_alias: bool,
382}
383
384impl<'tcx> ForbidMCGParamUsesFolder<'tcx> {
385    fn error(&self) -> ErrorGuaranteed {
386        let msg = if self.is_self_alias {
387            "generic `Self` types are currently not permitted in anonymous constants"
388        } else if self.tcx.features().opaque_generic_const_args() {
389            "generic parameters in const blocks are only allowed as the direct value of a `type const`"
390        } else {
391            "generic parameters may not be used in const operations"
392        };
393        let mut diag = self.tcx.dcx().struct_span_err(self.span, msg);
394        if self.is_self_alias {
395            let anon_const_hir_id: HirId = HirId::make_owner(self.anon_const_def_id);
396            let parent_impl = self.tcx.hir_parent_owner_iter(anon_const_hir_id).find_map(
397                |(_, node)| match node {
398                    hir::OwnerNode::Item(hir::Item {
399                        kind: hir::ItemKind::Impl(impl_), ..
400                    }) => Some(impl_),
401                    _ => None,
402                },
403            );
404            if let Some(impl_) = parent_impl {
405                diag.span_note(impl_.self_ty.span, "not a concrete type");
406            }
407        }
408        if self.tcx.features().min_generic_const_args() {
409            if !self.tcx.features().opaque_generic_const_args() {
410                diag.help("add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items");
411            } else {
412                diag.help("consider factoring the expression into a `type const` item and use it as the const argument instead");
413            }
414        };
415        diag.emit()
416    }
417}
418
419impl<'tcx> ty::TypeFolder<TyCtxt<'tcx>> for ForbidMCGParamUsesFolder<'tcx> {
420    fn cx(&self) -> TyCtxt<'tcx> {
421        self.tcx
422    }
423
424    fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
425        if #[allow(non_exhaustive_omitted_patterns)] match t.kind() {
    ty::Param(..) => true,
    _ => false,
}matches!(t.kind(), ty::Param(..)) {
426            return Ty::new_error(self.tcx, self.error());
427        }
428        t.super_fold_with(self)
429    }
430
431    fn fold_const(&mut self, c: Const<'tcx>) -> Const<'tcx> {
432        if #[allow(non_exhaustive_omitted_patterns)] match c.kind() {
    ty::ConstKind::Param(..) => true,
    _ => false,
}matches!(c.kind(), ty::ConstKind::Param(..)) {
433            return Const::new_error(self.tcx, self.error());
434        }
435        c.super_fold_with(self)
436    }
437
438    fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
439        if #[allow(non_exhaustive_omitted_patterns)] match r.kind() {
    ty::RegionKind::ReEarlyParam(..) | ty::RegionKind::ReLateParam(..) =>
        true,
    _ => false,
}matches!(r.kind(), ty::RegionKind::ReEarlyParam(..) | ty::RegionKind::ReLateParam(..)) {
440            return ty::Region::new_error(self.tcx, self.error());
441        }
442        r
443    }
444}
445
446impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
447    /// See `check_param_uses_if_mcg`.
448    ///
449    /// FIXME(mgca): this is pub only for instantiate_value_path and would be nice to avoid altogether
450    pub fn check_param_res_if_mcg_for_instantiate_value_path(
451        &self,
452        res: Res,
453        span: Span,
454    ) -> Result<(), ErrorGuaranteed> {
455        let tcx = self.tcx();
456        let parent_def_id = self.item_def_id();
457        if let Res::Def(DefKind::ConstParam, _) = res
458            && tcx.def_kind(parent_def_id) == DefKind::AnonConst
459            && let ty::AnonConstKind::MCG = tcx.anon_const_kind(parent_def_id)
460        {
461            let folder = ForbidMCGParamUsesFolder {
462                tcx,
463                anon_const_def_id: parent_def_id,
464                span,
465                is_self_alias: false,
466            };
467            return Err(folder.error());
468        }
469        Ok(())
470    }
471
472    /// Check for uses of generic parameters that are not in scope due to this being
473    /// in a non-generic anon const context.
474    #[must_use = "need to use transformed output"]
475    fn check_param_uses_if_mcg<T>(&self, term: T, span: Span, is_self_alias: bool) -> T
476    where
477        T: ty::TypeFoldable<TyCtxt<'tcx>>,
478    {
479        let tcx = self.tcx();
480        let parent_def_id = self.item_def_id();
481        if tcx.def_kind(parent_def_id) == DefKind::AnonConst
482            && let ty::AnonConstKind::MCG = tcx.anon_const_kind(parent_def_id)
483            // Fast path if contains no params/escaping bound vars.
484            && (term.has_param() || term.has_escaping_bound_vars())
485        {
486            let mut folder = ForbidMCGParamUsesFolder {
487                tcx,
488                anon_const_def_id: parent_def_id,
489                span,
490                is_self_alias,
491            };
492            term.fold_with(&mut folder)
493        } else {
494            term
495        }
496    }
497
498    /// Lower a lifetime from the HIR to our internal notion of a lifetime called a *region*.
499    x;#[instrument(level = "debug", skip(self), ret)]
500    pub fn lower_lifetime(
501        &self,
502        lifetime: &hir::Lifetime,
503        reason: RegionInferReason<'_>,
504    ) -> ty::Region<'tcx> {
505        if let Some(resolved) = self.tcx().named_bound_var(lifetime.hir_id) {
506            let region = self.lower_resolved_lifetime(resolved);
507            self.check_param_uses_if_mcg(region, lifetime.ident.span, false)
508        } else {
509            self.re_infer(lifetime.ident.span, reason)
510        }
511    }
512
513    /// Lower a lifetime from the HIR to our internal notion of a lifetime called a *region*.
514    x;#[instrument(level = "debug", skip(self), ret)]
515    fn lower_resolved_lifetime(&self, resolved: rbv::ResolvedArg) -> ty::Region<'tcx> {
516        let tcx = self.tcx();
517
518        match resolved {
519            rbv::ResolvedArg::StaticLifetime => tcx.lifetimes.re_static,
520
521            rbv::ResolvedArg::LateBound(debruijn, index, def_id) => {
522                let br = ty::BoundRegion {
523                    var: ty::BoundVar::from_u32(index),
524                    kind: ty::BoundRegionKind::Named(def_id.to_def_id()),
525                };
526                ty::Region::new_bound(tcx, debruijn, br)
527            }
528
529            rbv::ResolvedArg::EarlyBound(def_id) => {
530                let name = tcx.hir_ty_param_name(def_id);
531                let item_def_id = tcx.hir_ty_param_owner(def_id);
532                let generics = tcx.generics_of(item_def_id);
533                let index = generics.param_def_id_to_index[&def_id.to_def_id()];
534                ty::Region::new_early_param(tcx, ty::EarlyParamRegion { index, name })
535            }
536
537            rbv::ResolvedArg::Free(scope, id) => {
538                ty::Region::new_late_param(
539                    tcx,
540                    scope.to_def_id(),
541                    ty::LateParamRegionKind::Named(id.to_def_id()),
542                )
543
544                // (*) -- not late-bound, won't change
545            }
546
547            rbv::ResolvedArg::Error(guar) => ty::Region::new_error(tcx, guar),
548        }
549    }
550
551    pub fn lower_generic_args_of_path_segment(
552        &self,
553        span: Span,
554        def_id: DefId,
555        item_segment: &hir::PathSegment<'tcx>,
556    ) -> GenericArgsRef<'tcx> {
557        let (args, _) = self.lower_generic_args_of_path(span, def_id, &[], item_segment, None);
558        if let Some(c) = item_segment.args().constraints.first() {
559            prohibit_assoc_item_constraint(self, c, Some((def_id, item_segment, span)));
560        }
561        args
562    }
563
564    /// Lower the generic arguments provided to some path.
565    ///
566    /// If this is a trait reference, you also need to pass the self type `self_ty`.
567    /// The lowering process may involve applying defaulted type parameters.
568    ///
569    /// Associated item constraints are not handled here! They are either lowered via
570    /// `lower_assoc_item_constraint` or rejected via `prohibit_assoc_item_constraint`.
571    ///
572    /// ### Example
573    ///
574    /// ```ignore (illustrative)
575    ///    T: std::ops::Index<usize, Output = u32>
576    /// // ^1 ^^^^^^^^^^^^^^2 ^^^^3  ^^^^^^^^^^^4
577    /// ```
578    ///
579    /// 1. The `self_ty` here would refer to the type `T`.
580    /// 2. The path in question is the path to the trait `std::ops::Index`,
581    ///    which will have been resolved to a `def_id`
582    /// 3. The `generic_args` contains info on the `<...>` contents. The `usize` type
583    ///    parameters are returned in the `GenericArgsRef`
584    /// 4. Associated item constraints like `Output = u32` are contained in `generic_args.constraints`.
585    ///
586    /// Note that the type listing given here is *exactly* what the user provided.
587    ///
588    /// For (generic) associated types
589    ///
590    /// ```ignore (illustrative)
591    /// <Vec<u8> as Iterable<u8>>::Iter::<'a>
592    /// ```
593    ///
594    /// We have the parent args are the args for the parent trait:
595    /// `[Vec<u8>, u8]` and `generic_args` are the arguments for the associated
596    /// type itself: `['a]`. The returned `GenericArgsRef` concatenates these two
597    /// lists: `[Vec<u8>, u8, 'a]`.
598    x;#[instrument(level = "debug", skip(self, span), ret)]
599    fn lower_generic_args_of_path(
600        &self,
601        span: Span,
602        def_id: DefId,
603        parent_args: &[ty::GenericArg<'tcx>],
604        segment: &hir::PathSegment<'tcx>,
605        self_ty: Option<Ty<'tcx>>,
606    ) -> (GenericArgsRef<'tcx>, GenericArgCountResult) {
607        // If the type is parameterized by this region, then replace this
608        // region with the current anon region binding (in other words,
609        // whatever & would get replaced with).
610
611        let tcx = self.tcx();
612        let generics = tcx.generics_of(def_id);
613        debug!(?generics);
614
615        if generics.has_self {
616            if generics.parent.is_some() {
617                // The parent is a trait so it should have at least one
618                // generic parameter for the `Self` type.
619                assert!(!parent_args.is_empty())
620            } else {
621                // This item (presumably a trait) needs a self-type.
622                assert!(self_ty.is_some());
623            }
624        } else {
625            assert!(self_ty.is_none());
626        }
627
628        let arg_count = check_generic_arg_count(
629            self,
630            def_id,
631            segment,
632            generics,
633            GenericArgPosition::Type,
634            self_ty.is_some(),
635        );
636
637        // Skip processing if type has no generic parameters.
638        // Traits always have `Self` as a generic parameter, which means they will not return early
639        // here and so associated item constraints will be handled regardless of whether there are
640        // any non-`Self` generic parameters.
641        if generics.is_own_empty() {
642            return (tcx.mk_args(parent_args), arg_count);
643        }
644
645        struct GenericArgsCtxt<'a, 'tcx> {
646            lowerer: &'a dyn HirTyLowerer<'tcx>,
647            def_id: DefId,
648            generic_args: &'a GenericArgs<'tcx>,
649            span: Span,
650            infer_args: bool,
651            incorrect_args: &'a Result<(), GenericArgCountMismatch>,
652        }
653
654        impl<'a, 'tcx> GenericArgsLowerer<'a, 'tcx> for GenericArgsCtxt<'a, 'tcx> {
655            fn args_for_def_id(&mut self, did: DefId) -> (Option<&'a GenericArgs<'tcx>>, bool) {
656                if did == self.def_id {
657                    (Some(self.generic_args), self.infer_args)
658                } else {
659                    // The last component of this tuple is unimportant.
660                    (None, false)
661                }
662            }
663
664            fn provided_kind(
665                &mut self,
666                preceding_args: &[ty::GenericArg<'tcx>],
667                param: &ty::GenericParamDef,
668                arg: &GenericArg<'tcx>,
669            ) -> ty::GenericArg<'tcx> {
670                let tcx = self.lowerer.tcx();
671
672                if let Err(incorrect) = self.incorrect_args {
673                    if incorrect.invalid_args.contains(&(param.index as usize)) {
674                        return param.to_error(tcx);
675                    }
676                }
677
678                let handle_ty_args = |has_default, ty: &hir::Ty<'tcx>| {
679                    if has_default {
680                        tcx.check_optional_stability(
681                            param.def_id,
682                            Some(arg.hir_id()),
683                            arg.span(),
684                            None,
685                            AllowUnstable::No,
686                            |_, _| {
687                                // Default generic parameters may not be marked
688                                // with stability attributes, i.e. when the
689                                // default parameter was defined at the same time
690                                // as the rest of the type. As such, we ignore missing
691                                // stability attributes.
692                            },
693                        );
694                    }
695                    self.lowerer.lower_ty(ty).into()
696                };
697
698                match (&param.kind, arg) {
699                    (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
700                        self.lowerer.lower_lifetime(lt, RegionInferReason::Param(param)).into()
701                    }
702                    (&GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => {
703                        // We handle the other parts of `Ty` in the match arm below
704                        handle_ty_args(has_default, ty.as_unambig_ty())
705                    }
706                    (&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
707                        handle_ty_args(has_default, &inf.to_ty())
708                    }
709                    (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => self
710                        .lowerer
711                        // Ambig portions of `ConstArg` are handled in the match arm below
712                        .lower_const_arg(
713                            ct.as_unambig_ct(),
714                            tcx.type_of(param.def_id).instantiate(tcx, preceding_args),
715                        )
716                        .into(),
717                    (&GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => {
718                        self.lowerer.ct_infer(Some(param), inf.span).into()
719                    }
720                    (kind, arg) => span_bug!(
721                        self.span,
722                        "mismatched path argument for kind {kind:?}: found arg {arg:?}"
723                    ),
724                }
725            }
726
727            fn inferred_kind(
728                &mut self,
729                preceding_args: &[ty::GenericArg<'tcx>],
730                param: &ty::GenericParamDef,
731                infer_args: bool,
732            ) -> ty::GenericArg<'tcx> {
733                let tcx = self.lowerer.tcx();
734
735                if let Err(incorrect) = self.incorrect_args {
736                    if incorrect.invalid_args.contains(&(param.index as usize)) {
737                        return param.to_error(tcx);
738                    }
739                }
740                match param.kind {
741                    GenericParamDefKind::Lifetime => {
742                        self.lowerer.re_infer(self.span, RegionInferReason::Param(param)).into()
743                    }
744                    GenericParamDefKind::Type { has_default, .. } => {
745                        if !infer_args && has_default {
746                            // No type parameter provided, but a default exists.
747                            if let Some(prev) =
748                                preceding_args.iter().find_map(|arg| match arg.kind() {
749                                    GenericArgKind::Type(ty) => ty.error_reported().err(),
750                                    _ => None,
751                                })
752                            {
753                                // Avoid ICE #86756 when type error recovery goes awry.
754                                return Ty::new_error(tcx, prev).into();
755                            }
756                            tcx.at(self.span)
757                                .type_of(param.def_id)
758                                .instantiate(tcx, preceding_args)
759                                .into()
760                        } else if infer_args {
761                            self.lowerer.ty_infer(Some(param), self.span).into()
762                        } else {
763                            // We've already errored above about the mismatch.
764                            Ty::new_misc_error(tcx).into()
765                        }
766                    }
767                    GenericParamDefKind::Const { has_default, .. } => {
768                        let ty = tcx
769                            .at(self.span)
770                            .type_of(param.def_id)
771                            .instantiate(tcx, preceding_args);
772                        if let Err(guar) = ty.error_reported() {
773                            return ty::Const::new_error(tcx, guar).into();
774                        }
775                        if !infer_args && has_default {
776                            tcx.const_param_default(param.def_id)
777                                .instantiate(tcx, preceding_args)
778                                .into()
779                        } else if infer_args {
780                            self.lowerer.ct_infer(Some(param), self.span).into()
781                        } else {
782                            // We've already errored above about the mismatch.
783                            ty::Const::new_misc_error(tcx).into()
784                        }
785                    }
786                }
787            }
788        }
789
790        let mut args_ctx = GenericArgsCtxt {
791            lowerer: self,
792            def_id,
793            span,
794            generic_args: segment.args(),
795            infer_args: segment.infer_args,
796            incorrect_args: &arg_count.correct,
797        };
798        let args = lower_generic_args(
799            self,
800            def_id,
801            parent_args,
802            self_ty.is_some(),
803            self_ty,
804            &arg_count,
805            &mut args_ctx,
806        );
807
808        (args, arg_count)
809    }
810
811    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_generic_args_of_assoc_item",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(811u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["span",
                                                    "item_def_id", "item_segment", "parent_args"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&item_def_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&item_segment)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&parent_args)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: GenericArgsRef<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let (args, _) =
                self.lower_generic_args_of_path(span, item_def_id,
                    parent_args, item_segment, None);
            if let Some(c) = item_segment.args().constraints.first() {
                prohibit_assoc_item_constraint(self, c,
                    Some((item_def_id, item_segment, span)));
            }
            args
        }
    }
}#[instrument(level = "debug", skip(self))]
812    pub fn lower_generic_args_of_assoc_item(
813        &self,
814        span: Span,
815        item_def_id: DefId,
816        item_segment: &hir::PathSegment<'tcx>,
817        parent_args: GenericArgsRef<'tcx>,
818    ) -> GenericArgsRef<'tcx> {
819        let (args, _) =
820            self.lower_generic_args_of_path(span, item_def_id, parent_args, item_segment, None);
821        if let Some(c) = item_segment.args().constraints.first() {
822            prohibit_assoc_item_constraint(self, c, Some((item_def_id, item_segment, span)));
823        }
824        args
825    }
826
827    /// Lower a trait reference as found in an impl header as the implementee.
828    ///
829    /// The self type `self_ty` is the implementer of the trait.
830    pub fn lower_impl_trait_ref(
831        &self,
832        trait_ref: &hir::TraitRef<'tcx>,
833        self_ty: Ty<'tcx>,
834    ) -> ty::TraitRef<'tcx> {
835        let [leading_segments @ .., segment] = trait_ref.path.segments else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
836
837        let _ = self.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
838
839        self.lower_mono_trait_ref(
840            trait_ref.path.span,
841            trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise()),
842            self_ty,
843            segment,
844            true,
845        )
846    }
847
848    /// Lower a polymorphic trait reference given a self type into `bounds`.
849    ///
850    /// *Polymorphic* in the sense that it may bind late-bound vars.
851    ///
852    /// This may generate auxiliary bounds iff the trait reference contains associated item constraints.
853    ///
854    /// ### Example
855    ///
856    /// Given the trait ref `Iterator<Item = u32>` and the self type `Ty`, this will add the
857    ///
858    /// 1. *trait predicate* `<Ty as Iterator>` (known as `Ty: Iterator` in the surface syntax) and the
859    /// 2. *projection predicate* `<Ty as Iterator>::Item = u32`
860    ///
861    /// to `bounds`.
862    ///
863    /// ### A Note on Binders
864    ///
865    /// Against our usual convention, there is an implied binder around the `self_ty` and the
866    /// `trait_ref` here. So they may reference late-bound vars.
867    ///
868    /// If for example you had `for<'a> Foo<'a>: Bar<'a>`, then the `self_ty` would be `Foo<'a>`
869    /// where `'a` is a bound region at depth 0. Similarly, the `trait_ref` would be `Bar<'a>`.
870    /// The lowered poly-trait-ref will track this binder explicitly, however.
871    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_poly_trait_ref",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(871u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["bound_generic_params",
                                                    "constness", "polarity", "trait_ref", "span", "self_ty",
                                                    "predicate_filter", "overlapping_assoc_item_constraints"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&bound_generic_params)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&constness)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&polarity)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&trait_ref)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&predicate_filter)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&overlapping_assoc_item_constraints)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: GenericArgCountResult = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = self.tcx();
            let _ = bound_generic_params;
            let trait_def_id =
                trait_ref.trait_def_id().unwrap_or_else(||
                        FatalError.raise());
            let transient =
                match polarity {
                    hir::BoundPolarity::Positive => {
                        tcx.is_lang_item(trait_def_id, hir::LangItem::PointeeSized)
                    }
                    hir::BoundPolarity::Negative(_) => false,
                    hir::BoundPolarity::Maybe(_) => {
                        self.require_bound_to_relax_default_trait(trait_ref, span);
                        true
                    }
                };
            let bounds = if transient { &mut Vec::new() } else { bounds };
            let polarity =
                match polarity {
                    hir::BoundPolarity::Positive | hir::BoundPolarity::Maybe(_)
                        => {
                        ty::PredicatePolarity::Positive
                    }
                    hir::BoundPolarity::Negative(_) =>
                        ty::PredicatePolarity::Negative,
                };
            let [leading_segments @ .., segment] =
                trait_ref.path.segments else {
                    ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))
                };
            let _ =
                self.prohibit_generic_args(leading_segments.iter(),
                    GenericsArgsErrExtend::None);
            self.report_internal_fn_trait(span, trait_def_id, segment, false);
            let (generic_args, arg_count) =
                self.lower_generic_args_of_path(trait_ref.path.span,
                    trait_def_id, &[], segment, Some(self_ty));
            let constraints = segment.args().constraints;
            if transient &&
                    (!generic_args[1..].is_empty() || !constraints.is_empty()) {
                self.dcx().span_delayed_bug(span,
                    "transient bound should not have args or constraints");
            }
            let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:951",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(951u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["bound_vars"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&bound_vars)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let poly_trait_ref =
                ty::Binder::bind_with_vars(ty::TraitRef::new_from_args(tcx,
                        trait_def_id, generic_args), bound_vars);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:958",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(958u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["poly_trait_ref"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&poly_trait_ref)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            match predicate_filter {
                PredicateFilter::All | PredicateFilter::SelfOnly |
                    PredicateFilter::SelfTraitThatDefines(..) |
                    PredicateFilter::SelfAndAssociatedTypeBounds => {
                    let bound =
                        poly_trait_ref.map_bound(|trait_ref|
                                {
                                    ty::ClauseKind::Trait(ty::TraitPredicate {
                                            trait_ref,
                                            polarity,
                                        })
                                });
                    let bound = (bound.upcast(tcx), span);
                    if tcx.is_lang_item(trait_def_id,
                            rustc_hir::LangItem::Sized) {
                        bounds.insert(0, bound);
                    } else { bounds.push(bound); }
                }
                PredicateFilter::ConstIfConst |
                    PredicateFilter::SelfConstIfConst => {}
            }
            if let hir::BoundConstness::Always(span) |
                        hir::BoundConstness::Maybe(span) = constness &&
                    !tcx.is_const_trait(trait_def_id) {
                let (def_span, suggestion, suggestion_pre) =
                    match (trait_def_id.as_local(), tcx.sess.is_nightly_build())
                        {
                        (Some(trait_def_id), true) => {
                            let span = tcx.hir_expect_item(trait_def_id).vis_span;
                            let span =
                                tcx.sess.source_map().span_extend_while_whitespace(span);
                            (None, Some(span.shrink_to_hi()),
                                if self.tcx().features().const_trait_impl() {
                                    ""
                                } else {
                                    "enable `#![feature(const_trait_impl)]` in your crate and "
                                })
                        }
                        (None, _) | (_, false) =>
                            (Some(tcx.def_span(trait_def_id)), None, ""),
                    };
                self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
                        span,
                        modifier: constness.as_str(),
                        def_span,
                        trait_name: tcx.def_path_str(trait_def_id),
                        suggestion,
                        suggestion_pre,
                    });
            } else {
                match predicate_filter {
                    PredicateFilter::SelfTraitThatDefines(..) => {}
                    PredicateFilter::All | PredicateFilter::SelfOnly |
                        PredicateFilter::SelfAndAssociatedTypeBounds => {
                        match constness {
                            hir::BoundConstness::Always(_) => {
                                if polarity == ty::PredicatePolarity::Positive {
                                    bounds.push((poly_trait_ref.to_host_effect_clause(tcx,
                                                ty::BoundConstness::Const), span));
                                }
                            }
                            hir::BoundConstness::Maybe(_) => {}
                            hir::BoundConstness::Never => {}
                        }
                    }
                    PredicateFilter::ConstIfConst |
                        PredicateFilter::SelfConstIfConst => {
                        match constness {
                            hir::BoundConstness::Maybe(_) => {
                                if polarity == ty::PredicatePolarity::Positive {
                                    bounds.push((poly_trait_ref.to_host_effect_clause(tcx,
                                                ty::BoundConstness::Maybe), span));
                                }
                            }
                            hir::BoundConstness::Always(_) | hir::BoundConstness::Never
                                => {}
                        }
                    }
                }
            }
            let mut dup_constraints =
                (overlapping_assoc_item_constraints ==
                            OverlappingAsssocItemConstraints::Forbidden).then_some(FxIndexMap::default());
            for constraint in constraints {
                if polarity == ty::PredicatePolarity::Negative {
                    self.dcx().span_delayed_bug(constraint.span,
                        "negative trait bounds should not have assoc item constraints");
                    break;
                }
                let _: Result<_, ErrorGuaranteed> =
                    self.lower_assoc_item_constraint(trait_ref.hir_ref_id,
                        poly_trait_ref, constraint, bounds,
                        dup_constraints.as_mut(), constraint.span,
                        predicate_filter);
            }
            arg_count
        }
    }
}#[instrument(level = "debug", skip(self, bounds))]
872    pub(crate) fn lower_poly_trait_ref(
873        &self,
874        &hir::PolyTraitRef {
875            bound_generic_params,
876            modifiers: hir::TraitBoundModifiers { constness, polarity },
877            trait_ref,
878            span,
879        }: &hir::PolyTraitRef<'tcx>,
880        self_ty: Ty<'tcx>,
881        bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
882        predicate_filter: PredicateFilter,
883        overlapping_assoc_item_constraints: OverlappingAsssocItemConstraints,
884    ) -> GenericArgCountResult {
885        let tcx = self.tcx();
886
887        // We use the *resolved* bound vars later instead of the HIR ones since the former
888        // also include the bound vars of the overarching predicate if applicable.
889        let _ = bound_generic_params;
890
891        let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
892
893        // Relaxed bounds `?Trait` and `PointeeSized` bounds aren't represented in the middle::ty IR
894        // as they denote the *absence* of a default bound. However, we can't bail out early here since
895        // we still need to perform several validation steps (see below). Instead, simply "pour" all
896        // resulting bounds "down the drain", i.e., into a new `Vec` that just gets dropped at the end.
897        let transient = match polarity {
898            hir::BoundPolarity::Positive => {
899                // To elaborate on the comment directly above, regarding `PointeeSized` specifically,
900                // we don't "reify" such bounds to avoid trait system limitations -- namely,
901                // non-global where-clauses being preferred over item bounds (where `PointeeSized`
902                // bounds would be proven) -- which can result in errors when a `PointeeSized`
903                // supertrait / bound / predicate is added to some items.
904                tcx.is_lang_item(trait_def_id, hir::LangItem::PointeeSized)
905            }
906            hir::BoundPolarity::Negative(_) => false,
907            hir::BoundPolarity::Maybe(_) => {
908                self.require_bound_to_relax_default_trait(trait_ref, span);
909                true
910            }
911        };
912        let bounds = if transient { &mut Vec::new() } else { bounds };
913
914        let polarity = match polarity {
915            hir::BoundPolarity::Positive | hir::BoundPolarity::Maybe(_) => {
916                ty::PredicatePolarity::Positive
917            }
918            hir::BoundPolarity::Negative(_) => ty::PredicatePolarity::Negative,
919        };
920
921        let [leading_segments @ .., segment] = trait_ref.path.segments else { bug!() };
922
923        let _ = self.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
924        self.report_internal_fn_trait(span, trait_def_id, segment, false);
925
926        let (generic_args, arg_count) = self.lower_generic_args_of_path(
927            trait_ref.path.span,
928            trait_def_id,
929            &[],
930            segment,
931            Some(self_ty),
932        );
933
934        let constraints = segment.args().constraints;
935
936        if transient && (!generic_args[1..].is_empty() || !constraints.is_empty()) {
937            // Since the bound won't be present in the middle::ty IR as established above, any
938            // arguments or constraints won't be checked for well-formedness in later passes.
939            //
940            // This is only an issue if the trait ref is otherwise valid which can only happen if
941            // the corresponding default trait has generic parameters or associated items. Such a
942            // trait would be degenerate. We delay a bug to detect and guard us against these.
943            //
944            // E.g: Given `/*default*/ trait Bound<'a: 'static, T, const N: usize> {}`,
945            // `?Bound<Vec<str>, { panic!() }>` won't be wfchecked.
946            self.dcx()
947                .span_delayed_bug(span, "transient bound should not have args or constraints");
948        }
949
950        let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
951        debug!(?bound_vars);
952
953        let poly_trait_ref = ty::Binder::bind_with_vars(
954            ty::TraitRef::new_from_args(tcx, trait_def_id, generic_args),
955            bound_vars,
956        );
957
958        debug!(?poly_trait_ref);
959
960        // We deal with const conditions later.
961        match predicate_filter {
962            PredicateFilter::All
963            | PredicateFilter::SelfOnly
964            | PredicateFilter::SelfTraitThatDefines(..)
965            | PredicateFilter::SelfAndAssociatedTypeBounds => {
966                let bound = poly_trait_ref.map_bound(|trait_ref| {
967                    ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity })
968                });
969                let bound = (bound.upcast(tcx), span);
970                // FIXME(-Znext-solver): We can likely remove this hack once the
971                // new trait solver lands. This fixed an overflow in the old solver.
972                // This may have performance implications, so please check perf when
973                // removing it.
974                // This was added in <https://github.com/rust-lang/rust/pull/123302>.
975                if tcx.is_lang_item(trait_def_id, rustc_hir::LangItem::Sized) {
976                    bounds.insert(0, bound);
977                } else {
978                    bounds.push(bound);
979                }
980            }
981            PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
982        }
983
984        if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
985            && !tcx.is_const_trait(trait_def_id)
986        {
987            let (def_span, suggestion, suggestion_pre) =
988                match (trait_def_id.as_local(), tcx.sess.is_nightly_build()) {
989                    (Some(trait_def_id), true) => {
990                        let span = tcx.hir_expect_item(trait_def_id).vis_span;
991                        let span = tcx.sess.source_map().span_extend_while_whitespace(span);
992
993                        (
994                            None,
995                            Some(span.shrink_to_hi()),
996                            if self.tcx().features().const_trait_impl() {
997                                ""
998                            } else {
999                                "enable `#![feature(const_trait_impl)]` in your crate and "
1000                            },
1001                        )
1002                    }
1003                    (None, _) | (_, false) => (Some(tcx.def_span(trait_def_id)), None, ""),
1004                };
1005            self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
1006                span,
1007                modifier: constness.as_str(),
1008                def_span,
1009                trait_name: tcx.def_path_str(trait_def_id),
1010                suggestion,
1011                suggestion_pre,
1012            });
1013        } else {
1014            match predicate_filter {
1015                // This is only concerned with trait predicates.
1016                PredicateFilter::SelfTraitThatDefines(..) => {}
1017                PredicateFilter::All
1018                | PredicateFilter::SelfOnly
1019                | PredicateFilter::SelfAndAssociatedTypeBounds => {
1020                    match constness {
1021                        hir::BoundConstness::Always(_) => {
1022                            if polarity == ty::PredicatePolarity::Positive {
1023                                bounds.push((
1024                                    poly_trait_ref
1025                                        .to_host_effect_clause(tcx, ty::BoundConstness::Const),
1026                                    span,
1027                                ));
1028                            }
1029                        }
1030                        hir::BoundConstness::Maybe(_) => {
1031                            // We don't emit a const bound here, since that would mean that we
1032                            // unconditionally need to prove a `HostEffect` predicate, even when
1033                            // the predicates are being instantiated in a non-const context. This
1034                            // is instead handled in the `const_conditions` query.
1035                        }
1036                        hir::BoundConstness::Never => {}
1037                    }
1038                }
1039                // On the flip side, when filtering `ConstIfConst` bounds, we only need to convert
1040                // `[const]` bounds. All other predicates are handled in their respective queries.
1041                //
1042                // Note that like `PredicateFilter::SelfOnly`, we don't need to do any filtering
1043                // here because we only call this on self bounds, and deal with the recursive case
1044                // in `lower_assoc_item_constraint`.
1045                PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {
1046                    match constness {
1047                        hir::BoundConstness::Maybe(_) => {
1048                            if polarity == ty::PredicatePolarity::Positive {
1049                                bounds.push((
1050                                    poly_trait_ref
1051                                        .to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1052                                    span,
1053                                ));
1054                            }
1055                        }
1056                        hir::BoundConstness::Always(_) | hir::BoundConstness::Never => {}
1057                    }
1058                }
1059            }
1060        }
1061
1062        let mut dup_constraints = (overlapping_assoc_item_constraints
1063            == OverlappingAsssocItemConstraints::Forbidden)
1064            .then_some(FxIndexMap::default());
1065
1066        for constraint in constraints {
1067            // Don't register any associated item constraints for negative bounds,
1068            // since we should have emitted an error for them earlier, and they
1069            // would not be well-formed!
1070            if polarity == ty::PredicatePolarity::Negative {
1071                self.dcx().span_delayed_bug(
1072                    constraint.span,
1073                    "negative trait bounds should not have assoc item constraints",
1074                );
1075                break;
1076            }
1077
1078            // Specify type to assert that error was already reported in `Err` case.
1079            let _: Result<_, ErrorGuaranteed> = self.lower_assoc_item_constraint(
1080                trait_ref.hir_ref_id,
1081                poly_trait_ref,
1082                constraint,
1083                bounds,
1084                dup_constraints.as_mut(),
1085                constraint.span,
1086                predicate_filter,
1087            );
1088            // Okay to ignore `Err` because of `ErrorGuaranteed` (see above).
1089        }
1090
1091        arg_count
1092    }
1093
1094    /// Lower a monomorphic trait reference given a self type while prohibiting associated item bindings.
1095    ///
1096    /// *Monomorphic* in the sense that it doesn't bind any late-bound vars.
1097    fn lower_mono_trait_ref(
1098        &self,
1099        span: Span,
1100        trait_def_id: DefId,
1101        self_ty: Ty<'tcx>,
1102        trait_segment: &hir::PathSegment<'tcx>,
1103        is_impl: bool,
1104    ) -> ty::TraitRef<'tcx> {
1105        self.report_internal_fn_trait(span, trait_def_id, trait_segment, is_impl);
1106
1107        let (generic_args, _) =
1108            self.lower_generic_args_of_path(span, trait_def_id, &[], trait_segment, Some(self_ty));
1109        if let Some(c) = trait_segment.args().constraints.first() {
1110            prohibit_assoc_item_constraint(self, c, Some((trait_def_id, trait_segment, span)));
1111        }
1112        ty::TraitRef::new_from_args(self.tcx(), trait_def_id, generic_args)
1113    }
1114
1115    fn probe_trait_that_defines_assoc_item(
1116        &self,
1117        trait_def_id: DefId,
1118        assoc_tag: ty::AssocTag,
1119        assoc_ident: Ident,
1120    ) -> bool {
1121        self.tcx()
1122            .associated_items(trait_def_id)
1123            .find_by_ident_and_kind(self.tcx(), assoc_ident, assoc_tag, trait_def_id)
1124            .is_some()
1125    }
1126
1127    fn lower_path_segment(
1128        &self,
1129        span: Span,
1130        did: DefId,
1131        item_segment: &hir::PathSegment<'tcx>,
1132    ) -> Ty<'tcx> {
1133        let tcx = self.tcx();
1134        let args = self.lower_generic_args_of_path_segment(span, did, item_segment);
1135
1136        if let DefKind::TyAlias = tcx.def_kind(did)
1137            && tcx.type_alias_is_lazy(did)
1138        {
1139            // Type aliases defined in crates that have the
1140            // feature `lazy_type_alias` enabled get encoded as a type alias that normalization will
1141            // then actually instantiate the where bounds of.
1142            let alias_ty = ty::AliasTy::new_from_args(tcx, did, args);
1143            Ty::new_alias(tcx, ty::Free, alias_ty)
1144        } else {
1145            tcx.at(span).type_of(did).instantiate(tcx, args)
1146        }
1147    }
1148
1149    /// Search for a trait bound on a type parameter whose trait defines the associated item
1150    /// given by `assoc_ident` and `kind`.
1151    ///
1152    /// This fails if there is no such bound in the list of candidates or if there are multiple
1153    /// candidates in which case it reports ambiguity.
1154    ///
1155    /// `ty_param_def_id` is the `LocalDefId` of the type parameter.
1156    x;#[instrument(level = "debug", skip_all, ret)]
1157    fn probe_single_ty_param_bound_for_assoc_item(
1158        &self,
1159        ty_param_def_id: LocalDefId,
1160        ty_param_span: Span,
1161        assoc_tag: ty::AssocTag,
1162        assoc_ident: Ident,
1163        span: Span,
1164    ) -> Result<ty::PolyTraitRef<'tcx>, ErrorGuaranteed> {
1165        debug!(?ty_param_def_id, ?assoc_ident, ?span);
1166        let tcx = self.tcx();
1167
1168        let predicates = &self.probe_ty_param_bounds(span, ty_param_def_id, assoc_ident);
1169        debug!("predicates={:#?}", predicates);
1170
1171        self.probe_single_bound_for_assoc_item(
1172            || {
1173                let trait_refs = predicates
1174                    .iter_identity_copied()
1175                    .filter_map(|(p, _)| Some(p.as_trait_clause()?.map_bound(|t| t.trait_ref)));
1176                traits::transitive_bounds_that_define_assoc_item(tcx, trait_refs, assoc_ident)
1177            },
1178            AssocItemQSelf::TyParam(ty_param_def_id, ty_param_span),
1179            assoc_tag,
1180            assoc_ident,
1181            span,
1182            None,
1183        )
1184    }
1185
1186    /// Search for a single trait bound whose trait defines the associated item given by
1187    /// `assoc_ident`.
1188    ///
1189    /// This fails if there is no such bound in the list of candidates or if there are multiple
1190    /// candidates in which case it reports ambiguity.
1191    x;#[instrument(level = "debug", skip(self, all_candidates, qself, constraint), ret)]
1192    fn probe_single_bound_for_assoc_item<I>(
1193        &self,
1194        all_candidates: impl Fn() -> I,
1195        qself: AssocItemQSelf,
1196        assoc_tag: ty::AssocTag,
1197        assoc_ident: Ident,
1198        span: Span,
1199        constraint: Option<&hir::AssocItemConstraint<'tcx>>,
1200    ) -> Result<ty::PolyTraitRef<'tcx>, ErrorGuaranteed>
1201    where
1202        I: Iterator<Item = ty::PolyTraitRef<'tcx>>,
1203    {
1204        let tcx = self.tcx();
1205
1206        let mut matching_candidates = all_candidates().filter(|r| {
1207            self.probe_trait_that_defines_assoc_item(r.def_id(), assoc_tag, assoc_ident)
1208        });
1209
1210        let Some(bound) = matching_candidates.next() else {
1211            return Err(self.report_unresolved_assoc_item(
1212                all_candidates,
1213                qself,
1214                assoc_tag,
1215                assoc_ident,
1216                span,
1217                constraint,
1218            ));
1219        };
1220        debug!(?bound);
1221
1222        if let Some(bound2) = matching_candidates.next() {
1223            debug!(?bound2);
1224
1225            let assoc_kind_str = errors::assoc_tag_str(assoc_tag);
1226            let qself_str = qself.to_string(tcx);
1227            let mut err = self.dcx().create_err(crate::errors::AmbiguousAssocItem {
1228                span,
1229                assoc_kind: assoc_kind_str,
1230                assoc_ident,
1231                qself: &qself_str,
1232            });
1233            // Provide a more specific error code index entry for equality bindings.
1234            err.code(
1235                if let Some(constraint) = constraint
1236                    && let hir::AssocItemConstraintKind::Equality { .. } = constraint.kind
1237                {
1238                    E0222
1239                } else {
1240                    E0221
1241                },
1242            );
1243
1244            // FIXME(#97583): Print associated item bindings properly (i.e., not as equality
1245            // predicates!).
1246            // FIXME: Turn this into a structured, translatable & more actionable suggestion.
1247            let mut where_bounds = vec![];
1248            for bound in [bound, bound2].into_iter().chain(matching_candidates) {
1249                let bound_id = bound.def_id();
1250                let assoc_item = tcx.associated_items(bound_id).find_by_ident_and_kind(
1251                    tcx,
1252                    assoc_ident,
1253                    assoc_tag,
1254                    bound_id,
1255                );
1256                let bound_span = assoc_item.and_then(|item| tcx.hir_span_if_local(item.def_id));
1257
1258                if let Some(bound_span) = bound_span {
1259                    err.span_label(
1260                        bound_span,
1261                        format!("ambiguous `{assoc_ident}` from `{}`", bound.print_trait_sugared(),),
1262                    );
1263                    if let Some(constraint) = constraint {
1264                        match constraint.kind {
1265                            hir::AssocItemConstraintKind::Equality { term } => {
1266                                let term: ty::Term<'_> = match term {
1267                                    hir::Term::Ty(ty) => self.lower_ty(ty).into(),
1268                                    hir::Term::Const(ct) => {
1269                                        let assoc_item =
1270                                            assoc_item.expect("assoc_item should be present");
1271                                        let projection_term = bound.map_bound(|trait_ref| {
1272                                            let item_segment = hir::PathSegment {
1273                                                ident: constraint.ident,
1274                                                hir_id: constraint.hir_id,
1275                                                res: Res::Err,
1276                                                args: Some(constraint.gen_args),
1277                                                infer_args: false,
1278                                            };
1279
1280                                            let alias_args = self.lower_generic_args_of_assoc_item(
1281                                                constraint.ident.span,
1282                                                assoc_item.def_id,
1283                                                &item_segment,
1284                                                trait_ref.args,
1285                                            );
1286                                            ty::AliasTerm::new_from_args(
1287                                                tcx,
1288                                                assoc_item.def_id,
1289                                                alias_args,
1290                                            )
1291                                        });
1292
1293                                        // FIXME(mgca): code duplication with other places we lower
1294                                        // the rhs' of associated const bindings
1295                                        let ty = projection_term.map_bound(|alias| {
1296                                            tcx.type_of(alias.def_id).instantiate(tcx, alias.args)
1297                                        });
1298                                        let ty = bounds::check_assoc_const_binding_type(
1299                                            self,
1300                                            constraint.ident,
1301                                            ty,
1302                                            constraint.hir_id,
1303                                        );
1304
1305                                        self.lower_const_arg(ct, ty).into()
1306                                    }
1307                                };
1308                                if term.references_error() {
1309                                    continue;
1310                                }
1311                                // FIXME(#97583): This isn't syntactically well-formed!
1312                                where_bounds.push(format!(
1313                                    "        T: {trait}::{assoc_ident} = {term}",
1314                                    trait = bound.print_only_trait_path(),
1315                                ));
1316                            }
1317                            // FIXME: Provide a suggestion.
1318                            hir::AssocItemConstraintKind::Bound { bounds: _ } => {}
1319                        }
1320                    } else {
1321                        err.span_suggestion_verbose(
1322                            span.with_hi(assoc_ident.span.lo()),
1323                            "use fully-qualified syntax to disambiguate",
1324                            format!("<{qself_str} as {}>::", bound.print_only_trait_path()),
1325                            Applicability::MaybeIncorrect,
1326                        );
1327                    }
1328                } else {
1329                    let trait_ =
1330                        tcx.short_string(bound.print_only_trait_path(), err.long_ty_path());
1331                    err.note(format!(
1332                        "associated {assoc_kind_str} `{assoc_ident}` could derive from `{trait_}`",
1333                    ));
1334                }
1335            }
1336            if !where_bounds.is_empty() {
1337                err.help(format!(
1338                    "consider introducing a new type parameter `T` and adding `where` constraints:\
1339                     \n    where\n        T: {qself_str},\n{}",
1340                    where_bounds.join(",\n"),
1341                ));
1342                let reported = err.emit();
1343                return Err(reported);
1344            }
1345            err.emit();
1346        }
1347
1348        Ok(bound)
1349    }
1350
1351    /// Lower a [type-relative](hir::QPath::TypeRelative) path in type position to a type.
1352    ///
1353    /// If the path refers to an enum variant and `permit_variants` holds,
1354    /// the returned type is simply the provided self type `qself_ty`.
1355    ///
1356    /// A path like `A::B::C::D` is understood as `<A::B::C>::D`. I.e.,
1357    /// `qself_ty` / `qself` is `A::B::C` and `assoc_segment` is `D`.
1358    /// We return the lowered type and the `DefId` for the whole path.
1359    ///
1360    /// We only support associated type paths whose self type is a type parameter or a `Self`
1361    /// type alias (in a trait impl) like `T::Ty` (where `T` is a ty param) or `Self::Ty`.
1362    /// We **don't** support paths whose self type is an arbitrary type like `Struct::Ty` where
1363    /// struct `Struct` impls an in-scope trait that defines an associated type called `Ty`.
1364    /// For the latter case, we report ambiguity.
1365    /// While desirable to support, the implementation would be non-trivial. Tracked in [#22519].
1366    ///
1367    /// At the time of writing, *inherent associated types* are also resolved here. This however
1368    /// is [problematic][iat]. A proper implementation would be as non-trivial as the one
1369    /// described in the previous paragraph and their modeling of projections would likely be
1370    /// very similar in nature.
1371    ///
1372    /// [#22519]: https://github.com/rust-lang/rust/issues/22519
1373    /// [iat]: https://github.com/rust-lang/rust/issues/8995#issuecomment-1569208403
1374    //
1375    // NOTE: When this function starts resolving `Trait::AssocTy` successfully
1376    // it should also start reporting the `BARE_TRAIT_OBJECTS` lint.
1377    x;#[instrument(level = "debug", skip_all, ret)]
1378    pub fn lower_type_relative_ty_path(
1379        &self,
1380        self_ty: Ty<'tcx>,
1381        hir_self_ty: &'tcx hir::Ty<'tcx>,
1382        segment: &'tcx hir::PathSegment<'tcx>,
1383        qpath_hir_id: HirId,
1384        span: Span,
1385        permit_variants: PermitVariants,
1386    ) -> Result<(Ty<'tcx>, DefKind, DefId), ErrorGuaranteed> {
1387        let tcx = self.tcx();
1388        match self.lower_type_relative_path(
1389            self_ty,
1390            hir_self_ty,
1391            segment,
1392            qpath_hir_id,
1393            span,
1394            LowerTypeRelativePathMode::Type(permit_variants),
1395        )? {
1396            TypeRelativePath::AssocItem(def_id, args) => {
1397                let alias_ty = ty::AliasTy::new_from_args(tcx, def_id, args);
1398                let ty = Ty::new_alias(tcx, alias_ty.kind(tcx), alias_ty);
1399                let ty = self.check_param_uses_if_mcg(ty, span, false);
1400                Ok((ty, tcx.def_kind(def_id), def_id))
1401            }
1402            TypeRelativePath::Variant { adt, variant_did } => {
1403                let adt = self.check_param_uses_if_mcg(adt, span, false);
1404                Ok((adt, DefKind::Variant, variant_did))
1405            }
1406            TypeRelativePath::Ctor { .. } => {
1407                let e = tcx.dcx().span_err(span, "expected type, found tuple constructor");
1408                Err(e)
1409            }
1410        }
1411    }
1412
1413    /// Lower a [type-relative][hir::QPath::TypeRelative] path to a (type-level) constant.
1414    x;#[instrument(level = "debug", skip_all, ret)]
1415    fn lower_type_relative_const_path(
1416        &self,
1417        self_ty: Ty<'tcx>,
1418        hir_self_ty: &'tcx hir::Ty<'tcx>,
1419        segment: &'tcx hir::PathSegment<'tcx>,
1420        qpath_hir_id: HirId,
1421        span: Span,
1422    ) -> Result<Const<'tcx>, ErrorGuaranteed> {
1423        let tcx = self.tcx();
1424        match self.lower_type_relative_path(
1425            self_ty,
1426            hir_self_ty,
1427            segment,
1428            qpath_hir_id,
1429            span,
1430            LowerTypeRelativePathMode::Const,
1431        )? {
1432            TypeRelativePath::AssocItem(def_id, args) => {
1433                self.require_type_const_attribute(def_id, span)?;
1434                let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
1435                let ct = self.check_param_uses_if_mcg(ct, span, false);
1436                Ok(ct)
1437            }
1438            TypeRelativePath::Ctor { ctor_def_id, args } => match tcx.def_kind(ctor_def_id) {
1439                DefKind::Ctor(_, CtorKind::Fn) => {
1440                    Ok(ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, ctor_def_id, args)))
1441                }
1442                DefKind::Ctor(ctor_of, CtorKind::Const) => {
1443                    Ok(self.construct_const_ctor_value(ctor_def_id, ctor_of, args))
1444                }
1445                _ => unreachable!(),
1446            },
1447            // FIXME(mgca): implement support for this once ready to support all adt ctor expressions,
1448            // not just const ctors
1449            TypeRelativePath::Variant { .. } => {
1450                span_bug!(span, "unexpected variant res for type associated const path")
1451            }
1452        }
1453    }
1454
1455    /// Lower a [type-relative][hir::QPath::TypeRelative] (and type-level) path.
1456    x;#[instrument(level = "debug", skip_all, ret)]
1457    fn lower_type_relative_path(
1458        &self,
1459        self_ty: Ty<'tcx>,
1460        hir_self_ty: &'tcx hir::Ty<'tcx>,
1461        segment: &'tcx hir::PathSegment<'tcx>,
1462        qpath_hir_id: HirId,
1463        span: Span,
1464        mode: LowerTypeRelativePathMode,
1465    ) -> Result<TypeRelativePath<'tcx>, ErrorGuaranteed> {
1466        debug!(%self_ty, ?segment.ident);
1467        let tcx = self.tcx();
1468
1469        // Check if we have an enum variant or an inherent associated type.
1470        let mut variant_def_id = None;
1471        if let Some(adt_def) = self.probe_adt(span, self_ty) {
1472            if adt_def.is_enum() {
1473                let variant_def = adt_def
1474                    .variants()
1475                    .iter()
1476                    .find(|vd| tcx.hygienic_eq(segment.ident, vd.ident(tcx), adt_def.did()));
1477                if let Some(variant_def) = variant_def {
1478                    // FIXME(mgca): do we want constructor resolutions to take priority over
1479                    // other possible resolutions?
1480                    if matches!(mode, LowerTypeRelativePathMode::Const)
1481                        && let Some((_, ctor_def_id)) = variant_def.ctor
1482                    {
1483                        tcx.check_stability(variant_def.def_id, Some(qpath_hir_id), span, None);
1484                        let _ = self.prohibit_generic_args(
1485                            slice::from_ref(segment).iter(),
1486                            GenericsArgsErrExtend::EnumVariant {
1487                                qself: hir_self_ty,
1488                                assoc_segment: segment,
1489                                adt_def,
1490                            },
1491                        );
1492                        let ty::Adt(_, enum_args) = self_ty.kind() else { unreachable!() };
1493                        return Ok(TypeRelativePath::Ctor { ctor_def_id, args: enum_args });
1494                    }
1495                    if let PermitVariants::Yes = mode.permit_variants() {
1496                        tcx.check_stability(variant_def.def_id, Some(qpath_hir_id), span, None);
1497                        let _ = self.prohibit_generic_args(
1498                            slice::from_ref(segment).iter(),
1499                            GenericsArgsErrExtend::EnumVariant {
1500                                qself: hir_self_ty,
1501                                assoc_segment: segment,
1502                                adt_def,
1503                            },
1504                        );
1505                        return Ok(TypeRelativePath::Variant {
1506                            adt: self_ty,
1507                            variant_did: variant_def.def_id,
1508                        });
1509                    } else {
1510                        variant_def_id = Some(variant_def.def_id);
1511                    }
1512                }
1513            }
1514
1515            // FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
1516            if let Some((did, args)) = self.probe_inherent_assoc_item(
1517                segment,
1518                adt_def.did(),
1519                self_ty,
1520                qpath_hir_id,
1521                span,
1522                mode.assoc_tag(),
1523            )? {
1524                return Ok(TypeRelativePath::AssocItem(did, args));
1525            }
1526        }
1527
1528        let (item_def_id, bound) = self.resolve_type_relative_path(
1529            self_ty,
1530            hir_self_ty,
1531            mode.assoc_tag(),
1532            segment,
1533            qpath_hir_id,
1534            span,
1535            variant_def_id,
1536        )?;
1537
1538        let (item_def_id, args) = self.lower_assoc_item_path(span, item_def_id, segment, bound)?;
1539
1540        if let Some(variant_def_id) = variant_def_id {
1541            tcx.node_span_lint(AMBIGUOUS_ASSOCIATED_ITEMS, qpath_hir_id, span, |lint| {
1542                lint.primary_message("ambiguous associated item");
1543                let mut could_refer_to = |kind: DefKind, def_id, also| {
1544                    let note_msg = format!(
1545                        "`{}` could{} refer to the {} defined here",
1546                        segment.ident,
1547                        also,
1548                        tcx.def_kind_descr(kind, def_id)
1549                    );
1550                    lint.span_note(tcx.def_span(def_id), note_msg);
1551                };
1552
1553                could_refer_to(DefKind::Variant, variant_def_id, "");
1554                could_refer_to(mode.def_kind(), item_def_id, " also");
1555
1556                lint.span_suggestion(
1557                    span,
1558                    "use fully-qualified syntax",
1559                    format!(
1560                        "<{} as {}>::{}",
1561                        self_ty,
1562                        tcx.item_name(bound.def_id()),
1563                        segment.ident
1564                    ),
1565                    Applicability::MachineApplicable,
1566                );
1567            });
1568        }
1569
1570        Ok(TypeRelativePath::AssocItem(item_def_id, args))
1571    }
1572
1573    /// Resolve a [type-relative](hir::QPath::TypeRelative) (and type-level) path.
1574    fn resolve_type_relative_path(
1575        &self,
1576        self_ty: Ty<'tcx>,
1577        hir_self_ty: &'tcx hir::Ty<'tcx>,
1578        assoc_tag: ty::AssocTag,
1579        segment: &'tcx hir::PathSegment<'tcx>,
1580        qpath_hir_id: HirId,
1581        span: Span,
1582        variant_def_id: Option<DefId>,
1583    ) -> Result<(DefId, ty::PolyTraitRef<'tcx>), ErrorGuaranteed> {
1584        let tcx = self.tcx();
1585
1586        let self_ty_res = match hir_self_ty.kind {
1587            hir::TyKind::Path(hir::QPath::Resolved(_, path)) => path.res,
1588            _ => Res::Err,
1589        };
1590
1591        // Find the type of the assoc item, and the trait where the associated item is declared.
1592        let bound = match (self_ty.kind(), self_ty_res) {
1593            (_, Res::SelfTyAlias { alias_to: impl_def_id, is_trait_impl: true, .. }) => {
1594                // `Self` in an impl of a trait -- we have a concrete self type and a
1595                // trait reference.
1596                let trait_ref = tcx.impl_trait_ref(impl_def_id);
1597
1598                self.probe_single_bound_for_assoc_item(
1599                    || {
1600                        let trait_ref = ty::Binder::dummy(trait_ref.instantiate_identity());
1601                        traits::supertraits(tcx, trait_ref)
1602                    },
1603                    AssocItemQSelf::SelfTyAlias,
1604                    assoc_tag,
1605                    segment.ident,
1606                    span,
1607                    None,
1608                )?
1609            }
1610            (
1611                &ty::Param(_),
1612                Res::SelfTyParam { trait_: param_did } | Res::Def(DefKind::TyParam, param_did),
1613            ) => self.probe_single_ty_param_bound_for_assoc_item(
1614                param_did.expect_local(),
1615                hir_self_ty.span,
1616                assoc_tag,
1617                segment.ident,
1618                span,
1619            )?,
1620            _ => {
1621                return Err(self.report_unresolved_type_relative_path(
1622                    self_ty,
1623                    hir_self_ty,
1624                    assoc_tag,
1625                    segment.ident,
1626                    qpath_hir_id,
1627                    span,
1628                    variant_def_id,
1629                ));
1630            }
1631        };
1632
1633        let assoc_item = self
1634            .probe_assoc_item(segment.ident, assoc_tag, qpath_hir_id, span, bound.def_id())
1635            .expect("failed to find associated item");
1636
1637        Ok((assoc_item.def_id, bound))
1638    }
1639
1640    /// Search for inherent associated items for use at the type level.
1641    fn probe_inherent_assoc_item(
1642        &self,
1643        segment: &hir::PathSegment<'tcx>,
1644        adt_did: DefId,
1645        self_ty: Ty<'tcx>,
1646        block: HirId,
1647        span: Span,
1648        assoc_tag: ty::AssocTag,
1649    ) -> Result<Option<(DefId, GenericArgsRef<'tcx>)>, ErrorGuaranteed> {
1650        let tcx = self.tcx();
1651
1652        if !tcx.features().inherent_associated_types() {
1653            match assoc_tag {
1654                // Don't attempt to look up inherent associated types when the feature is not
1655                // enabled. Theoretically it'd be fine to do so since we feature-gate their
1656                // definition site. However, the current implementation of inherent associated
1657                // items is somewhat brittle, so let's not run it by default.
1658                ty::AssocTag::Type => return Ok(None),
1659                ty::AssocTag::Const => {
1660                    // We also gate the mgca codepath for type-level uses of inherent consts
1661                    // with the inherent_associated_types feature gate since it relies on the
1662                    // same machinery and has similar rough edges.
1663                    return Err(feature_err(
1664                        &tcx.sess,
1665                        sym::inherent_associated_types,
1666                        span,
1667                        "inherent associated types are unstable",
1668                    )
1669                    .emit());
1670                }
1671                ty::AssocTag::Fn => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1672            }
1673        }
1674
1675        let name = segment.ident;
1676        let candidates: Vec<_> = tcx
1677            .inherent_impls(adt_did)
1678            .iter()
1679            .filter_map(|&impl_| {
1680                let (item, scope) =
1681                    self.probe_assoc_item_unchecked(name, assoc_tag, block, impl_)?;
1682                Some(InherentAssocCandidate { impl_, assoc_item: item.def_id, scope })
1683            })
1684            .collect();
1685
1686        // At the moment, we actually bail out with a hard error if the selection of an inherent
1687        // associated item fails (see below). This means we never consider trait associated items
1688        // as potential fallback candidates (#142006). To temporarily mask that issue, let's not
1689        // select at all if there are no early inherent candidates.
1690        if candidates.is_empty() {
1691            return Ok(None);
1692        }
1693
1694        let (applicable_candidates, fulfillment_errors) =
1695            self.select_inherent_assoc_candidates(span, self_ty, candidates.clone());
1696
1697        // FIXME(#142006): Don't eagerly error here, there might be applicable trait candidates.
1698        let InherentAssocCandidate { impl_, assoc_item, scope: def_scope } =
1699            match &applicable_candidates[..] {
1700                &[] => Err(self.report_unresolved_inherent_assoc_item(
1701                    name,
1702                    self_ty,
1703                    candidates,
1704                    fulfillment_errors,
1705                    span,
1706                    assoc_tag,
1707                )),
1708
1709                &[applicable_candidate] => Ok(applicable_candidate),
1710
1711                &[_, ..] => Err(self.report_ambiguous_inherent_assoc_item(
1712                    name,
1713                    candidates.into_iter().map(|cand| cand.assoc_item).collect(),
1714                    span,
1715                )),
1716            }?;
1717
1718        // FIXME(#142006): Don't eagerly validate here, there might be trait candidates that are
1719        // accessible (visible and stable) contrary to the inherent candidate.
1720        self.check_assoc_item(assoc_item, name, def_scope, block, span);
1721
1722        // FIXME(fmease): Currently creating throwaway `parent_args` to please
1723        // `lower_generic_args_of_assoc_item`. Modify the latter instead (or sth. similar) to
1724        // not require the parent args logic.
1725        let parent_args = ty::GenericArgs::identity_for_item(tcx, impl_);
1726        let args = self.lower_generic_args_of_assoc_item(span, assoc_item, segment, parent_args);
1727        let args = tcx.mk_args_from_iter(
1728            std::iter::once(ty::GenericArg::from(self_ty))
1729                .chain(args.into_iter().skip(parent_args.len())),
1730        );
1731
1732        Ok(Some((assoc_item, args)))
1733    }
1734
1735    /// Given name and kind search for the assoc item in the provided scope and check if it's accessible[^1].
1736    ///
1737    /// [^1]: I.e., accessible in the provided scope wrt. visibility and stability.
1738    fn probe_assoc_item(
1739        &self,
1740        ident: Ident,
1741        assoc_tag: ty::AssocTag,
1742        block: HirId,
1743        span: Span,
1744        scope: DefId,
1745    ) -> Option<ty::AssocItem> {
1746        let (item, scope) = self.probe_assoc_item_unchecked(ident, assoc_tag, block, scope)?;
1747        self.check_assoc_item(item.def_id, ident, scope, block, span);
1748        Some(item)
1749    }
1750
1751    /// Given name and kind search for the assoc item in the provided scope
1752    /// *without* checking if it's accessible[^1].
1753    ///
1754    /// [^1]: I.e., accessible in the provided scope wrt. visibility and stability.
1755    fn probe_assoc_item_unchecked(
1756        &self,
1757        ident: Ident,
1758        assoc_tag: ty::AssocTag,
1759        block: HirId,
1760        scope: DefId,
1761    ) -> Option<(ty::AssocItem, /*scope*/ DefId)> {
1762        let tcx = self.tcx();
1763
1764        let (ident, def_scope) = tcx.adjust_ident_and_get_scope(ident, scope, block);
1765        // We have already adjusted the item name above, so compare with `.normalize_to_macros_2_0()`
1766        // instead of calling `filter_by_name_and_kind` which would needlessly normalize the
1767        // `ident` again and again.
1768        let item = tcx
1769            .associated_items(scope)
1770            .filter_by_name_unhygienic(ident.name)
1771            .find(|i| i.tag() == assoc_tag && i.ident(tcx).normalize_to_macros_2_0() == ident)?;
1772
1773        Some((*item, def_scope))
1774    }
1775
1776    /// Check if the given assoc item is accessible in the provided scope wrt. visibility and stability.
1777    fn check_assoc_item(
1778        &self,
1779        item_def_id: DefId,
1780        ident: Ident,
1781        scope: DefId,
1782        block: HirId,
1783        span: Span,
1784    ) {
1785        let tcx = self.tcx();
1786
1787        if !tcx.visibility(item_def_id).is_accessible_from(scope, tcx) {
1788            self.dcx().emit_err(crate::errors::AssocItemIsPrivate {
1789                span,
1790                kind: tcx.def_descr(item_def_id),
1791                name: ident,
1792                defined_here_label: tcx.def_span(item_def_id),
1793            });
1794        }
1795
1796        tcx.check_stability(item_def_id, Some(block), span, None);
1797    }
1798
1799    fn probe_traits_that_match_assoc_ty(
1800        &self,
1801        qself_ty: Ty<'tcx>,
1802        assoc_ident: Ident,
1803    ) -> Vec<String> {
1804        let tcx = self.tcx();
1805
1806        // In contexts that have no inference context, just make a new one.
1807        // We do need a local variable to store it, though.
1808        let infcx_;
1809        let infcx = if let Some(infcx) = self.infcx() {
1810            infcx
1811        } else {
1812            if !!qself_ty.has_infer() {
    ::core::panicking::panic("assertion failed: !qself_ty.has_infer()")
};assert!(!qself_ty.has_infer());
1813            infcx_ = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
1814            &infcx_
1815        };
1816
1817        tcx.all_traits_including_private()
1818            .filter(|trait_def_id| {
1819                // Consider only traits with the associated type
1820                tcx.associated_items(*trait_def_id)
1821                        .in_definition_order()
1822                        .any(|i| {
1823                            i.is_type()
1824                                && !i.is_impl_trait_in_trait()
1825                                && i.ident(tcx).normalize_to_macros_2_0() == assoc_ident
1826                        })
1827                    // Consider only accessible traits
1828                    && tcx.visibility(*trait_def_id)
1829                        .is_accessible_from(self.item_def_id(), tcx)
1830                    && tcx.all_impls(*trait_def_id)
1831                        .any(|impl_def_id| {
1832                            let header = tcx.impl_trait_header(impl_def_id);
1833                            let trait_ref = header.trait_ref.instantiate(
1834                                tcx,
1835                                infcx.fresh_args_for_item(DUMMY_SP, impl_def_id),
1836                            );
1837
1838                            let value = fold_regions(tcx, qself_ty, |_, _| tcx.lifetimes.re_erased);
1839                            // FIXME: Don't bother dealing with non-lifetime binders here...
1840                            if value.has_escaping_bound_vars() {
1841                                return false;
1842                            }
1843                            infcx
1844                                .can_eq(
1845                                    ty::ParamEnv::empty(),
1846                                    trait_ref.self_ty(),
1847                                    value,
1848                                ) && header.polarity != ty::ImplPolarity::Negative
1849                        })
1850            })
1851            .map(|trait_def_id| tcx.def_path_str(trait_def_id))
1852            .collect()
1853    }
1854
1855    /// Lower a [resolved][hir::QPath::Resolved] associated type path to a projection.
1856    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_resolved_assoc_ty_path",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1856u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Ty<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            match self.lower_resolved_assoc_item_path(span, opt_self_ty,
                    item_def_id, trait_segment, item_segment,
                    ty::AssocTag::Type) {
                Ok((item_def_id, item_args)) => {
                    Ty::new_projection_from_args(self.tcx(), item_def_id,
                        item_args)
                }
                Err(guar) => Ty::new_error(self.tcx(), guar),
            }
        }
    }
}#[instrument(level = "debug", skip_all)]
1857    fn lower_resolved_assoc_ty_path(
1858        &self,
1859        span: Span,
1860        opt_self_ty: Option<Ty<'tcx>>,
1861        item_def_id: DefId,
1862        trait_segment: Option<&hir::PathSegment<'tcx>>,
1863        item_segment: &hir::PathSegment<'tcx>,
1864    ) -> Ty<'tcx> {
1865        match self.lower_resolved_assoc_item_path(
1866            span,
1867            opt_self_ty,
1868            item_def_id,
1869            trait_segment,
1870            item_segment,
1871            ty::AssocTag::Type,
1872        ) {
1873            Ok((item_def_id, item_args)) => {
1874                Ty::new_projection_from_args(self.tcx(), item_def_id, item_args)
1875            }
1876            Err(guar) => Ty::new_error(self.tcx(), guar),
1877        }
1878    }
1879
1880    /// Lower a [resolved][hir::QPath::Resolved] associated const path to a (type-level) constant.
1881    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_resolved_assoc_const_path",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1881u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<Const<'tcx>, ErrorGuaranteed> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let (item_def_id, item_args) =
                self.lower_resolved_assoc_item_path(span, opt_self_ty,
                        item_def_id, trait_segment, item_segment,
                        ty::AssocTag::Const)?;
            self.require_type_const_attribute(item_def_id, span)?;
            let uv = ty::UnevaluatedConst::new(item_def_id, item_args);
            Ok(Const::new_unevaluated(self.tcx(), uv))
        }
    }
}#[instrument(level = "debug", skip_all)]
1882    fn lower_resolved_assoc_const_path(
1883        &self,
1884        span: Span,
1885        opt_self_ty: Option<Ty<'tcx>>,
1886        item_def_id: DefId,
1887        trait_segment: Option<&hir::PathSegment<'tcx>>,
1888        item_segment: &hir::PathSegment<'tcx>,
1889    ) -> Result<Const<'tcx>, ErrorGuaranteed> {
1890        let (item_def_id, item_args) = self.lower_resolved_assoc_item_path(
1891            span,
1892            opt_self_ty,
1893            item_def_id,
1894            trait_segment,
1895            item_segment,
1896            ty::AssocTag::Const,
1897        )?;
1898        self.require_type_const_attribute(item_def_id, span)?;
1899        let uv = ty::UnevaluatedConst::new(item_def_id, item_args);
1900        Ok(Const::new_unevaluated(self.tcx(), uv))
1901    }
1902
1903    /// Lower a [resolved][hir::QPath::Resolved] (type-level) associated item path.
1904    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_resolved_assoc_item_path",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1904u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<(DefId, GenericArgsRef<'tcx>), ErrorGuaranteed> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = self.tcx();
            let trait_def_id = tcx.parent(item_def_id);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:1917",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1917u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["trait_def_id"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&trait_def_id)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let Some(self_ty) =
                opt_self_ty else {
                    return Err(self.report_missing_self_ty_for_resolved_path(trait_def_id,
                                span, item_segment, assoc_tag));
                };
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:1927",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1927u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["self_ty"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&self_ty) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            let trait_ref =
                self.lower_mono_trait_ref(span, trait_def_id, self_ty,
                    trait_segment.unwrap(), false);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:1931",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1931u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["trait_ref"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&trait_ref)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let item_args =
                self.lower_generic_args_of_assoc_item(span, item_def_id,
                    item_segment, trait_ref.args);
            Ok((item_def_id, item_args))
        }
    }
}#[instrument(level = "debug", skip_all)]
1905    fn lower_resolved_assoc_item_path(
1906        &self,
1907        span: Span,
1908        opt_self_ty: Option<Ty<'tcx>>,
1909        item_def_id: DefId,
1910        trait_segment: Option<&hir::PathSegment<'tcx>>,
1911        item_segment: &hir::PathSegment<'tcx>,
1912        assoc_tag: ty::AssocTag,
1913    ) -> Result<(DefId, GenericArgsRef<'tcx>), ErrorGuaranteed> {
1914        let tcx = self.tcx();
1915
1916        let trait_def_id = tcx.parent(item_def_id);
1917        debug!(?trait_def_id);
1918
1919        let Some(self_ty) = opt_self_ty else {
1920            return Err(self.report_missing_self_ty_for_resolved_path(
1921                trait_def_id,
1922                span,
1923                item_segment,
1924                assoc_tag,
1925            ));
1926        };
1927        debug!(?self_ty);
1928
1929        let trait_ref =
1930            self.lower_mono_trait_ref(span, trait_def_id, self_ty, trait_segment.unwrap(), false);
1931        debug!(?trait_ref);
1932
1933        let item_args =
1934            self.lower_generic_args_of_assoc_item(span, item_def_id, item_segment, trait_ref.args);
1935
1936        Ok((item_def_id, item_args))
1937    }
1938
1939    pub fn prohibit_generic_args<'a>(
1940        &self,
1941        segments: impl Iterator<Item = &'a hir::PathSegment<'a>> + Clone,
1942        err_extend: GenericsArgsErrExtend<'a>,
1943    ) -> Result<(), ErrorGuaranteed> {
1944        let args_visitors = segments.clone().flat_map(|segment| segment.args().args);
1945        let mut result = Ok(());
1946        if let Some(_) = args_visitors.clone().next() {
1947            result = Err(self.report_prohibited_generic_args(
1948                segments.clone(),
1949                args_visitors,
1950                err_extend,
1951            ));
1952        }
1953
1954        for segment in segments {
1955            // Only emit the first error to avoid overloading the user with error messages.
1956            if let Some(c) = segment.args().constraints.first() {
1957                return Err(prohibit_assoc_item_constraint(self, c, None));
1958            }
1959        }
1960
1961        result
1962    }
1963
1964    /// Probe path segments that are semantically allowed to have generic arguments.
1965    ///
1966    /// ### Example
1967    ///
1968    /// ```ignore (illustrative)
1969    ///    Option::None::<()>
1970    /// //         ^^^^ permitted to have generic args
1971    ///
1972    /// // ==> [GenericPathSegment(Option_def_id, 1)]
1973    ///
1974    ///    Option::<()>::None
1975    /// // ^^^^^^        ^^^^ *not* permitted to have generic args
1976    /// // permitted to have generic args
1977    ///
1978    /// // ==> [GenericPathSegment(Option_def_id, 0)]
1979    /// ```
1980    // FIXME(eddyb, varkor) handle type paths here too, not just value ones.
1981    pub fn probe_generic_path_segments(
1982        &self,
1983        segments: &[hir::PathSegment<'_>],
1984        self_ty: Option<Ty<'tcx>>,
1985        kind: DefKind,
1986        def_id: DefId,
1987        span: Span,
1988    ) -> Vec<GenericPathSegment> {
1989        // We need to extract the generic arguments supplied by the user in
1990        // the path `path`. Due to the current setup, this is a bit of a
1991        // tricky process; the problem is that resolve only tells us the
1992        // end-point of the path resolution, and not the intermediate steps.
1993        // Luckily, we can (at least for now) deduce the intermediate steps
1994        // just from the end-point.
1995        //
1996        // There are basically five cases to consider:
1997        //
1998        // 1. Reference to a constructor of a struct:
1999        //
2000        //        struct Foo<T>(...)
2001        //
2002        //    In this case, the generic arguments are declared in the type space.
2003        //
2004        // 2. Reference to a constructor of an enum variant:
2005        //
2006        //        enum E<T> { Foo(...) }
2007        //
2008        //    In this case, the generic arguments are defined in the type space,
2009        //    but may be specified either on the type or the variant.
2010        //
2011        // 3. Reference to a free function or constant:
2012        //
2013        //        fn foo<T>() {}
2014        //
2015        //    In this case, the path will again always have the form
2016        //    `a::b::foo::<T>` where only the final segment should have generic
2017        //    arguments. However, in this case, those arguments are declared on
2018        //    a value, and hence are in the value space.
2019        //
2020        // 4. Reference to an associated function or constant:
2021        //
2022        //        impl<A> SomeStruct<A> {
2023        //            fn foo<B>(...) {}
2024        //        }
2025        //
2026        //    Here we can have a path like `a::b::SomeStruct::<A>::foo::<B>`,
2027        //    in which case generic arguments may appear in two places. The
2028        //    penultimate segment, `SomeStruct::<A>`, contains generic arguments
2029        //    in the type space, and the final segment, `foo::<B>` contains
2030        //    generic arguments in value space.
2031        //
2032        // The first step then is to categorize the segments appropriately.
2033
2034        let tcx = self.tcx();
2035
2036        if !!segments.is_empty() {
    ::core::panicking::panic("assertion failed: !segments.is_empty()")
};assert!(!segments.is_empty());
2037        let last = segments.len() - 1;
2038
2039        let mut generic_segments = ::alloc::vec::Vec::new()vec![];
2040
2041        match kind {
2042            // Case 1. Reference to a struct constructor.
2043            DefKind::Ctor(CtorOf::Struct, ..) => {
2044                // Everything but the final segment should have no
2045                // parameters at all.
2046                let generics = tcx.generics_of(def_id);
2047                // Variant and struct constructors use the
2048                // generics of their parent type definition.
2049                let generics_def_id = generics.parent.unwrap_or(def_id);
2050                generic_segments.push(GenericPathSegment(generics_def_id, last));
2051            }
2052
2053            // Case 2. Reference to a variant constructor.
2054            DefKind::Ctor(CtorOf::Variant, ..) | DefKind::Variant => {
2055                let (generics_def_id, index) = if let Some(self_ty) = self_ty {
2056                    let adt_def = self.probe_adt(span, self_ty).unwrap();
2057                    if true {
    if !adt_def.is_enum() {
        ::core::panicking::panic("assertion failed: adt_def.is_enum()")
    };
};debug_assert!(adt_def.is_enum());
2058                    (adt_def.did(), last)
2059                } else if last >= 1 && segments[last - 1].args.is_some() {
2060                    // Everything but the penultimate segment should have no
2061                    // parameters at all.
2062                    let mut def_id = def_id;
2063
2064                    // `DefKind::Ctor` -> `DefKind::Variant`
2065                    if let DefKind::Ctor(..) = kind {
2066                        def_id = tcx.parent(def_id);
2067                    }
2068
2069                    // `DefKind::Variant` -> `DefKind::Enum`
2070                    let enum_def_id = tcx.parent(def_id);
2071                    (enum_def_id, last - 1)
2072                } else {
2073                    // FIXME: lint here recommending `Enum::<...>::Variant` form
2074                    // instead of `Enum::Variant::<...>` form.
2075
2076                    // Everything but the final segment should have no
2077                    // parameters at all.
2078                    let generics = tcx.generics_of(def_id);
2079                    // Variant and struct constructors use the
2080                    // generics of their parent type definition.
2081                    (generics.parent.unwrap_or(def_id), last)
2082                };
2083                generic_segments.push(GenericPathSegment(generics_def_id, index));
2084            }
2085
2086            // Case 3. Reference to a top-level value.
2087            DefKind::Fn | DefKind::Const | DefKind::ConstParam | DefKind::Static { .. } => {
2088                generic_segments.push(GenericPathSegment(def_id, last));
2089            }
2090
2091            // Case 4. Reference to a method or associated const.
2092            DefKind::AssocFn | DefKind::AssocConst => {
2093                if segments.len() >= 2 {
2094                    let generics = tcx.generics_of(def_id);
2095                    generic_segments.push(GenericPathSegment(generics.parent.unwrap(), last - 1));
2096                }
2097                generic_segments.push(GenericPathSegment(def_id, last));
2098            }
2099
2100            kind => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected definition kind {0:?} for {1:?}",
        kind, def_id))bug!("unexpected definition kind {:?} for {:?}", kind, def_id),
2101        }
2102
2103        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2103",
                        "rustc_hir_analysis::hir_ty_lowering",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(2103u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                        ::tracing_core::field::FieldSet::new(&["generic_segments"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&generic_segments)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?generic_segments);
2104
2105        generic_segments
2106    }
2107
2108    /// Lower a [resolved][hir::QPath::Resolved] path to a type.
2109    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_resolved_ty_path",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2109u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Ty<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2117",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2117u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["path.res",
                                                    "opt_self_ty", "path.segments"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&path.res)
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&opt_self_ty)
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&path.segments)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let tcx = self.tcx();
            let span = path.span;
            match path.res {
                Res::Def(DefKind::OpaqueTy, did) => {
                    match tcx.opaque_ty_origin(did) {
                        hir::OpaqueTyOrigin::TyAlias { .. } => {}
                        ref left_val => {
                            ::core::panicking::assert_matches_failed(left_val,
                                "hir::OpaqueTyOrigin::TyAlias { .. }",
                                ::core::option::Option::None);
                        }
                    };
                    let [leading_segments @ .., segment] =
                        path.segments else {
                            ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))
                        };
                    let _ =
                        self.prohibit_generic_args(leading_segments.iter(),
                            GenericsArgsErrExtend::OpaqueTy);
                    let args =
                        self.lower_generic_args_of_path_segment(span, did, segment);
                    Ty::new_opaque(tcx, did, args)
                }
                Res::Def(DefKind::Enum | DefKind::TyAlias | DefKind::Struct |
                    DefKind::Union | DefKind::ForeignTy, did) => {
                    match (&opt_self_ty, &None) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    };
                    let [leading_segments @ .., segment] =
                        path.segments else {
                            ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))
                        };
                    let _ =
                        self.prohibit_generic_args(leading_segments.iter(),
                            GenericsArgsErrExtend::None);
                    self.lower_path_segment(span, did, segment)
                }
                Res::Def(kind @ DefKind::Variant, def_id) if
                    let PermitVariants::Yes = permit_variants => {
                    match (&opt_self_ty, &None) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    };
                    let generic_segments =
                        self.probe_generic_path_segments(path.segments, None, kind,
                            def_id, span);
                    let indices: FxHashSet<_> =
                        generic_segments.iter().map(|GenericPathSegment(_, index)|
                                    index).collect();
                    let _ =
                        self.prohibit_generic_args(path.segments.iter().enumerate().filter_map(|(index,
                                        seg)|
                                    {
                                        if !indices.contains(&index) { Some(seg) } else { None }
                                    }), GenericsArgsErrExtend::DefVariant(&path.segments));
                    let &GenericPathSegment(def_id, index) =
                        generic_segments.last().unwrap();
                    self.lower_path_segment(span, def_id, &path.segments[index])
                }
                Res::Def(DefKind::TyParam, def_id) => {
                    match (&opt_self_ty, &None) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    };
                    let _ =
                        self.prohibit_generic_args(path.segments.iter(),
                            GenericsArgsErrExtend::Param(def_id));
                    self.lower_ty_param(hir_id)
                }
                Res::SelfTyParam { .. } => {
                    match (&opt_self_ty, &None) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    };
                    let _ =
                        self.prohibit_generic_args(path.segments.iter(),
                            if let [hir::PathSegment { args: Some(args), ident, .. }] =
                                    &path.segments {
                                GenericsArgsErrExtend::SelfTyParam(ident.span.shrink_to_hi().to(args.span_ext))
                            } else { GenericsArgsErrExtend::None });
                    self.check_param_uses_if_mcg(tcx.types.self_param, span,
                        false)
                }
                Res::SelfTyAlias { alias_to: def_id, .. } => {
                    match (&opt_self_ty, &None) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    };
                    let ty =
                        tcx.at(span).type_of(def_id).instantiate_identity();
                    let _ =
                        self.prohibit_generic_args(path.segments.iter(),
                            GenericsArgsErrExtend::SelfTyAlias { def_id, span });
                    self.check_param_uses_if_mcg(ty, span, true)
                }
                Res::Def(DefKind::AssocTy, def_id) => {
                    let trait_segment =
                        if let [modules @ .., trait_, _item] = path.segments {
                            let _ =
                                self.prohibit_generic_args(modules.iter(),
                                    GenericsArgsErrExtend::None);
                            Some(trait_)
                        } else { None };
                    self.lower_resolved_assoc_ty_path(span, opt_self_ty, def_id,
                        trait_segment, path.segments.last().unwrap())
                }
                Res::PrimTy(prim_ty) => {
                    match (&opt_self_ty, &None) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    };
                    let _ =
                        self.prohibit_generic_args(path.segments.iter(),
                            GenericsArgsErrExtend::PrimTy(prim_ty));
                    match prim_ty {
                        hir::PrimTy::Bool => tcx.types.bool,
                        hir::PrimTy::Char => tcx.types.char,
                        hir::PrimTy::Int(it) => Ty::new_int(tcx, it),
                        hir::PrimTy::Uint(uit) => Ty::new_uint(tcx, uit),
                        hir::PrimTy::Float(ft) => Ty::new_float(tcx, ft),
                        hir::PrimTy::Str => tcx.types.str_,
                    }
                }
                Res::Err => {
                    let e =
                        self.tcx().dcx().span_delayed_bug(path.span,
                            "path with `Res::Err` but no error emitted");
                    Ty::new_error(tcx, e)
                }
                Res::Def(..) => {
                    match (&path.segments.get(0).map(|seg| seg.ident.name),
                            &Some(kw::SelfUpper)) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val,
                                    ::core::option::Option::Some(format_args!("only expected incorrect resolution for `Self`")));
                            }
                        }
                    };
                    Ty::new_error(self.tcx(),
                        self.dcx().span_delayed_bug(span,
                            "incorrect resolution for `Self`"))
                }
                _ =>
                    ::rustc_middle::util::bug::span_bug_fmt(span,
                        format_args!("unexpected resolution: {0:?}", path.res)),
            }
        }
    }
}#[instrument(level = "debug", skip_all)]
2110    pub fn lower_resolved_ty_path(
2111        &self,
2112        opt_self_ty: Option<Ty<'tcx>>,
2113        path: &hir::Path<'tcx>,
2114        hir_id: HirId,
2115        permit_variants: PermitVariants,
2116    ) -> Ty<'tcx> {
2117        debug!(?path.res, ?opt_self_ty, ?path.segments);
2118        let tcx = self.tcx();
2119
2120        let span = path.span;
2121        match path.res {
2122            Res::Def(DefKind::OpaqueTy, did) => {
2123                // Check for desugared `impl Trait`.
2124                assert_matches!(tcx.opaque_ty_origin(did), hir::OpaqueTyOrigin::TyAlias { .. });
2125                let [leading_segments @ .., segment] = path.segments else { bug!() };
2126                let _ = self.prohibit_generic_args(
2127                    leading_segments.iter(),
2128                    GenericsArgsErrExtend::OpaqueTy,
2129                );
2130                let args = self.lower_generic_args_of_path_segment(span, did, segment);
2131                Ty::new_opaque(tcx, did, args)
2132            }
2133            Res::Def(
2134                DefKind::Enum
2135                | DefKind::TyAlias
2136                | DefKind::Struct
2137                | DefKind::Union
2138                | DefKind::ForeignTy,
2139                did,
2140            ) => {
2141                assert_eq!(opt_self_ty, None);
2142                let [leading_segments @ .., segment] = path.segments else { bug!() };
2143                let _ = self
2144                    .prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
2145                self.lower_path_segment(span, did, segment)
2146            }
2147            Res::Def(kind @ DefKind::Variant, def_id)
2148                if let PermitVariants::Yes = permit_variants =>
2149            {
2150                // Lower "variant type" as if it were a real type.
2151                // The resulting `Ty` is type of the variant's enum for now.
2152                assert_eq!(opt_self_ty, None);
2153
2154                let generic_segments =
2155                    self.probe_generic_path_segments(path.segments, None, kind, def_id, span);
2156                let indices: FxHashSet<_> =
2157                    generic_segments.iter().map(|GenericPathSegment(_, index)| index).collect();
2158                let _ = self.prohibit_generic_args(
2159                    path.segments.iter().enumerate().filter_map(|(index, seg)| {
2160                        if !indices.contains(&index) { Some(seg) } else { None }
2161                    }),
2162                    GenericsArgsErrExtend::DefVariant(&path.segments),
2163                );
2164
2165                let &GenericPathSegment(def_id, index) = generic_segments.last().unwrap();
2166                self.lower_path_segment(span, def_id, &path.segments[index])
2167            }
2168            Res::Def(DefKind::TyParam, def_id) => {
2169                assert_eq!(opt_self_ty, None);
2170                let _ = self.prohibit_generic_args(
2171                    path.segments.iter(),
2172                    GenericsArgsErrExtend::Param(def_id),
2173                );
2174                self.lower_ty_param(hir_id)
2175            }
2176            Res::SelfTyParam { .. } => {
2177                // `Self` in trait or type alias.
2178                assert_eq!(opt_self_ty, None);
2179                let _ = self.prohibit_generic_args(
2180                    path.segments.iter(),
2181                    if let [hir::PathSegment { args: Some(args), ident, .. }] = &path.segments {
2182                        GenericsArgsErrExtend::SelfTyParam(
2183                            ident.span.shrink_to_hi().to(args.span_ext),
2184                        )
2185                    } else {
2186                        GenericsArgsErrExtend::None
2187                    },
2188                );
2189                self.check_param_uses_if_mcg(tcx.types.self_param, span, false)
2190            }
2191            Res::SelfTyAlias { alias_to: def_id, .. } => {
2192                // `Self` in impl (we know the concrete type).
2193                assert_eq!(opt_self_ty, None);
2194                // Try to evaluate any array length constants.
2195                let ty = tcx.at(span).type_of(def_id).instantiate_identity();
2196                let _ = self.prohibit_generic_args(
2197                    path.segments.iter(),
2198                    GenericsArgsErrExtend::SelfTyAlias { def_id, span },
2199                );
2200                self.check_param_uses_if_mcg(ty, span, true)
2201            }
2202            Res::Def(DefKind::AssocTy, def_id) => {
2203                let trait_segment = if let [modules @ .., trait_, _item] = path.segments {
2204                    let _ = self.prohibit_generic_args(modules.iter(), GenericsArgsErrExtend::None);
2205                    Some(trait_)
2206                } else {
2207                    None
2208                };
2209                self.lower_resolved_assoc_ty_path(
2210                    span,
2211                    opt_self_ty,
2212                    def_id,
2213                    trait_segment,
2214                    path.segments.last().unwrap(),
2215                )
2216            }
2217            Res::PrimTy(prim_ty) => {
2218                assert_eq!(opt_self_ty, None);
2219                let _ = self.prohibit_generic_args(
2220                    path.segments.iter(),
2221                    GenericsArgsErrExtend::PrimTy(prim_ty),
2222                );
2223                match prim_ty {
2224                    hir::PrimTy::Bool => tcx.types.bool,
2225                    hir::PrimTy::Char => tcx.types.char,
2226                    hir::PrimTy::Int(it) => Ty::new_int(tcx, it),
2227                    hir::PrimTy::Uint(uit) => Ty::new_uint(tcx, uit),
2228                    hir::PrimTy::Float(ft) => Ty::new_float(tcx, ft),
2229                    hir::PrimTy::Str => tcx.types.str_,
2230                }
2231            }
2232            Res::Err => {
2233                let e = self
2234                    .tcx()
2235                    .dcx()
2236                    .span_delayed_bug(path.span, "path with `Res::Err` but no error emitted");
2237                Ty::new_error(tcx, e)
2238            }
2239            Res::Def(..) => {
2240                assert_eq!(
2241                    path.segments.get(0).map(|seg| seg.ident.name),
2242                    Some(kw::SelfUpper),
2243                    "only expected incorrect resolution for `Self`"
2244                );
2245                Ty::new_error(
2246                    self.tcx(),
2247                    self.dcx().span_delayed_bug(span, "incorrect resolution for `Self`"),
2248                )
2249            }
2250            _ => span_bug!(span, "unexpected resolution: {:?}", path.res),
2251        }
2252    }
2253
2254    /// Lower a type parameter from the HIR to our internal notion of a type.
2255    ///
2256    /// Early-bound type parameters get lowered to [`ty::Param`]
2257    /// and late-bound ones to [`ty::Bound`].
2258    pub(crate) fn lower_ty_param(&self, hir_id: HirId) -> Ty<'tcx> {
2259        let tcx = self.tcx();
2260
2261        let ty = match tcx.named_bound_var(hir_id) {
2262            Some(rbv::ResolvedArg::LateBound(debruijn, index, def_id)) => {
2263                let br = ty::BoundTy {
2264                    var: ty::BoundVar::from_u32(index),
2265                    kind: ty::BoundTyKind::Param(def_id.to_def_id()),
2266                };
2267                Ty::new_bound(tcx, debruijn, br)
2268            }
2269            Some(rbv::ResolvedArg::EarlyBound(def_id)) => {
2270                let item_def_id = tcx.hir_ty_param_owner(def_id);
2271                let generics = tcx.generics_of(item_def_id);
2272                let index = generics.param_def_id_to_index[&def_id.to_def_id()];
2273                Ty::new_param(tcx, index, tcx.hir_ty_param_name(def_id))
2274            }
2275            Some(rbv::ResolvedArg::Error(guar)) => Ty::new_error(tcx, guar),
2276            arg => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected bound var resolution for {0:?}: {1:?}",
        hir_id, arg))bug!("unexpected bound var resolution for {hir_id:?}: {arg:?}"),
2277        };
2278        self.check_param_uses_if_mcg(ty, tcx.hir_span(hir_id), false)
2279    }
2280
2281    /// Lower a const parameter from the HIR to our internal notion of a constant.
2282    ///
2283    /// Early-bound const parameters get lowered to [`ty::ConstKind::Param`]
2284    /// and late-bound ones to [`ty::ConstKind::Bound`].
2285    pub(crate) fn lower_const_param(&self, param_def_id: DefId, path_hir_id: HirId) -> Const<'tcx> {
2286        let tcx = self.tcx();
2287
2288        let ct = match tcx.named_bound_var(path_hir_id) {
2289            Some(rbv::ResolvedArg::EarlyBound(_)) => {
2290                // Find the name and index of the const parameter by indexing the generics of
2291                // the parent item and construct a `ParamConst`.
2292                let item_def_id = tcx.parent(param_def_id);
2293                let generics = tcx.generics_of(item_def_id);
2294                let index = generics.param_def_id_to_index[&param_def_id];
2295                let name = tcx.item_name(param_def_id);
2296                ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
2297            }
2298            Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => ty::Const::new_bound(
2299                tcx,
2300                debruijn,
2301                ty::BoundConst::new(ty::BoundVar::from_u32(index)),
2302            ),
2303            Some(rbv::ResolvedArg::Error(guar)) => ty::Const::new_error(tcx, guar),
2304            arg => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected bound var resolution for {0:?}: {1:?}",
        path_hir_id, arg))bug!("unexpected bound var resolution for {:?}: {arg:?}", path_hir_id),
2305        };
2306        self.check_param_uses_if_mcg(ct, tcx.hir_span(path_hir_id), false)
2307    }
2308
2309    /// Lower a [`hir::ConstArg`] to a (type-level) [`ty::Const`](Const).
2310    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_const_arg",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2310u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["const_arg", "ty"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&const_arg)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Const<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = self.tcx();
            if let hir::ConstArgKind::Anon(anon) = &const_arg.kind {
                if tcx.features().generic_const_parameter_types() &&
                        (ty.has_free_regions() || ty.has_erased_regions()) {
                    let e =
                        self.dcx().span_err(const_arg.span,
                            "anonymous constants with lifetimes in their type are not yet supported");
                    tcx.feed_anon_const_type(anon.def_id,
                        ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
                    return ty::Const::new_error(tcx, e);
                }
                if ty.has_non_region_infer() {
                    let e =
                        self.dcx().span_err(const_arg.span,
                            "anonymous constants with inferred types are not yet supported");
                    tcx.feed_anon_const_type(anon.def_id,
                        ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
                    return ty::Const::new_error(tcx, e);
                }
                if ty.has_non_region_param() {
                    let e =
                        self.dcx().span_err(const_arg.span,
                            "anonymous constants referencing generics are not yet supported");
                    tcx.feed_anon_const_type(anon.def_id,
                        ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
                    return ty::Const::new_error(tcx, e);
                }
                tcx.feed_anon_const_type(anon.def_id,
                    ty::EarlyBinder::bind(ty));
            }
            let hir_id = const_arg.hir_id;
            match const_arg.kind {
                hir::ConstArgKind::Tup(exprs) =>
                    self.lower_const_arg_tup(exprs, ty, const_arg.span),
                hir::ConstArgKind::Path(hir::QPath::Resolved(maybe_qself,
                    path)) => {
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2362",
                                            "rustc_hir_analysis::hir_ty_lowering",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                            ::tracing_core::__macro_support::Option::Some(2362u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                            ::tracing_core::field::FieldSet::new(&["maybe_qself",
                                                            "path"], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                            ::tracing::metadata::Kind::EVENT)
                                    };
                                ::tracing::callsite::DefaultCallsite::new(&META)
                            };
                        let enabled =
                            ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                    ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::LevelFilter::current() &&
                                {
                                    let interest = __CALLSITE.interest();
                                    !interest.is_never() &&
                                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                            interest)
                                };
                        if enabled {
                            (|value_set: ::tracing::field::ValueSet|
                                        {
                                            let meta = __CALLSITE.metadata();
                                            ::tracing::Event::dispatch(meta, &value_set);
                                            ;
                                        })({
                                    #[allow(unused_imports)]
                                    use ::tracing::field::{debug, display, Value};
                                    let mut iter = __CALLSITE.metadata().fields().iter();
                                    __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&debug(&maybe_qself)
                                                                as &dyn Value)),
                                                    (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&debug(&path) as
                                                                &dyn Value))])
                                });
                        } else { ; }
                    };
                    let opt_self_ty =
                        maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
                    self.lower_resolved_const_path(opt_self_ty, path, hir_id)
                }
                hir::ConstArgKind::Path(hir::QPath::TypeRelative(hir_self_ty,
                    segment)) => {
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2367",
                                            "rustc_hir_analysis::hir_ty_lowering",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                            ::tracing_core::__macro_support::Option::Some(2367u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                            ::tracing_core::field::FieldSet::new(&["hir_self_ty",
                                                            "segment"],
                                                ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                            ::tracing::metadata::Kind::EVENT)
                                    };
                                ::tracing::callsite::DefaultCallsite::new(&META)
                            };
                        let enabled =
                            ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                    ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::LevelFilter::current() &&
                                {
                                    let interest = __CALLSITE.interest();
                                    !interest.is_never() &&
                                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                            interest)
                                };
                        if enabled {
                            (|value_set: ::tracing::field::ValueSet|
                                        {
                                            let meta = __CALLSITE.metadata();
                                            ::tracing::Event::dispatch(meta, &value_set);
                                            ;
                                        })({
                                    #[allow(unused_imports)]
                                    use ::tracing::field::{debug, display, Value};
                                    let mut iter = __CALLSITE.metadata().fields().iter();
                                    __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&debug(&hir_self_ty)
                                                                as &dyn Value)),
                                                    (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&debug(&segment) as
                                                                &dyn Value))])
                                });
                        } else { ; }
                    };
                    let self_ty = self.lower_ty(hir_self_ty);
                    self.lower_type_relative_const_path(self_ty, hir_self_ty,
                            segment, hir_id,
                            const_arg.span).unwrap_or_else(|guar|
                            Const::new_error(tcx, guar))
                }
                hir::ConstArgKind::Struct(qpath, inits) => {
                    self.lower_const_arg_struct(hir_id, qpath, inits,
                        const_arg.span)
                }
                hir::ConstArgKind::TupleCall(qpath, args) => {
                    self.lower_const_arg_tuple_call(hir_id, qpath, args,
                        const_arg.span)
                }
                hir::ConstArgKind::Array(array_expr) =>
                    self.lower_const_arg_array(array_expr, ty),
                hir::ConstArgKind::Anon(anon) =>
                    self.lower_const_arg_anon(anon),
                hir::ConstArgKind::Infer(()) =>
                    self.ct_infer(None, const_arg.span),
                hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
                hir::ConstArgKind::Literal { lit, negated } => {
                    self.lower_const_arg_literal(&lit, negated, ty,
                        const_arg.span)
                }
            }
        }
    }
}#[instrument(skip(self), level = "debug")]
2311    pub fn lower_const_arg(&self, const_arg: &hir::ConstArg<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
2312        let tcx = self.tcx();
2313
2314        if let hir::ConstArgKind::Anon(anon) = &const_arg.kind {
2315            // FIXME(generic_const_parameter_types): Ideally we remove these errors below when
2316            // we have the ability to intermix typeck of anon const const args with the parent
2317            // bodies typeck.
2318
2319            // We also error if the type contains any regions as effectively any region will wind
2320            // up as a region variable in mir borrowck. It would also be somewhat concerning if
2321            // hir typeck was using equality but mir borrowck wound up using subtyping as that could
2322            // result in a non-infer in hir typeck but a region variable in borrowck.
2323            if tcx.features().generic_const_parameter_types()
2324                && (ty.has_free_regions() || ty.has_erased_regions())
2325            {
2326                let e = self.dcx().span_err(
2327                    const_arg.span,
2328                    "anonymous constants with lifetimes in their type are not yet supported",
2329                );
2330                tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
2331                return ty::Const::new_error(tcx, e);
2332            }
2333            // We must error if the instantiated type has any inference variables as we will
2334            // use this type to feed the `type_of` and query results must not contain inference
2335            // variables otherwise we will ICE.
2336            if ty.has_non_region_infer() {
2337                let e = self.dcx().span_err(
2338                    const_arg.span,
2339                    "anonymous constants with inferred types are not yet supported",
2340                );
2341                tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
2342                return ty::Const::new_error(tcx, e);
2343            }
2344            // We error when the type contains unsubstituted generics since we do not currently
2345            // give the anon const any of the generics from the parent.
2346            if ty.has_non_region_param() {
2347                let e = self.dcx().span_err(
2348                    const_arg.span,
2349                    "anonymous constants referencing generics are not yet supported",
2350                );
2351                tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(Ty::new_error(tcx, e)));
2352                return ty::Const::new_error(tcx, e);
2353            }
2354
2355            tcx.feed_anon_const_type(anon.def_id, ty::EarlyBinder::bind(ty));
2356        }
2357
2358        let hir_id = const_arg.hir_id;
2359        match const_arg.kind {
2360            hir::ConstArgKind::Tup(exprs) => self.lower_const_arg_tup(exprs, ty, const_arg.span),
2361            hir::ConstArgKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
2362                debug!(?maybe_qself, ?path);
2363                let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2364                self.lower_resolved_const_path(opt_self_ty, path, hir_id)
2365            }
2366            hir::ConstArgKind::Path(hir::QPath::TypeRelative(hir_self_ty, segment)) => {
2367                debug!(?hir_self_ty, ?segment);
2368                let self_ty = self.lower_ty(hir_self_ty);
2369                self.lower_type_relative_const_path(
2370                    self_ty,
2371                    hir_self_ty,
2372                    segment,
2373                    hir_id,
2374                    const_arg.span,
2375                )
2376                .unwrap_or_else(|guar| Const::new_error(tcx, guar))
2377            }
2378            hir::ConstArgKind::Struct(qpath, inits) => {
2379                self.lower_const_arg_struct(hir_id, qpath, inits, const_arg.span)
2380            }
2381            hir::ConstArgKind::TupleCall(qpath, args) => {
2382                self.lower_const_arg_tuple_call(hir_id, qpath, args, const_arg.span)
2383            }
2384            hir::ConstArgKind::Array(array_expr) => self.lower_const_arg_array(array_expr, ty),
2385            hir::ConstArgKind::Anon(anon) => self.lower_const_arg_anon(anon),
2386            hir::ConstArgKind::Infer(()) => self.ct_infer(None, const_arg.span),
2387            hir::ConstArgKind::Error(e) => ty::Const::new_error(tcx, e),
2388            hir::ConstArgKind::Literal { lit, negated } => {
2389                self.lower_const_arg_literal(&lit, negated, ty, const_arg.span)
2390            }
2391        }
2392    }
2393
2394    fn lower_const_arg_array(
2395        &self,
2396        array_expr: &'tcx hir::ConstArgArrayExpr<'tcx>,
2397        ty: Ty<'tcx>,
2398    ) -> Const<'tcx> {
2399        let tcx = self.tcx();
2400
2401        let ty::Array(elem_ty, _) = ty.kind() else {
2402            let e = tcx
2403                .dcx()
2404                .span_err(array_expr.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected `{0}`, found const array",
                ty))
    })format!("expected `{}`, found const array", ty));
2405            return Const::new_error(tcx, e);
2406        };
2407
2408        let elems = array_expr
2409            .elems
2410            .iter()
2411            .map(|elem| self.lower_const_arg(elem, *elem_ty))
2412            .collect::<Vec<_>>();
2413
2414        let valtree = ty::ValTree::from_branches(tcx, elems);
2415
2416        ty::Const::new_value(tcx, valtree, ty)
2417    }
2418
2419    fn lower_const_arg_tuple_call(
2420        &self,
2421        hir_id: HirId,
2422        qpath: hir::QPath<'tcx>,
2423        args: &'tcx [&'tcx hir::ConstArg<'tcx>],
2424        span: Span,
2425    ) -> Const<'tcx> {
2426        let tcx = self.tcx();
2427
2428        let non_adt_or_variant_res = || {
2429            let e = tcx.dcx().span_err(span, "tuple constructor with invalid base path");
2430            ty::Const::new_error(tcx, e)
2431        };
2432
2433        let ctor_const = match qpath {
2434            hir::QPath::Resolved(maybe_qself, path) => {
2435                let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2436                self.lower_resolved_const_path(opt_self_ty, path, hir_id)
2437            }
2438            hir::QPath::TypeRelative(hir_self_ty, segment) => {
2439                let self_ty = self.lower_ty(hir_self_ty);
2440                match self.lower_type_relative_const_path(
2441                    self_ty,
2442                    hir_self_ty,
2443                    segment,
2444                    hir_id,
2445                    span,
2446                ) {
2447                    Ok(c) => c,
2448                    Err(_) => return non_adt_or_variant_res(),
2449                }
2450            }
2451        };
2452
2453        let Some(value) = ctor_const.try_to_value() else {
2454            return non_adt_or_variant_res();
2455        };
2456
2457        let (adt_def, adt_args, variant_did) = match value.ty.kind() {
2458            ty::FnDef(def_id, fn_args)
2459                if let DefKind::Ctor(CtorOf::Variant, _) = tcx.def_kind(*def_id) =>
2460            {
2461                let parent_did = tcx.parent(*def_id);
2462                let enum_did = tcx.parent(parent_did);
2463                (tcx.adt_def(enum_did), fn_args, parent_did)
2464            }
2465            ty::FnDef(def_id, fn_args)
2466                if let DefKind::Ctor(CtorOf::Struct, _) = tcx.def_kind(*def_id) =>
2467            {
2468                let parent_did = tcx.parent(*def_id);
2469                (tcx.adt_def(parent_did), fn_args, parent_did)
2470            }
2471            _ => {
2472                let e = self.dcx().span_err(
2473                    span,
2474                    "complex const arguments must be placed inside of a `const` block",
2475                );
2476                return Const::new_error(tcx, e);
2477            }
2478        };
2479
2480        let variant_def = adt_def.variant_with_id(variant_did);
2481        let variant_idx = adt_def.variant_index_with_id(variant_did).as_u32();
2482
2483        if args.len() != variant_def.fields.len() {
2484            let e = tcx.dcx().span_err(
2485                span,
2486                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("tuple constructor has {0} arguments but {1} were provided",
                variant_def.fields.len(), args.len()))
    })format!(
2487                    "tuple constructor has {} arguments but {} were provided",
2488                    variant_def.fields.len(),
2489                    args.len()
2490                ),
2491            );
2492            return ty::Const::new_error(tcx, e);
2493        }
2494
2495        let fields = variant_def
2496            .fields
2497            .iter()
2498            .zip(args)
2499            .map(|(field_def, arg)| {
2500                self.lower_const_arg(arg, tcx.type_of(field_def.did).instantiate(tcx, adt_args))
2501            })
2502            .collect::<Vec<_>>();
2503
2504        let opt_discr_const = if adt_def.is_enum() {
2505            let valtree = ty::ValTree::from_scalar_int(tcx, variant_idx.into());
2506            Some(ty::Const::new_value(tcx, valtree, tcx.types.u32))
2507        } else {
2508            None
2509        };
2510
2511        let valtree = ty::ValTree::from_branches(tcx, opt_discr_const.into_iter().chain(fields));
2512        let adt_ty = Ty::new_adt(tcx, adt_def, adt_args);
2513        ty::Const::new_value(tcx, valtree, adt_ty)
2514    }
2515
2516    fn lower_const_arg_tup(
2517        &self,
2518        exprs: &'tcx [&'tcx hir::ConstArg<'tcx>],
2519        ty: Ty<'tcx>,
2520        span: Span,
2521    ) -> Const<'tcx> {
2522        let tcx = self.tcx();
2523
2524        let ty::Tuple(tys) = ty.kind() else {
2525            let e = tcx.dcx().span_err(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected `{0}`, found const tuple",
                ty))
    })format!("expected `{}`, found const tuple", ty));
2526            return Const::new_error(tcx, e);
2527        };
2528
2529        let exprs = exprs
2530            .iter()
2531            .zip(tys.iter())
2532            .map(|(expr, ty)| self.lower_const_arg(expr, ty))
2533            .collect::<Vec<_>>();
2534
2535        let valtree = ty::ValTree::from_branches(tcx, exprs);
2536        ty::Const::new_value(tcx, valtree, ty)
2537    }
2538
2539    fn lower_const_arg_struct(
2540        &self,
2541        hir_id: HirId,
2542        qpath: hir::QPath<'tcx>,
2543        inits: &'tcx [&'tcx hir::ConstArgExprField<'tcx>],
2544        span: Span,
2545    ) -> Const<'tcx> {
2546        // FIXME(mgca): try to deduplicate this function with
2547        // the equivalent HIR typeck logic.
2548        let tcx = self.tcx();
2549
2550        let non_adt_or_variant_res = || {
2551            let e = tcx.dcx().span_err(span, "struct expression with invalid base path");
2552            ty::Const::new_error(tcx, e)
2553        };
2554
2555        let (ty, variant_did) = match qpath {
2556            hir::QPath::Resolved(maybe_qself, path) => {
2557                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2557",
                        "rustc_hir_analysis::hir_ty_lowering",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(2557u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                        ::tracing_core::field::FieldSet::new(&["maybe_qself",
                                        "path"], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&maybe_qself)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&path) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?maybe_qself, ?path);
2558                let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2559                let ty =
2560                    self.lower_resolved_ty_path(opt_self_ty, path, hir_id, PermitVariants::Yes);
2561                let variant_did = match path.res {
2562                    Res::Def(DefKind::Variant | DefKind::Struct, did) => did,
2563                    _ => return non_adt_or_variant_res(),
2564                };
2565
2566                (ty, variant_did)
2567            }
2568            hir::QPath::TypeRelative(hir_self_ty, segment) => {
2569                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2569",
                        "rustc_hir_analysis::hir_ty_lowering",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(2569u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                        ::tracing_core::field::FieldSet::new(&["hir_self_ty",
                                        "segment"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&hir_self_ty)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&segment) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?hir_self_ty, ?segment);
2570                let self_ty = self.lower_ty(hir_self_ty);
2571                let opt_res = self.lower_type_relative_ty_path(
2572                    self_ty,
2573                    hir_self_ty,
2574                    segment,
2575                    hir_id,
2576                    span,
2577                    PermitVariants::Yes,
2578                );
2579
2580                let (ty, _, res_def_id) = match opt_res {
2581                    Ok(r @ (_, DefKind::Variant | DefKind::Struct, _)) => r,
2582                    Ok(_) => return non_adt_or_variant_res(),
2583                    Err(e) => return ty::Const::new_error(tcx, e),
2584                };
2585
2586                (ty, res_def_id)
2587            }
2588        };
2589
2590        let ty::Adt(adt_def, adt_args) = ty.kind() else { ::core::panicking::panic("internal error: entered unreachable code")unreachable!() };
2591
2592        let variant_def = adt_def.variant_with_id(variant_did);
2593        let variant_idx = adt_def.variant_index_with_id(variant_did).as_u32();
2594
2595        let fields = variant_def
2596            .fields
2597            .iter()
2598            .map(|field_def| {
2599                // FIXME(mgca): we aren't really handling privacy, stability,
2600                // or macro hygeniene but we should.
2601                let mut init_expr =
2602                    inits.iter().filter(|init_expr| init_expr.field.name == field_def.name);
2603
2604                match init_expr.next() {
2605                    Some(expr) => {
2606                        if let Some(expr) = init_expr.next() {
2607                            let e = tcx.dcx().span_err(
2608                                expr.span,
2609                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("struct expression with multiple initialisers for `{0}`",
                field_def.name))
    })format!(
2610                                    "struct expression with multiple initialisers for `{}`",
2611                                    field_def.name,
2612                                ),
2613                            );
2614                            return ty::Const::new_error(tcx, e);
2615                        }
2616
2617                        self.lower_const_arg(
2618                            expr.expr,
2619                            tcx.type_of(field_def.did).instantiate(tcx, adt_args),
2620                        )
2621                    }
2622                    None => {
2623                        let e = tcx.dcx().span_err(
2624                            span,
2625                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("struct expression with missing field initialiser for `{0}`",
                field_def.name))
    })format!(
2626                                "struct expression with missing field initialiser for `{}`",
2627                                field_def.name
2628                            ),
2629                        );
2630                        ty::Const::new_error(tcx, e)
2631                    }
2632                }
2633            })
2634            .collect::<Vec<_>>();
2635
2636        let opt_discr_const = if adt_def.is_enum() {
2637            let valtree = ty::ValTree::from_scalar_int(tcx, variant_idx.into());
2638            Some(ty::Const::new_value(tcx, valtree, tcx.types.u32))
2639        } else {
2640            None
2641        };
2642
2643        let valtree = ty::ValTree::from_branches(tcx, opt_discr_const.into_iter().chain(fields));
2644        ty::Const::new_value(tcx, valtree, ty)
2645    }
2646
2647    /// Lower a [resolved][hir::QPath::Resolved] path to a (type-level) constant.
2648    fn lower_resolved_const_path(
2649        &self,
2650        opt_self_ty: Option<Ty<'tcx>>,
2651        path: &hir::Path<'tcx>,
2652        hir_id: HirId,
2653    ) -> Const<'tcx> {
2654        let tcx = self.tcx();
2655        let span = path.span;
2656        let ct = match path.res {
2657            Res::Def(DefKind::ConstParam, def_id) => {
2658                match (&opt_self_ty, &None) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(opt_self_ty, None);
2659                let _ = self.prohibit_generic_args(
2660                    path.segments.iter(),
2661                    GenericsArgsErrExtend::Param(def_id),
2662                );
2663                self.lower_const_param(def_id, hir_id)
2664            }
2665            Res::Def(DefKind::Const, did) => {
2666                if let Err(guar) = self.require_type_const_attribute(did, span) {
2667                    return Const::new_error(self.tcx(), guar);
2668                }
2669
2670                match (&opt_self_ty, &None) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(opt_self_ty, None);
2671                let [leading_segments @ .., segment] = path.segments else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
2672                let _ = self
2673                    .prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
2674                let args = self.lower_generic_args_of_path_segment(span, did, segment);
2675                ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
2676            }
2677            Res::Def(DefKind::Ctor(ctor_of, CtorKind::Const), did) => {
2678                match (&opt_self_ty, &None) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(opt_self_ty, None);
2679                let [leading_segments @ .., segment] = path.segments else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
2680                let _ = self
2681                    .prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
2682
2683                let parent_did = tcx.parent(did);
2684                let generics_did = match ctor_of {
2685                    CtorOf::Variant => tcx.parent(parent_did),
2686                    CtorOf::Struct => parent_did,
2687                };
2688                let args = self.lower_generic_args_of_path_segment(span, generics_did, segment);
2689
2690                self.construct_const_ctor_value(did, ctor_of, args)
2691            }
2692            Res::Def(DefKind::Ctor(_, CtorKind::Fn), did) => {
2693                match (&opt_self_ty, &None) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(opt_self_ty, None);
2694                let [leading_segments @ .., segment] = path.segments else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
2695                let _ = self
2696                    .prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
2697                let parent_did = tcx.parent(did);
2698                let generics_did = if let DefKind::Ctor(CtorOf::Variant, _) = tcx.def_kind(did) {
2699                    tcx.parent(parent_did)
2700                } else {
2701                    parent_did
2702                };
2703                let args = self.lower_generic_args_of_path_segment(span, generics_did, segment);
2704                ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args))
2705            }
2706            Res::Def(DefKind::AssocConst, did) => {
2707                let trait_segment = if let [modules @ .., trait_, _item] = path.segments {
2708                    let _ = self.prohibit_generic_args(modules.iter(), GenericsArgsErrExtend::None);
2709                    Some(trait_)
2710                } else {
2711                    None
2712                };
2713                self.lower_resolved_assoc_const_path(
2714                    span,
2715                    opt_self_ty,
2716                    did,
2717                    trait_segment,
2718                    path.segments.last().unwrap(),
2719                )
2720                .unwrap_or_else(|guar| Const::new_error(tcx, guar))
2721            }
2722            Res::Def(DefKind::Static { .. }, _) => {
2723                ::rustc_middle::util::bug::span_bug_fmt(span,
    format_args!("use of bare `static` ConstArgKind::Path\'s not yet supported"))span_bug!(span, "use of bare `static` ConstArgKind::Path's not yet supported")
2724            }
2725            // FIXME(const_generics): create real const to allow fn items as const paths
2726            Res::Def(DefKind::Fn | DefKind::AssocFn, did) => {
2727                self.dcx().span_delayed_bug(span, "function items cannot be used as const args");
2728                let args = self.lower_generic_args_of_path_segment(
2729                    span,
2730                    did,
2731                    path.segments.last().unwrap(),
2732                );
2733                ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args))
2734            }
2735
2736            // Exhaustive match to be clear about what exactly we're considering to be
2737            // an invalid Res for a const path.
2738            res @ (Res::Def(
2739                DefKind::Mod
2740                | DefKind::Enum
2741                | DefKind::Variant
2742                | DefKind::Struct
2743                | DefKind::OpaqueTy
2744                | DefKind::TyAlias
2745                | DefKind::TraitAlias
2746                | DefKind::AssocTy
2747                | DefKind::Union
2748                | DefKind::Trait
2749                | DefKind::ForeignTy
2750                | DefKind::TyParam
2751                | DefKind::Macro(_)
2752                | DefKind::LifetimeParam
2753                | DefKind::Use
2754                | DefKind::ForeignMod
2755                | DefKind::AnonConst
2756                | DefKind::InlineConst
2757                | DefKind::Field
2758                | DefKind::Impl { .. }
2759                | DefKind::Closure
2760                | DefKind::ExternCrate
2761                | DefKind::GlobalAsm
2762                | DefKind::SyntheticCoroutineBody,
2763                _,
2764            )
2765            | Res::PrimTy(_)
2766            | Res::SelfTyParam { .. }
2767            | Res::SelfTyAlias { .. }
2768            | Res::SelfCtor(_)
2769            | Res::Local(_)
2770            | Res::ToolMod
2771            | Res::NonMacroAttr(_)
2772            | Res::Err) => Const::new_error_with_message(
2773                tcx,
2774                span,
2775                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("invalid Res {0:?} for const path",
                res))
    })format!("invalid Res {res:?} for const path"),
2776            ),
2777        };
2778        self.check_param_uses_if_mcg(ct, span, false)
2779    }
2780
2781    /// Literals are eagerly converted to a constant, everything else becomes `Unevaluated`.
2782    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_const_arg_anon",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2782u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["anon"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&anon)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Const<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = self.tcx();
            let expr = &tcx.hir_body(anon.body).value;
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs:2787",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2787u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["expr"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&expr) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            let ty = tcx.type_of(anon.def_id).instantiate_identity();
            match self.try_lower_anon_const_lit(ty, expr) {
                Some(v) => v,
                None =>
                    ty::Const::new_unevaluated(tcx,
                        ty::UnevaluatedConst {
                            def: anon.def_id.to_def_id(),
                            args: ty::GenericArgs::identity_for_item(tcx,
                                anon.def_id.to_def_id()),
                        }),
            }
        }
    }
}#[instrument(skip(self), level = "debug")]
2783    fn lower_const_arg_anon(&self, anon: &AnonConst) -> Const<'tcx> {
2784        let tcx = self.tcx();
2785
2786        let expr = &tcx.hir_body(anon.body).value;
2787        debug!(?expr);
2788
2789        // FIXME(generic_const_parameter_types): We should use the proper generic args
2790        // here. It's only used as a hint for literals so doesn't matter too much to use the right
2791        // generic arguments, just weaker type inference.
2792        let ty = tcx.type_of(anon.def_id).instantiate_identity();
2793
2794        match self.try_lower_anon_const_lit(ty, expr) {
2795            Some(v) => v,
2796            None => ty::Const::new_unevaluated(
2797                tcx,
2798                ty::UnevaluatedConst {
2799                    def: anon.def_id.to_def_id(),
2800                    args: ty::GenericArgs::identity_for_item(tcx, anon.def_id.to_def_id()),
2801                },
2802            ),
2803        }
2804    }
2805
2806    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("lower_const_arg_literal",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2806u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["kind", "neg", "ty",
                                                    "span"], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&kind)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&neg as
                                                            &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Const<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = self.tcx();
            if let LitKind::Err(guar) = *kind {
                return ty::Const::new_error(tcx, guar);
            }
            let input = LitToConstInput { lit: *kind, ty, neg };
            match tcx.at(span).lit_to_const(input) {
                Some(value) =>
                    ty::Const::new_value(tcx, value.valtree, value.ty),
                None => {
                    let e =
                        tcx.dcx().span_err(span,
                            "type annotations needed for the literal");
                    ty::Const::new_error(tcx, e)
                }
            }
        }
    }
}#[instrument(skip(self), level = "debug")]
2807    fn lower_const_arg_literal(
2808        &self,
2809        kind: &LitKind,
2810        neg: bool,
2811        ty: Ty<'tcx>,
2812        span: Span,
2813    ) -> Const<'tcx> {
2814        let tcx = self.tcx();
2815        if let LitKind::Err(guar) = *kind {
2816            return ty::Const::new_error(tcx, guar);
2817        }
2818        let input = LitToConstInput { lit: *kind, ty, neg };
2819        match tcx.at(span).lit_to_const(input) {
2820            Some(value) => ty::Const::new_value(tcx, value.valtree, value.ty),
2821            None => {
2822                let e = tcx.dcx().span_err(span, "type annotations needed for the literal");
2823                ty::Const::new_error(tcx, e)
2824            }
2825        }
2826    }
2827
2828    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("try_lower_anon_const_lit",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(2828u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["ty", "expr"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ty)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Option<Const<'tcx>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = self.tcx();
            let expr =
                match &expr.kind {
                    hir::ExprKind::Block(block, _) if
                        block.stmts.is_empty() && block.expr.is_some() => {
                        block.expr.as_ref().unwrap()
                    }
                    _ => expr,
                };
            let lit_input =
                match expr.kind {
                    hir::ExprKind::Lit(lit) =>
                        Some(LitToConstInput { lit: lit.node, ty, neg: false }),
                    hir::ExprKind::Unary(hir::UnOp::Neg, expr) =>
                        match expr.kind {
                            hir::ExprKind::Lit(lit) =>
                                Some(LitToConstInput { lit: lit.node, ty, neg: true }),
                            _ => None,
                        },
                    _ => None,
                };
            lit_input.and_then(|l|
                    {
                        if const_lit_matches_ty(tcx, &l.lit, l.ty, l.neg) {
                            tcx.at(expr.span).lit_to_const(l).map(|value|
                                    ty::Const::new_value(tcx, value.valtree, value.ty))
                        } else { None }
                    })
        }
    }
}#[instrument(skip(self), level = "debug")]
2829    fn try_lower_anon_const_lit(
2830        &self,
2831        ty: Ty<'tcx>,
2832        expr: &'tcx hir::Expr<'tcx>,
2833    ) -> Option<Const<'tcx>> {
2834        let tcx = self.tcx();
2835
2836        // Unwrap a block, so that e.g. `{ 1 }` is recognised as a literal. This makes the
2837        // performance optimisation of directly lowering anon consts occur more often.
2838        let expr = match &expr.kind {
2839            hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
2840                block.expr.as_ref().unwrap()
2841            }
2842            _ => expr,
2843        };
2844
2845        let lit_input = match expr.kind {
2846            hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: false }),
2847            hir::ExprKind::Unary(hir::UnOp::Neg, expr) => match expr.kind {
2848                hir::ExprKind::Lit(lit) => Some(LitToConstInput { lit: lit.node, ty, neg: true }),
2849                _ => None,
2850            },
2851            _ => None,
2852        };
2853
2854        lit_input.and_then(|l| {
2855            if const_lit_matches_ty(tcx, &l.lit, l.ty, l.neg) {
2856                tcx.at(expr.span)
2857                    .lit_to_const(l)
2858                    .map(|value| ty::Const::new_value(tcx, value.valtree, value.ty))
2859            } else {
2860                None
2861            }
2862        })
2863    }
2864
2865    fn require_type_const_attribute(
2866        &self,
2867        def_id: DefId,
2868        span: Span,
2869    ) -> Result<(), ErrorGuaranteed> {
2870        let tcx = self.tcx();
2871        if tcx.is_type_const(def_id) {
2872            Ok(())
2873        } else {
2874            let mut err = self.dcx().struct_span_err(
2875                span,
2876                "use of `const` in the type system not defined as `type const`",
2877            );
2878            if def_id.is_local() {
2879                let name = tcx.def_path_str(def_id);
2880                err.span_suggestion(
2881                    tcx.def_span(def_id).shrink_to_lo(),
2882                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("add `type` before `const` for `{0}`",
                name))
    })format!("add `type` before `const` for `{name}`"),
2883                    ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("type ")) })format!("type "),
2884                    Applicability::MaybeIncorrect,
2885                );
2886            } else {
2887                err.note("only consts marked defined as `type const` may be used in types");
2888            }
2889            Err(err.emit())
2890        }
2891    }
2892
2893    fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> {
2894        let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id());
2895        match idx {
2896            hir::InferDelegationKind::Input(idx) => delegation_sig[idx],
2897            hir::InferDelegationKind::Output => *delegation_sig.last().unwrap(),
2898        }
2899    }
2900
2901    /// Lower a type from the HIR to our internal notion of a type.
2902    x;#[instrument(level = "debug", skip(self), ret)]
2903    pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
2904        let tcx = self.tcx();
2905
2906        let result_ty = match &hir_ty.kind {
2907            hir::TyKind::InferDelegation(_, idx) => self.lower_delegation_ty(*idx),
2908            hir::TyKind::Slice(ty) => Ty::new_slice(tcx, self.lower_ty(ty)),
2909            hir::TyKind::Ptr(mt) => Ty::new_ptr(tcx, self.lower_ty(mt.ty), mt.mutbl),
2910            hir::TyKind::Ref(region, mt) => {
2911                let r = self.lower_lifetime(region, RegionInferReason::Reference);
2912                debug!(?r);
2913                let t = self.lower_ty(mt.ty);
2914                Ty::new_ref(tcx, r, t, mt.mutbl)
2915            }
2916            hir::TyKind::Never => tcx.types.never,
2917            hir::TyKind::Tup(fields) => {
2918                Ty::new_tup_from_iter(tcx, fields.iter().map(|t| self.lower_ty(t)))
2919            }
2920            hir::TyKind::FnPtr(bf) => {
2921                check_c_variadic_abi(tcx, bf.decl, bf.abi, hir_ty.span);
2922
2923                Ty::new_fn_ptr(
2924                    tcx,
2925                    self.lower_fn_ty(hir_ty.hir_id, bf.safety, bf.abi, bf.decl, None, Some(hir_ty)),
2926                )
2927            }
2928            hir::TyKind::UnsafeBinder(binder) => Ty::new_unsafe_binder(
2929                tcx,
2930                ty::Binder::bind_with_vars(
2931                    self.lower_ty(binder.inner_ty),
2932                    tcx.late_bound_vars(hir_ty.hir_id),
2933                ),
2934            ),
2935            hir::TyKind::TraitObject(bounds, tagged_ptr) => {
2936                let lifetime = tagged_ptr.pointer();
2937                let syntax = tagged_ptr.tag();
2938                self.lower_trait_object_ty(hir_ty.span, hir_ty.hir_id, bounds, lifetime, syntax)
2939            }
2940            // If we encounter a fully qualified path with RTN generics, then it must have
2941            // *not* gone through `lower_ty_maybe_return_type_notation`, and therefore
2942            // it's certainly in an illegal position.
2943            hir::TyKind::Path(hir::QPath::Resolved(_, path))
2944                if path.segments.last().and_then(|segment| segment.args).is_some_and(|args| {
2945                    matches!(args.parenthesized, hir::GenericArgsParentheses::ReturnTypeNotation)
2946                }) =>
2947            {
2948                let guar = self.dcx().emit_err(BadReturnTypeNotation { span: hir_ty.span });
2949                Ty::new_error(tcx, guar)
2950            }
2951            hir::TyKind::Path(hir::QPath::Resolved(maybe_qself, path)) => {
2952                debug!(?maybe_qself, ?path);
2953                let opt_self_ty = maybe_qself.as_ref().map(|qself| self.lower_ty(qself));
2954                self.lower_resolved_ty_path(opt_self_ty, path, hir_ty.hir_id, PermitVariants::No)
2955            }
2956            &hir::TyKind::OpaqueDef(opaque_ty) => {
2957                // If this is an RPITIT and we are using the new RPITIT lowering scheme, we
2958                // generate the def_id of an associated type for the trait and return as
2959                // type a projection.
2960                let in_trait = match opaque_ty.origin {
2961                    hir::OpaqueTyOrigin::FnReturn {
2962                        parent,
2963                        in_trait_or_impl: Some(hir::RpitContext::Trait),
2964                        ..
2965                    }
2966                    | hir::OpaqueTyOrigin::AsyncFn {
2967                        parent,
2968                        in_trait_or_impl: Some(hir::RpitContext::Trait),
2969                        ..
2970                    } => Some(parent),
2971                    hir::OpaqueTyOrigin::FnReturn {
2972                        in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
2973                        ..
2974                    }
2975                    | hir::OpaqueTyOrigin::AsyncFn {
2976                        in_trait_or_impl: None | Some(hir::RpitContext::TraitImpl),
2977                        ..
2978                    }
2979                    | hir::OpaqueTyOrigin::TyAlias { .. } => None,
2980                };
2981
2982                self.lower_opaque_ty(opaque_ty.def_id, in_trait)
2983            }
2984            hir::TyKind::TraitAscription(hir_bounds) => {
2985                // Impl trait in bindings lower as an infer var with additional
2986                // set of type bounds.
2987                let self_ty = self.ty_infer(None, hir_ty.span);
2988                let mut bounds = Vec::new();
2989                self.lower_bounds(
2990                    self_ty,
2991                    hir_bounds.iter(),
2992                    &mut bounds,
2993                    ty::List::empty(),
2994                    PredicateFilter::All,
2995                    OverlappingAsssocItemConstraints::Allowed,
2996                );
2997                self.add_implicit_sizedness_bounds(
2998                    &mut bounds,
2999                    self_ty,
3000                    hir_bounds,
3001                    ImpliedBoundsContext::AssociatedTypeOrImplTrait,
3002                    hir_ty.span,
3003                );
3004                self.register_trait_ascription_bounds(bounds, hir_ty.hir_id, hir_ty.span);
3005                self_ty
3006            }
3007            // If we encounter a type relative path with RTN generics, then it must have
3008            // *not* gone through `lower_ty_maybe_return_type_notation`, and therefore
3009            // it's certainly in an illegal position.
3010            hir::TyKind::Path(hir::QPath::TypeRelative(_, segment))
3011                if segment.args.is_some_and(|args| {
3012                    matches!(args.parenthesized, hir::GenericArgsParentheses::ReturnTypeNotation)
3013                }) =>
3014            {
3015                let guar = self.dcx().emit_err(BadReturnTypeNotation { span: hir_ty.span });
3016                Ty::new_error(tcx, guar)
3017            }
3018            hir::TyKind::Path(hir::QPath::TypeRelative(hir_self_ty, segment)) => {
3019                debug!(?hir_self_ty, ?segment);
3020                let self_ty = self.lower_ty(hir_self_ty);
3021                self.lower_type_relative_ty_path(
3022                    self_ty,
3023                    hir_self_ty,
3024                    segment,
3025                    hir_ty.hir_id,
3026                    hir_ty.span,
3027                    PermitVariants::No,
3028                )
3029                .map(|(ty, _, _)| ty)
3030                .unwrap_or_else(|guar| Ty::new_error(tcx, guar))
3031            }
3032            hir::TyKind::Array(ty, length) => {
3033                let length = self.lower_const_arg(length, tcx.types.usize);
3034                Ty::new_array_with_const_len(tcx, self.lower_ty(ty), length)
3035            }
3036            hir::TyKind::Infer(()) => {
3037                // Infer also appears as the type of arguments or return
3038                // values in an ExprKind::Closure, or as
3039                // the type of local variables. Both of these cases are
3040                // handled specially and will not descend into this routine.
3041                self.ty_infer(None, hir_ty.span)
3042            }
3043            hir::TyKind::Pat(ty, pat) => {
3044                let ty_span = ty.span;
3045                let ty = self.lower_ty(ty);
3046                let pat_ty = match self.lower_pat_ty_pat(ty, ty_span, pat) {
3047                    Ok(kind) => Ty::new_pat(tcx, ty, tcx.mk_pat(kind)),
3048                    Err(guar) => Ty::new_error(tcx, guar),
3049                };
3050                self.record_ty(pat.hir_id, ty, pat.span);
3051                pat_ty
3052            }
3053            hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar),
3054        };
3055
3056        self.record_ty(hir_ty.hir_id, result_ty, hir_ty.span);
3057        result_ty
3058    }
3059
3060    fn lower_pat_ty_pat(
3061        &self,
3062        ty: Ty<'tcx>,
3063        ty_span: Span,
3064        pat: &hir::TyPat<'tcx>,
3065    ) -> Result<ty::PatternKind<'tcx>, ErrorGuaranteed> {
3066        let tcx = self.tcx();
3067        match pat.kind {
3068            hir::TyPatKind::Range(start, end) => {
3069                match ty.kind() {
3070                    // Keep this list of types in sync with the list of types that
3071                    // the `RangePattern` trait is implemented for.
3072                    ty::Int(_) | ty::Uint(_) | ty::Char => {
3073                        let start = self.lower_const_arg(start, ty);
3074                        let end = self.lower_const_arg(end, ty);
3075                        Ok(ty::PatternKind::Range { start, end })
3076                    }
3077                    _ => Err(self
3078                        .dcx()
3079                        .span_delayed_bug(ty_span, "invalid base type for range pattern")),
3080                }
3081            }
3082            hir::TyPatKind::NotNull => Ok(ty::PatternKind::NotNull),
3083            hir::TyPatKind::Or(patterns) => {
3084                self.tcx()
3085                    .mk_patterns_from_iter(patterns.iter().map(|pat| {
3086                        self.lower_pat_ty_pat(ty, ty_span, pat).map(|pat| tcx.mk_pat(pat))
3087                    }))
3088                    .map(ty::PatternKind::Or)
3089            }
3090            hir::TyPatKind::Err(e) => Err(e),
3091        }
3092    }
3093
3094    /// Lower an opaque type (i.e., an existential impl-Trait type) from the HIR.
3095    x;#[instrument(level = "debug", skip(self), ret)]
3096    fn lower_opaque_ty(&self, def_id: LocalDefId, in_trait: Option<LocalDefId>) -> Ty<'tcx> {
3097        let tcx = self.tcx();
3098
3099        let lifetimes = tcx.opaque_captured_lifetimes(def_id);
3100        debug!(?lifetimes);
3101
3102        // If this is an RPITIT and we are using the new RPITIT lowering scheme,
3103        // do a linear search to map this to the synthetic associated type that
3104        // it will be lowered to.
3105        let def_id = if let Some(parent_def_id) = in_trait {
3106            *tcx.associated_types_for_impl_traits_in_associated_fn(parent_def_id.to_def_id())
3107                .iter()
3108                .find(|rpitit| match tcx.opt_rpitit_info(**rpitit) {
3109                    Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
3110                        opaque_def_id.expect_local() == def_id
3111                    }
3112                    _ => unreachable!(),
3113                })
3114                .unwrap()
3115        } else {
3116            def_id.to_def_id()
3117        };
3118
3119        let generics = tcx.generics_of(def_id);
3120        debug!(?generics);
3121
3122        // We use `generics.count() - lifetimes.len()` here instead of `generics.parent_count`
3123        // since return-position impl trait in trait squashes all of the generics from its source fn
3124        // into its own generics, so the opaque's "own" params isn't always just lifetimes.
3125        let offset = generics.count() - lifetimes.len();
3126
3127        let args = ty::GenericArgs::for_item(tcx, def_id, |param, _| {
3128            if let Some(i) = (param.index as usize).checked_sub(offset) {
3129                let (lifetime, _) = lifetimes[i];
3130                // FIXME(mgca): should we be calling self.check_params_use_if_mcg here too?
3131                self.lower_resolved_lifetime(lifetime).into()
3132            } else {
3133                tcx.mk_param_from_def(param)
3134            }
3135        });
3136        debug!(?args);
3137
3138        if in_trait.is_some() {
3139            Ty::new_projection_from_args(tcx, def_id, args)
3140        } else {
3141            Ty::new_opaque(tcx, def_id, args)
3142        }
3143    }
3144
3145    /// Lower a function type from the HIR to our internal notion of a function signature.
3146    x;#[instrument(level = "debug", skip(self, hir_id, safety, abi, decl, generics, hir_ty), ret)]
3147    pub fn lower_fn_ty(
3148        &self,
3149        hir_id: HirId,
3150        safety: hir::Safety,
3151        abi: rustc_abi::ExternAbi,
3152        decl: &hir::FnDecl<'tcx>,
3153        generics: Option<&hir::Generics<'_>>,
3154        hir_ty: Option<&hir::Ty<'_>>,
3155    ) -> ty::PolyFnSig<'tcx> {
3156        let tcx = self.tcx();
3157        let bound_vars = tcx.late_bound_vars(hir_id);
3158        debug!(?bound_vars);
3159
3160        let (input_tys, output_ty) = self.lower_fn_sig(decl, generics, hir_id, hir_ty);
3161
3162        debug!(?output_ty);
3163
3164        let fn_ty = tcx.mk_fn_sig(input_tys, output_ty, decl.c_variadic, safety, abi);
3165        let fn_ptr_ty = ty::Binder::bind_with_vars(fn_ty, bound_vars);
3166
3167        if let hir::Node::Ty(hir::Ty { kind: hir::TyKind::FnPtr(fn_ptr_ty), span, .. }) =
3168            tcx.hir_node(hir_id)
3169        {
3170            check_abi(tcx, hir_id, *span, fn_ptr_ty.abi);
3171        }
3172
3173        // reject function types that violate cmse ABI requirements
3174        cmse::validate_cmse_abi(self.tcx(), self.dcx(), hir_id, abi, fn_ptr_ty);
3175
3176        if !fn_ptr_ty.references_error() {
3177            // Find any late-bound regions declared in return type that do
3178            // not appear in the arguments. These are not well-formed.
3179            //
3180            // Example:
3181            //     for<'a> fn() -> &'a str <-- 'a is bad
3182            //     for<'a> fn(&'a String) -> &'a str <-- 'a is ok
3183            let inputs = fn_ptr_ty.inputs();
3184            let late_bound_in_args =
3185                tcx.collect_constrained_late_bound_regions(inputs.map_bound(|i| i.to_owned()));
3186            let output = fn_ptr_ty.output();
3187            let late_bound_in_ret = tcx.collect_referenced_late_bound_regions(output);
3188
3189            self.validate_late_bound_regions(late_bound_in_args, late_bound_in_ret, |br_name| {
3190                struct_span_code_err!(
3191                    self.dcx(),
3192                    decl.output.span(),
3193                    E0581,
3194                    "return type references {}, which is not constrained by the fn input types",
3195                    br_name
3196                )
3197            });
3198        }
3199
3200        fn_ptr_ty
3201    }
3202
3203    /// Given a fn_hir_id for a impl function, suggest the type that is found on the
3204    /// corresponding function in the trait that the impl implements, if it exists.
3205    /// If arg_idx is Some, then it corresponds to an input type index, otherwise it
3206    /// corresponds to the return type.
3207    pub(super) fn suggest_trait_fn_ty_for_impl_fn_infer(
3208        &self,
3209        fn_hir_id: HirId,
3210        arg_idx: Option<usize>,
3211    ) -> Option<Ty<'tcx>> {
3212        let tcx = self.tcx();
3213        let hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), ident, .. }) =
3214            tcx.hir_node(fn_hir_id)
3215        else {
3216            return None;
3217        };
3218        let i = tcx.parent_hir_node(fn_hir_id).expect_item().expect_impl();
3219
3220        let trait_ref = self.lower_impl_trait_ref(&i.of_trait?.trait_ref, self.lower_ty(i.self_ty));
3221
3222        let assoc = tcx.associated_items(trait_ref.def_id).find_by_ident_and_kind(
3223            tcx,
3224            *ident,
3225            ty::AssocTag::Fn,
3226            trait_ref.def_id,
3227        )?;
3228
3229        let fn_sig = tcx.fn_sig(assoc.def_id).instantiate(
3230            tcx,
3231            trait_ref.args.extend_to(tcx, assoc.def_id, |param, _| tcx.mk_param_from_def(param)),
3232        );
3233        let fn_sig = tcx.liberate_late_bound_regions(fn_hir_id.expect_owner().to_def_id(), fn_sig);
3234
3235        Some(if let Some(arg_idx) = arg_idx {
3236            *fn_sig.inputs().get(arg_idx)?
3237        } else {
3238            fn_sig.output()
3239        })
3240    }
3241
3242    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("validate_late_bound_regions",
                                    "rustc_hir_analysis::hir_ty_lowering",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(3242u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering"),
                                    ::tracing_core::field::FieldSet::new(&["constrained_regions",
                                                    "referenced_regions"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&constrained_regions)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&referenced_regions)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            for br in referenced_regions.difference(&constrained_regions) {
                let br_name =
                    if let Some(name) = br.get_name(self.tcx()) {
                        ::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("lifetime `{0}`", name))
                            })
                    } else { "an anonymous lifetime".to_string() };
                let mut err = generate_err(&br_name);
                if !br.is_named(self.tcx()) {
                    err.note("lifetimes appearing in an associated or opaque type are not considered constrained");
                    err.note("consider introducing a named lifetime parameter");
                }
                err.emit();
            }
        }
    }
}#[instrument(level = "trace", skip(self, generate_err))]
3243    fn validate_late_bound_regions<'cx>(
3244        &'cx self,
3245        constrained_regions: FxIndexSet<ty::BoundRegionKind<'tcx>>,
3246        referenced_regions: FxIndexSet<ty::BoundRegionKind<'tcx>>,
3247        generate_err: impl Fn(&str) -> Diag<'cx>,
3248    ) {
3249        for br in referenced_regions.difference(&constrained_regions) {
3250            let br_name = if let Some(name) = br.get_name(self.tcx()) {
3251                format!("lifetime `{name}`")
3252            } else {
3253                "an anonymous lifetime".to_string()
3254            };
3255
3256            let mut err = generate_err(&br_name);
3257
3258            if !br.is_named(self.tcx()) {
3259                // The only way for an anonymous lifetime to wind up
3260                // in the return type but **also** be unconstrained is
3261                // if it only appears in "associated types" in the
3262                // input. See #47511 and #62200 for examples. In this case,
3263                // though we can easily give a hint that ought to be
3264                // relevant.
3265                err.note(
3266                    "lifetimes appearing in an associated or opaque type are not considered constrained",
3267                );
3268                err.note("consider introducing a named lifetime parameter");
3269            }
3270
3271            err.emit();
3272        }
3273    }
3274
3275    /// Given the bounds on an object, determines what single region bound (if any) we can
3276    /// use to summarize this type.
3277    ///
3278    /// The basic idea is that we will use the bound the user
3279    /// provided, if they provided one, and otherwise search the supertypes of trait bounds
3280    /// for region bounds. It may be that we can derive no bound at all, in which case
3281    /// we return `None`.
3282    x;#[instrument(level = "debug", skip(self, span), ret)]
3283    fn compute_object_lifetime_bound(
3284        &self,
3285        span: Span,
3286        existential_predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
3287    ) -> Option<ty::Region<'tcx>> // if None, use the default
3288    {
3289        let tcx = self.tcx();
3290
3291        // No explicit region bound specified. Therefore, examine trait
3292        // bounds and see if we can derive region bounds from those.
3293        let derived_region_bounds = object_region_bounds(tcx, existential_predicates);
3294
3295        // If there are no derived region bounds, then report back that we
3296        // can find no region bound. The caller will use the default.
3297        if derived_region_bounds.is_empty() {
3298            return None;
3299        }
3300
3301        // If any of the derived region bounds are 'static, that is always
3302        // the best choice.
3303        if derived_region_bounds.iter().any(|r| r.is_static()) {
3304            return Some(tcx.lifetimes.re_static);
3305        }
3306
3307        // Determine whether there is exactly one unique region in the set
3308        // of derived region bounds. If so, use that. Otherwise, report an
3309        // error.
3310        let r = derived_region_bounds[0];
3311        if derived_region_bounds[1..].iter().any(|r1| r != *r1) {
3312            self.dcx().emit_err(AmbiguousLifetimeBound { span });
3313        }
3314        Some(r)
3315    }
3316
3317    fn construct_const_ctor_value(
3318        &self,
3319        ctor_def_id: DefId,
3320        ctor_of: CtorOf,
3321        args: GenericArgsRef<'tcx>,
3322    ) -> Const<'tcx> {
3323        let tcx = self.tcx();
3324        let parent_did = tcx.parent(ctor_def_id);
3325
3326        let adt_def = tcx.adt_def(match ctor_of {
3327            CtorOf::Variant => tcx.parent(parent_did),
3328            CtorOf::Struct => parent_did,
3329        });
3330
3331        let variant_idx = adt_def.variant_index_with_id(parent_did);
3332
3333        let valtree = if adt_def.is_enum() {
3334            let discr = ty::ValTree::from_scalar_int(tcx, variant_idx.as_u32().into());
3335            ty::ValTree::from_branches(tcx, [ty::Const::new_value(tcx, discr, tcx.types.u32)])
3336        } else {
3337            ty::ValTree::zst(tcx)
3338        };
3339
3340        let adt_ty = Ty::new_adt(tcx, adt_def, args);
3341        ty::Const::new_value(tcx, valtree, adt_ty)
3342    }
3343}