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::ProjectionComputeAssocTermCandidate =>
                    "ProjectionComputeAssocTermCandidate",
            })
    }
}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    // FIXME: Consider renaming `PredicateKind::NormalizesTo` to match with this
58    /// Unlike other goals, `NormalizesTo` goals aren't independent goals but just implementation
59    /// details for handling projections of associated terms. When we encounter a `Projection` goal
60    /// whose `projection_term` is an associated term, we create a `NormalizesTo` goal whose
61    /// expected term is fully unconstrained and evaluate it.
62    ///
63    /// This would weaken inference however, as the nested goals of normalizes-to never get the
64    /// inference constraints from the actual expected term. We just gather candidates from the
65    /// normalizes-to goal and return any ambiguous nested goals of it to the caller (`Projection
66    /// goal`). The caller handle and evaluate them as if they were its own nested goals.
67    ///
68    /// Because of this, evaluating a normalizes-to goal is computing candidates for projection of
69    /// an associated term and it never leaks out of the solver.
70    ProjectionComputeAssocTermCandidate,
71}
72
73impl CurrentGoalKind {
74    fn from_query_input<I: Interner>(cx: I, input: QueryInput<I, I::Predicate>) -> CurrentGoalKind {
75        match input.goal.predicate.kind().skip_binder() {
76            ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
77                if cx.trait_is_coinductive(pred.trait_ref.def_id) {
78                    CurrentGoalKind::CoinductiveTrait
79                } else {
80                    CurrentGoalKind::Misc
81                }
82            }
83            ty::PredicateKind::NormalizesTo(_) => {
84                CurrentGoalKind::ProjectionComputeAssocTermCandidate
85            }
86            _ => CurrentGoalKind::Misc,
87        }
88    }
89}
90
91#[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)]
92enum RerunDecision {
93    Yes,
94    No,
95    EagerlyPropagateToParent,
96}
97pub struct EvalCtxt<'a, D, I = <D as SolverDelegate>::Interner>
98where
99    D: SolverDelegate<Interner = I>,
100    I: Interner,
101{
102    /// The inference context that backs (mostly) inference and placeholder terms
103    /// instantiated while solving goals.
104    ///
105    /// NOTE: The `InferCtxt` that backs the `EvalCtxt` is intentionally private,
106    /// because the `InferCtxt` is much more general than `EvalCtxt`. Methods such
107    /// as  `take_registered_region_obligations` can mess up query responses,
108    /// using `At::normalize` is totally wrong, calling `evaluate_root_goal` can
109    /// cause coinductive unsoundness, etc.
110    ///
111    /// Methods that are generally of use for trait solving are *intentionally*
112    /// re-declared through the `EvalCtxt` below, often with cleaner signatures
113    /// since we don't care about things like `ObligationCause`s and `Span`s here.
114    /// If some `InferCtxt` method is missing, please first think defensively about
115    /// the method's compatibility with this solver, or if an existing one does
116    /// the job already.
117    delegate: &'a D,
118
119    /// The variable info for the `var_values`, only used to make an ambiguous response
120    /// with no constraints.
121    var_kinds: I::CanonicalVarKinds,
122
123    /// What kind of goal we're currently computing, see the enum definition
124    /// for more info.
125    current_goal_kind: CurrentGoalKind,
126    pub(super) var_values: CanonicalVarValues<I>,
127
128    /// The highest universe index nameable by the caller.
129    ///
130    /// When we enter a new binder inside of the query we create new universes
131    /// which the caller cannot name. We have to be careful with variables from
132    /// these new universes when creating the query response.
133    ///
134    /// Both because these new universes can prevent us from reaching a fixpoint
135    /// if we have a coinductive cycle and because that's the only way we can return
136    /// new placeholders to the caller.
137    pub(super) max_input_universe: ty::UniverseIndex,
138    /// The opaque types from the canonical input. We only need to return opaque types
139    /// which have been added to the storage while evaluating this goal.
140    pub(super) initial_opaque_types_storage_num_entries:
141        <D::Infcx as InferCtxtLike>::OpaqueTypeStorageEntries,
142
143    pub(super) search_graph: &'a mut SearchGraph<D>,
144
145    nested_goals: Vec<(GoalSource, Goal<I, I::Predicate>, Option<GoalStalledOn<I>>)>,
146
147    pub(super) origin_span: I::Span,
148
149    // Has this `EvalCtxt` errored out with `NoSolution` in `try_evaluate_added_goals`?
150    //
151    // If so, then it can no longer be used to make a canonical query response,
152    // since subsequent calls to `try_evaluate_added_goals` have possibly dropped
153    // ambiguous goals. Instead, a probe needs to be introduced somewhere in the
154    // evaluation code.
155    tainted: Result<(), NoSolution>,
156
157    /// Tracks accesses of opaque types while in [`TypingMode::ErasedNotCoherence`].
158    pub(super) opaque_accesses: AccessedOpaques<I>,
159
160    pub(super) inspect: inspect::EvaluationStepBuilder<D>,
161}
162
163#[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)]
164#[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))]
165pub enum GenerateProofTree {
166    Yes,
167    No,
168}
169
170pub trait SolverDelegateEvalExt: SolverDelegate {
171    /// Evaluates a goal from **outside** of the trait solver.
172    ///
173    /// Using this while inside of the solver is wrong as it uses a new
174    /// search graph which would break cycle detection.
175    fn evaluate_root_goal(
176        &self,
177        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
178        span: <Self::Interner as Interner>::Span,
179        stalled_on: Option<GoalStalledOn<Self::Interner>>,
180    ) -> Result<GoalEvaluation<Self::Interner>, NoSolution>;
181
182    /// Checks whether evaluating `goal` may hold while treating not-yet-defined
183    /// opaque types as being kind of rigid.
184    ///
185    /// See the comment on [OpaqueTypesJank] for more details.
186    fn root_goal_may_hold_opaque_types_jank(
187        &self,
188        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
189    ) -> bool;
190
191    /// Check whether evaluating `goal` with a depth of `root_depth` may
192    /// succeed. This only returns `false` if the goal is guaranteed to
193    /// not hold. In case evaluation overflows and fails with ambiguity this
194    /// returns `true`.
195    ///
196    /// This is only intended to be used as a performance optimization
197    /// in coherence checking.
198    fn root_goal_may_hold_with_depth(
199        &self,
200        root_depth: usize,
201        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
202    ) -> bool;
203
204    // FIXME: This is only exposed because we need to use it in `analyse.rs`
205    // which is not yet uplifted. Once that's done, we should remove this.
206    fn evaluate_root_goal_for_proof_tree(
207        &self,
208        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
209        span: <Self::Interner as Interner>::Span,
210    ) -> (
211        Result<NestedNormalizationGoals<Self::Interner>, NoSolution>,
212        inspect::GoalEvaluation<Self::Interner>,
213    );
214}
215
216impl<D, I> SolverDelegateEvalExt for D
217where
218    D: SolverDelegate<Interner = I>,
219    I: Interner,
220{
221    x;#[instrument(level = "debug", skip(self), ret)]
222    fn evaluate_root_goal(
223        &self,
224        goal: Goal<I, I::Predicate>,
225        span: I::Span,
226        stalled_on: Option<GoalStalledOn<I>>,
227    ) -> Result<GoalEvaluation<I>, NoSolution> {
228        let result = EvalCtxt::enter_root(self, self.cx().recursion_limit(), span, |ecx| {
229            ecx.evaluate_goal(GoalSource::Misc, goal, stalled_on)
230        });
231
232        match result {
233            Ok(i) => Ok(i),
234            Err(NoSolutionOrRerunNonErased::NoSolution(NoSolution)) => Err(NoSolution),
235            Err(NoSolutionOrRerunNonErased::RerunNonErased(_)) => {
236                unreachable!("this never happens at the root, we're never in erased mode here");
237            }
238        }
239    }
240
241    x;#[instrument(level = "debug", skip(self), ret)]
242    fn root_goal_may_hold_opaque_types_jank(
243        &self,
244        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
245    ) -> bool {
246        self.probe(|| {
247            EvalCtxt::enter_root(self, self.cx().recursion_limit(), I::Span::dummy(), |ecx| {
248                ecx.evaluate_goal(GoalSource::Misc, goal, None)
249            })
250            .is_ok_and(|r| match r.certainty {
251                Certainty::Yes => true,
252                Certainty::Maybe(MaybeInfo {
253                    cause: _,
254                    opaque_types_jank,
255                    stalled_on_coroutines: _,
256                }) => match opaque_types_jank {
257                    OpaqueTypesJank::AllGood => true,
258                    OpaqueTypesJank::ErrorIfRigidSelfTy => false,
259                },
260            })
261        })
262    }
263
264    fn root_goal_may_hold_with_depth(
265        &self,
266        root_depth: usize,
267        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
268    ) -> bool {
269        self.probe(|| {
270            EvalCtxt::enter_root(self, root_depth, I::Span::dummy(), |ecx| {
271                ecx.evaluate_goal(GoalSource::Misc, goal, None)
272            })
273        })
274        .is_ok()
275    }
276
277    #[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(277u32),
                                    ::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))]
