Skip to main content

rustc_trait_selection/traits/
project.rs

1//! Code for projecting associated types out of trait references.
2
3use std::ops::ControlFlow;
4
5use rustc_data_structures::sso::SsoHashSet;
6use rustc_data_structures::stack::ensure_sufficient_stack;
7use rustc_errors::ErrorGuaranteed;
8use rustc_hir::lang_items::LangItem;
9use rustc_infer::infer::DefineOpaqueTypes;
10use rustc_infer::infer::resolve::OpportunisticRegionResolver;
11use rustc_infer::traits::{ObligationCauseCode, PredicateObligations};
12use rustc_middle::traits::select::OverflowError;
13use rustc_middle::traits::{BuiltinImplSource, ImplSource, ImplSourceUserDefinedData};
14use rustc_middle::ty::fast_reject::DeepRejectCtxt;
15use rustc_middle::ty::{
16    self, FieldInfo, Term, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, TypingMode, Upcast,
17};
18use rustc_middle::{bug, span_bug};
19use rustc_span::sym;
20use tracing::{debug, instrument};
21
22use super::{
23    MismatchedProjectionTypes, Normalized, NormalizedTerm, Obligation, ObligationCause,
24    PredicateObligation, ProjectionCacheEntry, ProjectionCacheKey, Selection, SelectionContext,
25    SelectionError, specialization_graph, translate_args, util,
26};
27use crate::errors::InherentProjectionNormalizationOverflow;
28use crate::infer::{BoundRegionConversionTime, InferOk};
29use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to};
30use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
31use crate::traits::select::ProjectionMatchesProjection;
32
33pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
34
35pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
36
37pub type ProjectionTermObligation<'tcx> = Obligation<'tcx, ty::AliasTerm<'tcx>>;
38
39pub(super) struct InProgress;
40
41/// When attempting to resolve `<T as TraitRef>::Name` ...
42#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ProjectionError<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ProjectionError::TooManyCandidates =>
                ::core::fmt::Formatter::write_str(f, "TooManyCandidates"),
            ProjectionError::TraitSelectionError(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "TraitSelectionError", &__self_0),
        }
    }
}Debug)]
43pub enum ProjectionError<'tcx> {
44    /// ...we found multiple sources of information and couldn't resolve the ambiguity.
45    TooManyCandidates,
46
47    /// ...an error occurred matching `T : TraitRef`
48    TraitSelectionError(SelectionError<'tcx>),
49}
50
51#[derive(#[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for ProjectionCandidate<'tcx> {
    #[inline]
    fn eq(&self, other: &ProjectionCandidate<'tcx>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (ProjectionCandidate::ParamEnv(__self_0),
                    ProjectionCandidate::ParamEnv(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (ProjectionCandidate::TraitDef(__self_0),
                    ProjectionCandidate::TraitDef(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (ProjectionCandidate::Object(__self_0),
                    ProjectionCandidate::Object(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (ProjectionCandidate::Select(__self_0),
                    ProjectionCandidate::Select(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for ProjectionCandidate<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _:
                ::core::cmp::AssertParamIsEq<ty::PolyProjectionPredicate<'tcx>>;
        let _:
                ::core::cmp::AssertParamIsEq<ty::PolyProjectionPredicate<'tcx>>;
        let _:
                ::core::cmp::AssertParamIsEq<ty::PolyProjectionPredicate<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<Selection<'tcx>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ProjectionCandidate<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ProjectionCandidate::ParamEnv(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "ParamEnv", &__self_0),
            ProjectionCandidate::TraitDef(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "TraitDef", &__self_0),
            ProjectionCandidate::Object(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Object",
                    &__self_0),
            ProjectionCandidate::Select(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Select",
                    &__self_0),
        }
    }
}Debug)]
52enum ProjectionCandidate<'tcx> {
53    /// From a where-clause in the env or object type
54    ParamEnv(ty::PolyProjectionPredicate<'tcx>),
55
56    /// From the definition of `Trait` when you have something like
57    /// `<<A as Trait>::B as Trait2>::C`.
58    TraitDef(ty::PolyProjectionPredicate<'tcx>),
59
60    /// Bounds specified on an object type
61    Object(ty::PolyProjectionPredicate<'tcx>),
62
63    /// From an "impl" (or a "pseudo-impl" returned by select)
64    Select(Selection<'tcx>),
65}
66
67enum ProjectionCandidateSet<'tcx> {
68    None,
69    Single(ProjectionCandidate<'tcx>),
70    Ambiguous,
71    Error(SelectionError<'tcx>),
72}
73
74impl<'tcx> ProjectionCandidateSet<'tcx> {
75    fn mark_ambiguous(&mut self) {
76        *self = ProjectionCandidateSet::Ambiguous;
77    }
78
79    fn mark_error(&mut self, err: SelectionError<'tcx>) {
80        *self = ProjectionCandidateSet::Error(err);
81    }
82
83    // Returns true if the push was successful, or false if the candidate
84    // was discarded -- this could be because of ambiguity, or because
85    // a higher-priority candidate is already there.
86    fn push_candidate(&mut self, candidate: ProjectionCandidate<'tcx>) -> bool {
87        // This wacky variable is just used to try and
88        // make code readable and avoid confusing paths.
89        // It is assigned a "value" of `()` only on those
90        // paths in which we wish to convert `*self` to
91        // ambiguous (and return false, because the candidate
92        // was not used). On other paths, it is not assigned,
93        // and hence if those paths *could* reach the code that
94        // comes after the match, this fn would not compile.
95        let convert_to_ambiguous;
96
97        match self {
98            ProjectionCandidateSet::None => {
99                *self = ProjectionCandidateSet::Single(candidate);
100                return true;
101            }
102
103            ProjectionCandidateSet::Single(current) => {
104                // Duplicates can happen inside ParamEnv. In the case, we
105                // perform a lazy deduplication.
106                if current == &candidate {
107                    return false;
108                }
109
110                // Prefer where-clauses. As in select, if there are multiple
111                // candidates, we prefer where-clause candidates over impls. This
112                // may seem a bit surprising, since impls are the source of
113                // "truth" in some sense, but in fact some of the impls that SEEM
114                // applicable are not, because of nested obligations. Where
115                // clauses are the safer choice. See the comment on
116                // `select::SelectionCandidate` and #21974 for more details.
117                match (current, candidate) {
118                    (ProjectionCandidate::ParamEnv(..), ProjectionCandidate::ParamEnv(..)) => {
119                        convert_to_ambiguous = ()
120                    }
121                    (ProjectionCandidate::ParamEnv(..), _) => return false,
122                    (_, ProjectionCandidate::ParamEnv(..)) => ::rustc_middle::util::bug::bug_fmt(format_args!("should never prefer non-param-env candidates over param-env candidates"))bug!(
123                        "should never prefer non-param-env candidates over param-env candidates"
124                    ),
125                    (_, _) => convert_to_ambiguous = (),
126                }
127            }
128
129            ProjectionCandidateSet::Ambiguous | ProjectionCandidateSet::Error(..) => {
130                return false;
131            }
132        }
133
134        // We only ever get here when we moved from a single candidate
135        // to ambiguous.
136        let () = convert_to_ambiguous;
137        *self = ProjectionCandidateSet::Ambiguous;
138        false
139    }
140}
141
142/// States returned from `poly_project_and_unify_type`. Takes the place
143/// of the old return type, which was:
144/// ```ignore (not-rust)
145/// Result<
146///     Result<Option<PredicateObligations<'tcx>>, InProgress>,
147///     MismatchedProjectionTypes<'tcx>,
148/// >
149/// ```
150pub(super) enum ProjectAndUnifyResult<'tcx> {
151    /// The projection bound holds subject to the given obligations. If the
152    /// projection cannot be normalized because the required trait bound does
153    /// not hold, this is returned, with `obligations` being a predicate that
154    /// cannot be proven.
155    Holds(PredicateObligations<'tcx>),
156    /// The projection cannot be normalized due to ambiguity. Resolving some
157    /// inference variables in the projection may fix this.
158    FailedNormalization,
159    /// The project cannot be normalized because `poly_project_and_unify_type`
160    /// is called recursively while normalizing the same projection.
161    Recursive,
162    // the projection can be normalized, but is not equal to the expected type.
163    // Returns the type error that arose from the mismatch.
164    MismatchedProjectionTypes(MismatchedProjectionTypes<'tcx>),
165}
166
167/// Evaluates constraints of the form:
168/// ```ignore (not-rust)
169/// for<...> <T as Trait>::U == V
170/// ```
171/// If successful, this may result in additional obligations. Also returns
172/// the projection cache key used to track these additional obligations.
173// FIXME(mgca): While this supports constants, it is only used for types by default right now
174#[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("poly_project_and_unify_term",
                                    "rustc_trait_selection::traits::project",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                    ::tracing_core::__macro_support::Option::Some(174u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                    ::tracing_core::field::FieldSet::new(&["obligation"],
                                        ::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(&obligation)
                                                            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: ProjectAndUnifyResult<'tcx> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let infcx = selcx.infcx;
            let r =
                infcx.commit_if_ok(|_snapshot|
                        {
                            let placeholder_predicate =
                                infcx.enter_forall_and_leak_universe(obligation.predicate);
                            let placeholder_obligation =
                                obligation.with(infcx.tcx, placeholder_predicate);
                            match project_and_unify_term(selcx, &placeholder_obligation)
                                {
                                ProjectAndUnifyResult::MismatchedProjectionTypes(e) =>
                                    Err(e),
                                other => Ok(other),
                            }
                        });
            match r {
                Ok(inner) => inner,
                Err(err) =>
                    ProjectAndUnifyResult::MismatchedProjectionTypes(err),
            }
        }
    }
}#[instrument(level = "debug", skip(selcx))]
175pub(super) fn poly_project_and_unify_term<'cx, 'tcx>(
176    selcx: &mut SelectionContext<'cx, 'tcx>,
177    obligation: &PolyProjectionObligation<'tcx>,
178) -> ProjectAndUnifyResult<'tcx> {
179    let infcx = selcx.infcx;
180    let r = infcx.commit_if_ok(|_snapshot| {
181        let placeholder_predicate = infcx.enter_forall_and_leak_universe(obligation.predicate);
182
183        let placeholder_obligation = obligation.with(infcx.tcx, placeholder_predicate);
184        match project_and_unify_term(selcx, &placeholder_obligation) {
185            ProjectAndUnifyResult::MismatchedProjectionTypes(e) => Err(e),
186            other => Ok(other),
187        }
188    });
189
190    match r {
191        Ok(inner) => inner,
192        Err(err) => ProjectAndUnifyResult::MismatchedProjectionTypes(err),
193    }
194}
195
196/// Evaluates constraints of the form:
197/// ```ignore (not-rust)
198/// <T as Trait>::U == V
199/// ```
200/// If successful, this may result in additional obligations.
201///
202/// See [poly_project_and_unify_term] for an explanation of the return value.
203// FIXME(mgca): While this supports constants, it is only used for types by default right now
204#[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("project_and_unify_term",
                                    "rustc_trait_selection::traits::project",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                    ::tracing_core::__macro_support::Option::Some(204u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                    ::tracing_core::field::FieldSet::new(&["obligation"],
                                        ::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(&obligation)
                                                            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: ProjectAndUnifyResult<'tcx> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let mut obligations = PredicateObligations::new();
            let infcx = selcx.infcx;
            let normalized =
                match opt_normalize_projection_term(selcx,
                        obligation.param_env, obligation.predicate.projection_term,
                        obligation.cause.clone(), obligation.recursion_depth,
                        &mut obligations) {
                    Ok(Some(n)) => n,
                    Ok(None) =>
                        return ProjectAndUnifyResult::FailedNormalization,
                    Err(InProgress) => return ProjectAndUnifyResult::Recursive,
                };
            {
                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/project.rs:224",
                                    "rustc_trait_selection::traits::project",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                    ::tracing_core::__macro_support::Option::Some(224u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                    ::tracing_core::field::FieldSet::new(&["message",
                                                    "normalized", "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(&format_args!("project_and_unify_type result")
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&normalized)
                                                        as &dyn Value)),
                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&obligations)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let actual = obligation.predicate.term;
            let InferOk { value: actual, obligations: new } =
                selcx.infcx.replace_opaque_types_with_inference_vars(actual,
                    obligation.cause.body_id, obligation.cause.span,
                    obligation.param_env);
            obligations.extend(new);
            match infcx.at(&obligation.cause,
                        obligation.param_env).eq(DefineOpaqueTypes::Yes, normalized,
                    actual) {
                Ok(InferOk { obligations: inferred_obligations, value: () })
                    => {
                    obligations.extend(inferred_obligations);
                    ProjectAndUnifyResult::Holds(obligations)
                }
                Err(err) => {
                    {
                        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/project.rs:249",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(249u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("equating types encountered error {0:?}",
                                                                        err) as &dyn Value))])
                                });
                        } else { ; }
                    };
                    ProjectAndUnifyResult::MismatchedProjectionTypes(MismatchedProjectionTypes {
                            err,
                        })
                }
            }
        }
    }
}#[instrument(level = "debug", skip(selcx))]
205fn project_and_unify_term<'cx, 'tcx>(
206    selcx: &mut SelectionContext<'cx, 'tcx>,
207    obligation: &ProjectionObligation<'tcx>,
208) -> ProjectAndUnifyResult<'tcx> {
209    let mut obligations = PredicateObligations::new();
210
211    let infcx = selcx.infcx;
212    let normalized = match opt_normalize_projection_term(
213        selcx,
214        obligation.param_env,
215        obligation.predicate.projection_term,
216        obligation.cause.clone(),
217        obligation.recursion_depth,
218        &mut obligations,
219    ) {
220        Ok(Some(n)) => n,
221        Ok(None) => return ProjectAndUnifyResult::FailedNormalization,
222        Err(InProgress) => return ProjectAndUnifyResult::Recursive,
223    };
224    debug!(?normalized, ?obligations, "project_and_unify_type result");
225    let actual = obligation.predicate.term;
226    // For an example where this is necessary see tests/ui/impl-trait/nested-return-type2.rs
227    // This allows users to omit re-mentioning all bounds on an associated type and just use an
228    // `impl Trait` for the assoc type to add more bounds.
229    let InferOk { value: actual, obligations: new } =
230        selcx.infcx.replace_opaque_types_with_inference_vars(
231            actual,
232            obligation.cause.body_id,
233            obligation.cause.span,
234            obligation.param_env,
235        );
236    obligations.extend(new);
237
238    // Need to define opaque types to support nested opaque types like `impl Fn() -> impl Trait`
239    match infcx.at(&obligation.cause, obligation.param_env).eq(
240        DefineOpaqueTypes::Yes,
241        normalized,
242        actual,
243    ) {
244        Ok(InferOk { obligations: inferred_obligations, value: () }) => {
245            obligations.extend(inferred_obligations);
246            ProjectAndUnifyResult::Holds(obligations)
247        }
248        Err(err) => {
249            debug!("equating types encountered error {:?}", err);
250            ProjectAndUnifyResult::MismatchedProjectionTypes(MismatchedProjectionTypes { err })
251        }
252    }
253}
254
255/// The guts of `normalize`: normalize a specific projection like `<T
256/// as Trait>::Item`. The result is always a type (and possibly
257/// additional obligations). If ambiguity arises, which implies that
258/// there are unresolved type variables in the projection, we will
259/// instantiate it with a fresh type variable `$X` and generate a new
260/// obligation `<T as Trait>::Item == $X` for later.
261// FIXME(mgca): While this supports constants, it is only used for types by default right now
262pub fn normalize_projection_term<'a, 'b, 'tcx>(
263    selcx: &'a mut SelectionContext<'b, 'tcx>,
264    param_env: ty::ParamEnv<'tcx>,
265    alias_term: ty::AliasTerm<'tcx>,
266    cause: ObligationCause<'tcx>,
267    depth: usize,
268    obligations: &mut PredicateObligations<'tcx>,
269) -> Term<'tcx> {
270    opt_normalize_projection_term(selcx, param_env, alias_term, cause.clone(), depth, obligations)
271        .ok()
272        .flatten()
273        .unwrap_or_else(move || {
274            // if we bottom out in ambiguity, create a type variable
275            // and a deferred predicate to resolve this when more type
276            // information is available.
277
278            selcx
279                .infcx
280                .projection_term_to_infer(param_env, alias_term, cause, depth + 1, obligations)
281                .into()
282        })
283}
284
285/// The guts of `normalize`: normalize a specific projection like `<T
286/// as Trait>::Item`. The result is always a type (and possibly
287/// additional obligations). Returns `None` in the case of ambiguity,
288/// which indicates that there are unbound type variables.
289///
290/// This function used to return `Option<NormalizedTy<'tcx>>`, which contains a
291/// `Ty<'tcx>` and an obligations vector. But that obligation vector was very
292/// often immediately appended to another obligations vector. So now this
293/// function takes an obligations vector and appends to it directly, which is
294/// slightly uglier but avoids the need for an extra short-lived allocation.
295// FIXME(mgca): While this supports constants, it is only used for types by default right now
296#[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("opt_normalize_projection_term",
                                    "rustc_trait_selection::traits::project",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                    ::tracing_core::__macro_support::Option::Some(296u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                    ::tracing_core::field::FieldSet::new(&["projection_term",
                                                    "depth"],
                                        ::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(&projection_term)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&depth 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<Option<Term<'tcx>>, InProgress> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let infcx = selcx.infcx;
            if true {
                if !!selcx.infcx.next_trait_solver() {
                    ::core::panicking::panic("assertion failed: !selcx.infcx.next_trait_solver()")
                };
            };
            let projection_term =
                infcx.resolve_vars_if_possible(projection_term);
            let cache_key =
                ProjectionCacheKey::new(projection_term, param_env);
            let cache_entry =
                infcx.inner.borrow_mut().projection_cache().try_start(cache_key);
            match cache_entry {
                Ok(()) => {
                    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/project.rs:319",
                                        "rustc_trait_selection::traits::project",
                                        ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                        ::tracing_core::__macro_support::Option::Some(319u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                        ::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!("no cache")
                                                            as &dyn Value))])
                            });
                    } else { ; }
                }
                Err(ProjectionCacheEntry::Ambiguous) => {
                    {
                        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/project.rs:324",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(324u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("found cache entry: ambiguous")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    return Ok(None);
                }
                Err(ProjectionCacheEntry::InProgress) => {
                    {
                        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/project.rs:336",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(336u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("found cache entry: in-progress")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    infcx.inner.borrow_mut().projection_cache().recur(cache_key);
                    return Err(InProgress);
                }
                Err(ProjectionCacheEntry::Recur) => {
                    {
                        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/project.rs:345",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(345u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("recur cache")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    return Err(InProgress);
                }
                Err(ProjectionCacheEntry::NormalizedTerm { ty, complete: _ })
                    => {
                    {
                        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/project.rs:360",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(360u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::tracing_core::field::FieldSet::new(&["message", "ty"],
                                                ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                            ::tracing::metadata::Kind::EVENT)
                                    };
                                ::tracing::callsite::DefaultCallsite::new(&META)
                            };
                        let enabled =
                            ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                    ::tracing::Level::DEBUG <=
                                        ::tracing::level_filters::LevelFilter::current() &&
                                {
                                    let interest = __CALLSITE.interest();
                                    !interest.is_never() &&
                                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                            interest)
                                };
                        if enabled {
                            (|value_set: ::tracing::field::ValueSet|
                                        {
                                            let meta = __CALLSITE.metadata();
                                            ::tracing::Event::dispatch(meta, &value_set);
                                            ;
                                        })({
                                    #[allow(unused_imports)]
                                    use ::tracing::field::{debug, display, Value};
                                    let mut iter = __CALLSITE.metadata().fields().iter();
                                    __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&format_args!("found normalized ty")
                                                                as &dyn Value)),
                                                    (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&debug(&ty) as
                                                                &dyn Value))])
                                });
                        } else { ; }
                    };
                    obligations.extend(ty.obligations);
                    return Ok(Some(ty.value));
                }
                Err(ProjectionCacheEntry::Error) => {
                    {
                        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/project.rs:365",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(365u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("opt_normalize_projection_type: found error")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    let result =
                        normalize_to_error(selcx, param_env, projection_term, cause,
                            depth);
                    obligations.extend(result.obligations);
                    return Ok(Some(result.value));
                }
            }
            let obligation =
                Obligation::with_depth(selcx.tcx(), cause.clone(), depth,
                    param_env, projection_term);
            match project(selcx, &obligation) {
                Ok(Projected::Progress(Progress {
                    term: projected_term, obligations: mut projected_obligations
                    })) => {
                    {
                        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/project.rs:380",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(380u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("opt_normalize_projection_type: progress")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    let projected_term =
                        selcx.infcx.resolve_vars_if_possible(projected_term);
                    let mut result =
                        if projected_term.has_aliases() {
                            let normalized_ty =
                                normalize_with_depth_to(selcx, param_env, cause, depth + 1,
                                    projected_term, &mut projected_obligations);
                            Normalized {
                                value: normalized_ty,
                                obligations: projected_obligations,
                            }
                        } else {
                            Normalized {
                                value: projected_term,
                                obligations: projected_obligations,
                            }
                        };
                    let mut deduped =
                        SsoHashSet::with_capacity(result.obligations.len());
                    result.obligations.retain(|obligation|
                            deduped.insert(obligation.clone()));
                    infcx.inner.borrow_mut().projection_cache().insert_term(cache_key,
                        result.clone());
                    obligations.extend(result.obligations);
                    Ok(Some(result.value))
                }
                Ok(Projected::NoProgress(projected_ty)) => {
                    {
                        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/project.rs:411",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(411u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("opt_normalize_projection_type: no progress")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    let result =
                        Normalized {
                            value: projected_ty,
                            obligations: PredicateObligations::new(),
                        };
                    infcx.inner.borrow_mut().projection_cache().insert_term(cache_key,
                        result.clone());
                    Ok(Some(result.value))
                }
                Err(ProjectionError::TooManyCandidates) => {
                    {
                        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/project.rs:419",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(419u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("opt_normalize_projection_type: too many candidates")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key);
                    Ok(None)
                }
                Err(ProjectionError::TraitSelectionError(_)) => {
                    {
                        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/project.rs:424",
                                            "rustc_trait_selection::traits::project",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                            ::tracing_core::__macro_support::Option::Some(424u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                            ::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!("opt_normalize_projection_type: ERROR")
                                                                as &dyn Value))])
                                });
                        } else { ; }
                    };
                    infcx.inner.borrow_mut().projection_cache().error(cache_key);
                    let result =
                        normalize_to_error(selcx, param_env, projection_term, cause,
                            depth);
                    obligations.extend(result.obligations);
                    Ok(Some(result.value))
                }
            }
        }
    }
}#[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
297pub(super) fn opt_normalize_projection_term<'a, 'b, 'tcx>(
298    selcx: &'a mut SelectionContext<'b, 'tcx>,
299    param_env: ty::ParamEnv<'tcx>,
300    projection_term: ty::AliasTerm<'tcx>,
301    cause: ObligationCause<'tcx>,
302    depth: usize,
303    obligations: &mut PredicateObligations<'tcx>,
304) -> Result<Option<Term<'tcx>>, InProgress> {
305    let infcx = selcx.infcx;
306    debug_assert!(!selcx.infcx.next_trait_solver());
307    let projection_term = infcx.resolve_vars_if_possible(projection_term);
308    let cache_key = ProjectionCacheKey::new(projection_term, param_env);
309
310    // FIXME(#20304) For now, I am caching here, which is good, but it
311    // means we don't capture the type variables that are created in
312    // the case of ambiguity. Which means we may create a large stream
313    // of such variables. OTOH, if we move the caching up a level, we
314    // would not benefit from caching when proving `T: Trait<U=Foo>`
315    // bounds. It might be the case that we want two distinct caches,
316    // or else another kind of cache entry.
317    let cache_entry = infcx.inner.borrow_mut().projection_cache().try_start(cache_key);
318    match cache_entry {
319        Ok(()) => debug!("no cache"),
320        Err(ProjectionCacheEntry::Ambiguous) => {
321            // If we found ambiguity the last time, that means we will continue
322            // to do so until some type in the key changes (and we know it
323            // hasn't, because we just fully resolved it).
324            debug!("found cache entry: ambiguous");
325            return Ok(None);
326        }
327        Err(ProjectionCacheEntry::InProgress) => {
328            // Under lazy normalization, this can arise when
329            // bootstrapping. That is, imagine an environment with a
330            // where-clause like `A::B == u32`. Now, if we are asked
331            // to normalize `A::B`, we will want to check the
332            // where-clauses in scope. So we will try to unify `A::B`
333            // with `A::B`, which can trigger a recursive
334            // normalization.
335
336            debug!("found cache entry: in-progress");
337
338            // Cache that normalizing this projection resulted in a cycle. This
339            // should ensure that, unless this happens within a snapshot that's
340            // rolled back, fulfillment or evaluation will notice the cycle.
341            infcx.inner.borrow_mut().projection_cache().recur(cache_key);
342            return Err(InProgress);
343        }
344        Err(ProjectionCacheEntry::Recur) => {
345            debug!("recur cache");
346            return Err(InProgress);
347        }
348        Err(ProjectionCacheEntry::NormalizedTerm { ty, complete: _ }) => {
349            // This is the hottest path in this function.
350            //
351            // If we find the value in the cache, then return it along
352            // with the obligations that went along with it. Note
353            // that, when using a fulfillment context, these
354            // obligations could in principle be ignored: they have
355            // already been registered when the cache entry was
356            // created (and hence the new ones will quickly be
357            // discarded as duplicated). But when doing trait
358            // evaluation this is not the case, and dropping the trait
359            // evaluations can causes ICEs (e.g., #43132).
360            debug!(?ty, "found normalized ty");
361            obligations.extend(ty.obligations);
362            return Ok(Some(ty.value));
363        }
364        Err(ProjectionCacheEntry::Error) => {
365            debug!("opt_normalize_projection_type: found error");
366            let result = normalize_to_error(selcx, param_env, projection_term, cause, depth);
367            obligations.extend(result.obligations);
368            return Ok(Some(result.value));
369        }
370    }
371
372    let obligation =
373        Obligation::with_depth(selcx.tcx(), cause.clone(), depth, param_env, projection_term);
374
375    match project(selcx, &obligation) {
376        Ok(Projected::Progress(Progress {
377            term: projected_term,
378            obligations: mut projected_obligations,
379        })) => {
380            debug!("opt_normalize_projection_type: progress");
381            // if projection succeeded, then what we get out of this
382            // is also non-normalized (consider: it was derived from
383            // an impl, where-clause etc) and hence we must
384            // re-normalize it
385
386            let projected_term = selcx.infcx.resolve_vars_if_possible(projected_term);
387
388            let mut result = if projected_term.has_aliases() {
389                let normalized_ty = normalize_with_depth_to(
390                    selcx,
391                    param_env,
392                    cause,
393                    depth + 1,
394                    projected_term,
395                    &mut projected_obligations,
396                );
397
398                Normalized { value: normalized_ty, obligations: projected_obligations }
399            } else {
400                Normalized { value: projected_term, obligations: projected_obligations }
401            };
402
403            let mut deduped = SsoHashSet::with_capacity(result.obligations.len());
404            result.obligations.retain(|obligation| deduped.insert(obligation.clone()));
405
406            infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
407            obligations.extend(result.obligations);
408            Ok(Some(result.value))
409        }
410        Ok(Projected::NoProgress(projected_ty)) => {
411            debug!("opt_normalize_projection_type: no progress");
412            let result =
413                Normalized { value: projected_ty, obligations: PredicateObligations::new() };
414            infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
415            // No need to extend `obligations`.
416            Ok(Some(result.value))
417        }
418        Err(ProjectionError::TooManyCandidates) => {
419            debug!("opt_normalize_projection_type: too many candidates");
420            infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key);
421            Ok(None)
422        }
423        Err(ProjectionError::TraitSelectionError(_)) => {
424            debug!("opt_normalize_projection_type: ERROR");
425            // if we got an error processing the `T as Trait` part,
426            // just return `ty::err` but add the obligation `T :
427            // Trait`, which when processed will cause the error to be
428            // reported later
429            infcx.inner.borrow_mut().projection_cache().error(cache_key);
430            let result = normalize_to_error(selcx, param_env, projection_term, cause, depth);
431            obligations.extend(result.obligations);
432            Ok(Some(result.value))
433        }
434    }
435}
436
437/// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
438/// hold. In various error cases, we cannot generate a valid
439/// normalized projection. Therefore, we create an inference variable
440/// return an associated obligation that, when fulfilled, will lead to
441/// an error.
442///
443/// Note that we used to return `Error` here, but that was quite
444/// dubious -- the premise was that an error would *eventually* be
445/// reported, when the obligation was processed. But in general once
446/// you see an `Error` you are supposed to be able to assume that an
447/// error *has been* reported, so that you can take whatever heuristic
448/// paths you want to take. To make things worse, it was possible for
449/// cycles to arise, where you basically had a setup like `<MyType<$0>
450/// as Trait>::Foo == $0`. Here, normalizing `<MyType<$0> as
451/// Trait>::Foo>` to `[type error]` would lead to an obligation of
452/// `<MyType<[type error]> as Trait>::Foo`. We are supposed to report
453/// an error for this obligation, but we legitimately should not,
454/// because it contains `[type error]`. Yuck! (See issue #29857 for
455/// one case where this arose.)
456// FIXME(mgca): While this supports constants, it is only used for types by default right now
457fn normalize_to_error<'a, 'tcx>(
458    selcx: &SelectionContext<'a, 'tcx>,
459    param_env: ty::ParamEnv<'tcx>,
460    projection_term: ty::AliasTerm<'tcx>,
461    cause: ObligationCause<'tcx>,
462    depth: usize,
463) -> NormalizedTerm<'tcx> {
464    let trait_ref = ty::Binder::dummy(projection_term.trait_ref(selcx.tcx()));
465    let new_value = match projection_term.kind(selcx.tcx()) {
466        ty::AliasTermKind::ProjectionTy
467        | ty::AliasTermKind::InherentTy
468        | ty::AliasTermKind::OpaqueTy
469        | ty::AliasTermKind::FreeTy => selcx.infcx.next_ty_var(cause.span).into(),
470        ty::AliasTermKind::FreeConst
471        | ty::AliasTermKind::InherentConst
472        | ty::AliasTermKind::UnevaluatedConst
473        | ty::AliasTermKind::ProjectionConst => selcx.infcx.next_const_var(cause.span).into(),
474    };
475    let mut obligations = PredicateObligations::new();
476    obligations.push(Obligation {
477        cause,
478        recursion_depth: depth,
479        param_env,
480        predicate: trait_ref.upcast(selcx.tcx()),
481    });
482    Normalized { value: new_value, obligations }
483}
484
485/// Confirm and normalize the given inherent projection.
486// FIXME(mgca): While this supports constants, it is only used for types by default right now
487#[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("normalize_inherent_projection",
                                    "rustc_trait_selection::traits::project",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                    ::tracing_core::__macro_support::Option::Some(487u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                    ::tracing_core::field::FieldSet::new(&["alias_term",
                                                    "depth"],
                                        ::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(&alias_term)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&depth 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::Term<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = selcx.tcx();
            if !tcx.recursion_limit().value_within_limit(depth) {
                tcx.dcx().emit_fatal(InherentProjectionNormalizationOverflow {
                        span: cause.span,
                        ty: alias_term.to_string(),
                    });
            }
            let args =
                compute_inherent_assoc_term_args(selcx, param_env, alias_term,
                    cause.clone(), depth, obligations);
            let predicates =
                tcx.predicates_of(alias_term.def_id).instantiate(tcx, args);
            for (predicate, span) in predicates {
                let predicate =
                    normalize_with_depth_to(selcx, param_env, cause.clone(),
                        depth + 1, predicate, obligations);
                let nested_cause =
                    ObligationCause::new(cause.span, cause.body_id,
                        ObligationCauseCode::WhereClause(alias_term.def_id, span));
                obligations.push(Obligation::with_depth(tcx, nested_cause,
                        depth + 1, param_env, predicate));
            }
            let term: Term<'tcx> =
                if alias_term.kind(tcx).is_type() {
                    tcx.type_of(alias_term.def_id).instantiate(tcx, args).into()
                } else {
                    tcx.const_of_item(alias_term.def_id).instantiate(tcx,
                            args).into()
                };
            let mut term = selcx.infcx.resolve_vars_if_possible(term);
            if term.has_aliases() {
                term =
                    normalize_with_depth_to(selcx, param_env, cause.clone(),
                        depth + 1, term, obligations);
            }
            term
        }
    }
}#[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
488pub fn normalize_inherent_projection<'a, 'b, 'tcx>(
489    selcx: &'a mut SelectionContext<'b, 'tcx>,
490    param_env: ty::ParamEnv<'tcx>,
491    alias_term: ty::AliasTerm<'tcx>,
492    cause: ObligationCause<'tcx>,
493    depth: usize,
494    obligations: &mut PredicateObligations<'tcx>,
495) -> ty::Term<'tcx> {
496    let tcx = selcx.tcx();
497
498    if !tcx.recursion_limit().value_within_limit(depth) {
499        // Halt compilation because it is important that overflows never be masked.
500        tcx.dcx().emit_fatal(InherentProjectionNormalizationOverflow {
501            span: cause.span,
502            ty: alias_term.to_string(),
503        });
504    }
505
506    let args = compute_inherent_assoc_term_args(
507        selcx,
508        param_env,
509        alias_term,
510        cause.clone(),
511        depth,
512        obligations,
513    );
514
515    // Register the obligations arising from the impl and from the associated type itself.
516    let predicates = tcx.predicates_of(alias_term.def_id).instantiate(tcx, args);
517    for (predicate, span) in predicates {
518        let predicate = normalize_with_depth_to(
519            selcx,
520            param_env,
521            cause.clone(),
522            depth + 1,
523            predicate,
524            obligations,
525        );
526
527        let nested_cause = ObligationCause::new(
528            cause.span,
529            cause.body_id,
530            // FIXME(inherent_associated_types): Since we can't pass along the self type to the
531            // cause code, inherent projections will be printed with identity instantiation in
532            // diagnostics which is not ideal.
533            // Consider creating separate cause codes for this specific situation.
534            ObligationCauseCode::WhereClause(alias_term.def_id, span),
535        );
536
537        obligations.push(Obligation::with_depth(
538            tcx,
539            nested_cause,
540            depth + 1,
541            param_env,
542            predicate,
543        ));
544    }
545
546    let term: Term<'tcx> = if alias_term.kind(tcx).is_type() {
547        tcx.type_of(alias_term.def_id).instantiate(tcx, args).into()
548    } else {
549        tcx.const_of_item(alias_term.def_id).instantiate(tcx, args).into()
550    };
551
552    let mut term = selcx.infcx.resolve_vars_if_possible(term);
553    if term.has_aliases() {
554        term =
555            normalize_with_depth_to(selcx, param_env, cause.clone(), depth + 1, term, obligations);
556    }
557
558    term
559}
560
561// FIXME(mgca): While this supports constants, it is only used for types by default right now
562pub fn compute_inherent_assoc_term_args<'a, 'b, 'tcx>(
563    selcx: &'a mut SelectionContext<'b, 'tcx>,
564    param_env: ty::ParamEnv<'tcx>,
565    alias_term: ty::AliasTerm<'tcx>,
566    cause: ObligationCause<'tcx>,
567    depth: usize,
568    obligations: &mut PredicateObligations<'tcx>,
569) -> ty::GenericArgsRef<'tcx> {
570    let tcx = selcx.tcx();
571
572    let impl_def_id = tcx.parent(alias_term.def_id);
573    let impl_args = selcx.infcx.fresh_args_for_item(cause.span, impl_def_id);
574
575    let mut impl_ty = tcx.type_of(impl_def_id).instantiate(tcx, impl_args);
576    if !selcx.infcx.next_trait_solver() {
577        impl_ty = normalize_with_depth_to(
578            selcx,
579            param_env,
580            cause.clone(),
581            depth + 1,
582            impl_ty,
583            obligations,
584        );
585    }
586
587    // Infer the generic parameters of the impl by unifying the
588    // impl type with the self type of the projection.
589    let mut self_ty = alias_term.self_ty();
590    if !selcx.infcx.next_trait_solver() {
591        self_ty = normalize_with_depth_to(
592            selcx,
593            param_env,
594            cause.clone(),
595            depth + 1,
596            self_ty,
597            obligations,
598        );
599    }
600
601    match selcx.infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, impl_ty, self_ty) {
602        Ok(mut ok) => obligations.append(&mut ok.obligations),
603        Err(_) => {
604            tcx.dcx().span_bug(
605                cause.span,
606                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?} was equal to {1:?} during selection but now it is not",
                self_ty, impl_ty))
    })format!("{self_ty:?} was equal to {impl_ty:?} during selection but now it is not"),
