Skip to main content

rustc_trait_selection/traits/
normalize.rs

1//! Deeply normalize types using the old trait solver.
2
3use rustc_data_structures::stack::ensure_sufficient_stack;
4use rustc_errors::msg;
5use rustc_hir::def::DefKind;
6use rustc_infer::infer::at::At;
7use rustc_infer::infer::{InferCtxt, InferOk};
8use rustc_infer::traits::{
9    FromSolverError, Normalized, Obligation, PredicateObligations, TraitEngine,
10};
11use rustc_macros::extension;
12use rustc_middle::span_bug;
13use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
14use rustc_middle::ty::{
15    self, AliasTerm, Term, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable,
16    TypeVisitableExt, TypingMode, Unnormalized,
17};
18use tracing::{debug, instrument};
19
20use super::{BoundVarReplacer, PlaceholderReplacer, SelectionContext, project};
21use crate::error_reporting::InferCtxtErrorExt;
22use crate::error_reporting::traits::OverflowCause;
23use crate::solve::NextSolverError;
24
25impl<'tcx> NormalizeExt<'tcx> for At<'_, 'tcx> {
    #[doc = " Normalize a value using the `AssocTypeNormalizer`."]
    #[doc = ""]
    #[doc =
    " This normalization should be used when the type contains inference variables or the"]
    #[doc = " projection may be fallible."]
    fn normalize<T: TypeFoldable<TyCtxt<'tcx>>>(&self,
        value: Unnormalized<'tcx, T>) -> InferOk<'tcx, T> {
        let value = value.skip_normalization();
        if self.infcx.next_trait_solver() {
            InferOk { value, obligations: PredicateObligations::new() }
        } else {
            let mut selcx = SelectionContext::new(self.infcx);
            let Normalized { value, obligations } =
                normalize_with_depth(&mut selcx, self.param_env,
                    self.cause.clone(), 0, value);
            InferOk { value, obligations }
        }
    }
    #[doc =
    " Deeply normalizes `value`, replacing all aliases which can by normalized in"]
    #[doc =
    " the current environment. In the new solver this errors in case normalization"]
    #[doc = " fails or is ambiguous."]
    #[doc = ""]
    #[doc =
    " In the old solver this simply uses `normalizes` and adds the nested obligations"]
    #[doc =
    " to the `fulfill_cx`. This is necessary as we otherwise end up recomputing the"]
    #[doc =
    " same goals in both a temporary and the shared context which negatively impacts"]
    #[doc = " performance as these don\'t share caching."]
    #[doc = ""]
    #[doc =
    " FIXME(-Znext-solver=no): For performance reasons, we currently reuse an existing"]
    #[doc =
    " fulfillment context in the old solver. Once we have removed the old solver, we"]
    #[doc = " can remove the `fulfill_cx` parameter on this function."]
    fn deeply_normalize<T,
        E>(self, value: Unnormalized<'tcx, T>,
        fulfill_cx: &mut dyn TraitEngine<'tcx, E>) -> Result<T, Vec<E>> where
        T: TypeFoldable<TyCtxt<'tcx>>,
        E: FromSolverError<'tcx, NextSolverError<'tcx>> {
        if self.infcx.next_trait_solver() {
            crate::solve::deeply_normalize(self, value)
        } else {
            if fulfill_cx.has_pending_obligations() {
                let pending_obligations = fulfill_cx.pending_obligations();
                ::rustc_middle::util::bug::span_bug_fmt(pending_obligations[0].cause.span,
                    format_args!("deeply_normalize should not be called with pending obligations: {0:#?}",
                        pending_obligations));
            }
            let value =
                self.normalize(value).into_value_registering_obligations(self.infcx,
                    &mut *fulfill_cx);
            let errors =
                fulfill_cx.evaluate_obligations_error_on_ambiguity(self.infcx);
            let value = self.infcx.resolve_vars_if_possible(value);
            if errors.is_empty() {
                Ok(value)
            } else {
                let _ = fulfill_cx.collect_remaining_errors(self.infcx);
                Err(errors)
            }
        }
    }
}#[extension(pub trait NormalizeExt<'tcx>)]
26impl<'tcx> At<'_, 'tcx> {
27    /// Normalize a value using the `AssocTypeNormalizer`.
28    ///
29    /// This normalization should be used when the type contains inference variables or the
30    /// projection may be fallible.
31    fn normalize<T: TypeFoldable<TyCtxt<'tcx>>>(
32        &self,
33        value: Unnormalized<'tcx, T>,
34    ) -> InferOk<'tcx, T> {
35        let value = value.skip_normalization();
36        if self.infcx.next_trait_solver() {
37            InferOk { value, obligations: PredicateObligations::new() }
38        } else {
39            let mut selcx = SelectionContext::new(self.infcx);
40            let Normalized { value, obligations } =
41                normalize_with_depth(&mut selcx, self.param_env, self.cause.clone(), 0, value);
42            InferOk { value, obligations }
43        }
44    }
45
46    /// Deeply normalizes `value`, replacing all aliases which can by normalized in
47    /// the current environment. In the new solver this errors in case normalization
48    /// fails or is ambiguous.
49    ///
50    /// In the old solver this simply uses `normalizes` and adds the nested obligations
51    /// to the `fulfill_cx`. This is necessary as we otherwise end up recomputing the
52    /// same goals in both a temporary and the shared context which negatively impacts
53    /// performance as these don't share caching.
54    ///
55    /// FIXME(-Znext-solver=no): For performance reasons, we currently reuse an existing
56    /// fulfillment context in the old solver. Once we have removed the old solver, we
57    /// can remove the `fulfill_cx` parameter on this function.
58    fn deeply_normalize<T, E>(
59        self,
60        value: Unnormalized<'tcx, T>,
61        fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
62    ) -> Result<T, Vec<E>>
63    where
64        T: TypeFoldable<TyCtxt<'tcx>>,
65        E: FromSolverError<'tcx, NextSolverError<'tcx>>,
66    {
67        if self.infcx.next_trait_solver() {
68            crate::solve::deeply_normalize(self, value)
69        } else {
70            if fulfill_cx.has_pending_obligations() {
71                let pending_obligations = fulfill_cx.pending_obligations();
72                span_bug!(
73                    pending_obligations[0].cause.span,
74                    "deeply_normalize should not be called with pending obligations: \
75                    {pending_obligations:#?}"
76                );
77            }
78            let value = self
79                .normalize(value)
80                .into_value_registering_obligations(self.infcx, &mut *fulfill_cx);
81            let errors = fulfill_cx.evaluate_obligations_error_on_ambiguity(self.infcx);
82            let value = self.infcx.resolve_vars_if_possible(value);
83            if errors.is_empty() {
84                Ok(value)
85            } else {
86                // Drop pending obligations, since deep normalization may happen
87                // in a loop and we don't want to trigger the assertion on the next
88                // iteration due to pending ambiguous obligations we've left over.
89                let _ = fulfill_cx.collect_remaining_errors(self.infcx);
90                Err(errors)
91            }
92        }
93    }
94}
95
96/// As `normalize`, but with a custom depth.
97pub(crate) fn normalize_with_depth<'a, 'b, 'tcx, T>(
98    selcx: &'a mut SelectionContext<'b, 'tcx>,
99    param_env: ty::ParamEnv<'tcx>,
100    cause: ObligationCause<'tcx>,
101    depth: usize,
102    value: T,
103) -> Normalized<'tcx, T>
104where
105    T: TypeFoldable<TyCtxt<'tcx>>,
106{
107    let mut obligations = PredicateObligations::new();
108    let value = normalize_with_depth_to(selcx, param_env, cause, depth, value, &mut obligations);
109    Normalized { value, obligations }
110}
111
112#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
                ::tracing::Level::INFO <=
                    ::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("normalize_with_depth_to",
                                    "rustc_trait_selection::traits::normalize",
                                    ::tracing::Level::INFO,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/normalize.rs"),
                                    ::tracing_core::__macro_support::Option::Some(112u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::normalize"),
                                    ::tracing_core::field::FieldSet::new(&["depth", "value"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::INFO <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::INFO <=
                                    ::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(&depth 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(&value)
                                                            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: T = 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_trait_selection/src/traits/normalize.rs:124",
                                    "rustc_trait_selection::traits::normalize",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/normalize.rs"),
                                    ::tracing_core::__macro_support::Option::Some(124u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::normalize"),
                                    ::tracing_core::field::FieldSet::new(&["obligations.len"],
                                        ::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(&obligations.len()
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let mut normalizer =
                AssocTypeNormalizer::new(selcx, param_env, cause, depth,
                    obligations);
            let result =
                ensure_sufficient_stack(||
                        AssocTypeNormalizer::fold(&mut normalizer, 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_trait_selection/src/traits/normalize.rs:127",
                                    "rustc_trait_selection::traits::normalize",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/normalize.rs"),
                                    ::tracing_core::__macro_support::Option::Some(127u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::normalize"),
                                    ::tracing_core::field::FieldSet::new(&["result",
                                                    "obligations.len"],
                                        ::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(&result) as
                                                        &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&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/normalize.rs:128",
                                    "rustc_trait_selection::traits::normalize",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/normalize.rs"),
                                    ::tracing_core::__macro_support::Option::Some(128u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::normalize"),
                                    ::tracing_core::field::FieldSet::new(&["normalizer.obligations"],
                                        ::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(&normalizer.obligations)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            result
        }
    }
}#[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
113pub(crate) fn normalize_with_depth_to<'a, 'b, 'tcx, T>(
114    selcx: &'a mut SelectionContext<'b, 'tcx>,
115    param_env: ty::ParamEnv<'tcx>,
116    cause: ObligationCause<'tcx>,
117    depth: usize,
118    value: T,
119    obligations: &mut PredicateObligations<'tcx>,
120) -> T
121where
122    T: TypeFoldable<TyCtxt<'tcx>>,
123{
124    debug!(obligations.len = obligations.len());
125    let mut normalizer = AssocTypeNormalizer::new(selcx, param_env, cause, depth, obligations);
126    let result = ensure_sufficient_stack(|| AssocTypeNormalizer::fold(&mut normalizer, value));
127    debug!(?result, obligations.len = normalizer.obligations.len());
128    debug!(?normalizer.obligations,);
129    result
130}
131
132pub(super) fn needs_normalization<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
133    infcx: &InferCtxt<'tcx>,
134    value: &T,
135) -> bool {
136    let mut flags = ty::TypeFlags::HAS_ALIAS;
137
138    // Opaques are treated as rigid outside of `TypingMode::PostAnalysis`,
139    // so we can ignore those.
140    match infcx.typing_mode() {
141        // FIXME(#132279): We likely want to reveal opaques during post borrowck analysis
142        TypingMode::Coherence
143        | TypingMode::Analysis { .. }
144        | TypingMode::Borrowck { .. }
145        | TypingMode::PostBorrowckAnalysis { .. } => flags.remove(ty::TypeFlags::HAS_TY_OPAQUE),
146        TypingMode::PostAnalysis => {}
147    }
148
149    value.has_type_flags(flags)
150}
151
152struct AssocTypeNormalizer<'a, 'b, 'tcx> {
153    selcx: &'a mut SelectionContext<'b, 'tcx>,
154    param_env: ty::ParamEnv<'tcx>,
155    cause: ObligationCause<'tcx>,
156    obligations: &'a mut PredicateObligations<'tcx>,
157    depth: usize,
158    universes: Vec<Option<ty::UniverseIndex>>,
159}
160
161impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
162    fn new(
163        selcx: &'a mut SelectionContext<'b, 'tcx>,
164        param_env: ty::ParamEnv<'tcx>,
165        cause: ObligationCause<'tcx>,
166        depth: usize,
167        obligations: &'a mut PredicateObligations<'tcx>,
168    ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
169        if true {
    if !!selcx.infcx.next_trait_solver() {
        ::core::panicking::panic("assertion failed: !selcx.infcx.next_trait_solver()")
    };
};debug_assert!(!selcx.infcx.next_trait_solver());
170        AssocTypeNormalizer { selcx, param_env, cause, obligations, depth, universes: ::alloc::vec::Vec::new()vec![] }
171    }
172
173    fn fold<T: TypeFoldable<TyCtxt<'tcx>>>(&mut self, value: T) -> T {
174        let value = self.selcx.infcx.resolve_vars_if_possible(value);
175        {
    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/normalize.rs:175",
                        "rustc_trait_selection::traits::normalize",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/normalize.rs"),
                        ::tracing_core::__macro_support::Option::Some(175u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::normalize"),
                        ::tracing_core::field::FieldSet::new(&["value"],
                            ::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(&value) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?value);
176
177        if !!value.has_escaping_bound_vars() {
    {
        ::core::panicking::panic_fmt(format_args!("Normalizing {0:?} without wrapping in a `Binder`",
                value));
    }
};assert!(
178            !value.has_escaping_bound_vars(),
179            "Normalizing {value:?} without wrapping in a `Binder`"
180        );
181
182        if !needs_normalization(self.selcx.infcx, &value) { value } else { value.fold_with(self) }
183    }
184
185    // FIXME(mgca): While this supports constants, it is only used for types by default right now
186    x;#[instrument(level = "debug", skip(self), ret)]
187    fn normalize_trait_projection(&mut self, proj: AliasTerm<'tcx>) -> Term<'tcx> {
188        if !proj.has_escaping_bound_vars() {
189            // When we don't have escaping bound vars we can normalize ambig aliases
190            // to inference variables (done in `normalize_projection_ty`). This would
191            // be wrong if there were escaping bound vars as even if we instantiated
192            // the bound vars with placeholders, we wouldn't be able to map them back
193            // after normalization succeeded.
194            //
195            // Also, as an optimization: when we don't have escaping bound vars, we don't
196            // need to replace them with placeholders (see branch below).
197            let proj = proj.fold_with(self);
198            project::normalize_projection_term(
199                self.selcx,
200                self.param_env,
201                proj,
202                self.cause.clone(),
203                self.depth,
204                self.obligations,
205            )
206        } else {
207            // If there are escaping bound vars, we temporarily replace the
208            // bound vars with placeholders. Note though, that in the case
209            // that we still can't project for whatever reason (e.g. self
210            // type isn't known enough), we *can't* register an obligation
211            // and return an inference variable (since then that obligation
212            // would have bound vars and that's a can of worms). Instead,
213            // we just give up and fall back to pretending like we never tried!
214            //
215            // Note: this isn't necessarily the final approach here; we may
216            // want to figure out how to register obligations with escaping vars
217            // or handle this some other way.
218            let infcx = self.selcx.infcx;
219            let (proj, mapped_regions, mapped_types, mapped_consts) =
220                BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, proj);
221            let proj = proj.fold_with(self);
222            let normalized_term = project::opt_normalize_projection_term(
223                self.selcx,
224                self.param_env,
225                proj,
226                self.cause.clone(),
227                self.depth,
228                self.obligations,
229            )
230            .ok()
231            .flatten()
232            .unwrap_or_else(|| proj.to_term(infcx.tcx));
233
234            PlaceholderReplacer::replace_placeholders(
235                infcx,
236                mapped_regions,
237                mapped_types,
238                mapped_consts,
239                &self.universes,
240                normalized_term,
241            )
242        }
243    }
244
245    // FIXME(mgca): While this supports constants, it is only used for types by default right now
246    x;#[instrument(level = "debug", skip(self), ret)]
247    fn normalize_inherent_projection(&mut self, inherent: AliasTerm<'tcx>) -> Term<'tcx> {
248        if !inherent.has_escaping_bound_vars() {
249            // When we don't have escaping bound vars we can normalize ambig aliases
250            // to inference variables (done in `normalize_projection_ty`). This would
251            // be wrong if there were escaping bound vars as even if we instantiated
252            // the bound vars with placeholders, we wouldn't be able to map them back
253            // after normalization succeeded.
254            //
255            // Also, as an optimization: when we don't have escaping bound vars, we don't
256            // need to replace them with placeholders (see branch below).
257
258            let inherent = inherent.fold_with(self);
259            project::normalize_inherent_projection(
260                self.selcx,
261                self.param_env,
262                inherent,
263                self.cause.clone(),
264                self.depth,
265                self.obligations,
266            )
267        } else {
268            let infcx = self.selcx.infcx;
269            let (inherent, mapped_regions, mapped_types, mapped_consts) =
270                BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, inherent);
271            let inherent = inherent.fold_with(self);
272            let inherent = project::normalize_inherent_projection(
273                self.selcx,
274                self.param_env,
275                inherent,
276                self.cause.clone(),
277                self.depth,
278                self.obligations,
279            );
280
281            PlaceholderReplacer::replace_placeholders(
282                infcx,
283                mapped_regions,
284                mapped_types,
285                mapped_consts,
286                &self.universes,
287                inherent,
288            )
289        }
290    }
291
292    // FIXME(mgca): While this supports constants, it is only used for types by default right now
293    x;#[instrument(level = "debug", skip(self), ret)]
294    fn normalize_free_alias(&mut self, free: AliasTerm<'tcx>) -> Term<'tcx> {
295        let recursion_limit = self.cx().recursion_limit();
296        if !recursion_limit.value_within_limit(self.depth) {
297            self.selcx.infcx.err_ctxt().report_overflow_error(
298                OverflowCause::DeeplyNormalize(free.into()),
299                self.cause.span,
300                false,
301                |diag| {
302                    diag.note(msg!("in case this is a recursive type alias, consider using a struct, enum, or union instead"));
303                },
304            );
305        }
306
307        // We don't replace bound vars in the generic arguments of the free alias with
308        // placeholders. This doesn't cause any issues as instantiating parameters with
309        // bound variables is special-cased to rewrite the debruijn index to be higher
310        // whenever we fold through a binder.
311        //
312        // However, we do replace any escaping bound vars in the resulting goals with
313        // placeholders as the trait solver does not expect to encounter escaping bound
314        // vars in obligations.
315        //
316        // FIXME(lazy_type_alias): Check how much this actually matters for perf before
317        // stabilization. This is a bit weird and generally not how we handle binders in
318        // the compiler so ideally we'd do the same boundvar->placeholder->boundvar dance
319        // that other kinds of normalization do.
320        let infcx = self.selcx.infcx;
321        self.obligations.extend(
322            infcx
323                .tcx
324                .predicates_of(free.def_id())
325                .instantiate_own(infcx.tcx, free.args)
326                .map(|(pred, span)| (pred.skip_norm_wip(), span))
327                .map(|(mut predicate, span)| {
328                    if free.has_escaping_bound_vars() {
329                        (predicate, ..) = BoundVarReplacer::replace_bound_vars(
330                            infcx,
331                            &mut self.universes,
332                            predicate,
333                        );
334                    }
335                    let mut cause = self.cause.clone();
336                    cause
337                        .map_code(|code| ObligationCauseCode::TypeAlias(code, span, free.def_id()));
338                    Obligation::new(infcx.tcx, cause, self.param_env, predicate)
339                }),
340        );
341        self.depth += 1;
342        let res = if free.kind(infcx.tcx).is_type() {
343            infcx
344                .tcx
345                .type_of(free.def_id())
346                .instantiate(infcx.tcx, free.args)
347                .skip_norm_wip()
348                .fold_with(self)
349                .into()
350        } else {
351            infcx
352                .tcx
353                .const_of_item(free.def_id())
354                .instantiate(infcx.tcx, free.args)
355                .skip_norm_wip()
356                .fold_with(self)
357                .into()
358        };
359        self.depth -= 1;
360        res
361    }
362}
363
364impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx> {
365    fn cx(&self) -> TyCtxt<'tcx> {
366        self.selcx.tcx()
367    }
368
369    fn fold_binder<T: TypeFoldable<TyCtxt<'tcx>>>(
370        &mut self,
371        t: ty::Binder<'tcx, T>,
372    ) -> ty::Binder<'tcx, T> {
373        self.universes.push(None);
374        let t = t.super_fold_with(self);
375        self.universes.pop();
376        t
377    }
378
379    fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
380        if !needs_normalization(self.selcx.infcx, &ty) {
381            return ty;
382        }
383
384        let ty::Alias(data) = *ty.kind() else { return ty.super_fold_with(self) };
385
386        // We try to be a little clever here as a performance optimization in
387        // cases where there are nested projections under binders.
388        // For example:
389        // ```
390        // for<'a> fn(<T as Foo>::One<'a, Box<dyn Bar<'a, Item=<T as Foo>::Two<'a>>>>)
391        // ```
392        // We normalize the args on the projection before the projecting, but
393        // if we're naive, we'll
394        //   replace bound vars on inner, project inner, replace placeholders on inner,
395        //   replace bound vars on outer, project outer, replace placeholders on outer
396        //
397        // However, if we're a bit more clever, we can replace the bound vars
398        // on the entire type before normalizing nested projections, meaning we
399        //   replace bound vars on outer, project inner,
400        //   project outer, replace placeholders on outer
401        //
402        // This is possible because the inner `'a` will already be a placeholder
403        // when we need to normalize the inner projection
404        //
405        // On the other hand, this does add a bit of complexity, since we only
406        // replace bound vars if the current type is a `Projection` and we need
407        // to make sure we don't forget to fold the args regardless.
408
409        match data.kind {
410            ty::Opaque { def_id } => {
411                // Only normalize `impl Trait` outside of type inference, usually in codegen.
412                match self.selcx.infcx.typing_mode() {
413                    // FIXME(#132279): We likely want to reveal opaques during post borrowck analysis
414                    TypingMode::Coherence
415                    | TypingMode::Analysis { .. }
416                    | TypingMode::Borrowck { .. }
417                    | TypingMode::PostBorrowckAnalysis { .. } => ty.super_fold_with(self),
418                    TypingMode::PostAnalysis => {
419                        let recursion_limit = self.cx().recursion_limit();
420                        if !recursion_limit.value_within_limit(self.depth) {
421                            self.selcx.infcx.err_ctxt().report_overflow_error(
422                                OverflowCause::DeeplyNormalize(data.into()),
423                                self.cause.span,
424                                true,
425                                |_| {},
426                            );
427                        }
428
429                        let args = data.args.fold_with(self);
430                        let generic_ty = self.cx().type_of(def_id);
431                        let concrete_ty = generic_ty.instantiate(self.cx(), args).skip_norm_wip();
432                        self.depth += 1;
433                        let folded_ty = self.fold_ty(concrete_ty);
434                        self.depth -= 1;
435                        folded_ty
436                    }
437                }
438            }
439
440            ty::Projection { .. } => self.normalize_trait_projection(data.into()).expect_type(),
441            ty::Inherent { .. } => self.normalize_inherent_projection(data.into()).expect_type(),
442            ty::Free { .. } => self.normalize_free_alias(data.into()).expect_type(),
443        }
444    }
445
446    #[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("fold_const",
                                    "rustc_trait_selection::traits::normalize",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/normalize.rs"),
                                    ::tracing_core::__macro_support::Option::Some(446u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::normalize"),
                                    ::tracing_core::field::FieldSet::new(&["ct"],
                                        ::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(&ct)
                                                            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: ty::Const<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = self.selcx.tcx();
            if tcx.features().generic_const_exprs() &&
                        !#[allow(non_exhaustive_omitted_patterns)] match ct.kind() {
                                ty::ConstKind::Unevaluated(uv) if tcx.is_type_const(uv.def)
                                    => true,
                                _ => false,
                            } || !needs_normalization(self.selcx.infcx, &ct) {
                return ct;
            }
            let uv =
                match ct.kind() {
                    ty::ConstKind::Unevaluated(uv) => uv,
                    _ => return ct.super_fold_with(self),
                };
            let ct =
                match tcx.def_kind(uv.def) {
                    DefKind::AssocConst { .. } =>
                        match tcx.def_kind(tcx.parent(uv.def)) {
                            DefKind::Trait =>
                                self.normalize_trait_projection(ty::AliasTerm::from_unevaluated_const(tcx,
                                            uv)).expect_const(),
                            DefKind::Impl { of_trait: false } =>
                                self.normalize_inherent_projection(ty::AliasTerm::from_unevaluated_const(tcx,
                                            uv)).expect_const(),
                            kind => {
                                ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                        format_args!("unexpected `DefKind` for const alias\' resolution\'s parent def: {0:?}",
                                            kind)));
                            }
                        },
                    DefKind::Const { .. } =>
                        self.normalize_free_alias(ty::AliasTerm::from_unevaluated_const(tcx,
                                    uv)).expect_const(),
                    DefKind::AnonConst => {
                        let ct = ct.super_fold_with(self);
                        super::with_replaced_escaping_bound_vars(self.selcx.infcx,
                            &mut self.universes, ct,
                            |ct|
                                super::evaluate_const(self.selcx.infcx, ct, self.param_env))
                    }
                    kind => {
                        {
                            ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                    format_args!("unexpected `DefKind` for const alias to resolve to: {0:?}",
                                        kind)));
                        }
                    }
                };
            ct.super_fold_with(self)
        }
    }
}#[instrument(skip(self), level = "debug")]
447    fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
448        let tcx = self.selcx.tcx();
449
450        if tcx.features().generic_const_exprs()
451            // Normalize type_const items even with feature `generic_const_exprs`.
452            && !matches!(ct.kind(), ty::ConstKind::Unevaluated(uv) if tcx.is_type_const(uv.def))
453            || !needs_normalization(self.selcx.infcx, &ct)
454        {
455            return ct;
456        }
457
458        let uv = match ct.kind() {
459            ty::ConstKind::Unevaluated(uv) => uv,
460            _ => return ct.super_fold_with(self),
461        };
462
463        // Note that the AssocConst and Const cases are unreachable on stable,
464        // unless a `min_generic_const_args` feature gate error has already
465        // been emitted earlier in compilation.
466        //
467        // That's because we can only end up with an Unevaluated ty::Const for a const item
468        // if it was marked with `type const`. Using this attribute without the mgca
469        // feature gate causes a parse error.
470        let ct = match tcx.def_kind(uv.def) {
471            DefKind::AssocConst { .. } => match tcx.def_kind(tcx.parent(uv.def)) {
472                DefKind::Trait => self
473                    .normalize_trait_projection(ty::AliasTerm::from_unevaluated_const(tcx, uv))
474                    .expect_const(),
475                DefKind::Impl { of_trait: false } => self
476                    .normalize_inherent_projection(ty::AliasTerm::from_unevaluated_const(tcx, uv))
477                    .expect_const(),
478                kind => unreachable!(
479                    "unexpected `DefKind` for const alias' resolution's parent def: {:?}",
480                    kind
481                ),
482            },
483            DefKind::Const { .. } => self
484                .normalize_free_alias(ty::AliasTerm::from_unevaluated_const(tcx, uv))
485                .expect_const(),
486            DefKind::AnonConst => {
487                let ct = ct.super_fold_with(self);
488                super::with_replaced_escaping_bound_vars(
489                    self.selcx.infcx,
490                    &mut self.universes,
491                    ct,
492                    |ct| super::evaluate_const(self.selcx.infcx, ct, self.param_env),
493                )
494            }
495            kind => {
496                unreachable!("unexpected `DefKind` for const alias to resolve to: {:?}", kind)
497            }
498        };
499
500        // We re-fold the normalized const as the `ty` field on `ConstKind::Value` may be
501        // unnormalized after const evaluation returns.
502        ct.super_fold_with(self)
503    }
504
505    #[inline]
506    fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
507        if p.allow_normalization() && needs_normalization(self.selcx.infcx, &p) {
508            p.super_fold_with(self)
509        } else {
510            p
511        }
512    }
513}