Skip to main content

rustc_next_trait_solver/solve/eval_ctxt/
mod.rs

1use std::mem;
2use std::ops::ControlFlow;
3
4#[cfg(feature = "nightly")]
5use rustc_macros::StableHash;
6use rustc_type_ir::data_structures::{HashMap, HashSet};
7use rustc_type_ir::inherent::*;
8use rustc_type_ir::region_constraint::RegionConstraint;
9use rustc_type_ir::relate::Relate;
10use rustc_type_ir::relate::solver_relating::RelateExt;
11use rustc_type_ir::search_graph::{CandidateHeadUsages, PathKind};
12use rustc_type_ir::solve::{
13    AccessedOpaques, ExternalRegionConstraints, FetchEligibleAssocItemResponse, MaybeInfo,
14    NoSolutionOrRerunNonErased, OpaqueTypesJank, QueryResultOrRerunNonErased, RerunCondition,
15    RerunNonErased, RerunReason, RerunResultExt, SmallCopyList,
16};
17use rustc_type_ir::{
18    self as ty, CanonicalVarValues, ClauseKind, InferCtxtLike, Interner, MayBeErased,
19    OpaqueTypeKey, PredicateKind, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeSuperVisitable,
20    TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode,
21};
22use tracing::{Level, debug, instrument, trace, warn};
23
24use super::has_only_region_constraints;
25use crate::canonical::{
26    canonicalize_goal, canonicalize_response, instantiate_and_apply_query_response,
27    response_no_constraints_raw,
28};
29use crate::coherence;
30use crate::delegate::SolverDelegate;
31use crate::placeholder::BoundVarReplacer;
32use crate::resolve::eager_resolve_vars;
33use crate::solve::search_graph::SearchGraph;
34use crate::solve::ty::may_use_unstable_feature;
35use crate::solve::{
36    CanonicalInput, CanonicalResponse, Certainty, ExternalConstraintsData, FIXPOINT_STEP_LIMIT,
37    Goal, GoalEvaluation, GoalSource, GoalStalledOn, HasChanged, MaybeCause,
38    NestedNormalizationGoals, NoSolution, QueryInput, QueryResult, Response, VisibleForLeakCheck,
39    inspect,
40};
41
42mod probe;
43mod solver_region_constraints;
44
45/// The kind of goal we're currently proving.
46///
47/// This has effects on cycle handling handling and on how we compute
48/// query responses, see the variant descriptions for more info.
49#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CurrentGoalKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                CurrentGoalKind::Misc => "Misc",
                CurrentGoalKind::CoinductiveTrait => "CoinductiveTrait",
                CurrentGoalKind::NormalizesTo => "NormalizesTo",
            })
    }
}Debug, #[automatically_derived]
impl ::core::marker::Copy for CurrentGoalKind { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CurrentGoalKind {
    #[inline]
    fn clone(&self) -> CurrentGoalKind { *self }
}Clone)]
50enum CurrentGoalKind {
51    Misc,
52    /// We're proving an trait goal for a coinductive trait, either an auto trait or `Sized`.
53    ///
54    /// These are currently the only goals whose impl where-clauses are considered to be
55    /// productive steps.
56    CoinductiveTrait,
57    /// Unlike other goals, `NormalizesTo` goals act like functions with the expected term
58    /// always being fully unconstrained. This would weaken inference however, as the nested
59    /// goals never get the inference constraints from the actual normalized-to type.
60    ///
61    /// Because of this we return any ambiguous nested goals from `NormalizesTo` to the
62    /// caller when then adds these to its own context. The caller is always an `AliasRelate`
63    /// goal so this never leaks out of the solver.
64    NormalizesTo,
65}
66
67impl CurrentGoalKind {
68    fn from_query_input<I: Interner>(cx: I, input: QueryInput<I, I::Predicate>) -> CurrentGoalKind {
69        match input.goal.predicate.kind().skip_binder() {
70            ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
71                if cx.trait_is_coinductive(pred.trait_ref.def_id) {
72                    CurrentGoalKind::CoinductiveTrait
73                } else {
74                    CurrentGoalKind::Misc
75                }
76            }
77            ty::PredicateKind::NormalizesTo(_) => CurrentGoalKind::NormalizesTo,
78            _ => CurrentGoalKind::Misc,
79        }
80    }
81}
82
83#[derive(#[automatically_derived]
impl ::core::fmt::Debug for RerunDecision {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                RerunDecision::Yes => "Yes",
                RerunDecision::No => "No",
                RerunDecision::EagerlyPropagateToParent =>
                    "EagerlyPropagateToParent",
            })
    }
}Debug)]
84enum RerunDecision {
85    Yes,
86    No,
87    EagerlyPropagateToParent,
88}
89pub struct EvalCtxt<'a, D, I = <D as SolverDelegate>::Interner>
90where
91    D: SolverDelegate<Interner = I>,
92    I: Interner,
93{
94    /// The inference context that backs (mostly) inference and placeholder terms
95    /// instantiated while solving goals.
96    ///
97    /// NOTE: The `InferCtxt` that backs the `EvalCtxt` is intentionally private,
98    /// because the `InferCtxt` is much more general than `EvalCtxt`. Methods such
99    /// as  `take_registered_region_obligations` can mess up query responses,
100    /// using `At::normalize` is totally wrong, calling `evaluate_root_goal` can
101    /// cause coinductive unsoundness, etc.
102    ///
103    /// Methods that are generally of use for trait solving are *intentionally*
104    /// re-declared through the `EvalCtxt` below, often with cleaner signatures
105    /// since we don't care about things like `ObligationCause`s and `Span`s here.
106    /// If some `InferCtxt` method is missing, please first think defensively about
107    /// the method's compatibility with this solver, or if an existing one does
108    /// the job already.
109    delegate: &'a D,
110
111    /// The variable info for the `var_values`, only used to make an ambiguous response
112    /// with no constraints.
113    var_kinds: I::CanonicalVarKinds,
114
115    /// What kind of goal we're currently computing, see the enum definition
116    /// for more info.
117    current_goal_kind: CurrentGoalKind,
118    pub(super) var_values: CanonicalVarValues<I>,
119
120    /// The highest universe index nameable by the caller.
121    ///
122    /// When we enter a new binder inside of the query we create new universes
123    /// which the caller cannot name. We have to be careful with variables from
124    /// these new universes when creating the query response.
125    ///
126    /// Both because these new universes can prevent us from reaching a fixpoint
127    /// if we have a coinductive cycle and because that's the only way we can return
128    /// new placeholders to the caller.
129    pub(super) max_input_universe: ty::UniverseIndex,
130    /// The opaque types from the canonical input. We only need to return opaque types
131    /// which have been added to the storage while evaluating this goal.
132    pub(super) initial_opaque_types_storage_num_entries:
133        <D::Infcx as InferCtxtLike>::OpaqueTypeStorageEntries,
134
135    pub(super) search_graph: &'a mut SearchGraph<D>,
136
137    nested_goals: Vec<(GoalSource, Goal<I, I::Predicate>, Option<GoalStalledOn<I>>)>,
138
139    pub(super) origin_span: I::Span,
140
141    // Has this `EvalCtxt` errored out with `NoSolution` in `try_evaluate_added_goals`?
142    //
143    // If so, then it can no longer be used to make a canonical query response,
144    // since subsequent calls to `try_evaluate_added_goals` have possibly dropped
145    // ambiguous goals. Instead, a probe needs to be introduced somewhere in the
146    // evaluation code.
147    tainted: Result<(), NoSolution>,
148
149    /// Tracks accesses of opaque types while in [`TypingMode::ErasedNotCoherence`].
150    pub(super) opaque_accesses: AccessedOpaques<I>,
151
152    pub(super) inspect: inspect::EvaluationStepBuilder<D>,
153}
154
155#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for GenerateProofTree {
    #[inline]
    fn eq(&self, other: &GenerateProofTree) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for GenerateProofTree {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for GenerateProofTree {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                GenerateProofTree::Yes => "Yes",
                GenerateProofTree::No => "No",
            })
    }
}Debug, #[automatically_derived]
impl ::core::hash::Hash for GenerateProofTree {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash, #[automatically_derived]
impl ::core::clone::Clone for GenerateProofTree {
    #[inline]
    fn clone(&self) -> GenerateProofTree { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for GenerateProofTree { }Copy)]
156#[cfg_attr(feature = "nightly", derive(const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for
            GenerateProofTree {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    GenerateProofTree::Yes => {}
                    GenerateProofTree::No => {}
                }
            }
        }
    };StableHash))]