607            );
608        }
609    }
610
611    alias_term.rebase_inherent_args_onto_impl(impl_args, tcx)
612}
613
614enum Projected<'tcx> {
615    Progress(Progress<'tcx>),
616    NoProgress(ty::Term<'tcx>),
617}
618
619struct Progress<'tcx> {
620    term: ty::Term<'tcx>,
621    obligations: PredicateObligations<'tcx>,
622}
623
624impl<'tcx> Progress<'tcx> {
625    fn error_for_term(
626        tcx: TyCtxt<'tcx>,
627        alias_term: ty::AliasTerm<'tcx>,
628        guar: ErrorGuaranteed,
629    ) -> Self {
630        let err_term = if alias_term.kind(tcx).is_type() {
631            Ty::new_error(tcx, guar).into()
632        } else {
633            ty::Const::new_error(tcx, guar).into()
634        };
635        Progress { term: err_term, obligations: PredicateObligations::new() }
636    }
637
638    fn with_addl_obligations(mut self, mut obligations: PredicateObligations<'tcx>) -> Self {
639        self.obligations.append(&mut obligations);
640        self
641    }
642}
643
644/// Computes the result of a projection type (if we can).
645///
646/// IMPORTANT:
647/// - `obligation` must be fully normalized
648// FIXME(mgca): While this supports constants, it is only used for types by default right now
649#[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("project",
                                    "rustc_trait_selection::traits::project",
                                    ::tracing::Level::INFO,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                    ::tracing_core::__macro_support::Option::Some(649u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                    ::tracing_core::field::FieldSet::new(&["obligation"],
                                        ::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(&::tracing::field::debug(&obligation)
                                                            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<Projected<'tcx>, ProjectionError<'tcx>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth)
                {
                return Err(ProjectionError::TraitSelectionError(SelectionError::Overflow(OverflowError::Canonical)));
            }
            if let Err(guar) =
                    obligation.predicate.non_region_error_reported() {
                return Ok(Projected::Progress(Progress::error_for_term(selcx.tcx(),
                                obligation.predicate, guar)));
            }
            let mut candidates = ProjectionCandidateSet::None;
            assemble_candidates_from_param_env(selcx, obligation,
                &mut candidates);
            assemble_candidates_from_trait_def(selcx, obligation,
                &mut candidates);
            assemble_candidates_from_object_ty(selcx, obligation,
                &mut candidates);
            if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_))
                    = candidates
                {} else {
                assemble_candidates_from_impls(selcx, obligation,
                    &mut candidates);
            };
            match candidates {
                ProjectionCandidateSet::Single(candidate) => {
                    confirm_candidate(selcx, obligation, candidate)
                }
                ProjectionCandidateSet::None => {
                    let tcx = selcx.tcx();
                    let term = obligation.predicate.to_term(tcx);
                    Ok(Projected::NoProgress(term))
                }
                ProjectionCandidateSet::Error(e) =>
                    Err(ProjectionError::TraitSelectionError(e)),
                ProjectionCandidateSet::Ambiguous =>
                    Err(ProjectionError::TooManyCandidates),
            }
        }
    }
}#[instrument(level = "info", skip(selcx))]
650fn project<'cx, 'tcx>(
651    selcx: &mut SelectionContext<'cx, 'tcx>,
652    obligation: &ProjectionTermObligation<'tcx>,
653) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
654    if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth) {
655        // This should really be an immediate error, but some existing code
656        // relies on being able to recover from this.
657        return Err(ProjectionError::TraitSelectionError(SelectionError::Overflow(
658            OverflowError::Canonical,
659        )));
660    }
661
662    // We can still compute a projection type when there are only region errors,
663    // but type/const errors require early return.
664    if let Err(guar) = obligation.predicate.non_region_error_reported() {
665        return Ok(Projected::Progress(Progress::error_for_term(
666            selcx.tcx(),
667            obligation.predicate,
668            guar,
669        )));
670    }
671
672    let mut candidates = ProjectionCandidateSet::None;
673
674    // Make sure that the following procedures are kept in order. ParamEnv
675    // needs to be first because it has highest priority, and Select checks
676    // the return value of push_candidate which assumes it's ran at last.
677    assemble_candidates_from_param_env(selcx, obligation, &mut candidates);
678
679    assemble_candidates_from_trait_def(selcx, obligation, &mut candidates);
680
681    assemble_candidates_from_object_ty(selcx, obligation, &mut candidates);
682
683    if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_)) = candidates {
684        // Avoid normalization cycle from selection (see
685        // `assemble_candidates_from_object_ty`).
686        // FIXME(lazy_normalization): Lazy normalization should save us from
687        // having to special case this.
688    } else {
689        assemble_candidates_from_impls(selcx, obligation, &mut candidates);
690    };
691
692    match candidates {
693        ProjectionCandidateSet::Single(candidate) => {
694            confirm_candidate(selcx, obligation, candidate)
695        }
696        ProjectionCandidateSet::None => {
697            let tcx = selcx.tcx();
698            let term = obligation.predicate.to_term(tcx);
699            Ok(Projected::NoProgress(term))
700        }
701        // Error occurred while trying to processing impls.
702        ProjectionCandidateSet::Error(e) => Err(ProjectionError::TraitSelectionError(e)),
703        // Inherent ambiguity that prevents us from even enumerating the
704        // candidates.
705        ProjectionCandidateSet::Ambiguous => Err(ProjectionError::TooManyCandidates),
706    }
707}
708
709/// The first thing we have to do is scan through the parameter
710/// environment to see whether there are any projection predicates
711/// there that can answer this question.
712fn assemble_candidates_from_param_env<'cx, 'tcx>(
713    selcx: &mut SelectionContext<'cx, 'tcx>,
714    obligation: &ProjectionTermObligation<'tcx>,
715    candidate_set: &mut ProjectionCandidateSet<'tcx>,
716) {
717    assemble_candidates_from_predicates(
718        selcx,
719        obligation,
720        candidate_set,
721        ProjectionCandidate::ParamEnv,
722        obligation.param_env.caller_bounds().iter(),
723        false,
724    );
725}
726
727/// In the case of a nested projection like `<<A as Foo>::FooT as Bar>::BarT`, we may find
728/// that the definition of `Foo` has some clues:
729///
730/// ```ignore (illustrative)
731/// trait Foo {
732///     type FooT : Bar<BarT=i32>
733/// }
734/// ```
735///
736/// Here, for example, we could conclude that the result is `i32`.
737fn assemble_candidates_from_trait_def<'cx, 'tcx>(
738    selcx: &mut SelectionContext<'cx, 'tcx>,
739    obligation: &ProjectionTermObligation<'tcx>,
740    candidate_set: &mut ProjectionCandidateSet<'tcx>,
741) {
742    {
    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/project.rs:742",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(742u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::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!("assemble_candidates_from_trait_def(..)")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("assemble_candidates_from_trait_def(..)");
743    let mut ambiguous = false;
744    let _ = selcx.for_each_item_bound(
745        obligation.predicate.self_ty(),
746        |selcx, clause, _, _| {
747            let Some(clause) = clause.as_projection_clause() else {
748                return ControlFlow::Continue(());
749            };
750            if clause.item_def_id() != obligation.predicate.def_id {
751                return ControlFlow::Continue(());
752            }
753
754            let is_match =
755                selcx.infcx.probe(|_| selcx.match_projection_projections(obligation, clause, true));
756
757            match is_match {
758                ProjectionMatchesProjection::Yes => {
759                    candidate_set.push_candidate(ProjectionCandidate::TraitDef(clause));
760
761                    if !obligation.predicate.has_non_region_infer() {
762                        // HACK: Pick the first trait def candidate for a fully
763                        // inferred predicate. This is to allow duplicates that
764                        // differ only in normalization.
765                        return ControlFlow::Break(());
766                    }
767                }
768                ProjectionMatchesProjection::Ambiguous => {
769                    candidate_set.mark_ambiguous();
770                }
771                ProjectionMatchesProjection::No => {}
772            }
773
774            ControlFlow::Continue(())
775        },
776        // `ProjectionCandidateSet` is borrowed in the above closure,
777        // so just mark ambiguous outside of the closure.
778        || ambiguous = true,
779    );
780
781    if ambiguous {
782        candidate_set.mark_ambiguous();
783    }
784}
785
786/// In the case of a trait object like
787/// `<dyn Iterator<Item = ()> as Iterator>::Item` we can use the existential
788/// predicate in the trait object.
789///
790/// We don't go through the select candidate for these bounds to avoid cycles:
791/// In the above case, `dyn Iterator<Item = ()>: Iterator` would create a
792/// nested obligation of `<dyn Iterator<Item = ()> as Iterator>::Item: Sized`,
793/// this then has to be normalized without having to prove
794/// `dyn Iterator<Item = ()>: Iterator` again.
795fn assemble_candidates_from_object_ty<'cx, 'tcx>(
796    selcx: &mut SelectionContext<'cx, 'tcx>,
797    obligation: &ProjectionTermObligation<'tcx>,
798    candidate_set: &mut ProjectionCandidateSet<'tcx>,
799) {
800    {
    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/project.rs:800",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(800u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::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!("assemble_candidates_from_object_ty(..)")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("assemble_candidates_from_object_ty(..)");
801
802    let tcx = selcx.tcx();
803
804    let self_ty = obligation.predicate.self_ty();
805    let object_ty = selcx.infcx.shallow_resolve(self_ty);
806    let data = match object_ty.kind() {
807        ty::Dynamic(data, ..) => data,
808        ty::Infer(ty::TyVar(_)) => {
809            // If the self-type is an inference variable, then it MAY wind up
810            // being an object type, so induce an ambiguity.
811            candidate_set.mark_ambiguous();
812            return;
813        }
814        _ => return,
815    };
816    let env_predicates = data
817        .projection_bounds()
818        .filter(|bound| bound.item_def_id() == obligation.predicate.def_id)
819        .map(|p| p.with_self_ty(tcx, object_ty).upcast(tcx));
820
821    assemble_candidates_from_predicates(
822        selcx,
823        obligation,
824        candidate_set,
825        ProjectionCandidate::Object,
826        env_predicates,
827        false,
828    );
829}
830
831#[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("assemble_candidates_from_predicates",
                                    "rustc_trait_selection::traits::project",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                    ::tracing_core::__macro_support::Option::Some(831u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                    ::tracing_core::field::FieldSet::new(&["obligation"],
                                        ::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(&obligation)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let infcx = selcx.infcx;
            let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
            for predicate in env_predicates {
                let bound_predicate = predicate.kind();
                if let ty::ClauseKind::Projection(data) =
                        predicate.kind().skip_binder() {
                    let data = bound_predicate.rebind(data);
                    if data.item_def_id() != obligation.predicate.def_id {
                        continue;
                    }
                    if !drcx.args_may_unify(obligation.predicate.args,
                                data.skip_binder().projection_term.args) {
                        continue;
                    }
                    let is_match =
                        infcx.probe(|_|
                                {
                                    selcx.match_projection_projections(obligation, data,
                                        potentially_unnormalized_candidates)
                                });
                    match is_match {
                        ProjectionMatchesProjection::Yes => {
                            candidate_set.push_candidate(ctor(data));
                            if potentially_unnormalized_candidates &&
                                    !obligation.predicate.has_non_region_infer() {
                                return;
                            }
                        }
                        ProjectionMatchesProjection::Ambiguous => {
                            candidate_set.mark_ambiguous();
                        }
                        ProjectionMatchesProjection::No => {}
                    }
                }
            }
        }
    }
}#[instrument(
832    level = "debug",
833    skip(selcx, candidate_set, ctor, env_predicates, potentially_unnormalized_candidates)
834)]
835fn assemble_candidates_from_predicates<'cx, 'tcx>(
836    selcx: &mut SelectionContext<'cx, 'tcx>,
837    obligation: &ProjectionTermObligation<'tcx>,
838    candidate_set: &mut ProjectionCandidateSet<'tcx>,
839    ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionCandidate<'tcx>,
840    env_predicates: impl Iterator<Item = ty::Clause<'tcx>>,
841    potentially_unnormalized_candidates: bool,
842) {
843    let infcx = selcx.infcx;
844    let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
845    for predicate in env_predicates {
846        let bound_predicate = predicate.kind();
847        if let ty::ClauseKind::Projection(data) = predicate.kind().skip_binder() {
848            let data = bound_predicate.rebind(data);
849            if data.item_def_id() != obligation.predicate.def_id {
850                continue;
851            }
852
853            if !drcx
854                .args_may_unify(obligation.predicate.args, data.skip_binder().projection_term.args)
855            {
856                continue;
857            }
858
859            let is_match = infcx.probe(|_| {
860                selcx.match_projection_projections(
861                    obligation,
862                    data,
863                    potentially_unnormalized_candidates,
864                )
865            });
866
867            match is_match {
868                ProjectionMatchesProjection::Yes => {
869                    candidate_set.push_candidate(ctor(data));
870
871                    if potentially_unnormalized_candidates
872                        && !obligation.predicate.has_non_region_infer()
873                    {
874                        // HACK: Pick the first trait def candidate for a fully
875                        // inferred predicate. This is to allow duplicates that
876                        // differ only in normalization.
877                        return;
878                    }
879                }
880                ProjectionMatchesProjection::Ambiguous => {
881                    candidate_set.mark_ambiguous();
882                }
883                ProjectionMatchesProjection::No => {}
884            }
885        }
886    }
887}
888
889#[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("assemble_candidates_from_impls",
                                    "rustc_trait_selection::traits::project",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                    ::tracing_core::__macro_support::Option::Some(889u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let trait_ref = obligation.predicate.trait_ref(selcx.tcx());
            let trait_obligation = obligation.with(selcx.tcx(), trait_ref);
            let _ =
                selcx.infcx.commit_if_ok(|_|
                        {
                            let impl_source =
                                match selcx.select(&trait_obligation) {
                                    Ok(Some(impl_source)) => impl_source,
                                    Ok(None) => {
                                        candidate_set.mark_ambiguous();
                                        return Err(());
                                    }
                                    Err(e) => {
                                        {
                                            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/project.rs:907",
                                                                "rustc_trait_selection::traits::project",
                                                                ::tracing::Level::DEBUG,
                                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                                                ::tracing_core::__macro_support::Option::Some(907u32),
                                                                ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                                                ::tracing_core::field::FieldSet::new(&["message", "error"],
                                                                    ::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!("selection error")
                                                                                    as &dyn Value)),
                                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                                            ::tracing::__macro_support::Option::Some(&debug(&e) as
                                                                                    &dyn Value))])
                                                    });
                                            } else { ; }
                                        };
                                        candidate_set.mark_error(e);
                                        return Err(());
                                    }
                                };
                            let eligible =
                                match &impl_source {
                                    ImplSource::UserDefined(impl_data) => {
                                        match specialization_graph::assoc_def(selcx.tcx(),
                                                impl_data.impl_def_id, obligation.predicate.def_id) {
                                            Ok(node_item) => {
                                                if node_item.is_final() {
                                                    true
                                                } else {
                                                    match selcx.infcx.typing_mode() {
                                                        TypingMode::Coherence | TypingMode::Analysis { .. } |
                                                            TypingMode::Borrowck { .. } |
                                                            TypingMode::PostBorrowckAnalysis { .. } => {
                                                            {
                                                                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/project.rs:956",
                                                                                    "rustc_trait_selection::traits::project",
                                                                                    ::tracing::Level::DEBUG,
                                                                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                                                                                    ::tracing_core::__macro_support::Option::Some(956u32),
                                                                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                                                                                    ::tracing_core::field::FieldSet::new(&["message",
                                                                                                    "assoc_ty", "obligation.predicate"],
                                                                                        ::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!("not eligible due to default")
                                                                                                        as &dyn Value)),
                                                                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                                                                ::tracing::__macro_support::Option::Some(&debug(&selcx.tcx().def_path_str(node_item.item.def_id))
                                                                                                        as &dyn Value)),
                                                                                            (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                                                                ::tracing::__macro_support::Option::Some(&debug(&obligation.predicate)
                                                                                                        as &dyn Value))])
                                                                        });
                                                                } else { ; }
                                                            };
                                                            false
                                                        }
                                                        TypingMode::PostAnalysis => {
                                                            let poly_trait_ref =
                                                                selcx.infcx.resolve_vars_if_possible(trait_ref);
                                                            !poly_trait_ref.still_further_specializable()
                                                        }
                                                    }
                                                }
                                            }
                                            Err(ErrorGuaranteed { .. }) => true,
                                        }
                                    }
                                    ImplSource::Builtin(BuiltinImplSource::Misc |
                                        BuiltinImplSource::Trivial, _) => {
                                        let self_ty =
                                            selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
                                        let tcx = selcx.tcx();
                                        match selcx.tcx().as_lang_item(trait_ref.def_id) {
                                            Some(LangItem::Coroutine | LangItem::Future |
                                                LangItem::Iterator | LangItem::AsyncIterator |
                                                LangItem::Field | LangItem::Fn | LangItem::FnMut |
                                                LangItem::FnOnce | LangItem::AsyncFn | LangItem::AsyncFnMut
                                                | LangItem::AsyncFnOnce) => true,
                                            Some(LangItem::AsyncFnKindHelper) => {
                                                if obligation.predicate.args.type_at(0).is_ty_var() ||
                                                            obligation.predicate.args.type_at(4).is_ty_var() ||
                                                        obligation.predicate.args.type_at(5).is_ty_var() {
                                                    candidate_set.mark_ambiguous();
                                                    true
                                                } else {
                                                    obligation.predicate.args.type_at(0).to_opt_closure_kind().is_some()
                                                        &&
                                                        obligation.predicate.args.type_at(1).to_opt_closure_kind().is_some()
                                                }
                                            }
                                            Some(LangItem::DiscriminantKind) =>
                                                match self_ty.kind() {
                                                    ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) |
                                                        ty::Float(_) | ty::Adt(..) | ty::Foreign(_) | ty::Str |
                                                        ty::Array(..) | ty::Pat(..) | ty::Slice(_) | ty::RawPtr(..)
                                                        | ty::Ref(..) | ty::FnDef(..) | ty::FnPtr(..) |
                                                        ty::Dynamic(..) | ty::Closure(..) | ty::CoroutineClosure(..)
                                                        | ty::Coroutine(..) | ty::CoroutineWitness(..) | ty::Never |
                                                        ty::Tuple(..) |
                                                        ty::Infer(ty::InferTy::IntVar(_) |
                                                        ty::InferTy::FloatVar(..)) => true,
                                                    ty::UnsafeBinder(_) => {
                                                        ::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
                                                                format_args!("FIXME(unsafe_binder)")));
                                                    }
                                                    ty::Param(_) | ty::Alias(..) | ty::Bound(..) |
                                                        ty::Placeholder(..) | ty::Infer(..) | ty::Error(_) => false,
                                                },
                                            Some(LangItem::PointeeTrait) => {
                                                let tail =
                                                    selcx.tcx().struct_tail_raw(self_ty, &obligation.cause,
                                                        |ty|
                                                            {
                                                                normalize_with_depth(selcx, obligation.param_env,
                                                                        obligation.cause.clone(), obligation.recursion_depth + 1,
                                                                        ty).value
                                                            }, || {});
                                                match tail.kind() {
                                                    ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) |
                                                        ty::Float(_) | ty::Str | ty::Array(..) | ty::Pat(..) |
                                                        ty::Slice(_) | ty::RawPtr(..) | ty::Ref(..) | ty::FnDef(..)
                                                        | ty::FnPtr(..) | ty::Dynamic(..) | ty::Closure(..) |
                                                        ty::CoroutineClosure(..) | ty::Coroutine(..) |
                                                        ty::CoroutineWitness(..) | ty::Never | ty::Foreign(_) |
                                                        ty::Adt(..) | ty::Tuple(..) |
                                                        ty::Infer(ty::InferTy::IntVar(_) |
                                                        ty::InferTy::FloatVar(..)) | ty::Error(..) => true,
                                                    ty::Param(_) | ty::Alias(..) if
                                                        self_ty != tail ||
                                                            selcx.infcx.predicate_must_hold_modulo_regions(&obligation.with(selcx.tcx(),
                                                                        ty::TraitRef::new(selcx.tcx(),
                                                                            selcx.tcx().require_lang_item(LangItem::Sized,
                                                                                obligation.cause.span), [self_ty]))) => {
                                                        true
                                                    }
                                                    ty::UnsafeBinder(_) => {
                                                        ::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
                                                                format_args!("FIXME(unsafe_binder)")));
                                                    }
                                                    ty::Param(_) | ty::Alias(..) | ty::Bound(..) |
                                                        ty::Placeholder(..) | ty::Infer(..) => {
                                                        if tail.has_infer_types() {
                                                            candidate_set.mark_ambiguous();
                                                        }
                                                        false
                                                    }
                                                }
                                            }
                                            _ if tcx.trait_is_auto(trait_ref.def_id) => {
                                                tcx.dcx().span_delayed_bug(tcx.def_span(obligation.predicate.def_id),
                                                    "associated types not allowed on auto traits");
                                                false
                                            }
                                            _ => {
                                                ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected builtin trait with associated type: {0:?}",
                                                        trait_ref))
                                            }
                                        }
                                    }
                                    ImplSource::Param(..) => { false }
                                    ImplSource::Builtin(BuiltinImplSource::Object { .. }, _) =>
                                        {
                                        false
                                    }
                                    ImplSource::Builtin(BuiltinImplSource::TraitUpcasting { ..
                                        }, _) => {
                                        selcx.tcx().dcx().span_delayed_bug(obligation.cause.span,
                                            ::alloc::__export::must_use({
                                                    ::alloc::fmt::format(format_args!("Cannot project an associated type from `{0:?}`",
                                                            impl_source))
                                                }));
                                        return Err(());
                                    }
                                };
                            if eligible {
                                if candidate_set.push_candidate(ProjectionCandidate::Select(impl_source))
                                    {
                                    Ok(())
                                } else { Err(()) }
                            } else { Err(()) }
                        });
        }
    }
}#[instrument(level = "debug", skip(selcx, obligation, candidate_set))]
890fn assemble_candidates_from_impls<'cx, 'tcx>(
891    selcx: &mut SelectionContext<'cx, 'tcx>,
892    obligation: &ProjectionTermObligation<'tcx>,
893    candidate_set: &mut ProjectionCandidateSet<'tcx>,
894) {
895    // If we are resolving `<T as TraitRef<...>>::Item == Type`,
896    // start out by selecting the predicate `T as TraitRef<...>`:
897    let trait_ref = obligation.predicate.trait_ref(selcx.tcx());
898    let trait_obligation = obligation.with(selcx.tcx(), trait_ref);
899    let _ = selcx.infcx.commit_if_ok(|_| {
900        let impl_source = match selcx.select(&trait_obligation) {
901            Ok(Some(impl_source)) => impl_source,
902            Ok(None) => {
903                candidate_set.mark_ambiguous();
904                return Err(());
905            }
906            Err(e) => {
907                debug!(error = ?e, "selection error");
908                candidate_set.mark_error(e);
909                return Err(());
910            }
911        };
912
913        let eligible = match &impl_source {
914            ImplSource::UserDefined(impl_data) => {
915                // We have to be careful when projecting out of an
916                // impl because of specialization. If we are not in
917                // codegen (i.e., `TypingMode` is not `PostAnalysis`), and the
918                // impl's type is declared as default, then we disable
919                // projection (even if the trait ref is fully
920                // monomorphic). In the case where trait ref is not
921                // fully monomorphic (i.e., includes type parameters),
922                // this is because those type parameters may
923                // ultimately be bound to types from other crates that
924                // may have specialized impls we can't see. In the
925                // case where the trait ref IS fully monomorphic, this
926                // is a policy decision that we made in the RFC in
927                // order to preserve flexibility for the crate that
928                // defined the specializable impl to specialize later
929                // for existing types.
930                //
931                // In either case, we handle this by not adding a
932                // candidate for an impl if it contains a `default`
933                // type.
934                //
935                // NOTE: This should be kept in sync with the similar code in
936                // `rustc_ty_utils::instance::resolve_associated_item()`.
937                match specialization_graph::assoc_def(
938                    selcx.tcx(),
939                    impl_data.impl_def_id,
940                    obligation.predicate.def_id,
941                ) {
942                    Ok(node_item) => {
943                        if node_item.is_final() {
944                            // Non-specializable items are always projectable.
945                            true
946                        } else {
947                            // Only reveal a specializable default if we're past type-checking
948                            // and the obligation is monomorphic, otherwise passes such as
949                            // transmute checking and polymorphic MIR optimizations could
950                            // get a result which isn't correct for all monomorphizations.
951                            match selcx.infcx.typing_mode() {
952                                TypingMode::Coherence
953                                | TypingMode::Analysis { .. }
954                                | TypingMode::Borrowck { .. }
955                                | TypingMode::PostBorrowckAnalysis { .. } => {
956                                    debug!(
957                                        assoc_ty = ?selcx.tcx().def_path_str(node_item.item.def_id),
958                                        ?obligation.predicate,
959                                        "not eligible due to default",
960                                    );
961                                    false
962                                }
963                                TypingMode::PostAnalysis => {
964                                    // NOTE(eddyb) inference variables can resolve to parameters, so
965                                    // assume `poly_trait_ref` isn't monomorphic, if it contains any.
966                                    let poly_trait_ref =
967                                        selcx.infcx.resolve_vars_if_possible(trait_ref);
968                                    !poly_trait_ref.still_further_specializable()
969                                }
970                            }
971                        }
972                    }
973                    // Always project `ErrorGuaranteed`, since this will just help
974                    // us propagate `TyKind::Error` around which suppresses ICEs
975                    // and spurious, unrelated inference errors.
976                    Err(ErrorGuaranteed { .. }) => true,
977                }
978            }
979            ImplSource::Builtin(BuiltinImplSource::Misc | BuiltinImplSource::Trivial, _) => {
980                // While a builtin impl may be known to exist, the associated type may not yet
981                // be known. Any type with multiple potential associated types is therefore
982                // not eligible.
983                let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
984
985                let tcx = selcx.tcx();
986                match selcx.tcx().as_lang_item(trait_ref.def_id) {
987                    Some(
988                        LangItem::Coroutine
989                        | LangItem::Future
990                        | LangItem::Iterator
991                        | LangItem::AsyncIterator
992                        | LangItem::Field
993                        | LangItem::Fn
994                        | LangItem::FnMut
995                        | LangItem::FnOnce
996                        | LangItem::AsyncFn
997                        | LangItem::AsyncFnMut
998                        | LangItem::AsyncFnOnce,
999                    ) => true,
1000                    Some(LangItem::AsyncFnKindHelper) => {
1001                        // FIXME(async_closures): Validity constraints here could be cleaned up.
1002                        if obligation.predicate.args.type_at(0).is_ty_var()
1003                            || obligation.predicate.args.type_at(4).is_ty_var()
1004                            || obligation.predicate.args.type_at(5).is_ty_var()
1005                        {
1006                            candidate_set.mark_ambiguous();
1007                            true
1008                        } else {
1009                            obligation.predicate.args.type_at(0).to_opt_closure_kind().is_some()
1010                                && obligation
1011                                    .predicate
1012                                    .args
1013                                    .type_at(1)
1014                                    .to_opt_closure_kind()
1015                                    .is_some()
1016                        }
1017                    }
1018                    Some(LangItem::DiscriminantKind) => match self_ty.kind() {
1019                        ty::Bool
1020                        | ty::Char
1021                        | ty::Int(_)
1022                        | ty::Uint(_)
1023                        | ty::Float(_)
1024                        | ty::Adt(..)
1025                        | ty::Foreign(_)
1026                        | ty::Str
1027                        | ty::Array(..)
1028                        | ty::Pat(..)
1029                        | ty::Slice(_)
1030                        | ty::RawPtr(..)
1031                        | ty::Ref(..)
1032                        | ty::FnDef(..)
1033                        | ty::FnPtr(..)
1034                        | ty::Dynamic(..)
1035                        | ty::Closure(..)
1036                        | ty::CoroutineClosure(..)
1037                        | ty::Coroutine(..)
1038                        | ty::CoroutineWitness(..)
1039                        | ty::Never
1040                        | ty::Tuple(..)
1041                        // Integers and floats always have `u8` as their discriminant.
1042                        | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1043
1044                        ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
1045
1046                        // type parameters, opaques, and unnormalized projections don't have
1047                        // a known discriminant and may need to be normalized further or rely
1048                        // on param env for discriminant projections
1049                        ty::Param(_)
1050                        | ty::Alias(..)
1051                        | ty::Bound(..)
1052                        | ty::Placeholder(..)
1053                        | ty::Infer(..)
1054                        | ty::Error(_) => false,
1055                    },
1056                    Some(LangItem::PointeeTrait) => {
1057                        let tail = selcx.tcx().struct_tail_raw(
1058                            self_ty,
1059                            &obligation.cause,
1060                            |ty| {
1061                                // We throw away any obligations we get from this, since we normalize
1062                                // and confirm these obligations once again during confirmation
1063                                normalize_with_depth(
1064                                    selcx,
1065                                    obligation.param_env,
1066                                    obligation.cause.clone(),
1067                                    obligation.recursion_depth + 1,
1068                                    ty,
1069                                )
1070                                .value
1071                            },
1072                            || {},
1073                        );
1074
1075                        match tail.kind() {
1076                            ty::Bool
1077                            | ty::Char
1078                            | ty::Int(_)
1079                            | ty::Uint(_)
1080                            | ty::Float(_)
1081                            | ty::Str
1082                            | ty::Array(..)
1083                            | ty::Pat(..)
1084                            | ty::Slice(_)
1085                            | ty::RawPtr(..)
1086                            | ty::Ref(..)
1087                            | ty::FnDef(..)
1088                            | ty::FnPtr(..)
1089                            | ty::Dynamic(..)
1090                            | ty::Closure(..)
1091                            | ty::CoroutineClosure(..)
1092                            | ty::Coroutine(..)
1093                            | ty::CoroutineWitness(..)
1094                            | ty::Never
1095                            // Extern types have unit metadata, according to RFC 2850
1096                            | ty::Foreign(_)
1097                            // If returned by `struct_tail` this is a unit struct
1098                            // without any fields, or not a struct, and therefore is Sized.
1099                            | ty::Adt(..)
1100                            // If returned by `struct_tail` this is the empty tuple.
1101                            | ty::Tuple(..)
1102                            // Integers and floats are always Sized, and so have unit type metadata.
1103                            | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..))
1104                            // This happens if we reach the recursion limit when finding the struct tail.
1105                            | ty::Error(..) => true,
1106
1107                            // We normalize from `Wrapper<Tail>::Metadata` to `Tail::Metadata` if able.
1108                            // Otherwise, type parameters, opaques, and unnormalized projections have
1109                            // unit metadata if they're known (e.g. by the param_env) to be sized.
1110                            ty::Param(_) | ty::Alias(..)
1111                                if self_ty != tail
1112                                    || selcx.infcx.predicate_must_hold_modulo_regions(
1113                                        &obligation.with(
1114                                            selcx.tcx(),
1115                                            ty::TraitRef::new(
1116                                                selcx.tcx(),
1117                                                selcx.tcx().require_lang_item(
1118                                                    LangItem::Sized,
1119                                                    obligation.cause.span,
1120                                                ),
1121                                                [self_ty],
1122                                            ),
1123                                        ),
1124                                    ) =>
1125                            {
1126                                true
1127                            }
1128
1129                            ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
1130
1131                            // FIXME(compiler-errors): are Bound and Placeholder types ever known sized?
1132                            ty::Param(_)
1133                            | ty::Alias(..)
1134                            | ty::Bound(..)
1135                            | ty::Placeholder(..)
1136                            | ty::Infer(..) => {
1137                                if tail.has_infer_types() {
1138                                    candidate_set.mark_ambiguous();
1139                                }
1140                                false
1141                            }
1142                        }
1143                    }
1144                    _ if tcx.trait_is_auto(trait_ref.def_id) => {
1145                        tcx.dcx().span_delayed_bug(
1146                            tcx.def_span(obligation.predicate.def_id),
1147                            "associated types not allowed on auto traits",
1148                        );
1149                        false
1150                    }
1151                    _ => {
1152                        bug!("unexpected builtin trait with associated type: {trait_ref:?}")
1153                    }
1154                }
1155            }
1156            ImplSource::Param(..) => {
1157                // This case tell us nothing about the value of an
1158                // associated type. Consider:
1159                //
1160                // ```
1161                // trait SomeTrait { type Foo; }
1162                // fn foo<T:SomeTrait>(...) { }
1163                // ```
1164                //
1165                // If the user writes `<T as SomeTrait>::Foo`, then the `T
1166                // : SomeTrait` binding does not help us decide what the
1167                // type `Foo` is (at least, not more specifically than
1168                // what we already knew).
1169                //
1170                // But wait, you say! What about an example like this:
1171                //
1172                // ```
1173                // fn bar<T:SomeTrait<Foo=usize>>(...) { ... }
1174                // ```
1175                //
1176                // Doesn't the `T : SomeTrait<Foo=usize>` predicate help
1177                // resolve `T::Foo`? And of course it does, but in fact
1178                // that single predicate is desugared into two predicates
1179                // in the compiler: a trait predicate (`T : SomeTrait`) and a
1180                // projection. And the projection where clause is handled
1181                // in `assemble_candidates_from_param_env`.
1182                false
1183            }
1184            ImplSource::Builtin(BuiltinImplSource::Object { .. }, _) => {
1185                // Handled by the `Object` projection candidate. See
1186                // `assemble_candidates_from_object_ty` for an explanation of
1187                // why we special case object types.
1188                false
1189            }
1190            ImplSource::Builtin(BuiltinImplSource::TraitUpcasting { .. }, _) => {
1191                // These traits have no associated types.
1192                selcx.tcx().dcx().span_delayed_bug(
1193                    obligation.cause.span,
1194                    format!("Cannot project an associated type from `{impl_source:?}`"),
1195                );
1196                return Err(());
1197            }
1198        };
1199
1200        if eligible {
1201            if candidate_set.push_candidate(ProjectionCandidate::Select(impl_source)) {
1202                Ok(())
1203            } else {
1204                Err(())
1205            }
1206        } else {
1207            Err(())
1208        }
1209    });
1210}
1211
1212// FIXME(mgca): While this supports constants, it is only used for types by default right now
1213fn confirm_candidate<'cx, 'tcx>(
1214    selcx: &mut SelectionContext<'cx, 'tcx>,
1215    obligation: &ProjectionTermObligation<'tcx>,
1216    candidate: ProjectionCandidate<'tcx>,
1217) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1218    {
    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/project.rs:1218",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1218u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "obligation", "candidate"],
                            ::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!("confirm_candidate")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligation)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&candidate)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?obligation, ?candidate, "confirm_candidate");
