Skip to main content

rustc_trait_selection/traits/query/
normalize.rs

1//! Code for the 'normalization' query. This consists of a wrapper
2//! which folds deeply, invoking the underlying
3//! `normalize_canonicalized_projection` query when it encounters projections.
4
5use rustc_data_structures::sso::SsoHashMap;
6use rustc_data_structures::stack::ensure_sufficient_stack;
7use rustc_infer::traits::PredicateObligations;
8use rustc_macros::extension;
9pub use rustc_middle::traits::query::NormalizationResult;
10use rustc_middle::ty::{
11    self, FallibleTypeFolder, Flags, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
12    TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, Unnormalized,
13};
14use rustc_span::DUMMY_SP;
15use tracing::{debug, info, instrument};
16
17use super::NoSolution;
18use crate::error_reporting::InferCtxtErrorExt;
19use crate::error_reporting::traits::OverflowCause;
20use crate::infer::at::At;
21use crate::infer::canonical::OriginalQueryValues;
22use crate::infer::{InferCtxt, InferOk};
23use crate::traits::normalize::needs_normalization;
24use crate::traits::{
25    BoundVarReplacer, Normalized, ObligationCause, PlaceholderReplacer, ScrubbedTraitError,
26};
27
28impl<'a, 'tcx> QueryNormalizeExt<'tcx> for At<'a, 'tcx> {
    #[doc = " Normalize `value` in the context of the inference context,"]
    #[doc = " yielding a resulting type, or an error if `value` cannot be"]
    #[doc =
    " normalized. If you don\'t care about regions, you should prefer"]
    #[doc = " `normalize_erasing_regions`, which is more efficient."]
    #[doc = ""]
    #[doc = " If the normalization succeeds, returns back the normalized"]
    #[doc = " value along with various outlives relations (in the form of"]
    #[doc = " obligations that must be discharged)."]
    #[doc = ""]
    #[doc =
    " This normalization should *only* be used when the projection is well-formed and"]
    #[doc =
    " does not have possible ambiguity (contains inference variables)."]
    #[doc = ""]
    #[doc =
    " After codegen, when lifetimes do not matter, it is preferable to instead"]
    #[doc =
    " use [`TyCtxt::normalize_erasing_regions`], which wraps this procedure."]
    #[doc = ""]
    #[doc =
    " N.B. Once the new solver is stabilized this method of normalization will"]
    #[doc =
    " likely be removed as trait solver operations are already cached by the query"]
    #[doc = " system making this redundant."]
    fn query_normalize<T>(self, value: T)
        -> Result<Normalized<'tcx, T>, NoSolution> where
        T: TypeFoldable<TyCtxt<'tcx>> {
        {
            use ::tracing::__macro_support::Callsite as _;
            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                {
                    static META: ::tracing::Metadata<'static> =
                        {
                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/query/normalize.rs:52",
                                "rustc_trait_selection::traits::query::normalize",
                                ::tracing::Level::DEBUG,
                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                                ::tracing_core::__macro_support::Option::Some(52u32),
                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                                ::tracing_core::field::FieldSet::new(&["message"],
                                    ::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(&format_args!("normalize::<{0}>(value={1:?}, param_env={2:?}, cause={3:?})",
                                                            std::any::type_name::<T>(), value, self.param_env,
                                                            self.cause) as &dyn Value))])
                    });
            } else { ; }
        };
        let universes =
            if value.has_escaping_bound_vars() {
                let mut max_visitor =
                    MaxEscapingBoundVarVisitor {
                        outer_index: ty::INNERMOST,
                        escaping: 0,
                    };
                value.visit_with(&mut max_visitor);
                ::alloc::vec::from_elem(None, max_visitor.escaping)
            } else { ::alloc::vec::Vec::new() };
        if self.infcx.next_trait_solver() {
            match crate::solve::deeply_normalize_with_skipped_universes::<_,
                        ScrubbedTraitError<'tcx>>(self,
                    Unnormalized::new_wip(value), universes) {
                Ok(value) => {
                    return Ok(Normalized {
                                value,
                                obligations: PredicateObligations::new(),
                            });
                }
                Err(_errors) => { return Err(NoSolution); }
            }
        }
        if !needs_normalization(self.infcx, &value) {
            return Ok(Normalized {
                        value,
                        obligations: PredicateObligations::new(),
                    });
        }
        let mut normalizer =
            QueryNormalizer {
                infcx: self.infcx,
                cause: self.cause,
                param_env: self.param_env,
                obligations: PredicateObligations::new(),
                cache: SsoHashMap::new(),
                anon_depth: 0,
                universes,
            };
        let result = value.try_fold_with(&mut normalizer);
        {
            use ::tracing::__macro_support::Callsite as _;
            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                {
                    static META: ::tracing::Metadata<'static> =
                        {
                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/query/normalize.rs:109",
                                "rustc_trait_selection::traits::query::normalize",
                                ::tracing::Level::INFO,
                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                                ::tracing_core::__macro_support::Option::Some(109u32),
                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                                ::tracing_core::field::FieldSet::new(&["message"],
                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                ::tracing::metadata::Kind::EVENT)
                        };
                    ::tracing::callsite::DefaultCallsite::new(&META)
                };
            let enabled =
                ::tracing::Level::INFO <=
                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                        ::tracing::Level::INFO <=
                            ::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(&format_args!("normalize::<{0}>: result={1:?} with {2} obligations",
                                                            std::any::type_name::<T>(), result,
                                                            normalizer.obligations.len()) as &dyn Value))])
                    });
            } else { ; }
        };
        {
            use ::tracing::__macro_support::Callsite as _;
            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                {
                    static META: ::tracing::Metadata<'static> =
                        {
                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/query/normalize.rs:115",
                                "rustc_trait_selection::traits::query::normalize",
                                ::tracing::Level::DEBUG,
                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                                ::tracing_core::__macro_support::Option::Some(115u32),
                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                                ::tracing_core::field::FieldSet::new(&["message"],
                                    ::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(&format_args!("normalize::<{0}>: obligations={1:?}",
                                                            std::any::type_name::<T>(), normalizer.obligations) as
                                                    &dyn Value))])
                    });
            } else { ; }
        };
        result.map(|value|
                Normalized { value, obligations: normalizer.obligations })
    }
}#[extension(pub trait QueryNormalizeExt<'tcx>)]
29impl<'a, 'tcx> At<'a, 'tcx> {
30    /// Normalize `value` in the context of the inference context,
31    /// yielding a resulting type, or an error if `value` cannot be
32    /// normalized. If you don't care about regions, you should prefer
33    /// `normalize_erasing_regions`, which is more efficient.
34    ///
35    /// If the normalization succeeds, returns back the normalized
36    /// value along with various outlives relations (in the form of
37    /// obligations that must be discharged).
38    ///
39    /// This normalization should *only* be used when the projection is well-formed and
40    /// does not have possible ambiguity (contains inference variables).
41    ///
42    /// After codegen, when lifetimes do not matter, it is preferable to instead
43    /// use [`TyCtxt::normalize_erasing_regions`], which wraps this procedure.
44    ///
45    /// N.B. Once the new solver is stabilized this method of normalization will
46    /// likely be removed as trait solver operations are already cached by the query
47    /// system making this redundant.
48    fn query_normalize<T>(self, value: T) -> Result<Normalized<'tcx, T>, NoSolution>
49    where
50        T: TypeFoldable<TyCtxt<'tcx>>,
51    {
52        debug!(
53            "normalize::<{}>(value={:?}, param_env={:?}, cause={:?})",
54            std::any::type_name::<T>(),
55            value,
56            self.param_env,
57            self.cause,
58        );
59
60        // This is actually a consequence by the way `normalize_erasing_regions` works currently.
61        // Because it needs to call the `normalize_generic_arg_after_erasing_regions`, it folds
62        // through tys and consts in a `TypeFoldable`. Importantly, it skips binders, leaving us
63        // with trying to normalize with escaping bound vars.
64        //
65        // Here, we just add the universes that we *would* have created had we passed through the binders.
66        //
67        // We *could* replace escaping bound vars eagerly here, but it doesn't seem really necessary.
68        // The rest of the code is already set up to be lazy about replacing bound vars,
69        // and only when we actually have to normalize.
70        let universes = if value.has_escaping_bound_vars() {
71            let mut max_visitor =
72                MaxEscapingBoundVarVisitor { outer_index: ty::INNERMOST, escaping: 0 };
73            value.visit_with(&mut max_visitor);
74            vec![None; max_visitor.escaping]
75        } else {
76            vec![]
77        };
78
79        if self.infcx.next_trait_solver() {
80            match crate::solve::deeply_normalize_with_skipped_universes::<_, ScrubbedTraitError<'tcx>>(
81                self,
82                Unnormalized::new_wip(value),
83                universes,
84            ) {
85                Ok(value) => {
86                    return Ok(Normalized { value, obligations: PredicateObligations::new() });
87                }
88                Err(_errors) => {
89                    return Err(NoSolution);
90                }
91            }
92        }
93
94        if !needs_normalization(self.infcx, &value) {
95            return Ok(Normalized { value, obligations: PredicateObligations::new() });
96        }
97
98        let mut normalizer = QueryNormalizer {
99            infcx: self.infcx,
100            cause: self.cause,
101            param_env: self.param_env,
102            obligations: PredicateObligations::new(),
103            cache: SsoHashMap::new(),
104            anon_depth: 0,
105            universes,
106        };
107
108        let result = value.try_fold_with(&mut normalizer);
109        info!(
110            "normalize::<{}>: result={:?} with {} obligations",
111            std::any::type_name::<T>(),
112            result,
113            normalizer.obligations.len(),
114        );
115        debug!(
116            "normalize::<{}>: obligations={:?}",
117            std::any::type_name::<T>(),
118            normalizer.obligations,
119        );
120        result.map(|value| Normalized { value, obligations: normalizer.obligations })
121    }
122}
123
124// Visitor to find the maximum escaping bound var
125struct MaxEscapingBoundVarVisitor {
126    // The index which would count as escaping
127    outer_index: ty::DebruijnIndex,
128    escaping: usize,
129}
130
131impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for MaxEscapingBoundVarVisitor {
132    fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(&mut self, t: &ty::Binder<'tcx, T>) {
133        self.outer_index.shift_in(1);
134        t.super_visit_with(self);
135        self.outer_index.shift_out(1);
136    }
137
138    #[inline]
139    fn visit_ty(&mut self, t: Ty<'tcx>) {
140        if t.outer_exclusive_binder() > self.outer_index {
141            self.escaping = self
142                .escaping
143                .max(t.outer_exclusive_binder().as_usize() - self.outer_index.as_usize());
144        }
145    }
146
147    #[inline]
148    fn visit_region(&mut self, r: ty::Region<'tcx>) {
149        match r.kind() {
150            ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _)
151                if debruijn > self.outer_index =>
152            {
153                self.escaping =
154                    self.escaping.max(debruijn.as_usize() - self.outer_index.as_usize());
155            }
156            _ => {}
157        }
158    }
159
160    fn visit_const(&mut self, ct: ty::Const<'tcx>) {
161        if ct.outer_exclusive_binder() > self.outer_index {
162            self.escaping = self
163                .escaping
164                .max(ct.outer_exclusive_binder().as_usize() - self.outer_index.as_usize());
165        }
166    }
167}
168
169struct QueryNormalizer<'a, 'tcx> {
170    infcx: &'a InferCtxt<'tcx>,
171    cause: &'a ObligationCause<'tcx>,
172    param_env: ty::ParamEnv<'tcx>,
173    obligations: PredicateObligations<'tcx>,
174    cache: SsoHashMap<Ty<'tcx>, Ty<'tcx>>,
175    anon_depth: usize,
176    universes: Vec<Option<ty::UniverseIndex>>,
177}
178
179impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'a, 'tcx> {
180    type Error = NoSolution;
181
182    fn cx(&self) -> TyCtxt<'tcx> {
183        self.infcx.tcx
184    }
185
186    fn try_fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
187        &mut self,
188        t: ty::Binder<'tcx, T>,
189    ) -> Result<ty::Binder<'tcx, T>, Self::Error> {
190        self.universes.push(None);
191        let t = t.try_super_fold_with(self);
192        self.universes.pop();
193        t
194    }
195
196    #[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_fold_ty",
                                    "rustc_trait_selection::traits::query::normalize",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                                    ::tracing_core::__macro_support::Option::Some(196u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                                    ::tracing_core::field::FieldSet::new(&["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(&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: Result<Ty<'tcx>, Self::Error> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            if !needs_normalization(self.infcx, &ty) { return Ok(ty); }
            if let Some(ty) = self.cache.get(&ty) { return Ok(*ty); }
            let &ty::Alias(data) =
                ty.kind() else {
                    let res = ty.try_super_fold_with(self)?;
                    self.cache.insert(ty, res);
                    return Ok(res);
                };
            let res =
                match data.kind {
                    ty::Opaque { def_id } => {
                        match self.infcx.typing_mode_raw().assert_not_erased() {
                            TypingMode::Coherence | TypingMode::Typeck { .. } |
                                TypingMode::PostTypeckUntilBorrowck { .. } |
                                TypingMode::PostBorrowck { .. } =>
                                ty.try_super_fold_with(self)?,
                            TypingMode::PostAnalysis | TypingMode::Codegen => {
                                let args = data.args.try_fold_with(self)?;
                                let recursion_limit = self.cx().recursion_limit();
                                if !recursion_limit.value_within_limit(self.anon_depth) {
                                    let guar =
                                        self.infcx.err_ctxt().build_overflow_error(OverflowCause::DeeplyNormalize(data.into()),
                                                self.cause.span, true).delay_as_bug();
                                    return Ok(Ty::new_error(self.cx(), guar));
                                }
                                let generic_ty = self.cx().type_of(def_id);
                                let mut concrete_ty =
                                    generic_ty.instantiate(self.cx(), args).skip_norm_wip();
                                self.anon_depth += 1;
                                if concrete_ty == ty {
                                    concrete_ty =
                                        Ty::new_error_with_message(self.cx(), DUMMY_SP,
                                            "recursive opaque type");
                                }
                                let folded_ty =
                                    ensure_sufficient_stack(|| self.try_fold_ty(concrete_ty));
                                self.anon_depth -= 1;
                                folded_ty?
                            }
                        }
                    }
                    kind @
                        (ty::Projection { .. } | ty::Inherent { .. } | ty::Free { ..
                        }) =>
                        self.try_fold_free_or_assoc(ty::AliasTerm::new(self.cx(),
                                        kind.into(), data.args))?.expect_type(),
                };
            self.cache.insert(ty, res);
            Ok(res)
        }
    }
}#[instrument(level = "debug", skip(self))]
197    fn try_fold_ty(&mut self, ty: Ty<'tcx>) -> Result<Ty<'tcx>, Self::Error> {
198        if !needs_normalization(self.infcx, &ty) {
199            return Ok(ty);
200        }
201
202        if let Some(ty) = self.cache.get(&ty) {
203            return Ok(*ty);
204        }
205
206        let &ty::Alias(data) = ty.kind() else {
207            let res = ty.try_super_fold_with(self)?;
208            self.cache.insert(ty, res);
209            return Ok(res);
210        };
211
212        // See note in `rustc_trait_selection::traits::project` about why we
213        // wait to fold the args.
214        let res = match data.kind {
215            ty::Opaque { def_id } => {
216                // Only normalize `impl Trait` outside of type inference, usually in codegen.
217                match self.infcx.typing_mode_raw().assert_not_erased() {
218                    TypingMode::Coherence
219                    | TypingMode::Typeck { .. }
220                    | TypingMode::PostTypeckUntilBorrowck { .. }
221                    | TypingMode::PostBorrowck { .. } => ty.try_super_fold_with(self)?,
222
223                    TypingMode::PostAnalysis | TypingMode::Codegen => {
224                        let args = data.args.try_fold_with(self)?;
225                        let recursion_limit = self.cx().recursion_limit();
226
227                        if !recursion_limit.value_within_limit(self.anon_depth) {
228                            let guar = self
229                                .infcx
230                                .err_ctxt()
231                                .build_overflow_error(
232                                    OverflowCause::DeeplyNormalize(data.into()),
233                                    self.cause.span,
234                                    true,
235                                )
236                                .delay_as_bug();
237                            return Ok(Ty::new_error(self.cx(), guar));
238                        }
239
240                        let generic_ty = self.cx().type_of(def_id);
241                        let mut concrete_ty =
242                            generic_ty.instantiate(self.cx(), args).skip_norm_wip();
243                        self.anon_depth += 1;
244                        if concrete_ty == ty {
245                            concrete_ty = Ty::new_error_with_message(
246                                self.cx(),
247                                DUMMY_SP,
248                                "recursive opaque type",
249                            );
250                        }
251                        let folded_ty = ensure_sufficient_stack(|| self.try_fold_ty(concrete_ty));
252                        self.anon_depth -= 1;
253                        folded_ty?
254                    }
255                }
256            }
257
258            kind @ (ty::Projection { .. } | ty::Inherent { .. } | ty::Free { .. }) => self
259                .try_fold_free_or_assoc(ty::AliasTerm::new(self.cx(), kind.into(), data.args))?
260                .expect_type(),
261        };
262
263        self.cache.insert(ty, res);
264        Ok(res)
265    }
266
267    fn try_fold_const(
268        &mut self,
269        constant: ty::Const<'tcx>,
270    ) -> Result<ty::Const<'tcx>, Self::Error> {
271        if !needs_normalization(self.infcx, &constant) {
272            return Ok(constant);
273        }
274
275        let uv = match constant.kind() {
276            ty::ConstKind::Unevaluated(uv) => uv,
277            _ => return constant.try_super_fold_with(self),
278        };
279
280        let constant = match uv.kind {
281            ty::UnevaluatedConstKind::Anon { .. } => {
282                crate::traits::with_replaced_escaping_bound_vars(
283                    self.infcx,
284                    &mut self.universes,
285                    constant,
286                    |constant| crate::traits::evaluate_const(&self.infcx, constant, self.param_env),
287                )
288            }
289            _ => self.try_fold_free_or_assoc(uv.into())?.expect_const(),
290        };
291        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/query/normalize.rs:291",
                        "rustc_trait_selection::traits::query::normalize",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                        ::tracing_core::__macro_support::Option::Some(291u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                        ::tracing_core::field::FieldSet::new(&["constant",
                                        "self.param_env"],
                            ::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(&constant)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&self.param_env)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?constant, ?self.param_env);
292        constant.try_super_fold_with(self)
293    }
294
295    #[inline]
296    fn try_fold_predicate(
297        &mut self,
298        p: ty::Predicate<'tcx>,
299    ) -> Result<ty::Predicate<'tcx>, Self::Error> {
300        if p.allow_normalization() && needs_normalization(self.infcx, &p) {
301            p.try_super_fold_with(self)
302        } else {
303            Ok(p)
304        }
305    }
306}
307
308impl<'a, 'tcx> QueryNormalizer<'a, 'tcx> {
309    fn try_fold_free_or_assoc(
310        &mut self,
311        term: ty::AliasTerm<'tcx>,
312    ) -> Result<ty::Term<'tcx>, NoSolution> {
313        let infcx = self.infcx;
314        let tcx = infcx.tcx;
315        // Just an optimization: When we don't have escaping bound vars,
316        // we don't need to replace them with placeholders.
317        let (term, maps) = if term.has_escaping_bound_vars() {
318            let (term, mapped_regions, mapped_types, mapped_consts) =
319                BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, term);
320            (term, Some((mapped_regions, mapped_types, mapped_consts)))
321        } else {
322            (term, None)
323        };
324        let term = term.try_fold_with(self)?;
325
326        let mut orig_values = OriginalQueryValues::default();
327        let c_term = infcx.canonicalize_query(self.param_env.and(term), &mut orig_values);
328        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/query/normalize.rs:328",
                        "rustc_trait_selection::traits::query::normalize",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                        ::tracing_core::__macro_support::Option::Some(328u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("QueryNormalizer: c_term = {0:#?}",
                                                    c_term) as &dyn Value))])
            });
    } else { ; }
};debug!("QueryNormalizer: c_term = {:#?}", c_term);
329        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/query/normalize.rs:329",
                        "rustc_trait_selection::traits::query::normalize",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                        ::tracing_core::__macro_support::Option::Some(329u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("QueryNormalizer: orig_values = {0:#?}",
                                                    orig_values) as &dyn Value))])
            });
    } else { ; }
};debug!("QueryNormalizer: orig_values = {:#?}", orig_values);
330        let result = match term.kind {
331            ty::AliasTermKind::ProjectionTy { .. } | ty::AliasTermKind::ProjectionConst { .. } => {
332                tcx.normalize_canonicalized_projection(c_term)
333            }
334            ty::AliasTermKind::FreeTy { .. } | ty::AliasTermKind::FreeConst { .. } => {
335                tcx.normalize_canonicalized_free_alias(c_term)
336            }
337            ty::AliasTermKind::InherentTy { .. } | ty::AliasTermKind::InherentConst { .. } => {
338                tcx.normalize_canonicalized_inherent_projection(c_term)
339            }
340            kind @ (ty::AliasTermKind::OpaqueTy { .. } | ty::AliasTermKind::AnonConst { .. }) => {
341                {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("did not expect {0:?} due to match arm above",
                kind)));
}unreachable!("did not expect {kind:?} due to match arm above")
342            }
343        }?;
344        // We don't expect ambiguity.
345        if !result.value.is_proven() {
346            // Rustdoc normalizes possibly not well-formed types, so only
347            // treat this as a bug if we're not in rustdoc.
348            if !tcx.sess.opts.actually_rustdoc {
349                tcx.dcx().delayed_bug(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unexpected ambiguity: {0:?} {1:?}",
                c_term, result))
    })format!("unexpected ambiguity: {c_term:?} {result:?}"));