278    fn evaluate_root_goal_for_proof_tree(
279        &self,
280        goal: Goal<I, I::Predicate>,
281        span: I::Span,
282    ) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
283        evaluate_root_goal_for_proof_tree(self, goal, span)
284    }
285}
286
287impl<'a, D, I> EvalCtxt<'a, D>
288where
289    D: SolverDelegate<Interner = I>,
290    I: Interner,
291{
292    pub(super) fn typing_mode(&self) -> TypingMode<I> {
293        self.delegate.typing_mode_raw()
294    }
295
296    /// Computes the `PathKind` for the step from the current goal to the
297    /// nested goal required due to `source`.
298    ///
299    /// See #136824 for a more detailed reasoning for this behavior. We
300    /// consider cycles to be coinductive if they 'step into' a where-clause
301    /// of a coinductive trait. We will likely extend this function in the future
302    /// and will need to clearly document it in the rustc-dev-guide before
303    /// stabilization.
304    pub(super) fn step_kind_for_source(&self, source: GoalSource) -> PathKind {
305        match source {
306            // We treat these goals as unknown for now. It is likely that most miscellaneous
307            // nested goals will be converted to an inductive variant in the future.
308            //
309            // Having unknown cycles is always the safer option, as changing that to either
310            // succeed or hard error is backwards compatible. If we incorrectly treat a cycle
311            // as inductive even though it should not be, it may be unsound during coherence and
312            // fixing it may cause inference breakage or introduce ambiguity.
313            GoalSource::Misc => PathKind::Unknown,
314            GoalSource::NormalizeGoal(path_kind) => path_kind,
315            GoalSource::ImplWhereBound => match self.current_goal_kind {
316                // We currently only consider a cycle coinductive if it steps
317                // into a where-clause of a coinductive trait.
318                CurrentGoalKind::CoinductiveTrait => PathKind::Coinductive,
319                // While normalizing via an impl does step into a where-clause of
320                // an impl, accessing the associated item immediately steps out of
321                // it again. This means cycles/recursive calls are not guarded
322                // by impls used for normalization.
323                //
324                // See tests/ui/traits/next-solver/cycles/normalizes-to-is-not-productive.rs
325                // for how this can go wrong.
326                CurrentGoalKind::ProjectionComputeAssocTermCandidate => PathKind::Inductive,
327                // We probably want to make all traits coinductive in the future,
328                // so we treat cycles involving where-clauses of not-yet coinductive
329                // traits as ambiguous for now.
330                CurrentGoalKind::Misc => PathKind::Unknown,
331            },
332            // Relating types is always unproductive. If we were to map proof trees to
333            // corecursive functions as explained in #136824, relating types never
334            // introduces a constructor which could cause the recursion to be guarded.
335            GoalSource::TypeRelating => PathKind::Inductive,
336            // These goal sources are likely unproductive and can be changed to
337            // `PathKind::Inductive`. Keeping them as unknown until we're confident
338            // about this and have an example where it is necessary.
339            GoalSource::AliasBoundConstCondition | GoalSource::AliasWellFormed => PathKind::Unknown,
340        }
341    }
342
343    /// Creates a root evaluation context and search graph. This should only be
344    /// used from outside of any evaluation, and other methods should be preferred
345    /// over using this manually (such as [`SolverDelegateEvalExt::evaluate_root_goal`]).
346    pub(super) fn enter_root<R>(
347        delegate: &D,
348        root_depth: usize,
349        origin_span: I::Span,
350        f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
351    ) -> R {
352        let mut search_graph = SearchGraph::new(root_depth);
353
354        let mut ecx = EvalCtxt {
355            delegate,
356            search_graph: &mut search_graph,
357            nested_goals: Default::default(),
358            inspect: inspect::EvaluationStepBuilder::new_noop(),
359
360            // Only relevant when canonicalizing the response,
361            // which we don't do within this evaluation context.
362            max_input_universe: ty::UniverseIndex::ROOT,
363            initial_opaque_types_storage_num_entries: Default::default(),
364            var_kinds: Default::default(),
365            var_values: CanonicalVarValues::dummy(),
366            current_goal_kind: CurrentGoalKind::Misc,
367            origin_span,
368            tainted: Ok(()),
369            opaque_accesses: AccessedOpaques::default(),
370        };
371        let result = f(&mut ecx);
372        if !ecx.nested_goals.is_empty() {
    {
        ::core::panicking::panic_fmt(format_args!("root `EvalCtxt` should not have any goals added to it"));
    }
};assert!(
373            ecx.nested_goals.is_empty(),
374            "root `EvalCtxt` should not have any goals added to it"
375        );
376        if !!ecx.opaque_accesses.might_rerun() {
    ::core::panicking::panic("assertion failed: !ecx.opaque_accesses.might_rerun()")
};assert!(!ecx.opaque_accesses.might_rerun());
377        if !search_graph.is_empty() {
    ::core::panicking::panic("assertion failed: search_graph.is_empty()")
};assert!(search_graph.is_empty());
378        result
379    }
380
381    /// Creates a nested evaluation context that shares the same search graph as the
382    /// one passed in. This is suitable for evaluation, granted that the search graph
383    /// has had the nested goal recorded on its stack. This method only be used by
384    /// `search_graph::Delegate::compute_goal`.
385    ///
386    /// This function takes care of setting up the inference context, setting the anchor,
387    /// and registering opaques from the canonicalized input.
388    pub(super) fn enter_canonical<T>(
389        cx: I,
390        search_graph: &'a mut SearchGraph<D>,
391        canonical_input: CanonicalInput<I>,
392        proof_tree_builder: &mut inspect::ProofTreeBuilder<D>,
393        f: impl FnOnce(
394            &mut EvalCtxt<'_, D>,
395            Goal<I, I::Predicate>,
396        ) -> Result<T, NoSolutionOrRerunNonErased>,
397    ) -> (Result<T, NoSolution>, AccessedOpaques<I>) {
398        let (ref delegate, input, var_values) = D::build_with_canonical(cx, &canonical_input);
399        for (key, ty) in input.predefined_opaques_in_body.iter() {
400            let prev = delegate.register_hidden_type_in_storage(key, ty, I::Span::dummy());
401            // It may be possible that two entries in the opaque type storage end up
402            // with the same key after resolving contained inference variables.
403            //
404            // We could put them in the duplicate list but don't have to. The opaques we
405            // encounter here are already tracked in the caller, so there's no need to
406            // also store them here. We'd take them out when computing the query response
407            // and then discard them, as they're already present in the input.
408            //
409            // Ideally we'd drop duplicate opaque type definitions when computing
410            // the canonical input. This is more annoying to implement and may cause a
411            // perf regression, so we do it inside of the query for now.
412            if let Some(prev) = prev {
413                {
    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:413",
                        "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(413u32),
                        ::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`");
414            }
415        }
416
417        let initial_opaque_types_storage_num_entries = delegate.opaque_types_storage_num_entries();
418        if truecfg!(debug_assertions) && delegate.typing_mode_raw().is_erased_not_coherence() {
419            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());
420        }
421
422        let mut ecx = EvalCtxt {
423            delegate,
424            var_kinds: canonical_input.canonical.var_kinds,
425            var_values,
426            current_goal_kind: CurrentGoalKind::from_query_input(cx, input),
427            max_input_universe: canonical_input.canonical.max_universe,
428            initial_opaque_types_storage_num_entries,
429            search_graph,
430            nested_goals: Default::default(),
431            origin_span: I::Span::dummy(),
432            tainted: Ok(()),
433            inspect: proof_tree_builder.new_evaluation_step(var_values),
434            opaque_accesses: AccessedOpaques::default(),
435        };
436
437        let result = f(&mut ecx, input.goal);
438        ecx.inspect.probe_final_state(ecx.delegate, ecx.max_input_universe);
439        proof_tree_builder.finish_evaluation_step(ecx.inspect);
440
441        if canonical_input.typing_mode.0.is_erased_not_coherence() {
442            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());
443        }
444
445        // When creating a query response we clone the opaque type constraints
446        // instead of taking them. This would cause an ICE here, since we have
447        // assertions against dropping an `InferCtxt` without taking opaques.
448        // FIXME: Once we remove support for the old impl we can remove this.
449        // FIXME: Could we make `build_with_canonical` into `enter_with_canonical` and call this at the end?
450        delegate.reset_opaque_types();
451
452        let opaque_accesses = ecx.opaque_accesses;
453        (
454            match result {
455                Ok(i) => Ok(i),
456                Err(NoSolutionOrRerunNonErased::NoSolution(NoSolution)) => Err(NoSolution),
457                Err(NoSolutionOrRerunNonErased::RerunNonErased(_)) => {
458                    // check th t the opaque_accesses state mirrors the result we got.
459                    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());
460                    Err(NoSolution)
461                }
462            },
463            opaque_accesses,
464        )
465    }
466
467    pub(super) fn ignore_candidate_head_usages(&mut self, usages: CandidateHeadUsages) {
468        self.search_graph.ignore_candidate_head_usages(usages);
469    }
470
471    /// Recursively evaluates `goal`, returning whether any inference vars have
472    /// been constrained and the certainty of the result.
473    fn evaluate_goal(
474        &mut self,
475        source: GoalSource,
476        goal: Goal<I, I::Predicate>,
477        stalled_on: Option<GoalStalledOn<I>>,
478    ) -> Result<GoalEvaluation<I>, NoSolutionOrRerunNonErased> {
479        let (normalization_nested_goals, goal_evaluation) =
480            self.evaluate_goal_raw(source, goal, stalled_on)?;
481        if !normalization_nested_goals.is_empty() {
    ::core::panicking::panic("assertion failed: normalization_nested_goals.is_empty()")
};assert!(normalization_nested_goals.is_empty());
482        Ok(goal_evaluation)
483    }
484
485    /// Recursively evaluates `goal`, returning the nested goals in case
486    /// the nested goal is a `NormalizesTo` goal.
487    ///
488    /// As all other goal kinds do not return any nested goals and
489    /// `NormalizesTo` is only used by `Projection`, all other callsites
490    /// should use [`EvalCtxt::evaluate_goal`] which discards that empty
491    /// storage.
492    pub(super) fn evaluate_goal_raw(
493        &mut self,
494        source: GoalSource,
495        goal: Goal<I, I::Predicate>,
496        stalled_on: Option<GoalStalledOn<I>>,
497    ) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolutionOrRerunNonErased> {
498        // If we have run this goal before, and it was stalled, check that any of the goal's
499        // args have changed. Otherwise, we don't need to re-run the goal because it'll remain
500        // stalled, since it'll canonicalize the same way and evaluation is pure.
501        if let Some(GoalStalledOn {
502            num_opaques,
503            ref stalled_vars,
504            ref sub_roots,
505            stalled_certainty,
506        }) = stalled_on
507            && !self.delegate.disable_trait_solver_fast_paths()
508            && !stalled_vars.iter().any(|value| self.delegate.is_changed_arg(*value))
509            && !sub_roots
510                .iter()
511                .any(|&vid| self.delegate.sub_unification_table_root_var(vid) != vid)
512            && !self.delegate.opaque_types_storage_num_entries().needs_reevaluation(num_opaques)
513        {
514            return Ok((
515                NestedNormalizationGoals::empty(),
516                GoalEvaluation {
517                    goal,
518                    certainty: stalled_certainty,
519                    has_changed: HasChanged::No,
520                    stalled_on,
521                },
522            ));
523        }
524
525        // We only care about one entry per `OpaqueTypeKey` here,
526        // so we only canonicalize the lookup table and ignore
527        // duplicate entries.
528        let opaque_types = self.delegate.clone_opaque_types_lookup_table();
529        let (goal, opaque_types) = eager_resolve_vars(self.delegate, (goal, opaque_types));
530        let typing_mode = self.typing_mode();
531        let step_kind = self.step_kind_for_source(source);
532
533        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(533u32),
                        ::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!(
534            Level::DEBUG,
535            "evaluate_goal_raw in typing mode",
536            "{:?} opaques={:?}",
537            typing_mode,
538            opaque_types
539        )
540        .entered();
541
542        let (result, orig_values, canonical_goal) = 'retry_canonicalize: {
543            let skip_erased_attempt = if typing_mode.is_coherence() {
544                true
545            } else {
546                let mut skip = false;
547                if opaque_types.iter().any(|(_, ty)| ty.is_ty_var())
548                    && let PredicateKind::Clause(ClauseKind::Trait(..)) =
549                        goal.predicate.kind().skip_binder()
550                {
551                    skip = true;
552                }
553
554                if let PredicateKind::Clause(ClauseKind::Trait(tr)) =
555                    goal.predicate.kind().skip_binder()
556                    && tr.self_ty().has_coroutines()
557                    && self.cx().trait_is_auto(tr.trait_ref.def_id)
558                {
559                    // FIXME(#155443): this doesn't make a difference now, but with eager normalization
560                    // it likely will.
561                    // skip_erased_attempt = true;
562                }
563
564                skip
565            };
566
567            if skip_erased_attempt {
568                if typing_mode.is_erased_not_coherence() {
569                    match self.opaque_accesses.rerun_always(RerunReason::SkipErasedAttempt)? {}
570                } else {
571                    {
    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:571",
                        "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(571u32),
                        ::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");
572                }
573            } else {
574                {
    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:574",
                        "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(574u32),
                        ::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:?}");
575
576                let (orig_values, canonical_goal) = canonicalize_goal(
577                    self.delegate,
578                    goal,
579                    &[],
580                    TypingMode::ErasedNotCoherence(MayBeErased),
581                );
582
583                let (canonical_result, accessed_opaques) = self.search_graph.evaluate_goal(
584                    self.cx(),
585                    canonical_goal,
586                    step_kind,
587                    &mut inspect::ProofTreeBuilder::new_noop(),
588                );
589
590                let should_rerun = self.should_rerun_after_erased_canonicalization(
591                    accessed_opaques,
592                    self.typing_mode(),
593                    &opaque_types,
594                );
595                match should_rerun {
596                    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:596",
                        "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(596u32),
                        ::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"),
597                    RerunDecision::No => {
598                        break 'retry_canonicalize (canonical_result, orig_values, canonical_goal);
599                    }
600                    RerunDecision::EagerlyPropagateToParent => {
601                        self.opaque_accesses.update(accessed_opaques)?;
602                        break 'retry_canonicalize (canonical_result, orig_values, canonical_goal);
603                    }
604                }
605            }
606
607            let (orig_values, canonical_goal) =
608                canonicalize_goal(self.delegate, goal, &opaque_types, typing_mode);
609
610            let (canonical_result, accessed_opaques) = self.search_graph.evaluate_goal(
611                self.cx(),
612                canonical_goal,
613                step_kind,
614                &mut inspect::ProofTreeBuilder::new_noop(),
615            );
616            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!(
617                !accessed_opaques.might_rerun(),
618                "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:?}"
619            );
620
621            (canonical_result, orig_values, canonical_goal)
622        };
623
624        {
    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:624",
                        "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(624u32),
                        ::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);
625        let response = match result {
626            Ok(response) => {
627                {
    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:627",
                        "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(627u32),
                        ::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");
628                response
629            }
630            Err(NoSolution) => {
631                {
    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:631",
                        "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(631u32),
                        ::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");
632                return Err(NoSolution.into());
633            }
634        };
635
636        drop(tracing_span);
637
638        let has_changed =
639            if !has_only_region_constraints(response) { HasChanged::Yes } else { HasChanged::No };
640
641        // FIXME: We should revisit and consider removing this after
642        // *assumptions on binders* is available, like once we had done in the
643        // stabilization of `-Znext-solver=coherence`(#121848).
644        // We ignore constraints from the nested goals in leak check. This is to match
645        // with the old solver's behavior, which has separated evaluation and fulfillment,
646        // and the former doesn't consider outlives obligations from the later.
647        let vis = match goal.predicate.kind().skip_binder() {
648            ty::PredicateKind::Clause(_)
649            | ty::PredicateKind::DynCompatible(_)
650            | ty::PredicateKind::Subtype(_)
651            | ty::PredicateKind::Coerce(_)
652            | ty::PredicateKind::ConstEquate(_, _)
653            | ty::PredicateKind::Ambiguous
654            | ty::PredicateKind::NormalizesTo(_) => VisibleForLeakCheck::No,
655            ty::PredicateKind::AliasRelate(_, _, _) => VisibleForLeakCheck::Yes,
656        };
657
658        let (normalization_nested_goals, certainty) = instantiate_and_apply_query_response(
659            self.delegate,
660            goal.param_env,
661            &orig_values,
662            response,
663            vis,
664            self.origin_span,
665        );
666
667        // FIXME: We previously had an assert here that checked that recomputing
668        // a goal after applying its constraints did not change its response.
669        //
670        // This assert was removed as it did not hold for goals constraining
671        // an inference variable to a recursive alias, e.g. in
672        // tests/ui/traits/next-solver/overflow/recursive-self-normalization.rs.
673        //
674        // Once we have decided on how to handle trait-system-refactor-initiative#75,
675        // we should re-add an assert here.
676
677        let stalled_on = match certainty {
678            Certainty::Yes => None,
679            Certainty::Maybe { .. } => match has_changed {
680                // FIXME: We could recompute a *new* set of stalled variables by walking
681                // through the orig values, resolving, and computing the root vars of anything
682                // that is not resolved. Only when *these* have changed is it meaningful
683                // to recompute this goal.
684                HasChanged::Yes => None,
685                HasChanged::No => {
686                    // Remove the canonicalized universal vars, since we only care about stalled existentials.
687                    let mut sub_roots = Vec::new();
688                    let mut stalled_vars = orig_values;
689                    stalled_vars.retain(|arg| match arg.kind() {
690                        // Lifetimes can never stall goals.
691                        ty::GenericArgKind::Lifetime(_) => false,
692                        ty::GenericArgKind::Type(ty) => match ty.kind() {
693                            ty::Infer(ty::TyVar(vid)) => {
694                                sub_roots.push(self.delegate.sub_unification_table_root_var(vid));
695                                true
696                            }
697                            ty::Infer(_) => true,
698                            ty::Param(_) | ty::Placeholder(_) => false,
699                            _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("unexpected orig_value: {0:?}", ty)));
}unreachable!("unexpected orig_value: {ty:?}"),
700                        },
701                        ty::GenericArgKind::Const(ct) => match ct.kind() {
702                            ty::ConstKind::Infer(_) => true,
703                            ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => false,
704                            _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("unexpected orig_value: {0:?}", ct)));
}unreachable!("unexpected orig_value: {ct:?}"),
705                        },
706                    });
707
708                    Some(GoalStalledOn {
709                        num_opaques: canonical_goal
710                            .canonical
711                            .value
712                            .predefined_opaques_in_body
713                            .len(),
714                        stalled_vars,
715                        sub_roots,
716                        stalled_certainty: certainty,
717                    })
718                }
719            },
720        };
721
722        Ok((
723            normalization_nested_goals,
724            GoalEvaluation { goal, certainty, has_changed, stalled_on },
725        ))
726    }
727
728    fn should_rerun_after_erased_canonicalization(
729        &self,
730        AccessedOpaques { reason: _, rerun }: AccessedOpaques<I>,
731        original_typing_mode: TypingMode<I>,
732        parent_opaque_types: &[(OpaqueTypeKey<I>, I::Ty)],
733    ) -> RerunDecision {
734        let parent_opaque_defids = parent_opaque_types.iter().map(|(key, _)| key.def_id.into());
735        let opaque_in_storage = |opaques: I::LocalDefIds, defids: SmallCopyList<_>| {
736            if defids.as_ref().is_empty() {
737                RerunDecision::No
738            } else if opaques
739                .iter()
740                .chain(parent_opaque_defids)
741                .any(|opaque| defids.as_ref().contains(&opaque))
742            {
743                RerunDecision::Yes
744            } else {
745                RerunDecision::No
746            }
747        };
748        let any_opaque_has_infer_as_hidden = || {
749            if parent_opaque_types.iter().any(|(_, ty)| ty.is_ty_var()) {
750                RerunDecision::Yes
751            } else {
752                RerunDecision::No
753            }
754        };
755
756        let res = match (rerun, original_typing_mode) {
757            // =============================
758            (RerunCondition::Never, _) => RerunDecision::No,
759            // =============================
760            (_, TypingMode::ErasedNotCoherence(MayBeErased)) => {
761                RerunDecision::EagerlyPropagateToParent
762            }
763            // =============================
764            // In coherence, we never switch to erased mode, so we will never register anything
765            // in the rerun state, so we should've taken the first branch of this match
766            (_, TypingMode::Coherence) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
767            // =============================
768            (RerunCondition::Always, _) => RerunDecision::Yes,
769            // =============================
770            (
771                RerunCondition::OpaqueInStorage(..),
772                TypingMode::PostAnalysis | TypingMode::Codegen,
773            ) => RerunDecision::Yes,
774            (
775                RerunCondition::OpaqueInStorage(defids),
776                TypingMode::PostBorrowck { defined_opaque_types: opaques }
777                | TypingMode::Typeck { defining_opaque_types_and_generators: opaques }
778                | TypingMode::PostTypeckUntilBorrowck { defining_opaque_types: opaques },
779            ) => opaque_in_storage(opaques, defids),
780            // =============================
781            (RerunCondition::AnyOpaqueHasInferAsHidden, TypingMode::Typeck { .. }) => {
782                any_opaque_has_infer_as_hidden()
783            }
784            (
785                RerunCondition::AnyOpaqueHasInferAsHidden,
786                TypingMode::PostBorrowck { .. }
787                | TypingMode::PostAnalysis
788                | TypingMode::Codegen
789                | TypingMode::PostTypeckUntilBorrowck { .. },
790            ) => RerunDecision::No,
791            // =============================
792            (
793                RerunCondition::OpaqueInStorageOrAnyOpaqueHasInferAsHidden(_),
794                TypingMode::PostAnalysis | TypingMode::Codegen,
795            ) => RerunDecision::No,
796            (
797                RerunCondition::OpaqueInStorageOrAnyOpaqueHasInferAsHidden(defids),
798                TypingMode::Typeck { 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::PostBorrowck { defined_opaque_types: opaques }
811                | TypingMode::PostTypeckUntilBorrowck { 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        // If this loop did not result in any progress, what's our final certainty.
912        let mut unchanged_certainty = Some(Certainty::Yes);
913        for (source, goal, stalled_on) in mem::take(&mut self.nested_goals) {
914            // We never handle `NormalizesTo` as a nested goal
915            if true {
    if !!#[allow(non_exhaustive_omitted_patterns)] match goal.predicate.kind().skip_binder()
                    {
                    PredicateKind::NormalizesTo(_) => true,
                    _ => false,
                } {
        ::core::panicking::panic("assertion failed: !matches!(goal.predicate.kind().skip_binder(), PredicateKind::NormalizesTo(_))")
    };
};debug_assert!(!matches!(
916                goal.predicate.kind().skip_binder(),
917                PredicateKind::NormalizesTo(_)
918            ));
919
920            if !self.delegate.disable_trait_solver_fast_paths()
921                && let Some(certainty) =
922                    self.delegate.compute_goal_fast_path(goal, self.origin_span)
923            {
924                match certainty {
925                    Certainty::Yes => {}
926                    Certainty::Maybe { .. } => {
927                        self.nested_goals.push((source, goal, None));
928                        unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
929                    }
930                }
931                continue;
932            }
933
934            let GoalEvaluation { goal, certainty, has_changed, stalled_on } =
935                self.evaluate_goal(source, goal, stalled_on)?;
936            if has_changed == HasChanged::Yes {
937                unchanged_certainty = None;
938            }
939
940            match certainty {
941                Certainty::Yes => {}
942                Certainty::Maybe { .. } => {
943                    self.nested_goals.push((source, goal, stalled_on));
944                    unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
945                }
946            }
947        }
948
949        Ok(unchanged_certainty)
950    }
951
952    /// Record impl args in the proof tree for later access by `InspectCandidate`.
953    pub(crate) fn record_impl_args(&mut self, impl_args: I::GenericArgs) {
954        self.inspect.record_impl_args(self.delegate, self.max_input_universe, impl_args)
955    }
956
957    pub(super) fn cx(&self) -> I {
958        self.delegate.cx()
959    }
960
961    pub(super) fn add_goal(&mut self, source: GoalSource, mut goal: Goal<I, I::Predicate>) {
962        goal.predicate = self.replace_alias_with_infer(goal.predicate, source, goal.param_env);
963        self.add_goal_raw(source, goal);
964    }
965
966    #[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_raw",
                                    "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(966u32),
                                    ::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;
        }
        {
            self.inspect.add_goal(self.delegate, self.max_input_universe,
                source, goal);
            self.nested_goals.push((source, goal, None));
        }
    }
}#[instrument(level = "debug", skip(self))]
967    fn add_goal_raw(&mut self, source: GoalSource, goal: Goal<I, I::Predicate>) {
968        self.inspect.add_goal(self.delegate, self.max_input_universe, source, goal);
969        self.nested_goals.push((source, goal, None));
970    }
971
972    #[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(972u32),
                                    ::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))]
973    pub(super) fn add_goals(
974        &mut self,
975        source: GoalSource,
976        goals: impl IntoIterator<Item = Goal<I, I::Predicate>>,
977    ) {
978        for goal in goals {
979            self.add_goal(source, goal);
980        }
981    }
982
983    pub(super) fn replace_alias_with_infer<T: TypeFoldable<I>>(
984        &mut self,
985        value: T,
986        source: GoalSource,
987        param_env: I::ParamEnv,
988    ) -> T {
989        value.fold_with(&mut ReplaceAliasWithInfer::new(self, source, param_env))
990    }
991
992    pub(super) fn next_region_var(&mut self) -> I::Region {
993        let region = self.delegate.next_region_infer();
994        self.inspect.add_var_value(region);
995        region
996    }
997
998    pub(super) fn next_ty_infer(&mut self) -> I::Ty {
999        let ty = self.delegate.next_ty_infer();
1000        self.inspect.add_var_value(ty);
1001        ty
1002    }
1003
1004    pub(super) fn next_const_infer(&mut self) -> I::Const {
1005        let ct = self.delegate.next_const_infer();
1006        self.inspect.add_var_value(ct);
1007        ct
1008    }
1009
1010    /// Returns a ty infer or a const infer depending on whether `kind` is a `Ty` or `Const`.
1011    /// If `kind` is an integer inference variable this will still return a ty infer var.
1012    pub(super) fn next_term_infer_of_kind(&mut self, term: I::Term) -> I::Term {
1013        match term.kind() {
1014            ty::TermKind::Ty(_) => self.next_ty_infer().into(),
1015            ty::TermKind::Const(_) => self.next_const_infer().into(),
1016        }
1017    }
1018
1019    /// Is the projection predicate is of the form `exists<T> <Ty as Trait>::Assoc = T`.
1020    ///
1021    /// This is the case if the `term` does not occur in any other part of the predicate
1022    /// and is able to name all other placeholder and inference variables.
1023    x;#[instrument(level = "trace", skip(self), ret)]
1024    pub(super) fn term_is_fully_unconstrained(&self, goal: Goal<I, ty::NormalizesTo<I>>) -> bool {
1025        let universe_of_term = match goal.predicate.term.kind() {
1026            ty::TermKind::Ty(ty) => {
1027                if let ty::Infer(ty::TyVar(vid)) = ty.kind() {
1028                    self.delegate.universe_of_ty(vid).unwrap()
1029                } else {
1030                    return false;
1031                }
1032            }
1033            ty::TermKind::Const(ct) => {
1034                if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
1035                    self.delegate.universe_of_ct(vid).unwrap()
1036                } else {
1037                    return false;
1038                }
1039            }
1040        };
1041
1042        struct ContainsTermOrNotNameable<'a, D: SolverDelegate<Interner = I>, I: Interner> {
1043            term: I::Term,
1044            universe_of_term: ty::UniverseIndex,
1045            delegate: &'a D,
1046            cache: HashSet<I::Ty>,
1047        }
1048
1049        impl<D: SolverDelegate<Interner = I>, I: Interner> ContainsTermOrNotNameable<'_, D, I> {
1050            fn check_nameable(&self, universe: ty::UniverseIndex) -> ControlFlow<()> {
1051                if self.universe_of_term.can_name(universe) {
1052                    ControlFlow::Continue(())
1053                } else {
1054                    ControlFlow::Break(())
1055                }
1056            }
1057        }
1058
1059        impl<D: SolverDelegate<Interner = I>, I: Interner> TypeVisitor<I>
1060            for ContainsTermOrNotNameable<'_, D, I>
1061        {
1062            type Result = ControlFlow<()>;
1063            fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
1064                if self.cache.contains(&t) {
1065                    return ControlFlow::Continue(());
1066                }
1067
1068                match t.kind() {
1069                    ty::Infer(ty::TyVar(vid)) => {
1070                        if let ty::TermKind::Ty(term) = self.term.kind()
1071                            && let ty::Infer(ty::TyVar(term_vid)) = term.kind()
1072                            && self.delegate.root_ty_var(vid) == self.delegate.root_ty_var(term_vid)
1073                        {
1074                            return ControlFlow::Break(());
1075                        }
1076
1077                        self.check_nameable(self.delegate.universe_of_ty(vid).unwrap())?;
1078                    }
1079                    ty::Placeholder(p) => self.check_nameable(p.universe())?,
1080                    _ => {
1081                        if t.has_non_region_infer() || t.has_placeholders() {
1082                            t.super_visit_with(self)?
1083                        }
1084                    }
1085                }
1086
1087                assert!(self.cache.insert(t));
1088                ControlFlow::Continue(())
1089            }
1090
1091            fn visit_const(&mut self, c: I::Const) -> Self::Result {
1092                match c.kind() {
1093                    ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
1094                        if let ty::TermKind::Const(term) = self.term.kind()
1095                            && let ty::ConstKind::Infer(ty::InferConst::Var(term_vid)) = term.kind()
1096                            && self.delegate.root_const_var(vid)
1097                                == self.delegate.root_const_var(term_vid)
1098                        {
1099                            return ControlFlow::Break(());
1100                        }
1101
1102                        self.check_nameable(self.delegate.universe_of_ct(vid).unwrap())
1103                    }
1104                    ty::ConstKind::Placeholder(p) => self.check_nameable(p.universe()),
1105                    _ => {
1106                        if c.has_non_region_infer() || c.has_placeholders() {
1107                            c.super_visit_with(self)
1108                        } else {
1109                            ControlFlow::Continue(())
1110                        }
1111                    }
1112                }
1113            }
1114
1115            fn visit_predicate(&mut self, p: I::Predicate) -> Self::Result {
1116                if p.has_non_region_infer() || p.has_placeholders() {
1117                    p.super_visit_with(self)
1118                } else {
1119                    ControlFlow::Continue(())
1120                }
1121            }
1122
1123            fn visit_clauses(&mut self, c: I::Clauses) -> Self::Result {
1124                if c.has_non_region_infer() || c.has_placeholders() {
1125                    c.super_visit_with(self)
1126                } else {
1127                    ControlFlow::Continue(())
1128                }
1129            }
1130        }
1131
1132        let mut visitor = ContainsTermOrNotNameable {
1133            delegate: self.delegate,
1134            universe_of_term,
1135            term: goal.predicate.term,
1136            cache: Default::default(),
1137        };
1138        goal.predicate.alias.visit_with(&mut visitor).is_continue()
1139            && goal.param_env.visit_with(&mut visitor).is_continue()
1140    }
1141
1142    pub(super) fn sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
1143        self.delegate.sub_unify_ty_vids_raw(a, b)
1144    }
1145
1146    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1147    pub(super) fn eq<T: Relate<I>>(
1148        &mut self,
1149        param_env: I::ParamEnv,
1150        lhs: T,
1151        rhs: T,
1152    ) -> Result<(), NoSolution> {
1153        self.relate(param_env, lhs, ty::Variance::Invariant, rhs)
1154    }
1155
1156    /// This should be used when relating a rigid alias with another type.
1157    ///
1158    /// Normally we emit a nested `AliasRelate` when equating an inference
1159    /// variable and an alias. This causes us to instead constrain the inference
1160    /// variable to the alias without emitting a nested alias relate goals.
1161    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1162    pub(super) fn relate_rigid_alias_non_alias(
1163        &mut self,
1164        param_env: I::ParamEnv,
1165        alias: ty::AliasTerm<I>,
1166        variance: ty::Variance,
1167        term: I::Term,
1168    ) -> Result<(), NoSolution> {
1169        // NOTE: this check is purely an optimization, the structural eq would
1170        // always fail if the term is not an inference variable.
1171        if term.is_infer() {
1172            let cx = self.cx();
1173            // We need to relate `alias` to `term` treating only the outermost
1174            // constructor as rigid, relating any contained generic arguments as
1175            // normal. We do this by first structurally equating the `term`
1176            // with the alias constructor instantiated with unconstrained infer vars,
1177            // and then relate this with the whole `alias`.
1178            //
1179            // Alternatively we could modify `Equate` for this case by adding another
1180            // variant to `StructurallyRelateAliases`.
1181            let def_id = match alias.kind {
1182                ty::AliasTermKind::ProjectionTy { def_id } => def_id.into(),
1183                ty::AliasTermKind::InherentTy { def_id } => def_id.into(),
1184                ty::AliasTermKind::OpaqueTy { def_id } => def_id.into(),
1185                ty::AliasTermKind::FreeTy { def_id } => def_id.into(),
1186                ty::AliasTermKind::AnonConst { def_id } => def_id.into(),
1187                ty::AliasTermKind::ProjectionConst { def_id } => def_id.into(),
1188                ty::AliasTermKind::FreeConst { def_id } => def_id.into(),
1189                ty::AliasTermKind::InherentConst { def_id } => def_id.into(),
1190            };
1191            let identity_args = self.fresh_args_for_item(def_id);
1192            let rigid_ctor = alias.with_args(cx, identity_args);
1193            let ctor_term = rigid_ctor.to_term(cx);
1194            let obligations = self.delegate.eq_structurally_relating_aliases(
1195                param_env,
1196                term,
1197                ctor_term,
1198                self.origin_span,
1199            )?;
1200            debug_assert!(obligations.is_empty());
1201            self.relate(param_env, alias, variance, rigid_ctor)
1202        } else {
1203            Err(NoSolution)
1204        }
1205    }
1206
1207    /// This should only be used when we're either instantiating a previously
1208    /// unconstrained "return value" or when we're sure that all aliases in
1209    /// the types are rigid.
1210    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1211    pub(super) fn eq_structurally_relating_aliases<T: Relate<I>>(
1212        &mut self,
1213        param_env: I::ParamEnv,
1214        lhs: T,
1215        rhs: T,
1216    ) -> Result<(), NoSolution> {
1217        let result = self.delegate.eq_structurally_relating_aliases(
1218            param_env,
1219            lhs,
1220            rhs,
1221            self.origin_span,
1222        )?;
1223        assert_eq!(result, vec![]);
1224        Ok(())
1225    }
1226
1227    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1228    pub(super) fn sub<T: Relate<I>>(
1229        &mut self,
1230        param_env: I::ParamEnv,
1231        sub: T,
1232        sup: T,
1233    ) -> Result<(), NoSolution> {
1234        self.relate(param_env, sub, ty::Variance::Covariant, sup)
1235    }
1236
1237    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1238    pub(super) fn relate<T: Relate<I>>(
1239        &mut self,
1240        param_env: I::ParamEnv,
1241        lhs: T,
1242        variance: ty::Variance,
1243        rhs: T,
1244    ) -> Result<(), NoSolution> {
1245        let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
1246        for &goal in goals.iter() {
1247            let source = match goal.predicate.kind().skip_binder() {
1248                ty::PredicateKind::Subtype { .. } | ty::PredicateKind::AliasRelate(..) => {
1249                    GoalSource::TypeRelating
1250                }
1251                // FIXME(-Znext-solver=coinductive): should these WF goals also be unproductive?
1252                ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => GoalSource::Misc,
1253                p => unreachable!("unexpected nested goal in `relate`: {p:?}"),
1254            };
1255            self.add_goal(source, goal);
1256        }
1257        Ok(())
1258    }
1259
1260    /// Equates two values returning the nested goals without adding them
1261    /// to the nested goals of the `EvalCtxt`.
1262    ///
1263    /// If possible, try using `eq` instead which automatically handles nested
1264    /// goals correctly.
1265    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1266    pub(super) fn eq_and_get_goals<T: Relate<I>>(
1267        &self,
1268        param_env: I::ParamEnv,
1269        lhs: T,
1270        rhs: T,
1271    ) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
1272        Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs, self.origin_span)?)
1273    }
1274
1275    pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(
1276        &self,
1277        value: ty::Binder<I, T>,
1278    ) -> T {
1279        self.delegate.instantiate_binder_with_infer(value)
1280    }
1281
1282    /// `enter_forall_with_assumptions`, but takes `&mut self` and passes it back through
1283    /// the callback since it can't be aliased during the call.
1284    ///
1285    /// The `param_env` is used to *compute* the assumptions of the binder, not *as* the
1286    /// assumptions associated with the binder.
1287    ///
1288    /// FIXME(inherent_associated_types): fix this?
1289    pub(super) fn enter_forall_with_assumptions<T: TypeFoldable<I>, U>(
1290        &mut self,
1291        value: ty::Binder<I, T>,
1292        param_env: I::ParamEnv,
1293        f: impl FnOnce(&mut Self, T) -> U,
1294    ) -> U {
1295        self.delegate.enter_forall_without_assumptions(value, |value| {
1296            let u = self.delegate.universe();
1297            let assumptions = if self.cx().assumptions_on_binders() {
1298                self.region_assumptions_for_placeholders_in_universe(value.clone(), u, param_env)
1299            } else {
1300                None
1301            };
1302            self.delegate.insert_placeholder_assumptions(u, assumptions);
1303            f(self, value)
1304        })
1305    }
1306
1307    pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
1308    where
1309        T: TypeFoldable<I>,
1310    {
1311        self.delegate.resolve_vars_if_possible(value)
1312    }
1313
1314    pub(super) fn shallow_resolve(&self, ty: I::Ty) -> I::Ty {
1315        self.delegate.shallow_resolve(ty)
1316    }
1317
1318    pub(super) fn eager_resolve_region(&self, r: I::Region) -> I::Region {
1319        if let ty::ReVar(vid) = r.kind() {
1320            self.delegate.opportunistic_resolve_lt_var(vid)
1321        } else {
1322            r
1323        }
1324    }
1325
1326    pub(super) fn fresh_args_for_item(&mut self, def_id: I::DefId) -> I::GenericArgs {
1327        let args = self.delegate.fresh_args_for_item(def_id);
1328        for arg in args.iter() {
1329            self.inspect.add_var_value(arg);
1330        }
1331        args
1332    }
1333
1334    pub(super) fn register_solver_region_constraint(&self, c: RegionConstraint<I>) {
1335        self.delegate.register_solver_region_constraint(c);
1336    }
1337
1338    pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) {
1339        self.delegate.register_ty_outlives(ty, lt, self.origin_span);
1340    }
1341
1342    pub(super) fn register_region_outlives(
1343        &self,
1344        a: I::Region,
1345        b: I::Region,
1346        vis: VisibleForLeakCheck,
1347    ) {
1348        // `'a: 'b` ==> `'b <= 'a`
1349        self.delegate.sub_regions(b, a, vis, self.origin_span);
1350    }
1351
1352    /// Computes the list of goals required for `arg` to be well-formed
1353    pub(super) fn well_formed_goals(
1354        &self,
1355        param_env: I::ParamEnv,
1356        term: I::Term,
1357    ) -> Option<Vec<Goal<I, I::Predicate>>> {
1358        self.delegate.well_formed_goals(param_env, term)
1359    }
1360
1361    pub(super) fn trait_ref_is_knowable(
1362        &mut self,
1363        param_env: I::ParamEnv,
1364        trait_ref: ty::TraitRef<I>,
1365    ) -> Result<bool, NoSolutionOrRerunNonErased> {
1366        let delegate = self.delegate;
1367        let lazily_normalize_ty = |ty| self.structurally_normalize_ty(param_env, ty);
1368        coherence::trait_ref_is_knowable(&**delegate, trait_ref, lazily_normalize_ty)
1369            .map(|is_knowable| is_knowable.is_ok())
1370    }
1371
1372    pub(super) fn fetch_eligible_assoc_item(
1373        &self,
1374        goal_trait_ref: ty::TraitRef<I>,
1375        trait_assoc_def_id: I::TraitAssocTermId,
1376        impl_def_id: I::ImplId,
1377    ) -> FetchEligibleAssocItemResponse<I> {
1378        self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
1379    }
1380
1381    x;#[instrument(level = "debug", skip(self), ret)]
1382    pub(super) fn register_hidden_type_in_storage(
1383        &mut self,
1384        opaque_type_key: ty::OpaqueTypeKey<I>,
1385        hidden_ty: I::Ty,
1386    ) -> Option<I::Ty> {
1387        self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
1388    }
1389
1390    pub(super) fn add_item_bounds_for_hidden_type(
1391        &mut self,
1392        opaque_def_id: I::OpaqueTyId,
1393        opaque_args: I::GenericArgs,
1394        param_env: I::ParamEnv,
1395        hidden_ty: I::Ty,
1396    ) {
1397        let mut goals = Vec::new();
1398        self.delegate.add_item_bounds_for_hidden_type(
1399            opaque_def_id,
1400            opaque_args,
1401            param_env,
1402            hidden_ty,
1403            &mut goals,
1404        );
1405        self.add_goals(GoalSource::AliasWellFormed, goals);
1406    }
1407
1408    // Try to evaluate a const, or return `None` if the const is too generic.
1409    // This doesn't mean the const isn't evaluatable, though, and should be treated
1410    // as an ambiguity rather than no-solution.
1411    pub(super) fn evaluate_const(
1412        &mut self,
1413        param_env: I::ParamEnv,
1414        uv: ty::UnevaluatedConst<I>,
1415    ) -> Result<Option<I::Const>, RerunNonErased> {
1416        if self.typing_mode().is_erased_not_coherence() {
1417            self.opaque_accesses.rerun_always(RerunReason::EvaluateConst)?;
1418        }
1419
1420        Ok(self.delegate.evaluate_const(param_env, uv))
1421    }
1422
1423    pub(super) fn evaluate_const_and_instantiate_projection_term(
1424        &mut self,
1425        param_env: I::ParamEnv,
1426        projection_term: ty::AliasTerm<I>,
1427        expected_term: I::Term,
1428        uv: ty::UnevaluatedConst<I>,
1429    ) -> QueryResultOrRerunNonErased<I> {
1430        match self.evaluate_const(param_env, uv)? {
1431            Some(evaluated) => {
1432                self.eq(param_env, expected_term, evaluated.into())?;
1433                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1434            }
1435            None if self.cx().features().generic_const_args() => {
1436                // HACK(khyperia): calling `resolve_vars_if_possible` here shouldn't be necessary,
1437                // `try_evaluate_const` calls `resolve_vars_if_possible` already. However, we want
1438                // to check `has_non_region_infer` against the type with vars resolved (i.e. check
1439                // if there are vars we failed to resolve), so we need to call it again here.
1440                // Perhaps we could split EvaluateConstErr::HasGenericsOrInfers into HasGenerics and
1441                // HasInfers or something, make evaluate_const return that, and make this branch be
1442                // based on that, rather than checking `has_non_region_infer`.
1443                if self.resolve_vars_if_possible(uv).has_non_region_infer() {
1444                    self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1445                } else {
1446                    // We do not instantiate to the `uv` passed in, but rather
1447                    // `goal.predicate.alias`. The `uv` passed in might correspond to the `impl`
1448                    // form of a constant (with generic arguments corresponding to the impl block),
1449                    // however, we want to structurally instantiate to the original, non-rebased,
1450                    // trait `Self` form of the constant (with generic arguments being the trait
1451                    // `Self` type).
1452                    self.relate_rigid_alias_non_alias(
1453                        param_env,
1454                        projection_term,
1455                        ty::Invariant,
1456                        expected_term,
1457                    )?;
1458                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1459                }
1460            }
1461            None => {
1462                // Legacy behavior: always treat as ambiguous
1463                self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1464            }
1465        }
1466    }
1467
1468    pub(super) fn is_transmutable(
1469        &mut self,
1470        src: I::Ty,
1471        dst: I::Ty,
1472        assume: I::Const,
1473    ) -> Result<Certainty, NoSolution> {
1474        self.delegate.is_transmutable(dst, src, assume)
1475    }
1476
1477    pub(super) fn replace_bound_vars<T: TypeFoldable<I>>(
1478        &self,
1479        t: T,
1480        universes: &mut Vec<Option<ty::UniverseIndex>>,
1481    ) -> T {
1482        BoundVarReplacer::replace_bound_vars(&**self.delegate, universes, t).0
1483    }
1484
1485    pub(super) fn may_use_unstable_feature(
1486        &mut self,
1487        param_env: I::ParamEnv,
1488        symbol: I::Symbol,
1489    ) -> Result<bool, RerunNonErased> {
1490        if self.typing_mode().is_erased_not_coherence() {
1491            self.opaque_accesses.rerun_always(RerunReason::MayUseUnstableFeature)?;
1492        }
1493
1494        Ok(may_use_unstable_feature(&**self.delegate, param_env, symbol))
1495    }
1496
1497    pub(crate) fn opaques_with_sub_unified_hidden_type(
1498        &self,
1499        self_ty: I::Ty,
1500    ) -> Vec<ty::AliasTy<I>> {
1501        if let ty::Infer(ty::TyVar(vid)) = self_ty.kind() {
1502            self.delegate.opaques_with_sub_unified_hidden_type(vid)
1503        } else {
1504            ::alloc::vec::Vec::new()vec![]
1505        }
1506    }
1507
1508    /// To return the constraints of a canonical query to the caller, we canonicalize:
1509    ///
1510    /// - `var_values`: a map from bound variables in the canonical goal to
1511    ///   the values inferred while solving the instantiated goal.
1512    /// - `external_constraints`: additional constraints which aren't expressible
1513    ///   using simple unification of inference variables.
1514    ///
1515    /// This takes the `shallow_certainty` which represents whether we're confident
1516    /// that the final result of the current goal only depends on the nested goals.
1517    ///
1518    /// In case this is `Certainty::Maybe`, there may still be additional nested goals
1519    /// or inference constraints required for this candidate to be hold. The candidate
1520    /// always requires all already added constraints and nested goals.
1521    x;#[instrument(level = "trace", skip(self), ret)]
1522    pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
1523        &mut self,
1524        shallow_certainty: Certainty,
1525    ) -> QueryResultOrRerunNonErased<I> {
1526        self.inspect.make_canonical_response(shallow_certainty);
1527
1528        let goals_certainty = self.try_evaluate_added_goals()?;
1529        assert_eq!(
1530            self.tainted,
1531            Ok(()),
1532            "EvalCtxt is tainted -- nested goals may have been dropped in a \
1533            previous call to `try_evaluate_added_goals!`"
1534        );
1535
1536        let goals_certainty = match self.delegate.cx().assumptions_on_binders() {
1537            true => {
1538                let certainty = self.eagerly_handle_placeholders()?;
1539                certainty.and(goals_certainty)
1540            }
1541            false => {
1542                // We only check for leaks from universes which were entered inside
1543                // of the query.
1544                self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
1545                    trace!("failed the leak check");
1546                    NoSolution
1547                })?;
1548
1549                goals_certainty
1550            }
1551        };
1552
1553        let (certainty, normalization_nested_goals) =
1554            match (self.current_goal_kind, shallow_certainty) {
1555                // When normalizing, we've replaced the expected term with an unconstrained
1556                // inference variable. This means that we dropped information which could
1557                // have been important. We handle this by instead returning the nested goals
1558                // to the caller, where they are then handled. We only do so if we do not
1559                // need to recompute the `NormalizesTo` goal afterwards to avoid repeatedly
1560                // uplifting its nested goals. This is the case if the `shallow_certainty` is
1561                // `Certainty::Yes`.
1562                (CurrentGoalKind::ProjectionComputeAssocTermCandidate, Certainty::Yes) => {
1563                    let goals = std::mem::take(&mut self.nested_goals);
1564                    // As we return all ambiguous nested goals, we can ignore the certainty
1565                    // returned by `self.try_evaluate_added_goals()`.
1566                    if goals.is_empty() {
1567                        assert!(matches!(goals_certainty, Certainty::Yes));
1568                    }
1569                    (
1570                        Certainty::Yes,
1571                        NestedNormalizationGoals(
1572                            goals.into_iter().map(|(s, g, _)| (s, g)).collect(),
1573                        ),
1574                    )
1575                }
1576                _ => {
1577                    let certainty = shallow_certainty.and(goals_certainty);
1578                    (certainty, NestedNormalizationGoals::empty())
1579                }
1580            };
1581
1582        if let Certainty::Maybe(
1583            maybe_info @ MaybeInfo {
1584                cause: MaybeCause::Overflow { keep_constraints: false, .. },
1585                opaque_types_jank: _,
1586                stalled_on_coroutines: _,
1587            },
1588        ) = certainty
1589        {
1590            // If we have overflow, it's probable that we're substituting a type
1591            // into itself infinitely and any partial substitutions in the query
1592            // response are probably not useful anyways, so just return an empty
1593            // query response.
1594            //
1595            // This may prevent us from potentially useful inference, e.g.
1596            // 2 candidates, one ambiguous and one overflow, which both
1597            // have the same inference constraints.
1598            //
1599            // Changing this to retain some constraints in the future
1600            // won't be a breaking change, so this is good enough for now.
1601            return Ok(self.make_ambiguous_response_no_constraints(maybe_info));
1602        }
1603
1604        let external_constraints =
1605            self.compute_external_query_constraints(certainty, normalization_nested_goals);
1606        let (var_values, mut external_constraints) =
1607            eager_resolve_vars(self.delegate, (self.var_values, external_constraints));
1608
1609        // Remove any trivial or duplicated region constraints once we've resolved regions
1610        let mut unique = HashSet::default();
1611        if let ExternalRegionConstraints::Old(r) = &mut external_constraints.region_constraints {
1612            r.retain(|(outlives, _)| !outlives.is_trivial() && unique.insert(*outlives));
1613        }
1614
1615        let canonical = canonicalize_response(
1616            self.delegate,
1617            self.max_input_universe,
1618            Response {
1619                var_values,
1620                certainty,
1621                external_constraints: self.cx().mk_external_constraints(external_constraints),
1622            },
1623        );
1624
1625        Ok(canonical)
1626    }
1627
1628    /// Constructs a totally unconstrained, ambiguous response to a goal.
1629    ///
1630    /// Take care when using this, since often it's useful to respond with
1631    /// ambiguity but return constrained variables to guide inference.
1632    pub(in crate::solve) fn make_ambiguous_response_no_constraints(
1633        &self,
1634        maybe: MaybeInfo,
1635    ) -> CanonicalResponse<I> {
1636        response_no_constraints_raw(
1637            self.cx(),
1638            self.max_input_universe,
1639            self.var_kinds,
1640            Certainty::Maybe(maybe),
1641        )
1642    }
1643
1644    /// Computes the region constraints and *new* opaque types registered when
1645    /// proving a goal.
1646    ///
1647    /// If an opaque was already constrained before proving this goal, then the
1648    /// external constraints do not need to record that opaque, since if it is
1649    /// further constrained by inference, that will be passed back in the var
1650    /// values.
1651    x;#[instrument(level = "trace", skip(self), ret)]
1652    fn compute_external_query_constraints(
1653        &self,
1654        certainty: Certainty,
1655        normalization_nested_goals: NestedNormalizationGoals<I>,
1656    ) -> ExternalConstraintsData<I> {
1657        // We only return region constraints once the certainty is `Yes`. This
1658        // is necessary as we may drop nested goals on ambiguity, which may result
1659        // in unconstrained inference variables in the region constraints. It also
1660        // prevents us from emitting duplicate region constraints, avoiding some
1661        // unnecessary work. This slightly weakens the leak check in case it uses
1662        // region constraints from an ambiguous nested goal. This is tested in both
1663        // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
1664        // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
1665        let region_constraints = if self.cx().assumptions_on_binders() {
1666            ExternalRegionConstraints::NextGen(if let Certainty::Yes = certainty {
1667                self.delegate.get_solver_region_constraint()
1668            } else {
1669                RegionConstraint::new_true()
1670            })
1671        } else {
1672            ExternalRegionConstraints::Old(if let Certainty::Yes = certainty {
1673                self.delegate.make_deduplicated_region_constraints()
1674            } else {
1675                vec![]
1676            })
1677        };
1678
1679        // We only return *newly defined* opaque types from canonical queries.
1680        //
1681        // Constraints for any existing opaque types are already tracked by changes
1682        // to the `var_values`.
1683        let opaque_types = self
1684            .delegate
1685            .clone_opaque_types_added_since(self.initial_opaque_types_storage_num_entries);
1686
1687        if self.typing_mode().is_erased_not_coherence() {
1688            assert!(opaque_types.is_empty());
1689        }
1690
1691        ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals }
1692    }
1693}
1694
1695// FIXME: This should be eventually removed in favor of proper normalization.
1696// cc #156742
1697/// Eagerly replace aliases with inference variables, emitting `AliasRelate`
1698/// goals, used when adding goals to the `EvalCtxt`. We compute the
1699/// `AliasRelate` goals before evaluating the actual goal to get all the
1700/// constraints we can.
1701///
1702/// This is a performance optimization to more eagerly detect cycles during trait
1703/// solving. See tests/ui/traits/next-solver/cycles/cycle-modulo-ambig-aliases.rs.
1704///
1705/// The emitted goals get evaluated in the context of the parent goal; by
1706/// replacing aliases in nested goals we essentially pull the normalization out of
1707/// the nested goal. We want to treat the goal as if the normalization still happens
1708/// inside of the nested goal by inheriting the `step_kind` of the nested goal and
1709/// storing it in the `GoalSource` of the emitted `AliasRelate` goals.
1710/// This is necessary for tests/ui/sized/coinductive-1.rs to compile.
1711struct ReplaceAliasWithInfer<'me, 'a, D, I>
1712where
1713    D: SolverDelegate<Interner = I>,
1714    I: Interner,
1715{
1716    ecx: &'me mut EvalCtxt<'a, D>,
1717    param_env: I::ParamEnv,
1718    normalization_goal_source: GoalSource,
1719    cache: HashMap<I::Ty, I::Ty>,
1720}
1721
1722impl<'me, 'a, D, I> ReplaceAliasWithInfer<'me, 'a, D, I>
1723where
1724    D: SolverDelegate<Interner = I>,
1725    I: Interner,
1726{
1727    fn new(
1728        ecx: &'me mut EvalCtxt<'a, D>,
1729        for_goal_source: GoalSource,
1730        param_env: I::ParamEnv,
1731    ) -> Self {
1732        let step_kind = ecx.step_kind_for_source(for_goal_source);
1733        ReplaceAliasWithInfer {
1734            ecx,
1735            param_env,
1736            normalization_goal_source: GoalSource::NormalizeGoal(step_kind),
1737            cache: Default::default(),
1738        }
1739    }
1740}
1741
1742impl<D, I> TypeFolder<I> for ReplaceAliasWithInfer<'_, '_, D, I>
1743where
1744    D: SolverDelegate<Interner = I>,
1745    I: Interner,
1746{
1747    fn cx(&self) -> I {
1748        self.ecx.cx()
1749    }
1750
1751    fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1752        match ty.kind() {
1753            ty::Alias(alias) if !ty.has_escaping_bound_vars() => {
1754                let infer_ty = self.ecx.next_ty_infer();
1755                let projection = ty::ProjectionPredicate {
1756                    projection_term: alias.into(),
1757                    term: infer_ty.into(),
1758                };
1759                self.ecx.add_goal_raw(
1760                    self.normalization_goal_source,
1761                    Goal::new(self.cx(), self.param_env, projection),
1762                );
1763                infer_ty
1764            }
1765            _ => {
1766                if !ty.has_aliases() {
1767                    ty
1768                } else if let Some(&entry) = self.cache.get(&ty) {
1769                    return entry;
1770                } else {
1771                    let res = ty.super_fold_with(self);
1772                    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());
1773                    res
1774                }
1775            }
1776        }
1777    }
1778
1779    fn fold_const(&mut self, ct: I::Const) -> I::Const {
1780        match ct.kind() {
1781            ty::ConstKind::Unevaluated(uv) if !ct.has_escaping_bound_vars() => {
1782                let infer_ct = self.ecx.next_const_infer();
1783                let projection =
1784                    ty::ProjectionPredicate { projection_term: uv.into(), term: infer_ct.into() };
1785                self.ecx.add_goal_raw(
1786                    self.normalization_goal_source,
1787                    Goal::new(self.cx(), self.param_env, projection),
1788                );
1789                infer_ct
1790            }
1791            _ => ct.super_fold_with(self),
1792        }
1793    }
1794
1795    fn fold_predicate(&mut self, predicate: I::Predicate) -> I::Predicate {
1796        if predicate.allow_normalization() { predicate.super_fold_with(self) } else { predicate }
1797    }
1798}
1799
1800/// Do not call this directly, use the `tcx` query instead.
1801pub fn evaluate_root_goal_for_proof_tree_raw_provider<
1802    D: SolverDelegate<Interner = I>,
1803    I: Interner,
1804>(
1805    cx: I,
1806    canonical_goal: CanonicalInput<I>,
1807) -> (QueryResult<I>, I::Probe) {
1808    let mut inspect = inspect::ProofTreeBuilder::new();
1809    let (canonical_result, accessed_opaques) = SearchGraph::<D>::evaluate_root_goal_for_proof_tree(
1810        cx,
1811        cx.recursion_limit(),
1812        canonical_goal,
1813        &mut inspect,
1814    );
1815    let final_revision = inspect.unwrap();
1816
1817    if !!accessed_opaques.might_rerun() {
    ::core::panicking::panic("assertion failed: !accessed_opaques.might_rerun()")
};assert!(!accessed_opaques.might_rerun());
1818    (canonical_result, cx.mk_probe(final_revision))
1819}
1820
1821/// Evaluate a goal to build a proof tree.
1822///
1823/// This is a copy of [EvalCtxt::evaluate_goal_raw] which avoids relying on the
1824/// [EvalCtxt] and uses a separate cache.
1825pub(super) fn evaluate_root_goal_for_proof_tree<D: SolverDelegate<Interner = I>, I: Interner>(
1826    delegate: &D,
1827    goal: Goal<I, I::Predicate>,
1828    origin_span: I::Span,
1829) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
1830    let opaque_types = delegate.clone_opaque_types_lookup_table();
1831    let (goal, opaque_types) = eager_resolve_vars(delegate, (goal, opaque_types));
1832    let typing_mode = delegate.typing_mode_raw().assert_not_erased();
1833
1834    let (orig_values, canonical_goal) =
1835        canonicalize_goal(delegate, goal, &opaque_types, typing_mode.into());
1836
1837    let (canonical_result, final_revision) =
1838        delegate.cx().evaluate_root_goal_for_proof_tree_raw(canonical_goal);
1839
1840    let proof_tree = inspect::GoalEvaluation {
1841        uncanonicalized_goal: goal,
1842        orig_values,
1843        final_revision,
1844        result: canonical_result,
1845    };
1846
1847    let response = match canonical_result {
1848        Err(e) => return (Err(e), proof_tree),
1849        Ok(response) => response,
1850    };
1851
1852    let (normalization_nested_goals, _certainty) = instantiate_and_apply_query_response(
1853        delegate,
1854        goal.param_env,
1855        &proof_tree.orig_values,
1856        response,
1857        VisibleForLeakCheck::Yes,
1858        origin_span,
1859    );
1860
1861    (Ok(normalization_nested_goals), proof_tree)
1862}