157pub enum GenerateProofTree {
158    Yes,
159    No,
160}
161
162pub trait SolverDelegateEvalExt: SolverDelegate {
163    /// Evaluates a goal from **outside** of the trait solver.
164    ///
165    /// Using this while inside of the solver is wrong as it uses a new
166    /// search graph which would break cycle detection.
167    fn evaluate_root_goal(
168        &self,
169        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
170        span: <Self::Interner as Interner>::Span,
171        stalled_on: Option<GoalStalledOn<Self::Interner>>,
172    ) -> Result<GoalEvaluation<Self::Interner>, NoSolution>;
173
174    /// Checks whether evaluating `goal` may hold while treating not-yet-defined
175    /// opaque types as being kind of rigid.
176    ///
177    /// See the comment on [OpaqueTypesJank] for more details.
178    fn root_goal_may_hold_opaque_types_jank(
179        &self,
180        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
181    ) -> bool;
182
183    /// Check whether evaluating `goal` with a depth of `root_depth` may
184    /// succeed. This only returns `false` if the goal is guaranteed to
185    /// not hold. In case evaluation overflows and fails with ambiguity this
186    /// returns `true`.
187    ///
188    /// This is only intended to be used as a performance optimization
189    /// in coherence checking.
190    fn root_goal_may_hold_with_depth(
191        &self,
192        root_depth: usize,
193        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
194    ) -> bool;
195
196    // FIXME: This is only exposed because we need to use it in `analyse.rs`
197    // which is not yet uplifted. Once that's done, we should remove this.
198    fn evaluate_root_goal_for_proof_tree(
199        &self,
200        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
201        span: <Self::Interner as Interner>::Span,
202    ) -> (
203        Result<NestedNormalizationGoals<Self::Interner>, NoSolution>,
204        inspect::GoalEvaluation<Self::Interner>,
205    );
206}
207
208impl<D, I> SolverDelegateEvalExt for D
209where
210    D: SolverDelegate<Interner = I>,
211    I: Interner,
212{
213    x;#[instrument(level = "debug", skip(self), ret)]
214    fn evaluate_root_goal(
215        &self,
216        goal: Goal<I, I::Predicate>,
217        span: I::Span,
218        stalled_on: Option<GoalStalledOn<I>>,
219    ) -> Result<GoalEvaluation<I>, NoSolution> {
220        let result = EvalCtxt::enter_root(self, self.cx().recursion_limit(), span, |ecx| {
221            ecx.evaluate_goal(GoalSource::Misc, goal, stalled_on)
222        });
223
224        match result {
225            Ok(i) => Ok(i),
226            Err(NoSolutionOrRerunNonErased::NoSolution(NoSolution)) => Err(NoSolution),
227            Err(NoSolutionOrRerunNonErased::RerunNonErased(_)) => {
228                unreachable!("this never happens at the root, we're never in erased mode here");
229            }
230        }
231    }
232
233    x;#[instrument(level = "debug", skip(self), ret)]
234    fn root_goal_may_hold_opaque_types_jank(
235        &self,
236        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
237    ) -> bool {
238        self.probe(|| {
239            EvalCtxt::enter_root(self, self.cx().recursion_limit(), I::Span::dummy(), |ecx| {
240                ecx.evaluate_goal(GoalSource::Misc, goal, None)
241            })
242            .is_ok_and(|r| match r.certainty {
243                Certainty::Yes => true,
244                Certainty::Maybe(MaybeInfo {
245                    cause: _,
246                    opaque_types_jank,
247                    stalled_on_coroutines: _,
248                }) => match opaque_types_jank {
249                    OpaqueTypesJank::AllGood => true,
250                    OpaqueTypesJank::ErrorIfRigidSelfTy => false,
251                },
252            })
253        })
254    }
255
256    fn root_goal_may_hold_with_depth(
257        &self,
258        root_depth: usize,
259        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
260    ) -> bool {
261        self.probe(|| {
262            EvalCtxt::enter_root(self, root_depth, I::Span::dummy(), |ecx| {
263                ecx.evaluate_goal(GoalSource::Misc, goal, None)
264            })
265        })
266        .is_ok()
267    }
268
269    #[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("evaluate_root_goal_for_proof_tree",
                                    "rustc_next_trait_solver::solve::eval_ctxt",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(269u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                                    ::tracing_core::field::FieldSet::new(&["goal", "span"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&goal)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    (Result<NestedNormalizationGoals<I>, NoSolution>,
                    inspect::GoalEvaluation<I>) = loop {};
            return __tracing_attr_fake_return;
        }
        { evaluate_root_goal_for_proof_tree(self, goal, span) }
    }
}#[instrument(level = "debug", skip(self))]
270    fn evaluate_root_goal_for_proof_tree(
271        &self,
272        goal: Goal<I, I::Predicate>,
273        span: I::Span,
274    ) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
275        evaluate_root_goal_for_proof_tree(self, goal, span)
276    }
277}
278
279impl<'a, D, I> EvalCtxt<'a, D>
280where
281    D: SolverDelegate<Interner = I>,
282    I: Interner,
283{
284    pub(super) fn typing_mode(&self) -> TypingMode<I> {
285        self.delegate.typing_mode_raw()
286    }
287
288    /// Computes the `PathKind` for the step from the current goal to the
289    /// nested goal required due to `source`.
290    ///
291    /// See #136824 for a more detailed reasoning for this behavior. We
292    /// consider cycles to be coinductive if they 'step into' a where-clause
293    /// of a coinductive trait. We will likely extend this function in the future
294    /// and will need to clearly document it in the rustc-dev-guide before
295    /// stabilization.
296    pub(super) fn step_kind_for_source(&self, source: GoalSource) -> PathKind {
297        match source {
298            // We treat these goals as unknown for now. It is likely that most miscellaneous
299            // nested goals will be converted to an inductive variant in the future.
300            //
301            // Having unknown cycles is always the safer option, as changing that to either
302            // succeed or hard error is backwards compatible. If we incorrectly treat a cycle
303            // as inductive even though it should not be, it may be unsound during coherence and
304            // fixing it may cause inference breakage or introduce ambiguity.
305            GoalSource::Misc => PathKind::Unknown,
306            GoalSource::NormalizeGoal(path_kind) => path_kind,
307            GoalSource::ImplWhereBound => match self.current_goal_kind {
308                // We currently only consider a cycle coinductive if it steps
309                // into a where-clause of a coinductive trait.
310                CurrentGoalKind::CoinductiveTrait => PathKind::Coinductive,
311                // While normalizing via an impl does step into a where-clause of
312                // an impl, accessing the associated item immediately steps out of
313                // it again. This means cycles/recursive calls are not guarded
314                // by impls used for normalization.
315                //
316                // See tests/ui/traits/next-solver/cycles/normalizes-to-is-not-productive.rs
317                // for how this can go wrong.
318                CurrentGoalKind::NormalizesTo => PathKind::Inductive,
319                // We probably want to make all traits coinductive in the future,
320                // so we treat cycles involving where-clauses of not-yet coinductive
321                // traits as ambiguous for now.
322                CurrentGoalKind::Misc => PathKind::Unknown,
323            },
324            // Relating types is always unproductive. If we were to map proof trees to
325            // corecursive functions as explained in #136824, relating types never
326            // introduces a constructor which could cause the recursion to be guarded.
327            GoalSource::TypeRelating => PathKind::Inductive,
328            // These goal sources are likely unproductive and can be changed to
329            // `PathKind::Inductive`. Keeping them as unknown until we're confident
330            // about this and have an example where it is necessary.
331            GoalSource::AliasBoundConstCondition | GoalSource::AliasWellFormed => PathKind::Unknown,
332        }
333    }
334
335    /// Creates a root evaluation context and search graph. This should only be
336    /// used from outside of any evaluation, and other methods should be preferred
337    /// over using this manually (such as [`SolverDelegateEvalExt::evaluate_root_goal`]).
338    pub(super) fn enter_root<R>(
339        delegate: &D,
340        root_depth: usize,
341        origin_span: I::Span,
342        f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
343    ) -> R {
344        let mut search_graph = SearchGraph::new(root_depth);
345
346        let mut ecx = EvalCtxt {
347            delegate,
348            search_graph: &mut search_graph,
349            nested_goals: Default::default(),
350            inspect: inspect::EvaluationStepBuilder::new_noop(),
351
352            // Only relevant when canonicalizing the response,
353            // which we don't do within this evaluation context.
354            max_input_universe: ty::UniverseIndex::ROOT,
355            initial_opaque_types_storage_num_entries: Default::default(),
356            var_kinds: Default::default(),
357            var_values: CanonicalVarValues::dummy(),
358            current_goal_kind: CurrentGoalKind::Misc,
359            origin_span,
360            tainted: Ok(()),
361            opaque_accesses: AccessedOpaques::default(),
362        };
363        let result = f(&mut ecx);
364        if !ecx.nested_goals.is_empty() {
    {
        ::core::panicking::panic_fmt(format_args!("root `EvalCtxt` should not have any goals added to it"));
    }
};assert!(
365            ecx.nested_goals.is_empty(),
366            "root `EvalCtxt` should not have any goals added to it"
367        );
368        if !!ecx.opaque_accesses.might_rerun() {
    ::core::panicking::panic("assertion failed: !ecx.opaque_accesses.might_rerun()")
};assert!(!ecx.opaque_accesses.might_rerun());
369        if !search_graph.is_empty() {
    ::core::panicking::panic("assertion failed: search_graph.is_empty()")
};assert!(search_graph.is_empty());
370        result
371    }
372
373    /// Creates a nested evaluation context that shares the same search graph as the
374    /// one passed in. This is suitable for evaluation, granted that the search graph
375    /// has had the nested goal recorded on its stack. This method only be used by
376    /// `search_graph::Delegate::compute_goal`.
377    ///
378    /// This function takes care of setting up the inference context, setting the anchor,
379    /// and registering opaques from the canonicalized input.
380    pub(super) fn enter_canonical<T>(
381        cx: I,
382        search_graph: &'a mut SearchGraph<D>,
383        canonical_input: CanonicalInput<I>,
384        proof_tree_builder: &mut inspect::ProofTreeBuilder<D>,
385        f: impl FnOnce(
386            &mut EvalCtxt<'_, D>,
387            Goal<I, I::Predicate>,
388        ) -> Result<T, NoSolutionOrRerunNonErased>,
389    ) -> (Result<T, NoSolution>, AccessedOpaques<I>) {
390        let (ref delegate, input, var_values) = D::build_with_canonical(cx, &canonical_input);
391        for (key, ty) in input.predefined_opaques_in_body.iter() {
392            let prev = delegate.register_hidden_type_in_storage(key, ty, I::Span::dummy());
393            // It may be possible that two entries in the opaque type storage end up
394            // with the same key after resolving contained inference variables.
395            //
396            // We could put them in the duplicate list but don't have to. The opaques we
397            // encounter here are already tracked in the caller, so there's no need to
398            // also store them here. We'd take them out when computing the query response
399            // and then discard them, as they're already present in the input.
400            //
401            // Ideally we'd drop duplicate opaque type definitions when computing
402            // the canonical input. This is more annoying to implement and may cause a
403            // perf regression, so we do it inside of the query for now.
404            if let Some(prev) = prev {
405                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:405",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(405u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::tracing_core::field::FieldSet::new(&["message", "key",
                                        "ty", "prev"],
                            ::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!("ignore duplicate in `opaque_types_storage`")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&key) 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)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&prev) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?key, ?ty, ?prev, "ignore duplicate in `opaque_types_storage`");