1219    let mut result = match candidate {
1220        ProjectionCandidate::ParamEnv(poly_projection)
1221        | ProjectionCandidate::Object(poly_projection) => Ok(Projected::Progress(
1222            confirm_param_env_candidate(selcx, obligation, poly_projection, false),
1223        )),
1224        ProjectionCandidate::TraitDef(poly_projection) => Ok(Projected::Progress(
1225            confirm_param_env_candidate(selcx, obligation, poly_projection, true),
1226        )),
1227        ProjectionCandidate::Select(impl_source) => {
1228            confirm_select_candidate(selcx, obligation, impl_source)
1229        }
1230    };
1231
1232    // When checking for cycle during evaluation, we compare predicates with
1233    // "syntactic" equality. Since normalization generally introduces a type
1234    // with new region variables, we need to resolve them to existing variables
1235    // when possible for this to work. See `auto-trait-projection-recursion.rs`
1236    // for a case where this matters.
1237    if let Ok(Projected::Progress(progress)) = &mut result
1238        && progress.term.has_infer_regions()
1239    {
1240        progress.term = progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx));
1241    }
1242
1243    result
1244}
1245
1246// FIXME(mgca): While this supports constants, it is only used for types by default right now
1247fn confirm_select_candidate<'cx, 'tcx>(
1248    selcx: &mut SelectionContext<'cx, 'tcx>,
1249    obligation: &ProjectionTermObligation<'tcx>,
1250    impl_source: Selection<'tcx>,
1251) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1252    match impl_source {
1253        ImplSource::UserDefined(data) => confirm_impl_candidate(selcx, obligation, data),
1254        ImplSource::Builtin(BuiltinImplSource::Misc | BuiltinImplSource::Trivial, data) => {
1255            let tcx = selcx.tcx();
1256            let trait_def_id = obligation.predicate.trait_def_id(tcx);
1257            let progress = if tcx.is_lang_item(trait_def_id, LangItem::Coroutine) {
1258                confirm_coroutine_candidate(selcx, obligation, data)
1259            } else if tcx.is_lang_item(trait_def_id, LangItem::Future) {
1260                confirm_future_candidate(selcx, obligation, data)
1261            } else if tcx.is_lang_item(trait_def_id, LangItem::Iterator) {
1262                confirm_iterator_candidate(selcx, obligation, data)
1263            } else if tcx.is_lang_item(trait_def_id, LangItem::AsyncIterator) {
1264                confirm_async_iterator_candidate(selcx, obligation, data)
1265            } else if selcx.tcx().fn_trait_kind_from_def_id(trait_def_id).is_some() {
1266                if obligation.predicate.self_ty().is_closure()
1267                    || obligation.predicate.self_ty().is_coroutine_closure()
1268                {
1269                    confirm_closure_candidate(selcx, obligation, data)
1270                } else {
1271                    confirm_fn_pointer_candidate(selcx, obligation, data)
1272                }
1273            } else if selcx.tcx().async_fn_trait_kind_from_def_id(trait_def_id).is_some() {
1274                confirm_async_closure_candidate(selcx, obligation, data)
1275            } else if tcx.is_lang_item(trait_def_id, LangItem::AsyncFnKindHelper) {
1276                confirm_async_fn_kind_helper_candidate(selcx, obligation, data)
1277            } else {
1278                confirm_builtin_candidate(selcx, obligation, data)
1279            };
1280            Ok(Projected::Progress(progress))
1281        }
1282        ImplSource::Builtin(BuiltinImplSource::Object { .. }, _)
1283        | ImplSource::Param(..)
1284        | ImplSource::Builtin(BuiltinImplSource::TraitUpcasting { .. }, _) => {
1285            // we don't create Select candidates with this kind of resolution
1286            ::rustc_middle::util::bug::span_bug_fmt(obligation.cause.span,
    format_args!("Cannot project an associated type from `{0:?}`",
        impl_source))span_bug!(
1287                obligation.cause.span,
1288                "Cannot project an associated type from `{:?}`",
1289                impl_source
1290            )
1291        }
1292    }
1293}
1294
1295fn confirm_coroutine_candidate<'cx, 'tcx>(
1296    selcx: &mut SelectionContext<'cx, 'tcx>,
1297    obligation: &ProjectionTermObligation<'tcx>,
1298    nested: PredicateObligations<'tcx>,
1299) -> Progress<'tcx> {
1300    let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1301    let ty::Coroutine(_, args) = self_ty.kind() else {
1302        {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("expected coroutine self type for built-in coroutine candidate, found {0}",
                self_ty)));
}unreachable!(
1303            "expected coroutine self type for built-in coroutine candidate, found {self_ty}"
1304        )
1305    };
1306    let coroutine_sig = args.as_coroutine().sig();
1307    let Normalized { value: coroutine_sig, obligations } = normalize_with_depth(
1308        selcx,
1309        obligation.param_env,
1310        obligation.cause.clone(),
1311        obligation.recursion_depth + 1,
1312        coroutine_sig,
1313    );
1314
1315    {
    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/project.rs:1315",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1315u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "obligation", "coroutine_sig", "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(&format_args!("confirm_coroutine_candidate")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligation)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&coroutine_sig)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligations)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?obligation, ?coroutine_sig, ?obligations, "confirm_coroutine_candidate");