350            }
351            return Err(NoSolution);
352        }
353        let InferOk { value: result, obligations } = infcx
354            .instantiate_query_response_and_region_obligations(
355                self.cause,
356                self.param_env,
357                &orig_values,
358                result,
359            )?;
360        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/query/normalize.rs:360",
                        "rustc_trait_selection::traits::query::normalize",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                        ::tracing_core::__macro_support::Option::Some(360u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("QueryNormalizer: result = {0:#?}",
                                                    result) as &dyn Value))])
            });
    } else { ; }
};debug!("QueryNormalizer: result = {:#?}", result);
361        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/query/normalize.rs:361",
                        "rustc_trait_selection::traits::query::normalize",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/query/normalize.rs"),
                        ::tracing_core::__macro_support::Option::Some(361u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::query::normalize"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("QueryNormalizer: obligations = {0:#?}",
                                                    obligations) as &dyn Value))])
            });
    } else { ; }
};debug!("QueryNormalizer: obligations = {:#?}", obligations);
362        self.obligations.extend(obligations);
363        let res = if let Some((mapped_regions, mapped_types, mapped_consts)) = maps {
364            PlaceholderReplacer::replace_placeholders(
365                infcx,
366                mapped_regions,
367                mapped_types,
368                mapped_consts,
369                &self.universes,
370                result.normalized_term,
371            )
372        } else {
373            result.normalized_term
374        };
375        // `tcx.normalize_canonicalized_projection` may normalize to a type that
376        // still has unevaluated consts, so keep normalizing here if that's the case.
377        // Similarly, `tcx.normalize_canonicalized_free_alias` will only unwrap one layer
378        // of type/const and we need to continue folding it to reveal the TAIT behind it
379        // or further normalize nested unevaluated consts.
380        if res != term.to_term(tcx)
381            && (res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION)
382                || #[allow(non_exhaustive_omitted_patterns)] match term.kind {
    ty::AliasTermKind::FreeTy { .. } | ty::AliasTermKind::FreeConst { .. } =>
        true,
    _ => false,
}matches!(
383                    term.kind,
384                    ty::AliasTermKind::FreeTy { .. } | ty::AliasTermKind::FreeConst { .. }
385                ))
386        {
387            res.try_fold_with(self)
388        } else {
389            Ok(res)
390        }
391    }
392}