406            }
407        }
408
409        let initial_opaque_types_storage_num_entries = delegate.opaque_types_storage_num_entries();
410        if truecfg!(debug_assertions) && delegate.typing_mode_raw().is_erased_not_coherence() {
411            if !delegate.clone_opaque_types_lookup_table().is_empty() {
    ::core::panicking::panic("assertion failed: delegate.clone_opaque_types_lookup_table().is_empty()")
};assert!(delegate.clone_opaque_types_lookup_table().is_empty());
412        }
413
414        let mut ecx = EvalCtxt {
415            delegate,
416            var_kinds: canonical_input.canonical.var_kinds,
417            var_values,
418            current_goal_kind: CurrentGoalKind::from_query_input(cx, input),
419            max_input_universe: canonical_input.canonical.max_universe,
420            initial_opaque_types_storage_num_entries,
421            search_graph,
422            nested_goals: Default::default(),
423            origin_span: I::Span::dummy(),
424            tainted: Ok(()),
425            inspect: proof_tree_builder.new_evaluation_step(var_values),
426            opaque_accesses: AccessedOpaques::default(),
427        };
428
429        let result = f(&mut ecx, input.goal);
430        ecx.inspect.probe_final_state(ecx.delegate, ecx.max_input_universe);
431        proof_tree_builder.finish_evaluation_step(ecx.inspect);
432
433        if canonical_input.typing_mode.0.is_erased_not_coherence() {
434            if true {
    if !delegate.clone_opaque_types_lookup_table().is_empty() {
        ::core::panicking::panic("assertion failed: delegate.clone_opaque_types_lookup_table().is_empty()")
    };
};debug_assert!(delegate.clone_opaque_types_lookup_table().is_empty());
435        }
436
437        // When creating a query response we clone the opaque type constraints
438        // instead of taking them. This would cause an ICE here, since we have
439        // assertions against dropping an `InferCtxt` without taking opaques.
440        // FIXME: Once we remove support for the old impl we can remove this.
441        // FIXME: Could we make `build_with_canonical` into `enter_with_canonical` and call this at the end?
442        delegate.reset_opaque_types();
443
444        let opaque_accesses = ecx.opaque_accesses;
445        (
446            match result {
447                Ok(i) => Ok(i),
448                Err(NoSolutionOrRerunNonErased::NoSolution(NoSolution)) => Err(NoSolution),
449                Err(NoSolutionOrRerunNonErased::RerunNonErased(_)) => {
450                    // check th t the opaque_accesses state mirrors the result we got.
451                    if !opaque_accesses.should_bail().is_err() {
    ::core::panicking::panic("assertion failed: opaque_accesses.should_bail().is_err()")
};assert!(opaque_accesses.should_bail().is_err());
452                    Err(NoSolution)
453                }
454            },
455            opaque_accesses,
456        )
457    }
458
459    pub(super) fn ignore_candidate_head_usages(&mut self, usages: CandidateHeadUsages) {
460        self.search_graph.ignore_candidate_head_usages(usages);
461    }
462
463    /// Recursively evaluates `goal`, returning whether any inference vars have
464    /// been constrained and the certainty of the result.
465    fn evaluate_goal(
466        &mut self,
467        source: GoalSource,
468        goal: Goal<I, I::Predicate>,
469        stalled_on: Option<GoalStalledOn<I>>,
470    ) -> Result<GoalEvaluation<I>, NoSolutionOrRerunNonErased> {
471        let (normalization_nested_goals, goal_evaluation) =
472            self.evaluate_goal_raw(source, goal, stalled_on)?;
473        if !normalization_nested_goals.is_empty() {
    ::core::panicking::panic("assertion failed: normalization_nested_goals.is_empty()")
};assert!(normalization_nested_goals.is_empty());
474        Ok(goal_evaluation)
475    }
476
477    /// Recursively evaluates `goal`, returning the nested goals in case
478    /// the nested goal is a `NormalizesTo` goal.
479    ///
480    /// As all other goal kinds do not return any nested goals and
481    /// `NormalizesTo` is only used by `AliasRelate`, all other callsites
482    /// should use [`EvalCtxt::evaluate_goal`] which discards that empty
483    /// storage.
484    pub(super) fn evaluate_goal_raw(
485        &mut self,
486        source: GoalSource,
487        goal: Goal<I, I::Predicate>,
488        stalled_on: Option<GoalStalledOn<I>>,
489    ) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolutionOrRerunNonErased> {
490        // If we have run this goal before, and it was stalled, check that any of the goal's
491        // args have changed. Otherwise, we don't need to re-run the goal because it'll remain
492        // stalled, since it'll canonicalize the same way and evaluation is pure.
493        if let Some(GoalStalledOn {
494            num_opaques,
495            ref stalled_vars,
496            ref sub_roots,
497            stalled_certainty,
498        }) = stalled_on
499            && !self.delegate.disable_trait_solver_fast_paths()
500            && !stalled_vars.iter().any(|value| self.delegate.is_changed_arg(*value))
501            && !sub_roots
502                .iter()
503                .any(|&vid| self.delegate.sub_unification_table_root_var(vid) != vid)
504            && !self.delegate.opaque_types_storage_num_entries().needs_reevaluation(num_opaques)
505        {
506            return Ok((
507                NestedNormalizationGoals::empty(),
508                GoalEvaluation {
509                    goal,
510                    certainty: stalled_certainty,
511                    has_changed: HasChanged::No,
512                    stalled_on,
513                },
514            ));
515        }
516
517        // We only care about one entry per `OpaqueTypeKey` here,
518        // so we only canonicalize the lookup table and ignore
519        // duplicate entries.
520        let opaque_types = self.delegate.clone_opaque_types_lookup_table();
521        let (goal, opaque_types) = eager_resolve_vars(self.delegate, (goal, opaque_types));
522        let typing_mode = self.typing_mode();
523        let step_kind = self.step_kind_for_source(source);
524
525        let tracing_span = {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("evaluate_goal_raw in typing mode",
                        "rustc_next_trait_solver::solve::eval_ctxt", Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(525u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::SPAN)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let mut interest = ::tracing::subscriber::Interest::never();
    if Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
                    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(&format_args!("{0:?} opaques={1:?}",
                                                        typing_mode, opaque_types) as &dyn Value))])
                })
    } else {
        let span =
            ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
        {};
        span
    }
}tracing::span!(
526            Level::DEBUG,
527            "evaluate_goal_raw in typing mode",
528            "{:?} opaques={:?}",
529            typing_mode,
530            opaque_types
531        )
532        .entered();
533
534        let (result, orig_values, canonical_goal) = 'retry_canonicalize: {
535            let skip_erased_attempt = if typing_mode.is_coherence() {
536                true
537            } else {
538                let mut skip = false;
539                if opaque_types.iter().any(|(_, ty)| ty.is_ty_var())
540                    && let PredicateKind::Clause(ClauseKind::Trait(..)) =
541                        goal.predicate.kind().skip_binder()
542                {
543                    skip = true;
544                }
545
546                if let PredicateKind::Clause(ClauseKind::Trait(tr)) =
547                    goal.predicate.kind().skip_binder()
548                    && tr.self_ty().has_coroutines()
549                    && self.cx().trait_is_auto(tr.trait_ref.def_id)
550                {
551                    // FIXME(#155443): this doesn't make a difference now, but with eager normalization
552                    // it likely will.
553                    // skip_erased_attempt = true;
554                }
555
556                skip
557            };
558
559            if skip_erased_attempt {
560                if typing_mode.is_erased_not_coherence() {
561                    match self.opaque_accesses.rerun_always(RerunReason::SkipErasedAttempt)? {}
562                } else {
563                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:563",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(563u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::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!("running in original typing mode")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("running in original typing mode");
564                }
565            } else {
566                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:566",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(566u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::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!("trying without opaques: {0:?}",
                                                    goal) as &dyn Value))])
            });
    } else { ; }
};debug!("trying without opaques: {goal:?}");
567
568                let (orig_values, canonical_goal) = canonicalize_goal(
569                    self.delegate,
570                    goal,
571                    &[],
572                    TypingMode::ErasedNotCoherence(MayBeErased),
573                );
574
575                let (canonical_result, accessed_opaques) = self.search_graph.evaluate_goal(
576                    self.cx(),
577                    canonical_goal,
578                    step_kind,
579                    &mut inspect::ProofTreeBuilder::new_noop(),
580                );
581
582                let should_rerun = self.should_rerun_after_erased_canonicalization(
583                    accessed_opaques,
584                    self.typing_mode(),
585                    &opaque_types,
586                );
587                match should_rerun {
588                    RerunDecision::Yes => {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:588",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(588u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::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!("rerunning in original typing mode")
                                            as &dyn Value))])
            });
    } else { ; }
}debug!("rerunning in original typing mode"),
589                    RerunDecision::No => {
590                        break 'retry_canonicalize (canonical_result, orig_values, canonical_goal);
591                    }
592                    RerunDecision::EagerlyPropagateToParent => {
593                        self.opaque_accesses.update(accessed_opaques)?;
594                        break 'retry_canonicalize (canonical_result, orig_values, canonical_goal);
595                    }
596                }
597            }
598
599            let (orig_values, canonical_goal) =
600                canonicalize_goal(self.delegate, goal, &opaque_types, typing_mode);
601
602            let (canonical_result, accessed_opaques) = self.search_graph.evaluate_goal(
603                self.cx(),
604                canonical_goal,
605                step_kind,
606                &mut inspect::ProofTreeBuilder::new_noop(),
607            );
608            if !!accessed_opaques.might_rerun() {
    {
        ::core::panicking::panic_fmt(format_args!("we run without TypingMode::ErasedNotCoherence, so opaques are available, and we don\'t retry if the outer typing mode is ErasedNotCoherence: {0:?} after {1:?}",
                accessed_opaques, goal));
    }
};assert!(
609                !accessed_opaques.might_rerun(),
610                "we run without TypingMode::ErasedNotCoherence, so opaques are available, and we don't retry if the outer typing mode is ErasedNotCoherence: {accessed_opaques:?} after {goal:?}"
611            );
612
613            (canonical_result, orig_values, canonical_goal)
614        };
615
616        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:616",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(616u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::tracing_core::field::FieldSet::new(&["result"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&result) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?result);
617        let response = match result {
618            Ok(response) => {
619                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:619",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(619u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::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!("success")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("success");
620                response
621            }
622            Err(NoSolution) => {
623                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:623",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(623u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::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!("normal failure")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("normal failure");
624                return Err(NoSolution.into());
625            }
626        };
627
628        drop(tracing_span);
629
630        let has_changed =
631            if !has_only_region_constraints(response) { HasChanged::Yes } else { HasChanged::No };
632
633        // FIXME: We should revisit and consider removing this after
634        // *assumptions on binders* is available, like once we had done in the
635        // stabilization of `-Znext-solver=coherence`(#121848).
636        // We ignore constraints from the nested goals in leak check. This is to match
637        // with the old solver's behavior, which has separated evaluation and fulfillment,
638        // and the former doesn't consider outlives obligations from the later.
639        let vis = match goal.predicate.kind().skip_binder() {
640            ty::PredicateKind::Clause(_)
641            | ty::PredicateKind::DynCompatible(_)
642            | ty::PredicateKind::Subtype(_)
643            | ty::PredicateKind::Coerce(_)
644            | ty::PredicateKind::ConstEquate(_, _)
645            | ty::PredicateKind::Ambiguous
646            | ty::PredicateKind::NormalizesTo(_) => VisibleForLeakCheck::No,
647            ty::PredicateKind::AliasRelate(_, _, _) => VisibleForLeakCheck::Yes,
648        };
649
650        let (normalization_nested_goals, certainty) = instantiate_and_apply_query_response(
651            self.delegate,
652            goal.param_env,
653            &orig_values,
654            response,
655            vis,
656            self.origin_span,
657        );
658
659        // FIXME: We previously had an assert here that checked that recomputing
660        // a goal after applying its constraints did not change its response.
661        //
662        // This assert was removed as it did not hold for goals constraining
663        // an inference variable to a recursive alias, e.g. in
664        // tests/ui/traits/next-solver/overflow/recursive-self-normalization.rs.
665        //
666        // Once we have decided on how to handle trait-system-refactor-initiative#75,
667        // we should re-add an assert here.
668
669        let stalled_on = match certainty {
670            Certainty::Yes => None,
671            Certainty::Maybe { .. } => match has_changed {
672                // FIXME: We could recompute a *new* set of stalled variables by walking
673                // through the orig values, resolving, and computing the root vars of anything
674                // that is not resolved. Only when *these* have changed is it meaningful
675                // to recompute this goal.
676                HasChanged::Yes => None,
677                HasChanged::No => {
678                    let mut stalled_vars = orig_values;
679
680                    // Remove the unconstrained RHS arg, which is expected to have changed.
681                    if let Some(normalizes_to) = goal.predicate.as_normalizes_to() {
682                        let normalizes_to = normalizes_to.skip_binder();
683                        let rhs_arg: I::GenericArg = normalizes_to.term.into();
684                        let idx = stalled_vars
685                            .iter()
686                            .rposition(|arg| *arg == rhs_arg)
687                            .expect("expected unconstrained arg");
688                        stalled_vars.swap_remove(idx);
689                    }
690
691                    // Remove the canonicalized universal vars, since we only care about stalled existentials.
692                    let mut sub_roots = Vec::new();
693                    stalled_vars.retain(|arg| match arg.kind() {
694                        // Lifetimes can never stall goals.
695                        ty::GenericArgKind::Lifetime(_) => false,
696                        ty::GenericArgKind::Type(ty) => match ty.kind() {
697                            ty::Infer(ty::TyVar(vid)) => {
698                                sub_roots.push(self.delegate.sub_unification_table_root_var(vid));
699                                true
700                            }
701                            ty::Infer(_) => true,
702                            ty::Param(_) | ty::Placeholder(_) => false,
703                            _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("unexpected orig_value: {0:?}", ty)));
}unreachable!("unexpected orig_value: {ty:?}"),
704                        },
705                        ty::GenericArgKind::Const(ct) => match ct.kind() {
706                            ty::ConstKind::Infer(_) => true,
707                            ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => false,
708                            _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("unexpected orig_value: {0:?}", ct)));
}unreachable!("unexpected orig_value: {ct:?}"),
709                        },
710                    });
711
712                    Some(GoalStalledOn {
713                        num_opaques: canonical_goal
714                            .canonical
715                            .value
716                            .predefined_opaques_in_body
717                            .len(),
718                        stalled_vars,
719                        sub_roots,
720                        stalled_certainty: certainty,
721                    })
722                }
723            },
724        };
725
726        Ok((
727            normalization_nested_goals,
728            GoalEvaluation { goal, certainty, has_changed, stalled_on },
729        ))
730    }
731
732    fn should_rerun_after_erased_canonicalization(
733        &self,
734        AccessedOpaques { reason: _, rerun }: AccessedOpaques<I>,
735        original_typing_mode: TypingMode<I>,
736        parent_opaque_types: &[(OpaqueTypeKey<I>, I::Ty)],
737    ) -> RerunDecision {
738        let parent_opaque_defids = parent_opaque_types.iter().map(|(key, _)| key.def_id.into());
739        let opaque_in_storage = |opaques: I::LocalDefIds, defids: SmallCopyList<_>| {
740            if defids.as_ref().is_empty() {
741                RerunDecision::No
742            } else if opaques
743                .iter()
744                .chain(parent_opaque_defids)
745                .any(|opaque| defids.as_ref().contains(&opaque))
746            {
747                RerunDecision::Yes
748            } else {
749                RerunDecision::No
750            }
751        };
752        let any_opaque_has_infer_as_hidden = || {
753            if parent_opaque_types.iter().any(|(_, ty)| ty.is_ty_var()) {
754                RerunDecision::Yes
755            } else {
756                RerunDecision::No
757            }
758        };
759
760        let res = match (rerun, original_typing_mode) {
761            // =============================
762            (RerunCondition::Never, _) => RerunDecision::No,
763            // =============================
764            (_, TypingMode::ErasedNotCoherence(MayBeErased)) => {
765                RerunDecision::EagerlyPropagateToParent
766            }
767            // =============================
768            // In coherence, we never switch to erased mode, so we will never register anything
769            // in the rerun state, so we should've taken the first branch of this match
770            (_, TypingMode::Coherence) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
771            // =============================
772            (RerunCondition::Always, _) => RerunDecision::Yes,
773            // =============================
774            (RerunCondition::OpaqueInStorage(..), TypingMode::PostAnalysis) => RerunDecision::Yes,
775            (
776                RerunCondition::OpaqueInStorage(defids),
777                TypingMode::PostBorrowckAnalysis { defined_opaque_types: opaques }
778                | TypingMode::Analysis { defining_opaque_types_and_generators: opaques }
779                | TypingMode::Borrowck { defining_opaque_types: opaques },
780            ) => opaque_in_storage(opaques, defids),
781            // =============================
782            (RerunCondition::AnyOpaqueHasInferAsHidden, TypingMode::Analysis { .. }) => {
783                any_opaque_has_infer_as_hidden()
784            }
785            (
786                RerunCondition::AnyOpaqueHasInferAsHidden,
787                TypingMode::PostBorrowckAnalysis { .. }
788                | TypingMode::PostAnalysis
789                | TypingMode::Borrowck { .. },
790            ) => RerunDecision::No,
791            // =============================
792            (
793                RerunCondition::OpaqueInStorageOrAnyOpaqueHasInferAsHidden(_),
794                TypingMode::PostAnalysis,
795            ) => RerunDecision::No,
796            (
797                RerunCondition::OpaqueInStorageOrAnyOpaqueHasInferAsHidden(defids),
798                TypingMode::Analysis { defining_opaque_types_and_generators: opaques },
799            ) => {
800                if let RerunDecision::Yes = any_opaque_has_infer_as_hidden() {
801                    RerunDecision::Yes
802                } else if let RerunDecision::Yes = opaque_in_storage(opaques, defids) {
803                    RerunDecision::Yes
804                } else {
805                    RerunDecision::No
806                }
807            }
808            (
809                RerunCondition::OpaqueInStorageOrAnyOpaqueHasInferAsHidden(defids),
810                TypingMode::PostBorrowckAnalysis { defined_opaque_types: opaques }
811                | TypingMode::Borrowck { defining_opaque_types: opaques },
812            ) => opaque_in_storage(opaques, defids),
813        };
814
815        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:815",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(815u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::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!("checking whether to rerun {0:?} in outer typing mode {1:?} and opaques {2:?}: {3:?}",
                                                    rerun, original_typing_mode, parent_opaque_types, res) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(
816            "checking whether to rerun {rerun:?} in outer typing mode {original_typing_mode:?} and opaques {parent_opaque_types:?}: {res:?}"
817        );
818
819        res
820    }
821
822    pub(super) fn compute_goal(
823        &mut self,
824        goal: Goal<I, I::Predicate>,
825    ) -> QueryResultOrRerunNonErased<I> {
826        let Goal { param_env, predicate } = goal;
827        let kind = predicate.kind();
828        self.enter_forall_with_assumptions(kind, param_env, |ecx, kind| {
829            Ok(match kind {
830                ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
831                    ecx.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)?
832                }
833                ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
834                    ecx.compute_host_effect_goal(Goal { param_env, predicate })?
835                }
836                ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
837                    ecx.compute_projection_goal(Goal { param_env, predicate })?
838                }
839                ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
840                    ecx.compute_type_outlives_goal(Goal { param_env, predicate })?
841                }
842                ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
843                    ecx.compute_region_outlives_goal(Goal { param_env, predicate })?
844                }
845                ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
846                    ecx.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })?
847                }
848                ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
849                    ecx.compute_unstable_feature_goal(param_env, symbol)?
850                }
851                ty::PredicateKind::Subtype(predicate) => {
852                    ecx.compute_subtype_goal(Goal { param_env, predicate })?
853                }
854                ty::PredicateKind::Coerce(predicate) => {
855                    ecx.compute_coerce_goal(Goal { param_env, predicate })?
856                }
857                ty::PredicateKind::DynCompatible(trait_def_id) => {
858                    ecx.compute_dyn_compatible_goal(trait_def_id)?
859                }
860                ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
861                    ecx.compute_well_formed_goal(Goal { param_env, predicate: term })?
862                }
863                ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
864                    ecx.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })?
865                }
866                ty::PredicateKind::ConstEquate(_, _) => {
867                    {
    ::core::panicking::panic_fmt(format_args!("ConstEquate should not be emitted when `-Znext-solver` is active"));
}panic!("ConstEquate should not be emitted when `-Znext-solver` is active")
868                }
869                ty::PredicateKind::NormalizesTo(predicate) => {
870                    ecx.compute_normalizes_to_goal(Goal { param_env, predicate })?
871                }
872                ty::PredicateKind::AliasRelate(lhs, rhs, direction) => ecx
873                    .compute_alias_relate_goal(Goal {
874                        param_env,
875                        predicate: (lhs, rhs, direction),
876                    })?,
877                ty::PredicateKind::Ambiguous => {
878                    ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)?
879                }
880            })
881        })
882    }
883
884    // Recursively evaluates all the goals added to this `EvalCtxt` to completion, returning
885    // the certainty of all the goals.
886    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("try_evaluate_added_goals",
                                    "rustc_next_trait_solver::solve::eval_ctxt",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(886u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                                    ::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::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    Result<Certainty, NoSolutionOrRerunNonErased> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            for _ in 0..FIXPOINT_STEP_LIMIT {
                match self.evaluate_added_goals_step().map_err_to_rerun()? {
                    Ok(None) => {}
                    Ok(Some(cert)) => return Ok(cert),
                    Err(NoSolution) => {
                        self.tainted = Err(NoSolution);
                        return Err(NoSolution.into());
                    }
                }
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:901",
                                    "rustc_next_trait_solver::solve::eval_ctxt",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(901u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                                    ::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!("try_evaluate_added_goals: encountered overflow")
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            Ok(Certainty::overflow(false))
        }
    }
}#[instrument(level = "trace", skip(self))]
887    pub(super) fn try_evaluate_added_goals(
888        &mut self,
889    ) -> Result<Certainty, NoSolutionOrRerunNonErased> {
890        for _ in 0..FIXPOINT_STEP_LIMIT {
891            match self.evaluate_added_goals_step().map_err_to_rerun()? {
892                Ok(None) => {}
893                Ok(Some(cert)) => return Ok(cert),
894                Err(NoSolution) => {
895                    self.tainted = Err(NoSolution);
896                    return Err(NoSolution.into());
897                }
898            }
899        }
900
901        debug!("try_evaluate_added_goals: encountered overflow");
902        Ok(Certainty::overflow(false))
903    }
904
905    /// Iterate over all added goals: returning `Ok(Some(_))` in case we can stop rerunning.
906    ///
907    /// Goals for the next step get directly added to the nested goals of the `EvalCtxt`.
908    fn evaluate_added_goals_step(
909        &mut self,
910    ) -> Result<Option<Certainty>, NoSolutionOrRerunNonErased> {
911        let cx = self.cx();
912        // If this loop did not result in any progress, what's our final certainty.
913        let mut unchanged_certainty = Some(Certainty::Yes);
914        for (source, goal, stalled_on) in mem::take(&mut self.nested_goals) {
915            if !self.delegate.disable_trait_solver_fast_paths()
916                && let Some(certainty) =
917                    self.delegate.compute_goal_fast_path(goal, self.origin_span)
918            {
919                match certainty {
920                    Certainty::Yes => {}
921                    Certainty::Maybe { .. } => {
922                        self.nested_goals.push((source, goal, None));
923                        unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
924                    }
925                }
926                continue;
927            }
928
929            // We treat normalizes-to goals specially here. In each iteration we take the
930            // RHS of the projection, replace it with a fresh inference variable, and only
931            // after evaluating that goal do we equate the fresh inference variable with the
932            // actual RHS of the predicate.
933            //
934            // This is both to improve caching, and to avoid using the RHS of the
935            // projection predicate to influence the normalizes-to candidate we select.
936            //
937            // Forgetting to replace the RHS with a fresh inference variable when we evaluate
938            // this goal results in an ICE.
939            if let Some(pred) = goal.predicate.as_normalizes_to() {
940                // We should never encounter higher-ranked normalizes-to goals.
941                let pred = pred.no_bound_vars().unwrap();
942                // Replace the goal with an unconstrained infer var, so the
943                // RHS does not affect projection candidate assembly.
944                let unconstrained_rhs = self.next_term_infer_of_kind(pred.term);
945                let unconstrained_goal =
946                    goal.with(cx, ty::NormalizesTo { alias: pred.alias, term: unconstrained_rhs });
947
948                let (
949                    NestedNormalizationGoals(nested_goals),
950                    GoalEvaluation { goal, certainty, stalled_on, has_changed: _ },
951                ) = self.evaluate_goal_raw(source, unconstrained_goal, stalled_on)?;
952                // Add the nested goals from normalization to our own nested goals.
953                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:953",
                        "rustc_next_trait_solver::solve::eval_ctxt",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(953u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                        ::tracing_core::field::FieldSet::new(&["nested_goals"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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(&nested_goals)
                                            as &dyn Value))])
            });
    } else { ; }
};trace!(?nested_goals);
954                self.nested_goals.extend(nested_goals.into_iter().map(|(s, g)| (s, g, None)));
955
956                // Finally, equate the goal's RHS with the unconstrained var.
957                //
958                // SUBTLE:
959                // We structurally relate aliases here. This is necessary
960                // as we otherwise emit a nested `AliasRelate` goal in case the
961                // returned term is a rigid alias, resulting in overflow.
962                //
963                // It is correct as both `goal.predicate.term` and `unconstrained_rhs`
964                // start out as an unconstrained inference variable so any aliases get
965                // fully normalized when instantiating it.
966                //
967                // FIXME: Strictly speaking this may be incomplete if the normalized-to
968                // type contains an ambiguous alias referencing bound regions. We should
969                // consider changing this to only use "shallow structural equality".
970                self.eq_structurally_relating_aliases(
971                    goal.param_env,
972                    pred.term,
973                    unconstrained_rhs,
974                )?;
975
976                // We only look at the `projection_ty` part here rather than
977                // looking at the "has changed" return from evaluate_goal,
978                // because we expect the `unconstrained_rhs` part of the predicate
979                // to have changed -- that means we actually normalized successfully!
980                // FIXME: Do we need to eagerly resolve here? Or should we check
981                // if the cache key has any changed vars?
982                let with_resolved_vars = self.resolve_vars_if_possible(goal);
983                if pred.alias
984                    != with_resolved_vars
985                        .predicate
986                        .as_normalizes_to()
987                        .unwrap()
988                        .no_bound_vars()
989                        .unwrap()
990                        .alias
991                {
992                    unchanged_certainty = None;
993                }
994
995                match certainty {
996                    Certainty::Yes => {}
997                    Certainty::Maybe { .. } => {
998                        self.nested_goals.push((source, with_resolved_vars, stalled_on));
999                        unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
1000                    }
1001                }
1002            } else {
1003                let GoalEvaluation { goal, certainty, has_changed, stalled_on } =
1004                    self.evaluate_goal(source, goal, stalled_on)?;
1005                if has_changed == HasChanged::Yes {
1006                    unchanged_certainty = None;
1007                }
1008
1009                match certainty {
1010                    Certainty::Yes => {}
1011                    Certainty::Maybe { .. } => {
1012                        self.nested_goals.push((source, goal, stalled_on));
1013                        unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
1014                    }
1015                }
1016            }
1017        }
1018
1019        Ok(unchanged_certainty)
1020    }
1021
1022    /// Record impl args in the proof tree for later access by `InspectCandidate`.
1023    pub(crate) fn record_impl_args(&mut self, impl_args: I::GenericArgs) {
1024        self.inspect.record_impl_args(self.delegate, self.max_input_universe, impl_args)
1025    }
1026
1027    pub(super) fn cx(&self) -> I {
1028        self.delegate.cx()
1029    }
1030
1031    #[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("add_goal",
                                    "rustc_next_trait_solver::solve::eval_ctxt",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1031u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                                    ::tracing_core::field::FieldSet::new(&["source", "goal"],
                                        ::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(&source)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&goal)
                                                            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;
        }
        {
            goal.predicate =
                goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(self,
                            source, goal.param_env));
            self.inspect.add_goal(self.delegate, self.max_input_universe,
                source, goal);
            self.nested_goals.push((source, goal, None));
        }
    }
}#[instrument(level = "debug", skip(self))]
1032    pub(super) fn add_goal(&mut self, source: GoalSource, mut goal: Goal<I, I::Predicate>) {
1033        goal.predicate =
1034            goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(self, source, goal.param_env));
1035        self.inspect.add_goal(self.delegate, self.max_input_universe, source, goal);
1036        self.nested_goals.push((source, goal, None));
1037    }
1038
1039    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("add_goals",
                                    "rustc_next_trait_solver::solve::eval_ctxt",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1039u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_next_trait_solver::solve::eval_ctxt"),
                                    ::tracing_core::field::FieldSet::new(&["source"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&source)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        { for goal in goals { self.add_goal(source, goal); } }
    }
}#[instrument(level = "trace", skip(self, goals))]
1040    pub(super) fn add_goals(
1041        &mut self,
1042        source: GoalSource,
1043        goals: impl IntoIterator<Item = Goal<I, I::Predicate>>,
1044    ) {
1045        for goal in goals {
1046            self.add_goal(source, goal);
1047        }
1048    }
1049
1050    pub(super) fn next_region_var(&mut self) -> I::Region {
1051        let region = self.delegate.next_region_infer();
1052        self.inspect.add_var_value(region);
1053        region
1054    }
1055
1056    pub(super) fn next_ty_infer(&mut self) -> I::Ty {
1057        let ty = self.delegate.next_ty_infer();
1058        self.inspect.add_var_value(ty);
1059        ty
1060    }
1061
1062    pub(super) fn next_const_infer(&mut self) -> I::Const {
1063        let ct = self.delegate.next_const_infer();
1064        self.inspect.add_var_value(ct);
1065        ct
1066    }
1067
1068    /// Returns a ty infer or a const infer depending on whether `kind` is a `Ty` or `Const`.
1069    /// If `kind` is an integer inference variable this will still return a ty infer var.
1070    pub(super) fn next_term_infer_of_kind(&mut self, term: I::Term) -> I::Term {
1071        match term.kind() {
1072            ty::TermKind::Ty(_) => self.next_ty_infer().into(),
1073            ty::TermKind::Const(_) => self.next_const_infer().into(),
1074        }
1075    }
1076
1077    /// Is the projection predicate is of the form `exists<T> <Ty as Trait>::Assoc = T`.
1078    ///
1079    /// This is the case if the `term` does not occur in any other part of the predicate
1080    /// and is able to name all other placeholder and inference variables.
1081    x;#[instrument(level = "trace", skip(self), ret)]
1082    pub(super) fn term_is_fully_unconstrained(&self, goal: Goal<I, ty::NormalizesTo<I>>) -> bool {
1083        let universe_of_term = match goal.predicate.term.kind() {
1084            ty::TermKind::Ty(ty) => {
1085                if let ty::Infer(ty::TyVar(vid)) = ty.kind() {
1086                    self.delegate.universe_of_ty(vid).unwrap()
1087                } else {
1088                    return false;
1089                }
1090            }
1091            ty::TermKind::Const(ct) => {
1092                if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
1093                    self.delegate.universe_of_ct(vid).unwrap()
1094                } else {
1095                    return false;
1096                }
1097            }
1098        };
1099
1100        struct ContainsTermOrNotNameable<'a, D: SolverDelegate<Interner = I>, I: Interner> {
1101            term: I::Term,
1102            universe_of_term: ty::UniverseIndex,
1103            delegate: &'a D,
1104            cache: HashSet<I::Ty>,
1105        }
1106
1107        impl<D: SolverDelegate<Interner = I>, I: Interner> ContainsTermOrNotNameable<'_, D, I> {
1108            fn check_nameable(&self, universe: ty::UniverseIndex) -> ControlFlow<()> {
1109                if self.universe_of_term.can_name(universe) {
1110                    ControlFlow::Continue(())
1111                } else {
1112                    ControlFlow::Break(())
1113                }
1114            }
1115        }
1116
1117        impl<D: SolverDelegate<Interner = I>, I: Interner> TypeVisitor<I>
1118            for ContainsTermOrNotNameable<'_, D, I>
1119        {
1120            type Result = ControlFlow<()>;
1121            fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
1122                if self.cache.contains(&t) {
1123                    return ControlFlow::Continue(());
1124                }
1125
1126                match t.kind() {
1127                    ty::Infer(ty::TyVar(vid)) => {
1128                        if let ty::TermKind::Ty(term) = self.term.kind()
1129                            && let ty::Infer(ty::TyVar(term_vid)) = term.kind()
1130                            && self.delegate.root_ty_var(vid) == self.delegate.root_ty_var(term_vid)
1131                        {
1132                            return ControlFlow::Break(());
1133                        }
1134
1135                        self.check_nameable(self.delegate.universe_of_ty(vid).unwrap())?;
1136                    }
1137                    ty::Placeholder(p) => self.check_nameable(p.universe())?,
1138                    _ => {
1139                        if t.has_non_region_infer() || t.has_placeholders() {
1140                            t.super_visit_with(self)?
1141                        }
1142                    }
1143                }
1144
1145                assert!(self.cache.insert(t));
1146                ControlFlow::Continue(())
1147            }
1148
1149            fn visit_const(&mut self, c: I::Const) -> Self::Result {
1150                match c.kind() {
1151                    ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
1152                        if let ty::TermKind::Const(term) = self.term.kind()
1153                            && let ty::ConstKind::Infer(ty::InferConst::Var(term_vid)) = term.kind()
1154                            && self.delegate.root_const_var(vid)
1155                                == self.delegate.root_const_var(term_vid)
1156                        {
1157                            return ControlFlow::Break(());
1158                        }
1159
1160                        self.check_nameable(self.delegate.universe_of_ct(vid).unwrap())
1161                    }
1162                    ty::ConstKind::Placeholder(p) => self.check_nameable(p.universe()),
1163                    _ => {
1164                        if c.has_non_region_infer() || c.has_placeholders() {
1165                            c.super_visit_with(self)
1166                        } else {
1167                            ControlFlow::Continue(())
1168                        }
1169                    }
1170                }
1171            }
1172
1173            fn visit_predicate(&mut self, p: I::Predicate) -> Self::Result {
1174                if p.has_non_region_infer() || p.has_placeholders() {
1175                    p.super_visit_with(self)
1176                } else {
1177                    ControlFlow::Continue(())
1178                }
1179            }
1180
1181            fn visit_clauses(&mut self, c: I::Clauses) -> Self::Result {
1182                if c.has_non_region_infer() || c.has_placeholders() {
1183                    c.super_visit_with(self)
1184                } else {
1185                    ControlFlow::Continue(())
1186                }
1187            }
1188        }
1189
1190        let mut visitor = ContainsTermOrNotNameable {
1191            delegate: self.delegate,
1192            universe_of_term,
1193            term: goal.predicate.term,
1194            cache: Default::default(),
1195        };
1196        goal.predicate.alias.visit_with(&mut visitor).is_continue()
1197            && goal.param_env.visit_with(&mut visitor).is_continue()
1198    }
1199
1200    pub(super) fn sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
1201        self.delegate.sub_unify_ty_vids_raw(a, b)
1202    }
1203
1204    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1205    pub(super) fn eq<T: Relate<I>>(
1206        &mut self,
1207        param_env: I::ParamEnv,
1208        lhs: T,
1209        rhs: T,
1210    ) -> Result<(), NoSolution> {
1211        self.relate(param_env, lhs, ty::Variance::Invariant, rhs)
1212    }
1213
1214    /// This should be used when relating a rigid alias with another type.
1215    ///
1216    /// Normally we emit a nested `AliasRelate` when equating an inference
1217    /// variable and an alias. This causes us to instead constrain the inference
1218    /// variable to the alias without emitting a nested alias relate goals.
1219    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1220    pub(super) fn relate_rigid_alias_non_alias(
1221        &mut self,
1222        param_env: I::ParamEnv,
1223        alias: ty::AliasTerm<I>,
1224        variance: ty::Variance,
1225        term: I::Term,
1226    ) -> Result<(), NoSolution> {
1227        // NOTE: this check is purely an optimization, the structural eq would
1228        // always fail if the term is not an inference variable.
1229        if term.is_infer() {
1230            let cx = self.cx();
1231            // We need to relate `alias` to `term` treating only the outermost
1232            // constructor as rigid, relating any contained generic arguments as
1233            // normal. We do this by first structurally equating the `term`
1234            // with the alias constructor instantiated with unconstrained infer vars,
1235            // and then relate this with the whole `alias`.
1236            //
1237            // Alternatively we could modify `Equate` for this case by adding another
1238            // variant to `StructurallyRelateAliases`.
1239            let identity_args = self.fresh_args_for_item(alias.def_id());
1240            let rigid_ctor = alias.with_args(cx, identity_args);
1241            let ctor_term = rigid_ctor.to_term(cx);
1242            let obligations = self.delegate.eq_structurally_relating_aliases(
1243                param_env,
1244                term,
1245                ctor_term,
1246                self.origin_span,
1247            )?;
1248            debug_assert!(obligations.is_empty());
1249            self.relate(param_env, alias, variance, rigid_ctor)
1250        } else {
1251            Err(NoSolution)
1252        }
1253    }
1254
1255    /// This should only be used when we're either instantiating a previously
1256    /// unconstrained "return value" or when we're sure that all aliases in
1257    /// the types are rigid.
1258    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1259    pub(super) fn eq_structurally_relating_aliases<T: Relate<I>>(
1260        &mut self,
1261        param_env: I::ParamEnv,
1262        lhs: T,
1263        rhs: T,
1264    ) -> Result<(), NoSolution> {
1265        let result = self.delegate.eq_structurally_relating_aliases(
1266            param_env,
1267            lhs,
1268            rhs,
1269            self.origin_span,
1270        )?;
1271        assert_eq!(result, vec![]);
1272        Ok(())
1273    }
1274
1275    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1276    pub(super) fn sub<T: Relate<I>>(
1277        &mut self,
1278        param_env: I::ParamEnv,
1279        sub: T,
1280        sup: T,
1281    ) -> Result<(), NoSolution> {
1282        self.relate(param_env, sub, ty::Variance::Covariant, sup)
1283    }
1284
1285    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1286    pub(super) fn relate<T: Relate<I>>(
1287        &mut self,
1288        param_env: I::ParamEnv,
1289        lhs: T,
1290        variance: ty::Variance,
1291        rhs: T,
1292    ) -> Result<(), NoSolution> {
1293        let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
1294        for &goal in goals.iter() {
1295            let source = match goal.predicate.kind().skip_binder() {
1296                ty::PredicateKind::Subtype { .. } | ty::PredicateKind::AliasRelate(..) => {
1297                    GoalSource::TypeRelating
1298                }
1299                // FIXME(-Znext-solver=coinductive): should these WF goals also be unproductive?
1300                ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => GoalSource::Misc,
1301                p => unreachable!("unexpected nested goal in `relate`: {p:?}"),
1302            };
1303            self.add_goal(source, goal);
1304        }
1305        Ok(())
1306    }
1307
1308    /// Equates two values returning the nested goals without adding them
1309    /// to the nested goals of the `EvalCtxt`.
1310    ///
1311    /// If possible, try using `eq` instead which automatically handles nested
1312    /// goals correctly.
1313    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1314    pub(super) fn eq_and_get_goals<T: Relate<I>>(
1315        &self,
1316        param_env: I::ParamEnv,
1317        lhs: T,
1318        rhs: T,
1319    ) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
1320        Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs, self.origin_span)?)
1321    }
1322
1323    pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(
1324        &self,
1325        value: ty::Binder<I, T>,
1326    ) -> T {
1327        self.delegate.instantiate_binder_with_infer(value)
1328    }
1329
1330    /// `enter_forall`, but takes `&mut self` and passes it back through the
1331    /// callback since it can't be aliased during the call.
1332    ///
1333    /// The `param_env` is used to *compute* the assumptions of the binder, not *as* the
1334    /// assumptions associated with the binder.
1335    ///
1336    /// FIXME(inherent_associated_types): fix this?
1337    pub(super) fn enter_forall_with_assumptions<T: TypeFoldable<I>, U>(
1338        &mut self,
1339        value: ty::Binder<I, T>,
1340        param_env: I::ParamEnv,
1341        f: impl FnOnce(&mut Self, T) -> U,
1342    ) -> U {
1343        self.delegate.enter_forall(value, |value| {
1344            let u = self.delegate.universe();
1345            let assumptions = if self.cx().assumptions_on_binders() {
1346                self.region_assumptions_for_placeholders_in_universe(value.clone(), u, param_env)
1347            } else {
1348                None
1349            };
1350            self.delegate.insert_placeholder_assumptions(u, assumptions);
1351            f(self, value)
1352        })
1353    }
1354
1355    pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
1356    where
1357        T: TypeFoldable<I>,
1358    {
1359        self.delegate.resolve_vars_if_possible(value)
1360    }
1361
1362    pub(super) fn shallow_resolve(&self, ty: I::Ty) -> I::Ty {
1363        self.delegate.shallow_resolve(ty)
1364    }
1365
1366    pub(super) fn eager_resolve_region(&self, r: I::Region) -> I::Region {
1367        if let ty::ReVar(vid) = r.kind() {
1368            self.delegate.opportunistic_resolve_lt_var(vid)
1369        } else {
1370            r
1371        }
1372    }
1373
1374    pub(super) fn fresh_args_for_item(&mut self, def_id: I::DefId) -> I::GenericArgs {
1375        let args = self.delegate.fresh_args_for_item(def_id);
1376        for arg in args.iter() {
1377            self.inspect.add_var_value(arg);
1378        }
1379        args
1380    }
1381
1382    pub(super) fn register_solver_region_constraint(&self, c: RegionConstraint<I>) {
1383        self.delegate.register_solver_region_constraint(c);
1384    }
1385
1386    pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) {
1387        self.delegate.register_ty_outlives(ty, lt, self.origin_span);
1388    }
1389
1390    pub(super) fn register_region_outlives(
1391        &self,
1392        a: I::Region,
1393        b: I::Region,
1394        vis: VisibleForLeakCheck,
1395    ) {
1396        // `'a: 'b` ==> `'b <= 'a`
1397        self.delegate.sub_regions(b, a, vis, self.origin_span);
1398    }
1399
1400    /// Computes the list of goals required for `arg` to be well-formed
1401    pub(super) fn well_formed_goals(
1402        &self,
1403        param_env: I::ParamEnv,
1404        term: I::Term,
1405    ) -> Option<Vec<Goal<I, I::Predicate>>> {
1406        self.delegate.well_formed_goals(param_env, term)
1407    }
1408
1409    pub(super) fn trait_ref_is_knowable(
1410        &mut self,
1411        param_env: I::ParamEnv,
1412        trait_ref: ty::TraitRef<I>,
1413    ) -> Result<bool, NoSolutionOrRerunNonErased> {
1414        let delegate = self.delegate;
1415        let lazily_normalize_ty = |ty| self.structurally_normalize_ty(param_env, ty);
1416        coherence::trait_ref_is_knowable(&**delegate, trait_ref, lazily_normalize_ty)
1417            .map(|is_knowable| is_knowable.is_ok())
1418    }
1419
1420    pub(super) fn fetch_eligible_assoc_item(
1421        &self,
1422        goal_trait_ref: ty::TraitRef<I>,
1423        trait_assoc_def_id: I::TraitAssocTermId,
1424        impl_def_id: I::ImplId,
1425    ) -> FetchEligibleAssocItemResponse<I> {
1426        self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
1427    }
1428
1429    x;#[instrument(level = "debug", skip(self), ret)]
1430    pub(super) fn register_hidden_type_in_storage(
1431        &mut self,
1432        opaque_type_key: ty::OpaqueTypeKey<I>,
1433        hidden_ty: I::Ty,
1434    ) -> Option<I::Ty> {
1435        self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
1436    }
1437
1438    pub(super) fn add_item_bounds_for_hidden_type(
1439        &mut self,
1440        opaque_def_id: I::OpaqueTyId,
1441        opaque_args: I::GenericArgs,
1442        param_env: I::ParamEnv,
1443        hidden_ty: I::Ty,
1444    ) {
1445        let mut goals = Vec::new();
1446        self.delegate.add_item_bounds_for_hidden_type(
1447            opaque_def_id,
1448            opaque_args,
1449            param_env,
1450            hidden_ty,
1451            &mut goals,
1452        );
1453        self.add_goals(GoalSource::AliasWellFormed, goals);
1454    }
1455
1456    // Try to evaluate a const, or return `None` if the const is too generic.
1457    // This doesn't mean the const isn't evaluatable, though, and should be treated
1458    // as an ambiguity rather than no-solution.
1459    pub(super) fn evaluate_const(
1460        &mut self,
1461        param_env: I::ParamEnv,
1462        uv: ty::UnevaluatedConst<I>,
1463    ) -> Result<Option<I::Const>, RerunNonErased> {
1464        if self.typing_mode().is_erased_not_coherence() {
1465            self.opaque_accesses.rerun_always(RerunReason::EvaluateConst)?;
1466        }
1467
1468        Ok(self.delegate.evaluate_const(param_env, uv))
1469    }
1470
1471    pub(super) fn evaluate_const_and_instantiate_normalizes_to_term(
1472        &mut self,
1473        goal: Goal<I, ty::NormalizesTo<I>>,
1474        uv: ty::UnevaluatedConst<I>,
1475    ) -> QueryResultOrRerunNonErased<I> {
1476        match self.evaluate_const(goal.param_env, uv)? {
1477            Some(evaluated) => {
1478                self.instantiate_normalizes_to_term(goal, evaluated.into());
1479                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1480            }
1481            None if self.cx().features().generic_const_args() => {
1482                // HACK(khyperia): calling `resolve_vars_if_possible` here shouldn't be necessary,
1483                // `try_evaluate_const` calls `resolve_vars_if_possible` already. However, we want
1484                // to check `has_non_region_infer` against the type with vars resolved (i.e. check
1485                // if there are vars we failed to resolve), so we need to call it again here.
1486                // Perhaps we could split EvaluateConstErr::HasGenericsOrInfers into HasGenerics and
1487                // HasInfers or something, make evaluate_const return that, and make this branch be
1488                // based on that, rather than checking `has_non_region_infer`.
1489                if self.resolve_vars_if_possible(uv).has_non_region_infer() {
1490                    self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1491                } else {
1492                    // We do not instantiate to the `uv` passed in, but rather
1493                    // `goal.predicate.alias`. The `uv` passed in might correspond to the `impl`
1494                    // form of a constant (with generic arguments corresponding to the impl block),
1495                    // however, we want to structurally instantiate to the original, non-rebased,
1496                    // trait `Self` form of the constant (with generic arguments being the trait
1497                    // `Self` type).
1498                    self.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
1499                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1500                }
1501            }
1502            None => {
1503                // Legacy behavior: always treat as ambiguous
1504                self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1505            }
1506        }
1507    }
1508
1509    pub(super) fn is_transmutable(
1510        &mut self,
1511        src: I::Ty,
1512        dst: I::Ty,
1513        assume: I::Const,
1514    ) -> Result<Certainty, NoSolution> {
1515        self.delegate.is_transmutable(dst, src, assume)
1516    }
1517
1518    pub(super) fn replace_bound_vars<T: TypeFoldable<I>>(
1519        &self,
1520        t: T,
1521        universes: &mut Vec<Option<ty::UniverseIndex>>,
1522    ) -> T {
1523        BoundVarReplacer::replace_bound_vars(&**self.delegate, universes, t).0
1524    }
1525
1526    pub(super) fn may_use_unstable_feature(
1527        &mut self,
1528        param_env: I::ParamEnv,
1529        symbol: I::Symbol,
1530    ) -> Result<bool, RerunNonErased> {
1531        if self.typing_mode().is_erased_not_coherence() {
1532            self.opaque_accesses.rerun_always(RerunReason::MayUseUnstableFeature)?;
1533        }
1534
1535        Ok(may_use_unstable_feature(&**self.delegate, param_env, symbol))
1536    }
1537
1538    pub(crate) fn opaques_with_sub_unified_hidden_type(
1539        &self,
1540        self_ty: I::Ty,
1541    ) -> Vec<ty::AliasTy<I>> {
1542        if let ty::Infer(ty::TyVar(vid)) = self_ty.kind() {
1543            self.delegate.opaques_with_sub_unified_hidden_type(vid)
1544        } else {
1545            ::alloc::vec::Vec::new()vec![]
1546        }
1547    }
1548
1549    /// To return the constraints of a canonical query to the caller, we canonicalize:
1550    ///
1551    /// - `var_values`: a map from bound variables in the canonical goal to
1552    ///   the values inferred while solving the instantiated goal.
1553    /// - `external_constraints`: additional constraints which aren't expressible
1554    ///   using simple unification of inference variables.
1555    ///
1556    /// This takes the `shallow_certainty` which represents whether we're confident
1557    /// that the final result of the current goal only depends on the nested goals.
1558    ///
1559    /// In case this is `Certainty::Maybe`, there may still be additional nested goals
1560    /// or inference constraints required for this candidate to be hold. The candidate
1561    /// always requires all already added constraints and nested goals.
1562    x;#[instrument(level = "trace", skip(self), ret)]
1563    pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
1564        &mut self,
1565        shallow_certainty: Certainty,
1566    ) -> QueryResultOrRerunNonErased<I> {
1567        self.inspect.make_canonical_response(shallow_certainty);
1568
1569        let goals_certainty = self.try_evaluate_added_goals()?;
1570        assert_eq!(
1571            self.tainted,
1572            Ok(()),
1573            "EvalCtxt is tainted -- nested goals may have been dropped in a \
1574            previous call to `try_evaluate_added_goals!`"
1575        );
1576
1577        let goals_certainty = match self.delegate.cx().assumptions_on_binders() {
1578            true => {
1579                let certainty = self.eagerly_handle_placeholders()?;
1580                certainty.and(goals_certainty)
1581            }
1582            false => {
1583                // We only check for leaks from universes which were entered inside
1584                // of the query.
1585                self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
1586                    trace!("failed the leak check");
1587                    NoSolution
1588                })?;
1589
1590                goals_certainty
1591            }
1592        };
1593
1594        let (certainty, normalization_nested_goals) =
1595            match (self.current_goal_kind, shallow_certainty) {
1596                // When normalizing, we've replaced the expected term with an unconstrained
1597                // inference variable. This means that we dropped information which could
1598                // have been important. We handle this by instead returning the nested goals
1599                // to the caller, where they are then handled. We only do so if we do not
1600                // need to recompute the `NormalizesTo` goal afterwards to avoid repeatedly
1601                // uplifting its nested goals. This is the case if the `shallow_certainty` is
1602                // `Certainty::Yes`.
1603                (CurrentGoalKind::NormalizesTo, Certainty::Yes) => {
1604                    let goals = std::mem::take(&mut self.nested_goals);
1605                    // As we return all ambiguous nested goals, we can ignore the certainty
1606                    // returned by `self.try_evaluate_added_goals()`.
1607                    if goals.is_empty() {
1608                        assert!(matches!(goals_certainty, Certainty::Yes));
1609                    }
1610                    (
1611                        Certainty::Yes,
1612                        NestedNormalizationGoals(
1613                            goals.into_iter().map(|(s, g, _)| (s, g)).collect(),
1614                        ),
1615                    )
1616                }
1617                _ => {
1618                    let certainty = shallow_certainty.and(goals_certainty);
1619                    (certainty, NestedNormalizationGoals::empty())
1620                }
1621            };
1622
1623        if let Certainty::Maybe(
1624            maybe_info @ MaybeInfo {
1625                cause: MaybeCause::Overflow { keep_constraints: false, .. },
1626                opaque_types_jank: _,
1627                stalled_on_coroutines: _,
1628            },
1629        ) = certainty
1630        {
1631            // If we have overflow, it's probable that we're substituting a type
1632            // into itself infinitely and any partial substitutions in the query
1633            // response are probably not useful anyways, so just return an empty
1634            // query response.
1635            //
1636            // This may prevent us from potentially useful inference, e.g.
1637            // 2 candidates, one ambiguous and one overflow, which both
1638            // have the same inference constraints.
1639            //
1640            // Changing this to retain some constraints in the future
1641            // won't be a breaking change, so this is good enough for now.
1642            return Ok(self.make_ambiguous_response_no_constraints(maybe_info));
1643        }
1644
1645        let external_constraints =
1646            self.compute_external_query_constraints(certainty, normalization_nested_goals);
1647        let (var_values, mut external_constraints) =
1648            eager_resolve_vars(self.delegate, (self.var_values, external_constraints));
1649
1650        // Remove any trivial or duplicated region constraints once we've resolved regions
1651        let mut unique = HashSet::default();
1652        if let ExternalRegionConstraints::Old(r) = &mut external_constraints.region_constraints {
1653            r.retain(|(outlives, _)| !outlives.is_trivial() && unique.insert(*outlives));
1654        }
1655
1656        let canonical = canonicalize_response(
1657            self.delegate,
1658            self.max_input_universe,
1659            Response {
1660                var_values,
1661                certainty,
1662                external_constraints: self.cx().mk_external_constraints(external_constraints),
1663            },
1664        );
1665
1666        Ok(canonical)
1667    }
1668
1669    /// Constructs a totally unconstrained, ambiguous response to a goal.
1670    ///
1671    /// Take care when using this, since often it's useful to respond with
1672    /// ambiguity but return constrained variables to guide inference.
1673    pub(in crate::solve) fn make_ambiguous_response_no_constraints(
1674        &self,
1675        maybe: MaybeInfo,
1676    ) -> CanonicalResponse<I> {
1677        response_no_constraints_raw(
1678            self.cx(),
1679            self.max_input_universe,
1680            self.var_kinds,
1681            Certainty::Maybe(maybe),
1682        )
1683    }
1684
1685    /// Computes the region constraints and *new* opaque types registered when
1686    /// proving a goal.
1687    ///
1688    /// If an opaque was already constrained before proving this goal, then the
1689    /// external constraints do not need to record that opaque, since if it is
1690    /// further constrained by inference, that will be passed back in the var
1691    /// values.
1692    x;#[instrument(level = "trace", skip(self), ret)]
1693    fn compute_external_query_constraints(
1694        &self,
1695        certainty: Certainty,
1696        normalization_nested_goals: NestedNormalizationGoals<I>,
1697    ) -> ExternalConstraintsData<I> {
1698        // We only return region constraints once the certainty is `Yes`. This
1699        // is necessary as we may drop nested goals on ambiguity, which may result
1700        // in unconstrained inference variables in the region constraints. It also
1701        // prevents us from emitting duplicate region constraints, avoiding some
1702        // unnecessary work. This slightly weakens the leak check in case it uses
1703        // region constraints from an ambiguous nested goal. This is tested in both
1704        // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
1705        // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
1706        let region_constraints = if self.cx().assumptions_on_binders() {
1707            ExternalRegionConstraints::NextGen(if let Certainty::Yes = certainty {
1708                self.delegate.get_solver_region_constraint()
1709            } else {
1710                RegionConstraint::new_true()
1711            })
1712        } else {
1713            ExternalRegionConstraints::Old(if let Certainty::Yes = certainty {
1714                self.delegate.make_deduplicated_region_constraints()
1715            } else {
1716                vec![]
1717            })
1718        };
1719
1720        // We only return *newly defined* opaque types from canonical queries.
1721        //
1722        // Constraints for any existing opaque types are already tracked by changes
1723        // to the `var_values`.
1724        let opaque_types = self
1725            .delegate
1726            .clone_opaque_types_added_since(self.initial_opaque_types_storage_num_entries);
1727
1728        if self.typing_mode().is_erased_not_coherence() {
1729            assert!(opaque_types.is_empty());
1730        }
1731
1732        ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals }
1733    }
1734}
1735
1736/// Eagerly replace aliases with inference variables, emitting `AliasRelate`
1737/// goals, used when adding goals to the `EvalCtxt`. We compute the
1738/// `AliasRelate` goals before evaluating the actual goal to get all the
1739/// constraints we can.
1740///
1741/// This is a performance optimization to more eagerly detect cycles during trait
1742/// solving. See tests/ui/traits/next-solver/cycles/cycle-modulo-ambig-aliases.rs.
1743///
1744/// The emitted goals get evaluated in the context of the parent goal; by
1745/// replacing aliases in nested goals we essentially pull the normalization out of
1746/// the nested goal. We want to treat the goal as if the normalization still happens
1747/// inside of the nested goal by inheriting the `step_kind` of the nested goal and
1748/// storing it in the `GoalSource` of the emitted `AliasRelate` goals.
1749/// This is necessary for tests/ui/sized/coinductive-1.rs to compile.
1750struct ReplaceAliasWithInfer<'me, 'a, D, I>
1751where
1752    D: SolverDelegate<Interner = I>,
1753    I: Interner,
1754{
1755    ecx: &'me mut EvalCtxt<'a, D>,
1756    param_env: I::ParamEnv,
1757    normalization_goal_source: GoalSource,
1758    cache: HashMap<I::Ty, I::Ty>,
1759}
1760
1761impl<'me, 'a, D, I> ReplaceAliasWithInfer<'me, 'a, D, I>
1762where
1763    D: SolverDelegate<Interner = I>,
1764    I: Interner,
1765{
1766    fn new(
1767        ecx: &'me mut EvalCtxt<'a, D>,
1768        for_goal_source: GoalSource,
1769        param_env: I::ParamEnv,
1770    ) -> Self {
1771        let step_kind = ecx.step_kind_for_source(for_goal_source);
1772        ReplaceAliasWithInfer {
1773            ecx,
1774            param_env,
1775            normalization_goal_source: GoalSource::NormalizeGoal(step_kind),
1776            cache: Default::default(),
1777        }
1778    }
1779}
1780
1781impl<D, I> TypeFolder<I> for ReplaceAliasWithInfer<'_, '_, D, I>
1782where
1783    D: SolverDelegate<Interner = I>,
1784    I: Interner,
1785{
1786    fn cx(&self) -> I {
1787        self.ecx.cx()
1788    }
1789
1790    fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1791        match ty.kind() {
1792            ty::Alias(..) if !ty.has_escaping_bound_vars() => {
1793                let infer_ty = self.ecx.next_ty_infer();
1794                let normalizes_to = ty::PredicateKind::AliasRelate(
1795                    ty.into(),
1796                    infer_ty.into(),
1797                    ty::AliasRelationDirection::Equate,
1798                );
1799                self.ecx.add_goal(
1800                    self.normalization_goal_source,
1801                    Goal::new(self.cx(), self.param_env, normalizes_to),
1802                );
1803                infer_ty
1804            }
1805            _ => {
1806                if !ty.has_aliases() {
1807                    ty
1808                } else if let Some(&entry) = self.cache.get(&ty) {
1809                    return entry;
1810                } else {
1811                    let res = ty.super_fold_with(self);
1812                    if !self.cache.insert(ty, res).is_none() {
    ::core::panicking::panic("assertion failed: self.cache.insert(ty, res).is_none()")
};assert!(self.cache.insert(ty, res).is_none());
1813                    res
1814                }
1815            }
1816        }
1817    }
1818
1819    fn fold_const(&mut self, ct: I::Const) -> I::Const {
1820        match ct.kind() {
1821            ty::ConstKind::Unevaluated(..) if !ct.has_escaping_bound_vars() => {
1822                let infer_ct = self.ecx.next_const_infer();
1823                let normalizes_to = ty::PredicateKind::AliasRelate(
1824                    ct.into(),
1825                    infer_ct.into(),
1826                    ty::AliasRelationDirection::Equate,
1827                );
1828                self.ecx.add_goal(
1829                    self.normalization_goal_source,
1830                    Goal::new(self.cx(), self.param_env, normalizes_to),
1831                );
1832                infer_ct
1833            }
1834            _ => ct.super_fold_with(self),
1835        }
1836    }
1837
1838    fn fold_predicate(&mut self, predicate: I::Predicate) -> I::Predicate {
1839        if predicate.allow_normalization() { predicate.super_fold_with(self) } else { predicate }
1840    }
1841}
1842
1843/// Do not call this directly, use the `tcx` query instead.
1844pub fn evaluate_root_goal_for_proof_tree_raw_provider<
1845    D: SolverDelegate<Interner = I>,
1846    I: Interner,
1847>(
1848    cx: I,
1849    canonical_goal: CanonicalInput<I>,
1850) -> (QueryResult<I>, I::Probe) {
1851    let mut inspect = inspect::ProofTreeBuilder::new();
1852    let (canonical_result, accessed_opaques) = SearchGraph::<D>::evaluate_root_goal_for_proof_tree(
1853        cx,
1854        cx.recursion_limit(),
1855        canonical_goal,
1856        &mut inspect,
1857    );
1858    let final_revision = inspect.unwrap();
1859
1860    if !!accessed_opaques.might_rerun() {
    ::core::panicking::panic("assertion failed: !accessed_opaques.might_rerun()")
};assert!(!accessed_opaques.might_rerun());
1861    (canonical_result, cx.mk_probe(final_revision))
1862}
1863
1864/// Evaluate a goal to build a proof tree.
1865///
1866/// This is a copy of [EvalCtxt::evaluate_goal_raw] which avoids relying on the
1867/// [EvalCtxt] and uses a separate cache.
1868pub(super) fn evaluate_root_goal_for_proof_tree<D: SolverDelegate<Interner = I>, I: Interner>(
1869    delegate: &D,
1870    goal: Goal<I, I::Predicate>,
1871    origin_span: I::Span,
1872) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
1873    let opaque_types = delegate.clone_opaque_types_lookup_table();
1874    let (goal, opaque_types) = eager_resolve_vars(delegate, (goal, opaque_types));
1875    let typing_mode = delegate.typing_mode_raw().assert_not_erased();
1876
1877    let (orig_values, canonical_goal) =
1878        canonicalize_goal(delegate, goal, &opaque_types, typing_mode.into());
1879
1880    let (canonical_result, final_revision) =
1881        delegate.cx().evaluate_root_goal_for_proof_tree_raw(canonical_goal);
1882
1883    let proof_tree = inspect::GoalEvaluation {
1884        uncanonicalized_goal: goal,
1885        orig_values,
1886        final_revision,
1887        result: canonical_result,
1888    };
1889
1890    let response = match canonical_result {
1891        Err(e) => return (Err(e), proof_tree),
1892        Ok(response) => response,
1893    };
1894
1895    let (normalization_nested_goals, _certainty) = instantiate_and_apply_query_response(
1896        delegate,
1897        goal.param_env,
1898        &proof_tree.orig_values,
1899        response,
1900        VisibleForLeakCheck::Yes,
1901        origin_span,
1902    );
1903
1904    (Ok(normalization_nested_goals), proof_tree)
1905}