1316
1317    let tcx = selcx.tcx();
1318
1319    let coroutine_def_id = tcx.require_lang_item(LangItem::Coroutine, obligation.cause.span);
1320
1321    let (trait_ref, yield_ty, return_ty) = super::util::coroutine_trait_ref_and_outputs(
1322        tcx,
1323        coroutine_def_id,
1324        obligation.predicate.self_ty(),
1325        coroutine_sig,
1326    );
1327
1328    let ty = if tcx.is_lang_item(obligation.predicate.def_id, LangItem::CoroutineReturn) {
1329        return_ty
1330    } else if tcx.is_lang_item(obligation.predicate.def_id, LangItem::CoroutineYield) {
1331        yield_ty
1332    } else {
1333        ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(obligation.predicate.def_id),
    format_args!("unexpected associated type: `Coroutine::{0}`",
        tcx.item_name(obligation.predicate.def_id)));span_bug!(
1334            tcx.def_span(obligation.predicate.def_id),
1335            "unexpected associated type: `Coroutine::{}`",
1336            tcx.item_name(obligation.predicate.def_id),
1337        );
1338    };
1339
1340    let predicate = ty::ProjectionPredicate {
1341        projection_term: ty::AliasTerm::new_from_args(
1342            tcx,
1343            obligation.predicate.def_id,
1344            trait_ref.args,
1345        ),
1346        term: ty.into(),
1347    };
1348
1349    confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1350        .with_addl_obligations(nested)
1351        .with_addl_obligations(obligations)
1352}
1353
1354fn confirm_future_candidate<'cx, 'tcx>(
1355    selcx: &mut SelectionContext<'cx, 'tcx>,
1356    obligation: &ProjectionTermObligation<'tcx>,
1357    nested: PredicateObligations<'tcx>,
1358) -> Progress<'tcx> {
1359    let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1360    let ty::Coroutine(_, args) = self_ty.kind() else {
1361        {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("expected coroutine self type for built-in async future candidate, found {0}",
                self_ty)));
}unreachable!(
1362            "expected coroutine self type for built-in async future candidate, found {self_ty}"
1363        )
1364    };
1365    let coroutine_sig = args.as_coroutine().sig();
1366    let Normalized { value: coroutine_sig, obligations } = normalize_with_depth(
1367        selcx,
1368        obligation.param_env,
1369        obligation.cause.clone(),
1370        obligation.recursion_depth + 1,
1371        coroutine_sig,
1372    );
1373
1374    {
    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/project.rs:1374",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1374u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "obligation", "coroutine_sig", "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(&format_args!("confirm_future_candidate")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligation)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&coroutine_sig)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligations)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?obligation, ?coroutine_sig, ?obligations, "confirm_future_candidate");
