Skip to main content

rustc_next_trait_solver/solve/eval_ctxt/
mod.rs

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

    #[warn(clippy :: suspicious_else_formatting)]
    {

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

    #[warn(clippy :: suspicious_else_formatting)]
    {

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