1375
1376    let tcx = selcx.tcx();
1377    let fut_def_id = tcx.require_lang_item(LangItem::Future, obligation.cause.span);
1378
1379    let (trait_ref, return_ty) = super::util::future_trait_ref_and_outputs(
1380        tcx,
1381        fut_def_id,
1382        obligation.predicate.self_ty(),
1383        coroutine_sig,
1384    );
1385
1386    if true {
    match (&tcx.associated_item(obligation.predicate.def_id).name(),
            &sym::Output) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Output);
1387
1388    let predicate = ty::ProjectionPredicate {
1389        projection_term: ty::AliasTerm::new_from_args(
1390            tcx,
1391            obligation.predicate.def_id,
1392            trait_ref.args,
1393        ),
1394        term: return_ty.into(),
1395    };
1396
1397    confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1398        .with_addl_obligations(nested)
1399        .with_addl_obligations(obligations)
1400}
1401
1402fn confirm_iterator_candidate<'cx, 'tcx>(
1403    selcx: &mut SelectionContext<'cx, 'tcx>,
1404    obligation: &ProjectionTermObligation<'tcx>,
1405    nested: PredicateObligations<'tcx>,
1406) -> Progress<'tcx> {
1407    let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1408    let ty::Coroutine(_, args) = self_ty.kind() else {
1409        {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("expected coroutine self type for built-in gen candidate, found {0}",
                self_ty)));
}unreachable!("expected coroutine self type for built-in gen candidate, found {self_ty}")
1410    };
1411    let gen_sig = args.as_coroutine().sig();
1412    let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1413        selcx,
1414        obligation.param_env,
1415        obligation.cause.clone(),
1416        obligation.recursion_depth + 1,
1417        gen_sig,
1418    );
1419
1420    {
    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/project.rs:1420",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1420u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "obligation", "gen_sig", "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(&format_args!("confirm_iterator_candidate")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligation)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&gen_sig) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligations)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?obligation, ?gen_sig, ?obligations, "confirm_iterator_candidate");
1421
1422    let tcx = selcx.tcx();
1423    let iter_def_id = tcx.require_lang_item(LangItem::Iterator, obligation.cause.span);
1424
1425    let (trait_ref, yield_ty) = super::util::iterator_trait_ref_and_outputs(
1426        tcx,
1427        iter_def_id,
1428        obligation.predicate.self_ty(),
1429        gen_sig,
1430    );
1431
1432    if true {
    match (&tcx.associated_item(obligation.predicate.def_id).name(),
            &sym::Item) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Item);
1433
1434    let predicate = ty::ProjectionPredicate {
1435        projection_term: ty::AliasTerm::new_from_args(
1436            tcx,
1437            obligation.predicate.def_id,
1438            trait_ref.args,
1439        ),
1440        term: yield_ty.into(),
1441    };
1442
1443    confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1444        .with_addl_obligations(nested)
1445        .with_addl_obligations(obligations)
1446}
1447
1448fn confirm_async_iterator_candidate<'cx, 'tcx>(
1449    selcx: &mut SelectionContext<'cx, 'tcx>,
1450    obligation: &ProjectionTermObligation<'tcx>,
1451    nested: PredicateObligations<'tcx>,
1452) -> Progress<'tcx> {
1453    let ty::Coroutine(_, args) = selcx.infcx.shallow_resolve(obligation.predicate.self_ty()).kind()
1454    else {
1455        ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
1456    };
1457    let gen_sig = args.as_coroutine().sig();
1458    let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1459        selcx,
1460        obligation.param_env,
1461        obligation.cause.clone(),
1462        obligation.recursion_depth + 1,
1463        gen_sig,
1464    );
1465
1466    {
    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/project.rs:1466",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1466u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "obligation", "gen_sig", "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(&format_args!("confirm_async_iterator_candidate")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligation)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&gen_sig) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligations)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?obligation, ?gen_sig, ?obligations, "confirm_async_iterator_candidate");
1467
1468    let tcx = selcx.tcx();
1469    let iter_def_id = tcx.require_lang_item(LangItem::AsyncIterator, obligation.cause.span);
1470
1471    let (trait_ref, yield_ty) = super::util::async_iterator_trait_ref_and_outputs(
1472        tcx,
1473        iter_def_id,
1474        obligation.predicate.self_ty(),
1475        gen_sig,
1476    );
1477
1478    if true {
    match (&tcx.associated_item(obligation.predicate.def_id).name(),
            &sym::Item) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Item);
1479
1480    let ty::Adt(_poll_adt, args) = *yield_ty.kind() else {
1481        ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
1482    };
1483    let ty::Adt(_option_adt, args) = *args.type_at(0).kind() else {
1484        ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
1485    };
1486    let item_ty = args.type_at(0);
1487
1488    let predicate = ty::ProjectionPredicate {
1489        projection_term: ty::AliasTerm::new_from_args(
1490            tcx,
1491            obligation.predicate.def_id,
1492            trait_ref.args,
1493        ),
1494        term: item_ty.into(),
1495    };
1496
1497    confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1498        .with_addl_obligations(nested)
1499        .with_addl_obligations(obligations)
1500}
1501
1502fn confirm_builtin_candidate<'cx, 'tcx>(
1503    selcx: &mut SelectionContext<'cx, 'tcx>,
1504    obligation: &ProjectionTermObligation<'tcx>,
1505    data: PredicateObligations<'tcx>,
1506) -> Progress<'tcx> {
1507    let tcx = selcx.tcx();
1508    let self_ty = obligation.predicate.self_ty();
1509    let item_def_id = obligation.predicate.def_id;
1510    let trait_def_id = tcx.parent(item_def_id);
1511    let args = tcx.mk_args(&[self_ty.into()]);
1512    let (term, obligations) = if tcx.is_lang_item(trait_def_id, LangItem::DiscriminantKind) {
1513        let discriminant_def_id =
1514            tcx.require_lang_item(LangItem::Discriminant, obligation.cause.span);
1515        match (&discriminant_def_id, &item_def_id) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(discriminant_def_id, item_def_id);
1516
1517        (self_ty.discriminant_ty(tcx).into(), PredicateObligations::new())
1518    } else if tcx.is_lang_item(trait_def_id, LangItem::PointeeTrait) {
1519        let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, obligation.cause.span);
1520        match (&metadata_def_id, &item_def_id) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(metadata_def_id, item_def_id);
1521
1522        let mut obligations = PredicateObligations::new();
1523        let normalize = |ty| {
1524            normalize_with_depth_to(
1525                selcx,
1526                obligation.param_env,
1527                obligation.cause.clone(),
1528                obligation.recursion_depth + 1,
1529                ty,
1530                &mut obligations,
1531            )
1532        };
1533        let metadata_ty = self_ty.ptr_metadata_ty_or_tail(tcx, normalize).unwrap_or_else(|tail| {
1534            if tail == self_ty {
1535                // This is the "fallback impl" for type parameters, unnormalizable projections
1536                // and opaque types: If the `self_ty` is `Sized`, then the metadata is `()`.
1537                // FIXME(ptr_metadata): This impl overlaps with the other impls and shouldn't
1538                // exist. Instead, `Pointee<Metadata = ()>` should be a supertrait of `Sized`.
1539                let sized_predicate = ty::TraitRef::new(
1540                    tcx,
1541                    tcx.require_lang_item(LangItem::Sized, obligation.cause.span),
1542                    [self_ty],
1543                );
1544                obligations.push(obligation.with(tcx, sized_predicate));
1545                tcx.types.unit
1546            } else {
1547                // We know that `self_ty` has the same metadata as `tail`. This allows us
1548                // to prove predicates like `Wrapper<Tail>::Metadata == Tail::Metadata`.
1549                Ty::new_projection(tcx, metadata_def_id, [tail])
1550            }
1551        });
1552        (metadata_ty.into(), obligations)
1553    } else if tcx.is_lang_item(trait_def_id, LangItem::Field) {
1554        let ty::Adt(def, args) = self_ty.kind() else {
1555            ::rustc_middle::util::bug::bug_fmt(format_args!("only field representing types can implement `Field`"))bug!("only field representing types can implement `Field`")
1556        };
1557        let Some(FieldInfo { base, ty, .. }) = def.field_representing_type_info(tcx, args) else {
1558            ::rustc_middle::util::bug::bug_fmt(format_args!("only field representing types can implement `Field`"))bug!("only field representing types can implement `Field`")
1559        };
1560        if tcx.is_lang_item(item_def_id, LangItem::FieldBase) {
1561            (base.into(), PredicateObligations::new())
1562        } else if tcx.is_lang_item(item_def_id, LangItem::FieldType) {
1563            (ty.into(), PredicateObligations::new())
1564        } else {
1565            ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected associated type {0:?} in `Field`",
        obligation.predicate));bug!("unexpected associated type {:?} in `Field`", obligation.predicate);
1566        }
1567    } else {
1568        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected builtin trait with associated type: {0:?}",
        obligation.predicate));bug!("unexpected builtin trait with associated type: {:?}", obligation.predicate);
1569    };
1570
1571    let predicate = ty::ProjectionPredicate {
1572        projection_term: ty::AliasTerm::new_from_args(tcx, item_def_id, args),
1573        term,
1574    };
1575
1576    confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1577        .with_addl_obligations(obligations)
1578        .with_addl_obligations(data)
1579}
1580
1581fn confirm_fn_pointer_candidate<'cx, 'tcx>(
1582    selcx: &mut SelectionContext<'cx, 'tcx>,
1583    obligation: &ProjectionTermObligation<'tcx>,
1584    nested: PredicateObligations<'tcx>,
1585) -> Progress<'tcx> {
1586    let tcx = selcx.tcx();
1587    let fn_type = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1588    let sig = fn_type.fn_sig(tcx);
1589    let Normalized { value: sig, obligations } = normalize_with_depth(
1590        selcx,
1591        obligation.param_env,
1592        obligation.cause.clone(),
1593        obligation.recursion_depth + 1,
1594        sig,
1595    );
1596
1597    confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
1598        .with_addl_obligations(nested)
1599        .with_addl_obligations(obligations)
1600}
1601
1602fn confirm_closure_candidate<'cx, 'tcx>(
1603    selcx: &mut SelectionContext<'cx, 'tcx>,
1604    obligation: &ProjectionTermObligation<'tcx>,
1605    nested: PredicateObligations<'tcx>,
1606) -> Progress<'tcx> {
1607    let tcx = selcx.tcx();
1608    let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1609    let closure_sig = match *self_ty.kind() {
1610        ty::Closure(_, args) => args.as_closure().sig(),
1611
1612        // Construct a "normal" `FnOnce` signature for coroutine-closure. This is
1613        // basically duplicated with the `AsyncFnOnce::CallOnce` confirmation, but
1614        // I didn't see a good way to unify those.
1615        ty::CoroutineClosure(def_id, args) => {
1616            let args = args.as_coroutine_closure();
1617            let kind_ty = args.kind_ty();
1618            args.coroutine_closure_sig().map_bound(|sig| {
1619                // If we know the kind and upvars, use that directly.
1620                // Otherwise, defer to `AsyncFnKindHelper::Upvars` to delay
1621                // the projection, like the `AsyncFn*` traits do.
1622                let output_ty = if let Some(_) = kind_ty.to_opt_closure_kind()
1623                    // Fall back to projection if upvars aren't constrained
1624                    && !args.tupled_upvars_ty().is_ty_var()
1625                {
1626                    sig.to_coroutine_given_kind_and_upvars(
1627                        tcx,
1628                        args.parent_args(),
1629                        tcx.coroutine_for_closure(def_id),
1630                        ty::ClosureKind::FnOnce,
1631                        tcx.lifetimes.re_static,
1632                        args.tupled_upvars_ty(),
1633                        args.coroutine_captures_by_ref_ty(),
1634                    )
1635                } else {
1636                    let upvars_projection_def_id =
1637                        tcx.require_lang_item(LangItem::AsyncFnKindUpvars, obligation.cause.span);
1638                    let tupled_upvars_ty = Ty::new_projection(
1639                        tcx,
1640                        upvars_projection_def_id,
1641                        [
1642                            ty::GenericArg::from(kind_ty),
1643                            Ty::from_closure_kind(tcx, ty::ClosureKind::FnOnce).into(),
1644                            tcx.lifetimes.re_static.into(),
1645                            sig.tupled_inputs_ty.into(),
1646                            args.tupled_upvars_ty().into(),
1647                            args.coroutine_captures_by_ref_ty().into(),
1648                        ],
1649                    );
1650                    sig.to_coroutine(
1651                        tcx,
1652                        args.parent_args(),
1653                        Ty::from_closure_kind(tcx, ty::ClosureKind::FnOnce),
1654                        tcx.coroutine_for_closure(def_id),
1655                        tupled_upvars_ty,
1656                    )
1657                };
1658                tcx.mk_fn_sig(
1659                    [sig.tupled_inputs_ty],
1660                    output_ty,
1661                    sig.c_variadic,
1662                    sig.safety,
1663                    sig.abi,
1664                )
1665            })
1666        }
1667
1668        _ => {
1669            {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("expected closure self type for closure candidate, found {0}",
                self_ty)));
};unreachable!("expected closure self type for closure candidate, found {self_ty}");
1670        }
1671    };
1672
1673    let Normalized { value: closure_sig, obligations } = normalize_with_depth(
1674        selcx,
1675        obligation.param_env,
1676        obligation.cause.clone(),
1677        obligation.recursion_depth + 1,
1678        closure_sig,
1679    );
1680
1681    {
    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/project.rs:1681",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1681u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "obligation", "closure_sig", "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(&format_args!("confirm_closure_candidate")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligation)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&closure_sig)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligations)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");
1682
1683    confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
1684        .with_addl_obligations(nested)
1685        .with_addl_obligations(obligations)
1686}
1687
1688fn confirm_callable_candidate<'cx, 'tcx>(
1689    selcx: &mut SelectionContext<'cx, 'tcx>,
1690    obligation: &ProjectionTermObligation<'tcx>,
1691    fn_sig: ty::PolyFnSig<'tcx>,
1692    flag: util::TupleArgumentsFlag,
1693) -> Progress<'tcx> {
1694    let tcx = selcx.tcx();
1695
1696    {
    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/project.rs:1696",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1696u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "obligation", "fn_sig"],
                            ::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!("confirm_callable_candidate")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligation)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&fn_sig) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?obligation, ?fn_sig, "confirm_callable_candidate");
1697
1698    let fn_once_def_id = tcx.require_lang_item(LangItem::FnOnce, obligation.cause.span);
1699    let fn_once_output_def_id =
1700        tcx.require_lang_item(LangItem::FnOnceOutput, obligation.cause.span);
1701
1702    let predicate = super::util::closure_trait_ref_and_return_type(
1703        tcx,
1704        fn_once_def_id,
1705        obligation.predicate.self_ty(),
1706        fn_sig,
1707        flag,
1708    )
1709    .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
1710        projection_term: ty::AliasTerm::new_from_args(tcx, fn_once_output_def_id, trait_ref.args),
1711        term: ret_type.into(),
1712    });
1713
1714    confirm_param_env_candidate(selcx, obligation, predicate, true)
1715}
1716
1717fn confirm_async_closure_candidate<'cx, 'tcx>(
1718    selcx: &mut SelectionContext<'cx, 'tcx>,
1719    obligation: &ProjectionTermObligation<'tcx>,
1720    nested: PredicateObligations<'tcx>,
1721) -> Progress<'tcx> {
1722    let tcx = selcx.tcx();
1723    let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1724
1725    let goal_kind =
1726        tcx.async_fn_trait_kind_from_def_id(obligation.predicate.trait_def_id(tcx)).unwrap();
1727    let env_region = match goal_kind {
1728        ty::ClosureKind::Fn | ty::ClosureKind::FnMut => obligation.predicate.args.region_at(2),
1729        ty::ClosureKind::FnOnce => tcx.lifetimes.re_static,
1730    };
1731    let item_name = tcx.item_name(obligation.predicate.def_id);
1732
1733    let poly_cache_entry = match *self_ty.kind() {
1734        ty::CoroutineClosure(def_id, args) => {
1735            let args = args.as_coroutine_closure();
1736            let kind_ty = args.kind_ty();
1737            let sig = args.coroutine_closure_sig().skip_binder();
1738
1739            let term = match item_name {
1740                sym::CallOnceFuture | sym::CallRefFuture => {
1741                    if let Some(closure_kind) = kind_ty.to_opt_closure_kind()
1742                        // Fall back to projection if upvars aren't constrained
1743                        && !args.tupled_upvars_ty().is_ty_var()
1744                    {
1745                        if !closure_kind.extends(goal_kind) {
1746                            ::rustc_middle::util::bug::bug_fmt(format_args!("we should not be confirming if the closure kind is not met"));bug!("we should not be confirming if the closure kind is not met");
1747                        }
1748                        sig.to_coroutine_given_kind_and_upvars(
1749                            tcx,
1750                            args.parent_args(),
1751                            tcx.coroutine_for_closure(def_id),
1752                            goal_kind,
1753                            env_region,
1754                            args.tupled_upvars_ty(),
1755                            args.coroutine_captures_by_ref_ty(),
1756                        )
1757                    } else {
1758                        let upvars_projection_def_id = tcx
1759                            .require_lang_item(LangItem::AsyncFnKindUpvars, obligation.cause.span);
1760                        // When we don't know the closure kind (and therefore also the closure's upvars,
1761                        // which are computed at the same time), we must delay the computation of the
1762                        // generator's upvars. We do this using the `AsyncFnKindHelper`, which as a trait
1763                        // goal functions similarly to the old `ClosureKind` predicate, and ensures that
1764                        // the goal kind <= the closure kind. As a projection `AsyncFnKindHelper::Upvars`
1765                        // will project to the right upvars for the generator, appending the inputs and
1766                        // coroutine upvars respecting the closure kind.
1767                        // N.B. No need to register a `AsyncFnKindHelper` goal here, it's already in `nested`.
1768                        let tupled_upvars_ty = Ty::new_projection(
1769                            tcx,
1770                            upvars_projection_def_id,
1771                            [
1772                                ty::GenericArg::from(kind_ty),
1773                                Ty::from_closure_kind(tcx, goal_kind).into(),
1774                                env_region.into(),
1775                                sig.tupled_inputs_ty.into(),
1776                                args.tupled_upvars_ty().into(),
1777                                args.coroutine_captures_by_ref_ty().into(),
1778                            ],
1779                        );
1780                        sig.to_coroutine(
1781                            tcx,
1782                            args.parent_args(),
1783                            Ty::from_closure_kind(tcx, goal_kind),
1784                            tcx.coroutine_for_closure(def_id),
1785                            tupled_upvars_ty,
1786                        )
1787                    }
1788                }
1789                sym::Output => sig.return_ty,
1790                name => ::rustc_middle::util::bug::bug_fmt(format_args!("no such associated type: {0}",
        name))bug!("no such associated type: {name}"),
1791            };
1792            let projection_term = match item_name {
1793                sym::CallOnceFuture | sym::Output => ty::AliasTerm::new(
1794                    tcx,
1795                    obligation.predicate.def_id,
1796                    [self_ty, sig.tupled_inputs_ty],
1797                ),
1798                sym::CallRefFuture => ty::AliasTerm::new(
1799                    tcx,
1800                    obligation.predicate.def_id,
1801                    [ty::GenericArg::from(self_ty), sig.tupled_inputs_ty.into(), env_region.into()],
1802                ),
1803                name => ::rustc_middle::util::bug::bug_fmt(format_args!("no such associated type: {0}",
        name))bug!("no such associated type: {name}"),
1804            };
1805
1806            args.coroutine_closure_sig()
1807                .rebind(ty::ProjectionPredicate { projection_term, term: term.into() })
1808        }
1809        ty::FnDef(..) | ty::FnPtr(..) => {
1810            let bound_sig = self_ty.fn_sig(tcx);
1811            let sig = bound_sig.skip_binder();
1812
1813            let term = match item_name {
1814                sym::CallOnceFuture | sym::CallRefFuture => sig.output(),
1815                sym::Output => {
1816                    let future_output_def_id =
1817                        tcx.require_lang_item(LangItem::FutureOutput, obligation.cause.span);
1818                    Ty::new_projection(tcx, future_output_def_id, [sig.output()])
1819                }
1820                name => ::rustc_middle::util::bug::bug_fmt(format_args!("no such associated type: {0}",
        name))bug!("no such associated type: {name}"),
1821            };
1822            let projection_term = match item_name {
1823                sym::CallOnceFuture | sym::Output => ty::AliasTerm::new(
1824                    tcx,
1825                    obligation.predicate.def_id,
1826                    [self_ty, Ty::new_tup(tcx, sig.inputs())],
1827                ),
1828                sym::CallRefFuture => ty::AliasTerm::new(
1829                    tcx,
1830                    obligation.predicate.def_id,
1831                    [
1832                        ty::GenericArg::from(self_ty),
1833                        Ty::new_tup(tcx, sig.inputs()).into(),
1834                        env_region.into(),
1835                    ],
1836                ),
1837                name => ::rustc_middle::util::bug::bug_fmt(format_args!("no such associated type: {0}",
        name))bug!("no such associated type: {name}"),
1838            };
1839
1840            bound_sig.rebind(ty::ProjectionPredicate { projection_term, term: term.into() })
1841        }
1842        ty::Closure(_, args) => {
1843            let args = args.as_closure();
1844            let bound_sig = args.sig();
1845            let sig = bound_sig.skip_binder();
1846
1847            let term = match item_name {
1848                sym::CallOnceFuture | sym::CallRefFuture => sig.output(),
1849                sym::Output => {
1850                    let future_output_def_id =
1851                        tcx.require_lang_item(LangItem::FutureOutput, obligation.cause.span);
1852                    Ty::new_projection(tcx, future_output_def_id, [sig.output()])
1853                }
1854                name => ::rustc_middle::util::bug::bug_fmt(format_args!("no such associated type: {0}",
        name))bug!("no such associated type: {name}"),
1855            };
1856            let projection_term = match item_name {
1857                sym::CallOnceFuture | sym::Output => {
1858                    ty::AliasTerm::new(tcx, obligation.predicate.def_id, [self_ty, sig.inputs()[0]])
1859                }
1860                sym::CallRefFuture => ty::AliasTerm::new(
1861                    tcx,
1862                    obligation.predicate.def_id,
1863                    [ty::GenericArg::from(self_ty), sig.inputs()[0].into(), env_region.into()],
1864                ),
1865                name => ::rustc_middle::util::bug::bug_fmt(format_args!("no such associated type: {0}",
        name))bug!("no such associated type: {name}"),
1866            };
1867
1868            bound_sig.rebind(ty::ProjectionPredicate { projection_term, term: term.into() })
1869        }
1870        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("expected callable type for AsyncFn candidate"))bug!("expected callable type for AsyncFn candidate"),
1871    };
1872
1873    confirm_param_env_candidate(selcx, obligation, poly_cache_entry, true)
1874        .with_addl_obligations(nested)
1875}
1876
1877fn confirm_async_fn_kind_helper_candidate<'cx, 'tcx>(
1878    selcx: &mut SelectionContext<'cx, 'tcx>,
1879    obligation: &ProjectionTermObligation<'tcx>,
1880    nested: PredicateObligations<'tcx>,
1881) -> Progress<'tcx> {
1882    let [
1883        // We already checked that the goal_kind >= closure_kind
1884        _closure_kind_ty,
1885        goal_kind_ty,
1886        borrow_region,
1887        tupled_inputs_ty,
1888        tupled_upvars_ty,
1889        coroutine_captures_by_ref_ty,
1890    ] = **obligation.predicate.args
1891    else {
1892        ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
1893    };
1894
1895    let predicate = ty::ProjectionPredicate {
1896        projection_term: ty::AliasTerm::new_from_args(
1897            selcx.tcx(),
1898            obligation.predicate.def_id,
1899            obligation.predicate.args,
1900        ),
1901        term: ty::CoroutineClosureSignature::tupled_upvars_by_closure_kind(
1902            selcx.tcx(),
1903            goal_kind_ty.expect_ty().to_opt_closure_kind().unwrap(),
1904            tupled_inputs_ty.expect_ty(),
1905            tupled_upvars_ty.expect_ty(),
1906            coroutine_captures_by_ref_ty.expect_ty(),
1907            borrow_region.expect_region(),
1908        )
1909        .into(),
1910    };
1911
1912    confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1913        .with_addl_obligations(nested)
1914}
1915
1916// FIXME(mgca): While this supports constants, it is only used for types by default right now
1917fn confirm_param_env_candidate<'cx, 'tcx>(
1918    selcx: &mut SelectionContext<'cx, 'tcx>,
1919    obligation: &ProjectionTermObligation<'tcx>,
1920    poly_cache_entry: ty::PolyProjectionPredicate<'tcx>,
1921    potentially_unnormalized_candidate: bool,
1922) -> Progress<'tcx> {
1923    let infcx = selcx.infcx;
1924    let cause = &obligation.cause;
1925    let param_env = obligation.param_env;
1926
1927    let cache_entry = infcx.instantiate_binder_with_fresh_vars(
1928        cause.span,
1929        BoundRegionConversionTime::HigherRankedType,
1930        poly_cache_entry,
1931    );
1932
1933    let cache_projection = cache_entry.projection_term;
1934    let mut nested_obligations = PredicateObligations::new();
1935    let obligation_projection = obligation.predicate;
1936    let obligation_projection = ensure_sufficient_stack(|| {
1937        normalize_with_depth_to(
1938            selcx,
1939            obligation.param_env,
1940            obligation.cause.clone(),
1941            obligation.recursion_depth + 1,
1942            obligation_projection,
1943            &mut nested_obligations,
1944        )
1945    });
1946    let cache_projection = if potentially_unnormalized_candidate {
1947        ensure_sufficient_stack(|| {
1948            normalize_with_depth_to(
1949                selcx,
1950                obligation.param_env,
1951                obligation.cause.clone(),
1952                obligation.recursion_depth + 1,
1953                cache_projection,
1954                &mut nested_obligations,
1955            )
1956        })
1957    } else {
1958        cache_projection
1959    };
1960
1961    {
    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/project.rs:1961",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1961u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::tracing_core::field::FieldSet::new(&["cache_projection",
                                        "obligation_projection"],
                            ::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(&cache_projection)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&obligation_projection)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?cache_projection, ?obligation_projection);
1962
1963    match infcx.at(cause, param_env).eq(
1964        DefineOpaqueTypes::Yes,
1965        cache_projection,
1966        obligation_projection,
1967    ) {
1968        Ok(InferOk { value: _, obligations }) => {
1969            nested_obligations.extend(obligations);
1970            assoc_term_own_obligations(selcx, obligation, &mut nested_obligations);
1971            Progress { term: cache_entry.term, obligations: nested_obligations }
1972        }
1973        Err(e) => {
1974            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Failed to unify obligation `{0:?}` with poly_projection `{1:?}`: {2:?}",
                obligation, poly_cache_entry, e))
    })format!(
1975                "Failed to unify obligation `{obligation:?}` with poly_projection `{poly_cache_entry:?}`: {e:?}",
1976            );
1977            {
    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/project.rs:1977",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(1977u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::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!("confirm_param_env_candidate: {0}",
                                                    msg) as &dyn Value))])
            });
    } else { ; }
};debug!("confirm_param_env_candidate: {}", msg);
1978            let err = Ty::new_error_with_message(infcx.tcx, obligation.cause.span, msg);
1979            Progress { term: err.into(), obligations: PredicateObligations::new() }
1980        }
1981    }
1982}
1983
1984// FIXME(mgca): While this supports constants, it is only used for types by default right now
1985fn confirm_impl_candidate<'cx, 'tcx>(
1986    selcx: &mut SelectionContext<'cx, 'tcx>,
1987    obligation: &ProjectionTermObligation<'tcx>,
1988    impl_impl_source: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
1989) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1990    let tcx = selcx.tcx();
1991
1992    let ImplSourceUserDefinedData { impl_def_id, args, mut nested } = impl_impl_source;
1993
1994    let assoc_item_id = obligation.predicate.def_id;
1995    let trait_def_id = tcx.impl_trait_id(impl_def_id);
1996
1997    let param_env = obligation.param_env;
1998    let assoc_term = match specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) {
1999        Ok(assoc_term) => assoc_term,
2000        Err(guar) => {
2001            return Ok(Projected::Progress(Progress::error_for_term(
2002                tcx,
2003                obligation.predicate,
2004                guar,
2005            )));
2006        }
2007    };
2008
2009    // This means that the impl is missing a definition for the
2010    // associated type. This is either because the associate item
2011    // has impossible-to-satisfy predicates (since those were
2012    // allowed in <https://github.com/rust-lang/rust/pull/135480>),
2013    // or because the impl is literally missing the definition.
2014    if !assoc_term.item.defaultness(tcx).has_value() {
2015        {
    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/project.rs:2015",
                        "rustc_trait_selection::traits::project",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/project.rs"),
                        ::tracing_core::__macro_support::Option::Some(2015u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::project"),
                        ::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!("confirm_impl_candidate: no associated type {0:?} for {1:?}",
                                                    assoc_term.item.name(), obligation.predicate) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(
2016            "confirm_impl_candidate: no associated type {:?} for {:?}",
2017            assoc_term.item.name(),
2018            obligation.predicate
2019        );
2020        if tcx.impl_self_is_guaranteed_unsized(impl_def_id) {
2021            // We treat this projection as rigid here, which is represented via
2022            // `Projected::NoProgress`. This will ensure that the projection is
2023            // checked for well-formedness, and it's either satisfied by a trivial
2024            // where clause in its env or it results in an error.
2025            return Ok(Projected::NoProgress(obligation.predicate.to_term(tcx)));
2026        } else {
2027            return Ok(Projected::Progress(Progress {
2028                term: if obligation.predicate.kind(tcx).is_type() {
2029                    Ty::new_misc_error(tcx).into()
2030                } else {
2031                    ty::Const::new_misc_error(tcx).into()
2032                },
2033                obligations: nested,
2034            }));
2035        }
2036    }
2037
2038    // If we're trying to normalize `<Vec<u32> as X>::A<S>` using
2039    //`impl<T> X for Vec<T> { type A<Y> = Box<Y>; }`, then:
2040    //
2041    // * `obligation.predicate.args` is `[Vec<u32>, S]`
2042    // * `args` is `[u32]`
2043    // * `args` ends up as `[u32, S]`
2044    let args = obligation.predicate.args.rebase_onto(tcx, trait_def_id, args);
2045    let args = translate_args(selcx.infcx, param_env, impl_def_id, args, assoc_term.defining_node);
2046
2047    let term = if obligation.predicate.kind(tcx).is_type() {
2048        tcx.type_of(assoc_term.item.def_id).map_bound(|ty| ty.into())
2049    } else {
2050        tcx.const_of_item(assoc_term.item.def_id).map_bound(|ct| ct.into())
2051    };
2052
2053    let progress = if !tcx.check_args_compatible(assoc_term.item.def_id, args) {
2054        let msg = "impl item and trait item have different parameters";
2055        let span = obligation.cause.span;
2056        let err = if obligation.predicate.kind(tcx).is_type() {
2057            Ty::new_error_with_message(tcx, span, msg).into()
2058        } else {
2059            ty::Const::new_error_with_message(tcx, span, msg).into()
2060        };
2061        Progress { term: err, obligations: nested }
2062    } else {
2063        assoc_term_own_obligations(selcx, obligation, &mut nested);
2064        Progress { term: term.instantiate(tcx, args), obligations: nested }
2065    };
2066    Ok(Projected::Progress(progress))
2067}
2068
2069// Get obligations corresponding to the predicates from the where-clause of the
2070// associated type itself.
2071//
2072// This is necessary for soundness until we properly handle implied bounds on binders.
2073// see tests/ui/generic-associated-types/must-prove-where-clauses-on-norm.rs.
2074// FIXME(mgca): While this supports constants, it is only used for types by default right now
2075fn assoc_term_own_obligations<'cx, 'tcx>(
2076    selcx: &mut SelectionContext<'cx, 'tcx>,
2077    obligation: &ProjectionTermObligation<'tcx>,
2078    nested: &mut PredicateObligations<'tcx>,
2079) {
2080    let tcx = selcx.tcx();
2081    let predicates = tcx
2082        .predicates_of(obligation.predicate.def_id)
2083        .instantiate_own(tcx, obligation.predicate.args);
2084    for (predicate, span) in predicates {
2085        let normalized = normalize_with_depth_to(
2086            selcx,
2087            obligation.param_env,
2088            obligation.cause.clone(),
2089            obligation.recursion_depth + 1,
2090            predicate,
2091            nested,
2092        );
2093
2094        let nested_cause = if #[allow(non_exhaustive_omitted_patterns)] match obligation.cause.code() {
    ObligationCauseCode::CompareImplItem { .. } |
        ObligationCauseCode::CheckAssociatedTypeBounds { .. } |
        ObligationCauseCode::AscribeUserTypeProvePredicate(..) => true,
    _ => false,
}matches!(
2095            obligation.cause.code(),
2096            ObligationCauseCode::CompareImplItem { .. }
2097                | ObligationCauseCode::CheckAssociatedTypeBounds { .. }
2098                | ObligationCauseCode::AscribeUserTypeProvePredicate(..)
2099        ) {
2100            obligation.cause.clone()
2101        } else {
2102            ObligationCause::new(
2103                obligation.cause.span,
2104                obligation.cause.body_id,
2105                ObligationCauseCode::WhereClause(obligation.predicate.def_id, span),
2106            )
2107        };
2108        nested.push(Obligation::with_depth(
2109            tcx,
2110            nested_cause,
2111            obligation.recursion_depth + 1,
2112            obligation.param_env,
2113            normalized,
2114        ));
2115    }
2116}
2117
2118pub(crate) trait ProjectionCacheKeyExt<'cx, 'tcx>: Sized {
2119    fn from_poly_projection_obligation(
2120        selcx: &mut SelectionContext<'cx, 'tcx>,
2121        obligation: &PolyProjectionObligation<'tcx>,
2122    ) -> Option<Self>;
2123}
2124
2125impl<'cx, 'tcx> ProjectionCacheKeyExt<'cx, 'tcx> for ProjectionCacheKey<'tcx> {
2126    fn from_poly_projection_obligation(
2127        selcx: &mut SelectionContext<'cx, 'tcx>,
2128        obligation: &PolyProjectionObligation<'tcx>,
2129    ) -> Option<Self> {
2130        let infcx = selcx.infcx;
2131        // We don't do cross-snapshot caching of obligations with escaping regions,
2132        // so there's no cache key to use
2133        obligation.predicate.no_bound_vars().map(|predicate| {
2134            ProjectionCacheKey::new(
2135                // We don't attempt to match up with a specific type-variable state
2136                // from a specific call to `opt_normalize_projection_type` - if
2137                // there's no precise match, the original cache entry is "stranded"
2138                // anyway.
2139                infcx.resolve_vars_if_possible(predicate.projection_term),
2140                obligation.param_env,
2141            )
2142        })
2143    }
2144}