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::relate::Relate;
9use rustc_type_ir::relate::solver_relating::RelateExt;
10use rustc_type_ir::search_graph::{CandidateHeadUsages, PathKind};
11use rustc_type_ir::solve::{MaybeInfo, OpaqueTypesJank};
12use rustc_type_ir::{
13    self as ty, CanonicalVarValues, InferCtxtLike, Interner, TypeFoldable, TypeFolder,
14    TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
15    TypingMode,
16};
17use tracing::{debug, instrument, trace};
18
19use super::has_only_region_constraints;
20use crate::canonical::{
21    canonicalize_goal, canonicalize_response, instantiate_and_apply_query_response,
22    response_no_constraints_raw,
23};
24use crate::coherence;
25use crate::delegate::SolverDelegate;
26use crate::placeholder::BoundVarReplacer;
27use crate::resolve::eager_resolve_vars;
28use crate::solve::search_graph::SearchGraph;
29use crate::solve::ty::may_use_unstable_feature;
30use crate::solve::{
31    CanonicalInput, CanonicalResponse, Certainty, ExternalConstraintsData, FIXPOINT_STEP_LIMIT,
32    Goal, GoalEvaluation, GoalSource, GoalStalledOn, HasChanged, MaybeCause,
33    NestedNormalizationGoals, NoSolution, QueryInput, QueryResult, Response, VisibleForLeakCheck,
34    inspect,
35};
36
37mod probe;
38
39/// The kind of goal we're currently proving.
40///
41/// This has effects on cycle handling handling and on how we compute
42/// query responses, see the variant descriptions for more info.
43#[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)]
44enum CurrentGoalKind {
45    Misc,
46    /// We're proving an trait goal for a coinductive trait, either an auto trait or `Sized`.
47    ///
48    /// These are currently the only goals whose impl where-clauses are considered to be
49    /// productive steps.
50    CoinductiveTrait,
51    /// Unlike other goals, `NormalizesTo` goals act like functions with the expected term
52    /// always being fully unconstrained. This would weaken inference however, as the nested
53    /// goals never get the inference constraints from the actual normalized-to type.
54    ///
55    /// Because of this we return any ambiguous nested goals from `NormalizesTo` to the
56    /// caller when then adds these to its own context. The caller is always an `AliasRelate`
57    /// goal so this never leaks out of the solver.
58    NormalizesTo,
59}
60
61impl CurrentGoalKind {
62    fn from_query_input<I: Interner>(cx: I, input: QueryInput<I, I::Predicate>) -> CurrentGoalKind {
63        match input.goal.predicate.kind().skip_binder() {
64            ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
65                if cx.trait_is_coinductive(pred.trait_ref.def_id) {
66                    CurrentGoalKind::CoinductiveTrait
67                } else {
68                    CurrentGoalKind::Misc
69                }
70            }
71            ty::PredicateKind::NormalizesTo(_) => CurrentGoalKind::NormalizesTo,
72            _ => CurrentGoalKind::Misc,
73        }
74    }
75}
76
77pub struct EvalCtxt<'a, D, I = <D as SolverDelegate>::Interner>
78where
79    D: SolverDelegate<Interner = I>,
80    I: Interner,
81{
82    /// The inference context that backs (mostly) inference and placeholder terms
83    /// instantiated while solving goals.
84    ///
85    /// NOTE: The `InferCtxt` that backs the `EvalCtxt` is intentionally private,
86    /// because the `InferCtxt` is much more general than `EvalCtxt`. Methods such
87    /// as  `take_registered_region_obligations` can mess up query responses,
88    /// using `At::normalize` is totally wrong, calling `evaluate_root_goal` can
89    /// cause coinductive unsoundness, etc.
90    ///
91    /// Methods that are generally of use for trait solving are *intentionally*
92    /// re-declared through the `EvalCtxt` below, often with cleaner signatures
93    /// since we don't care about things like `ObligationCause`s and `Span`s here.
94    /// If some `InferCtxt` method is missing, please first think defensively about
95    /// the method's compatibility with this solver, or if an existing one does
96    /// the job already.
97    delegate: &'a D,
98
99    /// The variable info for the `var_values`, only used to make an ambiguous response
100    /// with no constraints.
101    var_kinds: I::CanonicalVarKinds,
102
103    /// What kind of goal we're currently computing, see the enum definition
104    /// for more info.
105    current_goal_kind: CurrentGoalKind,
106    pub(super) var_values: CanonicalVarValues<I>,
107
108    /// The highest universe index nameable by the caller.
109    ///
110    /// When we enter a new binder inside of the query we create new universes
111    /// which the caller cannot name. We have to be careful with variables from
112    /// these new universes when creating the query response.
113    ///
114    /// Both because these new universes can prevent us from reaching a fixpoint
115    /// if we have a coinductive cycle and because that's the only way we can return
116    /// new placeholders to the caller.
117    pub(super) max_input_universe: ty::UniverseIndex,
118    /// The opaque types from the canonical input. We only need to return opaque types
119    /// which have been added to the storage while evaluating this goal.
120    pub(super) initial_opaque_types_storage_num_entries:
121        <D::Infcx as InferCtxtLike>::OpaqueTypeStorageEntries,
122
123    pub(super) search_graph: &'a mut SearchGraph<D>,
124
125    nested_goals: Vec<(GoalSource, Goal<I, I::Predicate>, Option<GoalStalledOn<I>>)>,
126
127    pub(super) origin_span: I::Span,
128
129    // Has this `EvalCtxt` errored out with `NoSolution` in `try_evaluate_added_goals`?
130    //
131    // If so, then it can no longer be used to make a canonical query response,
132    // since subsequent calls to `try_evaluate_added_goals` have possibly dropped
133    // ambiguous goals. Instead, a probe needs to be introduced somewhere in the
134    // evaluation code.
135    tainted: Result<(), NoSolution>,
136
137    pub(super) inspect: inspect::EvaluationStepBuilder<D>,
138}
139
140#[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)]
141#[cfg_attr(feature = "nightly", derive(const _: () =
    {
        impl ::rustc_data_structures::stable_hasher::StableHash for
            GenerateProofTree {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hasher::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
                match *self {
                    GenerateProofTree::Yes => {}
                    GenerateProofTree::No => {}
                }
            }
        }
    };StableHash))]
142pub enum GenerateProofTree {
143    Yes,
144    No,
145}
146
147pub trait SolverDelegateEvalExt: SolverDelegate {
148    /// Evaluates a goal from **outside** of the trait solver.
149    ///
150    /// Using this while inside of the solver is wrong as it uses a new
151    /// search graph which would break cycle detection.
152    fn evaluate_root_goal(
153        &self,
154        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
155        span: <Self::Interner as Interner>::Span,
156        stalled_on: Option<GoalStalledOn<Self::Interner>>,
157    ) -> Result<GoalEvaluation<Self::Interner>, NoSolution>;
158
159    /// Checks whether evaluating `goal` may hold while treating not-yet-defined
160    /// opaque types as being kind of rigid.
161    ///
162    /// See the comment on [OpaqueTypesJank] for more details.
163    fn root_goal_may_hold_opaque_types_jank(
164        &self,
165        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
166    ) -> bool;
167
168    /// Check whether evaluating `goal` with a depth of `root_depth` may
169    /// succeed. This only returns `false` if the goal is guaranteed to
170    /// not hold. In case evaluation overflows and fails with ambiguity this
171    /// returns `true`.
172    ///
173    /// This is only intended to be used as a performance optimization
174    /// in coherence checking.
175    fn root_goal_may_hold_with_depth(
176        &self,
177        root_depth: usize,
178        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
179    ) -> bool;
180
181    // FIXME: This is only exposed because we need to use it in `analyse.rs`
182    // which is not yet uplifted. Once that's done, we should remove this.
183    fn evaluate_root_goal_for_proof_tree(
184        &self,
185        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
186        span: <Self::Interner as Interner>::Span,
187    ) -> (
188        Result<NestedNormalizationGoals<Self::Interner>, NoSolution>,
189        inspect::GoalEvaluation<Self::Interner>,
190    );
191}
192
193impl<D, I> SolverDelegateEvalExt for D
194where
195    D: SolverDelegate<Interner = I>,
196    I: Interner,
197{
198    x;#[instrument(level = "debug", skip(self), ret)]
199    fn evaluate_root_goal(
200        &self,
201        goal: Goal<I, I::Predicate>,
202        span: I::Span,
203        stalled_on: Option<GoalStalledOn<I>>,
204    ) -> Result<GoalEvaluation<I>, NoSolution> {
205        EvalCtxt::enter_root(self, self.cx().recursion_limit(), span, |ecx| {
206            ecx.evaluate_goal(GoalSource::Misc, goal, stalled_on)
207        })
208    }
209
210    x;#[instrument(level = "debug", skip(self), ret)]
211    fn root_goal_may_hold_opaque_types_jank(
212        &self,
213        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
214    ) -> bool {
215        self.probe(|| {
216            EvalCtxt::enter_root(self, self.cx().recursion_limit(), I::Span::dummy(), |ecx| {
217                ecx.evaluate_goal(GoalSource::Misc, goal, None)
218            })
219            .is_ok_and(|r| match r.certainty {
220                Certainty::Yes => true,
221                Certainty::Maybe(MaybeInfo {
222                    cause: _,
223                    opaque_types_jank,
224                    stalled_on_coroutines: _,
225                }) => match opaque_types_jank {
226                    OpaqueTypesJank::AllGood => true,
227                    OpaqueTypesJank::ErrorIfRigidSelfTy => false,
228                },
229            })
230        })
231    }
232
233    fn root_goal_may_hold_with_depth(
234        &self,
235        root_depth: usize,
236        goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
237    ) -> bool {
238        self.probe(|| {
239            EvalCtxt::enter_root(self, root_depth, I::Span::dummy(), |ecx| {
240                ecx.evaluate_goal(GoalSource::Misc, goal, None)
241            })
242        })
243        .is_ok()
244    }
245
246    #[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(246u32),
                                    ::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))]
247    fn evaluate_root_goal_for_proof_tree(
248        &self,
249        goal: Goal<I, I::Predicate>,
250        span: I::Span,
251    ) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
252        evaluate_root_goal_for_proof_tree(self, goal, span)
253    }
254}
255
256impl<'a, D, I> EvalCtxt<'a, D>
257where
258    D: SolverDelegate<Interner = I>,
259    I: Interner,
260{
261    pub(super) fn typing_mode(&self) -> TypingMode<I> {
262        self.delegate.typing_mode()
263    }
264
265    /// Computes the `PathKind` for the step from the current goal to the
266    /// nested goal required due to `source`.
267    ///
268    /// See #136824 for a more detailed reasoning for this behavior. We
269    /// consider cycles to be coinductive if they 'step into' a where-clause
270    /// of a coinductive trait. We will likely extend this function in the future
271    /// and will need to clearly document it in the rustc-dev-guide before
272    /// stabilization.
273    pub(super) fn step_kind_for_source(&self, source: GoalSource) -> PathKind {
274        match source {
275            // We treat these goals as unknown for now. It is likely that most miscellaneous
276            // nested goals will be converted to an inductive variant in the future.
277            //
278            // Having unknown cycles is always the safer option, as changing that to either
279            // succeed or hard error is backwards compatible. If we incorrectly treat a cycle
280            // as inductive even though it should not be, it may be unsound during coherence and
281            // fixing it may cause inference breakage or introduce ambiguity.
282            GoalSource::Misc => PathKind::Unknown,
283            GoalSource::NormalizeGoal(path_kind) => path_kind,
284            GoalSource::ImplWhereBound => match self.current_goal_kind {
285                // We currently only consider a cycle coinductive if it steps
286                // into a where-clause of a coinductive trait.
287                CurrentGoalKind::CoinductiveTrait => PathKind::Coinductive,
288                // While normalizing via an impl does step into a where-clause of
289                // an impl, accessing the associated item immediately steps out of
290                // it again. This means cycles/recursive calls are not guarded
291                // by impls used for normalization.
292                //
293                // See tests/ui/traits/next-solver/cycles/normalizes-to-is-not-productive.rs
294                // for how this can go wrong.
295                CurrentGoalKind::NormalizesTo => PathKind::Inductive,
296                // We probably want to make all traits coinductive in the future,
297                // so we treat cycles involving where-clauses of not-yet coinductive
298                // traits as ambiguous for now.
299                CurrentGoalKind::Misc => PathKind::Unknown,
300            },
301            // Relating types is always unproductive. If we were to map proof trees to
302            // corecursive functions as explained in #136824, relating types never
303            // introduces a constructor which could cause the recursion to be guarded.
304            GoalSource::TypeRelating => PathKind::Inductive,
305            // These goal sources are likely unproductive and can be changed to
306            // `PathKind::Inductive`. Keeping them as unknown until we're confident
307            // about this and have an example where it is necessary.
308            GoalSource::AliasBoundConstCondition | GoalSource::AliasWellFormed => PathKind::Unknown,
309        }
310    }
311
312    /// Creates a root evaluation context and search graph. This should only be
313    /// used from outside of any evaluation, and other methods should be preferred
314    /// over using this manually (such as [`SolverDelegateEvalExt::evaluate_root_goal`]).
315    pub(super) fn enter_root<R>(
316        delegate: &D,
317        root_depth: usize,
318        origin_span: I::Span,
319        f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
320    ) -> R {
321        let mut search_graph = SearchGraph::new(root_depth);
322
323        let mut ecx = EvalCtxt {
324            delegate,
325            search_graph: &mut search_graph,
326            nested_goals: Default::default(),
327            inspect: inspect::EvaluationStepBuilder::new_noop(),
328
329            // Only relevant when canonicalizing the response,
330            // which we don't do within this evaluation context.
331            max_input_universe: ty::UniverseIndex::ROOT,
332            initial_opaque_types_storage_num_entries: Default::default(),
333            var_kinds: Default::default(),
334            var_values: CanonicalVarValues::dummy(),
335            current_goal_kind: CurrentGoalKind::Misc,
336            origin_span,
337            tainted: Ok(()),
338        };
339        let result = f(&mut ecx);
340        if !ecx.nested_goals.is_empty() {
    {
        ::core::panicking::panic_fmt(format_args!("root `EvalCtxt` should not have any goals added to it"));
    }
};assert!(
341            ecx.nested_goals.is_empty(),
342            "root `EvalCtxt` should not have any goals added to it"
343        );
344        if !search_graph.is_empty() {
    ::core::panicking::panic("assertion failed: search_graph.is_empty()")
};assert!(search_graph.is_empty());
345        result
346    }
347
348    /// Creates a nested evaluation context that shares the same search graph as the
349    /// one passed in. This is suitable for evaluation, granted that the search graph
350    /// has had the nested goal recorded on its stack. This method only be used by
351    /// `search_graph::Delegate::compute_goal`.
352    ///
353    /// This function takes care of setting up the inference context, setting the anchor,
354    /// and registering opaques from the canonicalized input.
355    pub(super) fn enter_canonical<R>(
356        cx: I,
357        search_graph: &'a mut SearchGraph<D>,
358        canonical_input: CanonicalInput<I>,
359        proof_tree_builder: &mut inspect::ProofTreeBuilder<D>,
360        f: impl FnOnce(&mut EvalCtxt<'_, D>, Goal<I, I::Predicate>) -> R,
361    ) -> R {
362        let (ref delegate, input, var_values) = D::build_with_canonical(cx, &canonical_input);
363        for (key, ty) in input.predefined_opaques_in_body.iter() {
364            let prev = delegate.register_hidden_type_in_storage(key, ty, I::Span::dummy());
365            // It may be possible that two entries in the opaque type storage end up
366            // with the same key after resolving contained inference variables.
367            //
368            // We could put them in the duplicate list but don't have to. The opaques we
369            // encounter here are already tracked in the caller, so there's no need to
370            // also store them here. We'd take them out when computing the query response
371            // and then discard them, as they're already present in the input.
372            //
373            // Ideally we'd drop duplicate opaque type definitions when computing
374            // the canonical input. This is more annoying to implement and may cause a
375            // perf regression, so we do it inside of the query for now.
376            if let Some(prev) = prev {
377                {
    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:377",
                        "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(377u32),
                        ::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`");
378            }
379        }
380
381        let initial_opaque_types_storage_num_entries = delegate.opaque_types_storage_num_entries();
382        let mut ecx = EvalCtxt {
383            delegate,
384            var_kinds: canonical_input.canonical.var_kinds,
385            var_values,
386            current_goal_kind: CurrentGoalKind::from_query_input(cx, input),
387            max_input_universe: canonical_input.canonical.max_universe,
388            initial_opaque_types_storage_num_entries,
389            search_graph,
390            nested_goals: Default::default(),
391            origin_span: I::Span::dummy(),
392            tainted: Ok(()),
393            inspect: proof_tree_builder.new_evaluation_step(var_values),
394        };
395
396        let result = f(&mut ecx, input.goal);
397        ecx.inspect.probe_final_state(ecx.delegate, ecx.max_input_universe);
398        proof_tree_builder.finish_evaluation_step(ecx.inspect);
399
400        // When creating a query response we clone the opaque type constraints
401        // instead of taking them. This would cause an ICE here, since we have
402        // assertions against dropping an `InferCtxt` without taking opaques.
403        // FIXME: Once we remove support for the old impl we can remove this.
404        // FIXME: Could we make `build_with_canonical` into `enter_with_canonical` and call this at the end?
405        delegate.reset_opaque_types();
406
407        result
408    }
409
410    pub(super) fn ignore_candidate_head_usages(&mut self, usages: CandidateHeadUsages) {
411        self.search_graph.ignore_candidate_head_usages(usages);
412    }
413
414    /// Recursively evaluates `goal`, returning whether any inference vars have
415    /// been constrained and the certainty of the result.
416    fn evaluate_goal(
417        &mut self,
418        source: GoalSource,
419        goal: Goal<I, I::Predicate>,
420        stalled_on: Option<GoalStalledOn<I>>,
421    ) -> Result<GoalEvaluation<I>, NoSolution> {
422        let (normalization_nested_goals, goal_evaluation) =
423            self.evaluate_goal_raw(source, goal, stalled_on)?;
424        if !normalization_nested_goals.is_empty() {
    ::core::panicking::panic("assertion failed: normalization_nested_goals.is_empty()")
};assert!(normalization_nested_goals.is_empty());
425        Ok(goal_evaluation)
426    }
427
428    /// Recursively evaluates `goal`, returning the nested goals in case
429    /// the nested goal is a `NormalizesTo` goal.
430    ///
431    /// As all other goal kinds do not return any nested goals and
432    /// `NormalizesTo` is only used by `AliasRelate`, all other callsites
433    /// should use [`EvalCtxt::evaluate_goal`] which discards that empty
434    /// storage.
435    pub(super) fn evaluate_goal_raw(
436        &mut self,
437        source: GoalSource,
438        goal: Goal<I, I::Predicate>,
439        stalled_on: Option<GoalStalledOn<I>>,
440    ) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolution> {
441        // If we have run this goal before, and it was stalled, check that any of the goal's
442        // args have changed. Otherwise, we don't need to re-run the goal because it'll remain
443        // stalled, since it'll canonicalize the same way and evaluation is pure.
444        if let Some(GoalStalledOn {
445            num_opaques,
446            ref stalled_vars,
447            ref sub_roots,
448            stalled_certainty,
449        }) = stalled_on
450            && !stalled_vars.iter().any(|value| self.delegate.is_changed_arg(*value))
451            && !sub_roots
452                .iter()
453                .any(|&vid| self.delegate.sub_unification_table_root_var(vid) != vid)
454            && !self.delegate.opaque_types_storage_num_entries().needs_reevaluation(num_opaques)
455        {
456            return Ok((
457                NestedNormalizationGoals::empty(),
458                GoalEvaluation {
459                    goal,
460                    certainty: stalled_certainty,
461                    has_changed: HasChanged::No,
462                    stalled_on,
463                },
464            ));
465        }
466
467        // We only care about one entry per `OpaqueTypeKey` here,
468        // so we only canonicalize the lookup table and ignore
469        // duplicate entries.
470        let opaque_types = self.delegate.clone_opaque_types_lookup_table();
471        let (goal, opaque_types) = eager_resolve_vars(self.delegate, (goal, opaque_types));
472
473        let (orig_values, canonical_goal) = canonicalize_goal(self.delegate, goal, &opaque_types);
474        let canonical_result = self.search_graph.evaluate_goal(
475            self.cx(),
476            canonical_goal,
477            self.step_kind_for_source(source),
478            &mut inspect::ProofTreeBuilder::new_noop(),
479        );
480        let response = match canonical_result {
481            Err(e) => return Err(e),
482            Ok(response) => response,
483        };
484
485        let has_changed =
486            if !has_only_region_constraints(response) { HasChanged::Yes } else { HasChanged::No };
487
488        // FIXME: We should revisit and consider removing this after
489        // *assumptions on binders* is available, like once we had done in the
490        // stabilization of `-Znext-solver=coherence`(#121848).
491        // We ignore constraints from the nested goals in leak check. This is to match
492        // with the old solver's behavior, which has separated evaluation and fulfillment,
493        // and the former doesn't consider outlives obligations from the later.
494        let vis = match goal.predicate.kind().skip_binder() {
495            ty::PredicateKind::Clause(_)
496            | ty::PredicateKind::DynCompatible(_)
497            | ty::PredicateKind::Subtype(_)
498            | ty::PredicateKind::Coerce(_)
499            | ty::PredicateKind::ConstEquate(_, _)
500            | ty::PredicateKind::Ambiguous
501            | ty::PredicateKind::NormalizesTo(_) => VisibleForLeakCheck::No,
502            ty::PredicateKind::AliasRelate(_, _, _) => VisibleForLeakCheck::Yes,
503        };
504
505        let (normalization_nested_goals, certainty) = instantiate_and_apply_query_response(
506            self.delegate,
507            goal.param_env,
508            &orig_values,
509            response,
510            vis,
511            self.origin_span,
512        );
513
514        // FIXME: We previously had an assert here that checked that recomputing
515        // a goal after applying its constraints did not change its response.
516        //
517        // This assert was removed as it did not hold for goals constraining
518        // an inference variable to a recursive alias, e.g. in
519        // tests/ui/traits/next-solver/overflow/recursive-self-normalization.rs.
520        //
521        // Once we have decided on how to handle trait-system-refactor-initiative#75,
522        // we should re-add an assert here.
523
524        let stalled_on = match certainty {
525            Certainty::Yes => None,
526            Certainty::Maybe { .. } => match has_changed {
527                // FIXME: We could recompute a *new* set of stalled variables by walking
528                // through the orig values, resolving, and computing the root vars of anything
529                // that is not resolved. Only when *these* have changed is it meaningful
530                // to recompute this goal.
531                HasChanged::Yes => None,
532                HasChanged::No => {
533                    let mut stalled_vars = orig_values;
534
535                    // Remove the unconstrained RHS arg, which is expected to have changed.
536                    if let Some(normalizes_to) = goal.predicate.as_normalizes_to() {
537                        let normalizes_to = normalizes_to.skip_binder();
538                        let rhs_arg: I::GenericArg = normalizes_to.term.into();
539                        let idx = stalled_vars
540                            .iter()
541                            .rposition(|arg| *arg == rhs_arg)
542                            .expect("expected unconstrained arg");
543                        stalled_vars.swap_remove(idx);
544                    }
545
546                    // Remove the canonicalized universal vars, since we only care about stalled existentials.
547                    let mut sub_roots = Vec::new();
548                    stalled_vars.retain(|arg| match arg.kind() {
549                        // Lifetimes can never stall goals.
550                        ty::GenericArgKind::Lifetime(_) => false,
551                        ty::GenericArgKind::Type(ty) => match ty.kind() {
552                            ty::Infer(ty::TyVar(vid)) => {
553                                sub_roots.push(self.delegate.sub_unification_table_root_var(vid));
554                                true
555                            }
556                            ty::Infer(_) => true,
557                            ty::Param(_) | ty::Placeholder(_) => false,
558                            _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("unexpected orig_value: {0:?}", ty)));
}unreachable!("unexpected orig_value: {ty:?}"),
559                        },
560                        ty::GenericArgKind::Const(ct) => match ct.kind() {
561                            ty::ConstKind::Infer(_) => true,
562                            ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => false,
563                            _ => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("unexpected orig_value: {0:?}", ct)));
}unreachable!("unexpected orig_value: {ct:?}"),
564                        },
565                    });
566
567                    Some(GoalStalledOn {
568                        num_opaques: canonical_goal
569                            .canonical
570                            .value
571                            .predefined_opaques_in_body
572                            .len(),
573                        stalled_vars,
574                        sub_roots,
575                        stalled_certainty: certainty,
576                    })
577                }
578            },
579        };
580
581        Ok((
582            normalization_nested_goals,
583            GoalEvaluation { goal, certainty, has_changed, stalled_on },
584        ))
585    }
586
587    pub(super) fn compute_goal(&mut self, goal: Goal<I, I::Predicate>) -> QueryResult<I> {
588        let Goal { param_env, predicate } = goal;
589        let kind = predicate.kind();
590        self.enter_forall(kind, |ecx, kind| match kind {
591            ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
592                ecx.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
593            }
594            ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
595                ecx.compute_host_effect_goal(Goal { param_env, predicate })
596            }
597            ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
598                ecx.compute_projection_goal(Goal { param_env, predicate })
599            }
600            ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
601                ecx.compute_type_outlives_goal(Goal { param_env, predicate })
602            }
603            ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
604                ecx.compute_region_outlives_goal(Goal { param_env, predicate })
605            }
606            ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
607                ecx.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
608            }
609            ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
610                ecx.compute_unstable_feature_goal(param_env, symbol)
611            }
612            ty::PredicateKind::Subtype(predicate) => {
613                ecx.compute_subtype_goal(Goal { param_env, predicate })
614            }
615            ty::PredicateKind::Coerce(predicate) => {
616                ecx.compute_coerce_goal(Goal { param_env, predicate })
617            }
618            ty::PredicateKind::DynCompatible(trait_def_id) => {
619                ecx.compute_dyn_compatible_goal(trait_def_id)
620            }
621            ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
622                ecx.compute_well_formed_goal(Goal { param_env, predicate: term })
623            }
624            ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
625                ecx.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
626            }
627            ty::PredicateKind::ConstEquate(_, _) => {
628                {
    ::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")
629            }
630            ty::PredicateKind::NormalizesTo(predicate) => {
631                ecx.compute_normalizes_to_goal(Goal { param_env, predicate })
632            }
633            ty::PredicateKind::AliasRelate(lhs, rhs, direction) => {
634                ecx.compute_alias_relate_goal(Goal { param_env, predicate: (lhs, rhs, direction) })
635            }
636            ty::PredicateKind::Ambiguous => {
637                ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
638            }
639        })
640    }
641
642    // Recursively evaluates all the goals added to this `EvalCtxt` to completion, returning
643    // the certainty of all the goals.
644    #[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(644u32),
                                    ::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, NoSolution> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            for _ in 0..FIXPOINT_STEP_LIMIT {
                match self.evaluate_added_goals_step() {
                    Ok(None) => {}
                    Ok(Some(cert)) => return Ok(cert),
                    Err(NoSolution) => {
                        self.tainted = Err(NoSolution);
                        return Err(NoSolution);
                    }
                }
            }
            {
                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:657",
                                    "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(657u32),
                                    ::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))]
645    pub(super) fn try_evaluate_added_goals(&mut self) -> Result<Certainty, NoSolution> {
646        for _ in 0..FIXPOINT_STEP_LIMIT {
647            match self.evaluate_added_goals_step() {
648                Ok(None) => {}
649                Ok(Some(cert)) => return Ok(cert),
650                Err(NoSolution) => {
651                    self.tainted = Err(NoSolution);
652                    return Err(NoSolution);
653                }
654            }
655        }
656
657        debug!("try_evaluate_added_goals: encountered overflow");
658        Ok(Certainty::overflow(false))
659    }
660
661    /// Iterate over all added goals: returning `Ok(Some(_))` in case we can stop rerunning.
662    ///
663    /// Goals for the next step get directly added to the nested goals of the `EvalCtxt`.
664    fn evaluate_added_goals_step(&mut self) -> Result<Option<Certainty>, NoSolution> {
665        let cx = self.cx();
666        // If this loop did not result in any progress, what's our final certainty.
667        let mut unchanged_certainty = Some(Certainty::Yes);
668        for (source, goal, stalled_on) in mem::take(&mut self.nested_goals) {
669            if let Some(certainty) = self.delegate.compute_goal_fast_path(goal, self.origin_span) {
670                match certainty {
671                    Certainty::Yes => {}
672                    Certainty::Maybe { .. } => {
673                        self.nested_goals.push((source, goal, None));
674                        unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
675                    }
676                }
677                continue;
678            }
679
680            // We treat normalizes-to goals specially here. In each iteration we take the
681            // RHS of the projection, replace it with a fresh inference variable, and only
682            // after evaluating that goal do we equate the fresh inference variable with the
683            // actual RHS of the predicate.
684            //
685            // This is both to improve caching, and to avoid using the RHS of the
686            // projection predicate to influence the normalizes-to candidate we select.
687            //
688            // Forgetting to replace the RHS with a fresh inference variable when we evaluate
689            // this goal results in an ICE.
690            if let Some(pred) = goal.predicate.as_normalizes_to() {
691                // We should never encounter higher-ranked normalizes-to goals.
692                let pred = pred.no_bound_vars().unwrap();
693                // Replace the goal with an unconstrained infer var, so the
694                // RHS does not affect projection candidate assembly.
695                let unconstrained_rhs = self.next_term_infer_of_kind(pred.term);
696                let unconstrained_goal =
697                    goal.with(cx, ty::NormalizesTo { alias: pred.alias, term: unconstrained_rhs });
698
699                let (
700                    NestedNormalizationGoals(nested_goals),
701                    GoalEvaluation { goal, certainty, stalled_on, has_changed: _ },
702                ) = self.evaluate_goal_raw(source, unconstrained_goal, stalled_on)?;
703                // Add the nested goals from normalization to our own nested goals.
704                {
    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:704",
                        "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(704u32),
                        ::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);
705                self.nested_goals.extend(nested_goals.into_iter().map(|(s, g)| (s, g, None)));
706
707                // Finally, equate the goal's RHS with the unconstrained var.
708                //
709                // SUBTLE:
710                // We structurally relate aliases here. This is necessary
711                // as we otherwise emit a nested `AliasRelate` goal in case the
712                // returned term is a rigid alias, resulting in overflow.
713                //
714                // It is correct as both `goal.predicate.term` and `unconstrained_rhs`
715                // start out as an unconstrained inference variable so any aliases get
716                // fully normalized when instantiating it.
717                //
718                // FIXME: Strictly speaking this may be incomplete if the normalized-to
719                // type contains an ambiguous alias referencing bound regions. We should
720                // consider changing this to only use "shallow structural equality".
721                self.eq_structurally_relating_aliases(
722                    goal.param_env,
723                    pred.term,
724                    unconstrained_rhs,
725                )?;
726
727                // We only look at the `projection_ty` part here rather than
728                // looking at the "has changed" return from evaluate_goal,
729                // because we expect the `unconstrained_rhs` part of the predicate
730                // to have changed -- that means we actually normalized successfully!
731                // FIXME: Do we need to eagerly resolve here? Or should we check
732                // if the cache key has any changed vars?
733                let with_resolved_vars = self.resolve_vars_if_possible(goal);
734                if pred.alias
735                    != with_resolved_vars
736                        .predicate
737                        .as_normalizes_to()
738                        .unwrap()
739                        .no_bound_vars()
740                        .unwrap()
741                        .alias
742                {
743                    unchanged_certainty = None;
744                }
745
746                match certainty {
747                    Certainty::Yes => {}
748                    Certainty::Maybe { .. } => {
749                        self.nested_goals.push((source, with_resolved_vars, stalled_on));
750                        unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
751                    }
752                }
753            } else {
754                let GoalEvaluation { goal, certainty, has_changed, stalled_on } =
755                    self.evaluate_goal(source, goal, stalled_on)?;
756                if has_changed == HasChanged::Yes {
757                    unchanged_certainty = None;
758                }
759
760                match certainty {
761                    Certainty::Yes => {}
762                    Certainty::Maybe { .. } => {
763                        self.nested_goals.push((source, goal, stalled_on));
764                        unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
765                    }
766                }
767            }
768        }
769
770        Ok(unchanged_certainty)
771    }
772
773    /// Record impl args in the proof tree for later access by `InspectCandidate`.
774    pub(crate) fn record_impl_args(&mut self, impl_args: I::GenericArgs) {
775        self.inspect.record_impl_args(self.delegate, self.max_input_universe, impl_args)
776    }
777
778    pub(super) fn cx(&self) -> I {
779        self.delegate.cx()
780    }
781
782    #[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(782u32),
                                    ::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))]
783    pub(super) fn add_goal(&mut self, source: GoalSource, mut goal: Goal<I, I::Predicate>) {
784        goal.predicate =
785            goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(self, source, goal.param_env));
786        self.inspect.add_goal(self.delegate, self.max_input_universe, source, goal);
787        self.nested_goals.push((source, goal, None));
788    }
789
790    #[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(790u32),
                                    ::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))]
791    pub(super) fn add_goals(
792        &mut self,
793        source: GoalSource,
794        goals: impl IntoIterator<Item = Goal<I, I::Predicate>>,
795    ) {
796        for goal in goals {
797            self.add_goal(source, goal);
798        }
799    }
800
801    pub(super) fn next_region_var(&mut self) -> I::Region {
802        let region = self.delegate.next_region_infer();
803        self.inspect.add_var_value(region);
804        region
805    }
806
807    pub(super) fn next_ty_infer(&mut self) -> I::Ty {
808        let ty = self.delegate.next_ty_infer();
809        self.inspect.add_var_value(ty);
810        ty
811    }
812
813    pub(super) fn next_const_infer(&mut self) -> I::Const {
814        let ct = self.delegate.next_const_infer();
815        self.inspect.add_var_value(ct);
816        ct
817    }
818
819    /// Returns a ty infer or a const infer depending on whether `kind` is a `Ty` or `Const`.
820    /// If `kind` is an integer inference variable this will still return a ty infer var.
821    pub(super) fn next_term_infer_of_kind(&mut self, term: I::Term) -> I::Term {
822        match term.kind() {
823            ty::TermKind::Ty(_) => self.next_ty_infer().into(),
824            ty::TermKind::Const(_) => self.next_const_infer().into(),
825        }
826    }
827
828    /// Is the projection predicate is of the form `exists<T> <Ty as Trait>::Assoc = T`.
829    ///
830    /// This is the case if the `term` does not occur in any other part of the predicate
831    /// and is able to name all other placeholder and inference variables.
832    x;#[instrument(level = "trace", skip(self), ret)]
833    pub(super) fn term_is_fully_unconstrained(&self, goal: Goal<I, ty::NormalizesTo<I>>) -> bool {
834        let universe_of_term = match goal.predicate.term.kind() {
835            ty::TermKind::Ty(ty) => {
836                if let ty::Infer(ty::TyVar(vid)) = ty.kind() {
837                    self.delegate.universe_of_ty(vid).unwrap()
838                } else {
839                    return false;
840                }
841            }
842            ty::TermKind::Const(ct) => {
843                if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
844                    self.delegate.universe_of_ct(vid).unwrap()
845                } else {
846                    return false;
847                }
848            }
849        };
850
851        struct ContainsTermOrNotNameable<'a, D: SolverDelegate<Interner = I>, I: Interner> {
852            term: I::Term,
853            universe_of_term: ty::UniverseIndex,
854            delegate: &'a D,
855            cache: HashSet<I::Ty>,
856        }
857
858        impl<D: SolverDelegate<Interner = I>, I: Interner> ContainsTermOrNotNameable<'_, D, I> {
859            fn check_nameable(&self, universe: ty::UniverseIndex) -> ControlFlow<()> {
860                if self.universe_of_term.can_name(universe) {
861                    ControlFlow::Continue(())
862                } else {
863                    ControlFlow::Break(())
864                }
865            }
866        }
867
868        impl<D: SolverDelegate<Interner = I>, I: Interner> TypeVisitor<I>
869            for ContainsTermOrNotNameable<'_, D, I>
870        {
871            type Result = ControlFlow<()>;
872            fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
873                if self.cache.contains(&t) {
874                    return ControlFlow::Continue(());
875                }
876
877                match t.kind() {
878                    ty::Infer(ty::TyVar(vid)) => {
879                        if let ty::TermKind::Ty(term) = self.term.kind()
880                            && let ty::Infer(ty::TyVar(term_vid)) = term.kind()
881                            && self.delegate.root_ty_var(vid) == self.delegate.root_ty_var(term_vid)
882                        {
883                            return ControlFlow::Break(());
884                        }
885
886                        self.check_nameable(self.delegate.universe_of_ty(vid).unwrap())?;
887                    }
888                    ty::Placeholder(p) => self.check_nameable(p.universe())?,
889                    _ => {
890                        if t.has_non_region_infer() || t.has_placeholders() {
891                            t.super_visit_with(self)?
892                        }
893                    }
894                }
895
896                assert!(self.cache.insert(t));
897                ControlFlow::Continue(())
898            }
899
900            fn visit_const(&mut self, c: I::Const) -> Self::Result {
901                match c.kind() {
902                    ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
903                        if let ty::TermKind::Const(term) = self.term.kind()
904                            && let ty::ConstKind::Infer(ty::InferConst::Var(term_vid)) = term.kind()
905                            && self.delegate.root_const_var(vid)
906                                == self.delegate.root_const_var(term_vid)
907                        {
908                            return ControlFlow::Break(());
909                        }
910
911                        self.check_nameable(self.delegate.universe_of_ct(vid).unwrap())
912                    }
913                    ty::ConstKind::Placeholder(p) => self.check_nameable(p.universe()),
914                    _ => {
915                        if c.has_non_region_infer() || c.has_placeholders() {
916                            c.super_visit_with(self)
917                        } else {
918                            ControlFlow::Continue(())
919                        }
920                    }
921                }
922            }
923
924            fn visit_predicate(&mut self, p: I::Predicate) -> Self::Result {
925                if p.has_non_region_infer() || p.has_placeholders() {
926                    p.super_visit_with(self)
927                } else {
928                    ControlFlow::Continue(())
929                }
930            }
931
932            fn visit_clauses(&mut self, c: I::Clauses) -> Self::Result {
933                if c.has_non_region_infer() || c.has_placeholders() {
934                    c.super_visit_with(self)
935                } else {
936                    ControlFlow::Continue(())
937                }
938            }
939        }
940
941        let mut visitor = ContainsTermOrNotNameable {
942            delegate: self.delegate,
943            universe_of_term,
944            term: goal.predicate.term,
945            cache: Default::default(),
946        };
947        goal.predicate.alias.visit_with(&mut visitor).is_continue()
948            && goal.param_env.visit_with(&mut visitor).is_continue()
949    }
950
951    pub(super) fn sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
952        self.delegate.sub_unify_ty_vids_raw(a, b)
953    }
954
955    x;#[instrument(level = "trace", skip(self, param_env), ret)]
956    pub(super) fn eq<T: Relate<I>>(
957        &mut self,
958        param_env: I::ParamEnv,
959        lhs: T,
960        rhs: T,
961    ) -> Result<(), NoSolution> {
962        self.relate(param_env, lhs, ty::Variance::Invariant, rhs)
963    }
964
965    /// This should be used when relating a rigid alias with another type.
966    ///
967    /// Normally we emit a nested `AliasRelate` when equating an inference
968    /// variable and an alias. This causes us to instead constrain the inference
969    /// variable to the alias without emitting a nested alias relate goals.
970    x;#[instrument(level = "trace", skip(self, param_env), ret)]
971    pub(super) fn relate_rigid_alias_non_alias(
972        &mut self,
973        param_env: I::ParamEnv,
974        alias: ty::AliasTerm<I>,
975        variance: ty::Variance,
976        term: I::Term,
977    ) -> Result<(), NoSolution> {
978        // NOTE: this check is purely an optimization, the structural eq would
979        // always fail if the term is not an inference variable.
980        if term.is_infer() {
981            let cx = self.cx();
982            // We need to relate `alias` to `term` treating only the outermost
983            // constructor as rigid, relating any contained generic arguments as
984            // normal. We do this by first structurally equating the `term`
985            // with the alias constructor instantiated with unconstrained infer vars,
986            // and then relate this with the whole `alias`.
987            //
988            // Alternatively we could modify `Equate` for this case by adding another
989            // variant to `StructurallyRelateAliases`.
990            let identity_args = self.fresh_args_for_item(alias.def_id());
991            let rigid_ctor = alias.with_args(cx, identity_args);
992            let ctor_term = rigid_ctor.to_term(cx);
993            let obligations = self.delegate.eq_structurally_relating_aliases(
994                param_env,
995                term,
996                ctor_term,
997                self.origin_span,
998            )?;
999            debug_assert!(obligations.is_empty());
1000            self.relate(param_env, alias, variance, rigid_ctor)
1001        } else {
1002            Err(NoSolution)
1003        }
1004    }
1005
1006    /// This should only be used when we're either instantiating a previously
1007    /// unconstrained "return value" or when we're sure that all aliases in
1008    /// the types are rigid.
1009    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1010    pub(super) fn eq_structurally_relating_aliases<T: Relate<I>>(
1011        &mut self,
1012        param_env: I::ParamEnv,
1013        lhs: T,
1014        rhs: T,
1015    ) -> Result<(), NoSolution> {
1016        let result = self.delegate.eq_structurally_relating_aliases(
1017            param_env,
1018            lhs,
1019            rhs,
1020            self.origin_span,
1021        )?;
1022        assert_eq!(result, vec![]);
1023        Ok(())
1024    }
1025
1026    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1027    pub(super) fn sub<T: Relate<I>>(
1028        &mut self,
1029        param_env: I::ParamEnv,
1030        sub: T,
1031        sup: T,
1032    ) -> Result<(), NoSolution> {
1033        self.relate(param_env, sub, ty::Variance::Covariant, sup)
1034    }
1035
1036    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1037    pub(super) fn relate<T: Relate<I>>(
1038        &mut self,
1039        param_env: I::ParamEnv,
1040        lhs: T,
1041        variance: ty::Variance,
1042        rhs: T,
1043    ) -> Result<(), NoSolution> {
1044        let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
1045        for &goal in goals.iter() {
1046            let source = match goal.predicate.kind().skip_binder() {
1047                ty::PredicateKind::Subtype { .. } | ty::PredicateKind::AliasRelate(..) => {
1048                    GoalSource::TypeRelating
1049                }
1050                // FIXME(-Znext-solver=coinductive): should these WF goals also be unproductive?
1051                ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => GoalSource::Misc,
1052                p => unreachable!("unexpected nested goal in `relate`: {p:?}"),
1053            };
1054            self.add_goal(source, goal);
1055        }
1056        Ok(())
1057    }
1058
1059    /// Equates two values returning the nested goals without adding them
1060    /// to the nested goals of the `EvalCtxt`.
1061    ///
1062    /// If possible, try using `eq` instead which automatically handles nested
1063    /// goals correctly.
1064    x;#[instrument(level = "trace", skip(self, param_env), ret)]
1065    pub(super) fn eq_and_get_goals<T: Relate<I>>(
1066        &self,
1067        param_env: I::ParamEnv,
1068        lhs: T,
1069        rhs: T,
1070    ) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
1071        Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs, self.origin_span)?)
1072    }
1073
1074    pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(
1075        &self,
1076        value: ty::Binder<I, T>,
1077    ) -> T {
1078        self.delegate.instantiate_binder_with_infer(value)
1079    }
1080
1081    /// `enter_forall`, but takes `&mut self` and passes it back through the
1082    /// callback since it can't be aliased during the call.
1083    pub(super) fn enter_forall<T: TypeFoldable<I>, U>(
1084        &mut self,
1085        value: ty::Binder<I, T>,
1086        f: impl FnOnce(&mut Self, T) -> U,
1087    ) -> U {
1088        self.delegate.enter_forall(value, |value| f(self, value))
1089    }
1090
1091    pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
1092    where
1093        T: TypeFoldable<I>,
1094    {
1095        self.delegate.resolve_vars_if_possible(value)
1096    }
1097
1098    pub(super) fn shallow_resolve(&self, ty: I::Ty) -> I::Ty {
1099        self.delegate.shallow_resolve(ty)
1100    }
1101
1102    pub(super) fn eager_resolve_region(&self, r: I::Region) -> I::Region {
1103        if let ty::ReVar(vid) = r.kind() {
1104            self.delegate.opportunistic_resolve_lt_var(vid)
1105        } else {
1106            r
1107        }
1108    }
1109
1110    pub(super) fn fresh_args_for_item(&mut self, def_id: I::DefId) -> I::GenericArgs {
1111        let args = self.delegate.fresh_args_for_item(def_id);
1112        for arg in args.iter() {
1113            self.inspect.add_var_value(arg);
1114        }
1115        args
1116    }
1117
1118    pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) {
1119        self.delegate.register_ty_outlives(ty, lt, self.origin_span);
1120    }
1121
1122    pub(super) fn register_region_outlives(
1123        &self,
1124        a: I::Region,
1125        b: I::Region,
1126        vis: VisibleForLeakCheck,
1127    ) {
1128        // `'a: 'b` ==> `'b <= 'a`
1129        self.delegate.sub_regions(b, a, vis, self.origin_span);
1130    }
1131
1132    /// Computes the list of goals required for `arg` to be well-formed
1133    pub(super) fn well_formed_goals(
1134        &self,
1135        param_env: I::ParamEnv,
1136        term: I::Term,
1137    ) -> Option<Vec<Goal<I, I::Predicate>>> {
1138        self.delegate.well_formed_goals(param_env, term)
1139    }
1140
1141    pub(super) fn trait_ref_is_knowable(
1142        &mut self,
1143        param_env: I::ParamEnv,
1144        trait_ref: ty::TraitRef<I>,
1145    ) -> Result<bool, NoSolution> {
1146        let delegate = self.delegate;
1147        let lazily_normalize_ty = |ty| self.structurally_normalize_ty(param_env, ty);
1148        coherence::trait_ref_is_knowable(&**delegate, trait_ref, lazily_normalize_ty)
1149            .map(|is_knowable| is_knowable.is_ok())
1150    }
1151
1152    pub(super) fn fetch_eligible_assoc_item(
1153        &self,
1154        goal_trait_ref: ty::TraitRef<I>,
1155        trait_assoc_def_id: I::DefId,
1156        impl_def_id: I::ImplId,
1157    ) -> Result<Option<I::DefId>, I::ErrorGuaranteed> {
1158        self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
1159    }
1160
1161    x;#[instrument(level = "debug", skip(self), ret)]
1162    pub(super) fn register_hidden_type_in_storage(
1163        &mut self,
1164        opaque_type_key: ty::OpaqueTypeKey<I>,
1165        hidden_ty: I::Ty,
1166    ) -> Option<I::Ty> {
1167        self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
1168    }
1169
1170    pub(super) fn add_item_bounds_for_hidden_type(
1171        &mut self,
1172        opaque_def_id: I::DefId,
1173        opaque_args: I::GenericArgs,
1174        param_env: I::ParamEnv,
1175        hidden_ty: I::Ty,
1176    ) {
1177        let mut goals = Vec::new();
1178        self.delegate.add_item_bounds_for_hidden_type(
1179            opaque_def_id,
1180            opaque_args,
1181            param_env,
1182            hidden_ty,
1183            &mut goals,
1184        );
1185        self.add_goals(GoalSource::AliasWellFormed, goals);
1186    }
1187
1188    // Try to evaluate a const, or return `None` if the const is too generic.
1189    // This doesn't mean the const isn't evaluatable, though, and should be treated
1190    // as an ambiguity rather than no-solution.
1191    pub(super) fn evaluate_const(
1192        &self,
1193        param_env: I::ParamEnv,
1194        uv: ty::UnevaluatedConst<I>,
1195    ) -> Option<I::Const> {
1196        self.delegate.evaluate_const(param_env, uv)
1197    }
1198
1199    pub(super) fn is_transmutable(
1200        &mut self,
1201        src: I::Ty,
1202        dst: I::Ty,
1203        assume: I::Const,
1204    ) -> Result<Certainty, NoSolution> {
1205        self.delegate.is_transmutable(dst, src, assume)
1206    }
1207
1208    pub(super) fn replace_bound_vars<T: TypeFoldable<I>>(
1209        &self,
1210        t: T,
1211        universes: &mut Vec<Option<ty::UniverseIndex>>,
1212    ) -> T {
1213        BoundVarReplacer::replace_bound_vars(&**self.delegate, universes, t).0
1214    }
1215
1216    pub(super) fn may_use_unstable_feature(
1217        &self,
1218        param_env: I::ParamEnv,
1219        symbol: I::Symbol,
1220    ) -> bool {
1221        may_use_unstable_feature(&**self.delegate, param_env, symbol)
1222    }
1223
1224    pub(crate) fn opaques_with_sub_unified_hidden_type(
1225        &self,
1226        self_ty: I::Ty,
1227    ) -> Vec<ty::AliasTy<I>> {
1228        if let ty::Infer(ty::TyVar(vid)) = self_ty.kind() {
1229            self.delegate.opaques_with_sub_unified_hidden_type(vid)
1230        } else {
1231            ::alloc::vec::Vec::new()vec![]
1232        }
1233    }
1234
1235    /// To return the constraints of a canonical query to the caller, we canonicalize:
1236    ///
1237    /// - `var_values`: a map from bound variables in the canonical goal to
1238    ///   the values inferred while solving the instantiated goal.
1239    /// - `external_constraints`: additional constraints which aren't expressible
1240    ///   using simple unification of inference variables.
1241    ///
1242    /// This takes the `shallow_certainty` which represents whether we're confident
1243    /// that the final result of the current goal only depends on the nested goals.
1244    ///
1245    /// In case this is `Certainty::Maybe`, there may still be additional nested goals
1246    /// or inference constraints required for this candidate to be hold. The candidate
1247    /// always requires all already added constraints and nested goals.
1248    x;#[instrument(level = "trace", skip(self), ret)]
1249    pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
1250        &mut self,
1251        shallow_certainty: Certainty,
1252    ) -> QueryResult<I> {
1253        self.inspect.make_canonical_response(shallow_certainty);
1254
1255        let goals_certainty = self.try_evaluate_added_goals()?;
1256        assert_eq!(
1257            self.tainted,
1258            Ok(()),
1259            "EvalCtxt is tainted -- nested goals may have been dropped in a \
1260            previous call to `try_evaluate_added_goals!`"
1261        );
1262
1263        // We only check for leaks from universes which were entered inside
1264        // of the query.
1265        self.delegate.leak_check(self.max_input_universe).map_err(|NoSolution| {
1266            trace!("failed the leak check");
1267            NoSolution
1268        })?;
1269
1270        let (certainty, normalization_nested_goals) =
1271            match (self.current_goal_kind, shallow_certainty) {
1272                // When normalizing, we've replaced the expected term with an unconstrained
1273                // inference variable. This means that we dropped information which could
1274                // have been important. We handle this by instead returning the nested goals
1275                // to the caller, where they are then handled. We only do so if we do not
1276                // need to recompute the `NormalizesTo` goal afterwards to avoid repeatedly
1277                // uplifting its nested goals. This is the case if the `shallow_certainty` is
1278                // `Certainty::Yes`.
1279                (CurrentGoalKind::NormalizesTo, Certainty::Yes) => {
1280                    let goals = std::mem::take(&mut self.nested_goals);
1281                    // As we return all ambiguous nested goals, we can ignore the certainty
1282                    // returned by `self.try_evaluate_added_goals()`.
1283                    if goals.is_empty() {
1284                        assert!(matches!(goals_certainty, Certainty::Yes));
1285                    }
1286                    (
1287                        Certainty::Yes,
1288                        NestedNormalizationGoals(
1289                            goals.into_iter().map(|(s, g, _)| (s, g)).collect(),
1290                        ),
1291                    )
1292                }
1293                _ => {
1294                    let certainty = shallow_certainty.and(goals_certainty);
1295                    (certainty, NestedNormalizationGoals::empty())
1296                }
1297            };
1298
1299        if let Certainty::Maybe(
1300            maybe_info @ MaybeInfo {
1301                cause: MaybeCause::Overflow { keep_constraints: false, .. },
1302                opaque_types_jank: _,
1303                stalled_on_coroutines: _,
1304            },
1305        ) = certainty
1306        {
1307            // If we have overflow, it's probable that we're substituting a type
1308            // into itself infinitely and any partial substitutions in the query
1309            // response are probably not useful anyways, so just return an empty
1310            // query response.
1311            //
1312            // This may prevent us from potentially useful inference, e.g.
1313            // 2 candidates, one ambiguous and one overflow, which both
1314            // have the same inference constraints.
1315            //
1316            // Changing this to retain some constraints in the future
1317            // won't be a breaking change, so this is good enough for now.
1318            return Ok(self.make_ambiguous_response_no_constraints(maybe_info));
1319        }
1320
1321        let external_constraints =
1322            self.compute_external_query_constraints(certainty, normalization_nested_goals);
1323        let (var_values, mut external_constraints) =
1324            eager_resolve_vars(self.delegate, (self.var_values, external_constraints));
1325
1326        // Remove any trivial or duplicated region constraints once we've resolved regions
1327        let mut unique = HashSet::default();
1328        external_constraints
1329            .region_constraints
1330            .retain(|(outlives, _)| !outlives.is_trivial() && unique.insert(*outlives));
1331
1332        let canonical = canonicalize_response(
1333            self.delegate,
1334            self.max_input_universe,
1335            Response {
1336                var_values,
1337                certainty,
1338                external_constraints: self.cx().mk_external_constraints(external_constraints),
1339            },
1340        );
1341
1342        Ok(canonical)
1343    }
1344
1345    /// Constructs a totally unconstrained, ambiguous response to a goal.
1346    ///
1347    /// Take care when using this, since often it's useful to respond with
1348    /// ambiguity but return constrained variables to guide inference.
1349    pub(in crate::solve) fn make_ambiguous_response_no_constraints(
1350        &self,
1351        maybe: MaybeInfo,
1352    ) -> CanonicalResponse<I> {
1353        response_no_constraints_raw(
1354            self.cx(),
1355            self.max_input_universe,
1356            self.var_kinds,
1357            Certainty::Maybe(maybe),
1358        )
1359    }
1360
1361    /// Computes the region constraints and *new* opaque types registered when
1362    /// proving a goal.
1363    ///
1364    /// If an opaque was already constrained before proving this goal, then the
1365    /// external constraints do not need to record that opaque, since if it is
1366    /// further constrained by inference, that will be passed back in the var
1367    /// values.
1368    x;#[instrument(level = "trace", skip(self), ret)]
1369    fn compute_external_query_constraints(
1370        &self,
1371        certainty: Certainty,
1372        normalization_nested_goals: NestedNormalizationGoals<I>,
1373    ) -> ExternalConstraintsData<I> {
1374        // We only return region constraints once the certainty is `Yes`. This
1375        // is necessary as we may drop nested goals on ambiguity, which may result
1376        // in unconstrained inference variables in the region constraints. It also
1377        // prevents us from emitting duplicate region constraints, avoiding some
1378        // unnecessary work. This slightly weakens the leak check in case it uses
1379        // region constraints from an ambiguous nested goal. This is tested in both
1380        // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-5-ambig.rs` and
1381        // `tests/ui/higher-ranked/leak-check/leak-check-in-selection-6-ambig-unify.rs`.
1382        let region_constraints = if certainty == Certainty::Yes {
1383            self.delegate.make_deduplicated_region_constraints()
1384        } else {
1385            Default::default()
1386        };
1387
1388        // We only return *newly defined* opaque types from canonical queries.
1389        //
1390        // Constraints for any existing opaque types are already tracked by changes
1391        // to the `var_values`.
1392        let opaque_types = self
1393            .delegate
1394            .clone_opaque_types_added_since(self.initial_opaque_types_storage_num_entries);
1395
1396        ExternalConstraintsData { region_constraints, opaque_types, normalization_nested_goals }
1397    }
1398}
1399
1400/// Eagerly replace aliases with inference variables, emitting `AliasRelate`
1401/// goals, used when adding goals to the `EvalCtxt`. We compute the
1402/// `AliasRelate` goals before evaluating the actual goal to get all the
1403/// constraints we can.
1404///
1405/// This is a performance optimization to more eagerly detect cycles during trait
1406/// solving. See tests/ui/traits/next-solver/cycles/cycle-modulo-ambig-aliases.rs.
1407///
1408/// The emitted goals get evaluated in the context of the parent goal; by
1409/// replacing aliases in nested goals we essentially pull the normalization out of
1410/// the nested goal. We want to treat the goal as if the normalization still happens
1411/// inside of the nested goal by inheriting the `step_kind` of the nested goal and
1412/// storing it in the `GoalSource` of the emitted `AliasRelate` goals.
1413/// This is necessary for tests/ui/sized/coinductive-1.rs to compile.
1414struct ReplaceAliasWithInfer<'me, 'a, D, I>
1415where
1416    D: SolverDelegate<Interner = I>,
1417    I: Interner,
1418{
1419    ecx: &'me mut EvalCtxt<'a, D>,
1420    param_env: I::ParamEnv,
1421    normalization_goal_source: GoalSource,
1422    cache: HashMap<I::Ty, I::Ty>,
1423}
1424
1425impl<'me, 'a, D, I> ReplaceAliasWithInfer<'me, 'a, D, I>
1426where
1427    D: SolverDelegate<Interner = I>,
1428    I: Interner,
1429{
1430    fn new(
1431        ecx: &'me mut EvalCtxt<'a, D>,
1432        for_goal_source: GoalSource,
1433        param_env: I::ParamEnv,
1434    ) -> Self {
1435        let step_kind = ecx.step_kind_for_source(for_goal_source);
1436        ReplaceAliasWithInfer {
1437            ecx,
1438            param_env,
1439            normalization_goal_source: GoalSource::NormalizeGoal(step_kind),
1440            cache: Default::default(),
1441        }
1442    }
1443}
1444
1445impl<D, I> TypeFolder<I> for ReplaceAliasWithInfer<'_, '_, D, I>
1446where
1447    D: SolverDelegate<Interner = I>,
1448    I: Interner,
1449{
1450    fn cx(&self) -> I {
1451        self.ecx.cx()
1452    }
1453
1454    fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1455        match ty.kind() {
1456            ty::Alias(..) if !ty.has_escaping_bound_vars() => {
1457                let infer_ty = self.ecx.next_ty_infer();
1458                let normalizes_to = ty::PredicateKind::AliasRelate(
1459                    ty.into(),
1460                    infer_ty.into(),
1461                    ty::AliasRelationDirection::Equate,
1462                );
1463                self.ecx.add_goal(
1464                    self.normalization_goal_source,
1465                    Goal::new(self.cx(), self.param_env, normalizes_to),
1466                );
1467                infer_ty
1468            }
1469            _ => {
1470                if !ty.has_aliases() {
1471                    ty
1472                } else if let Some(&entry) = self.cache.get(&ty) {
1473                    return entry;
1474                } else {
1475                    let res = ty.super_fold_with(self);
1476                    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());
1477                    res
1478                }
1479            }
1480        }
1481    }
1482
1483    fn fold_const(&mut self, ct: I::Const) -> I::Const {
1484        match ct.kind() {
1485            ty::ConstKind::Unevaluated(..) if !ct.has_escaping_bound_vars() => {
1486                let infer_ct = self.ecx.next_const_infer();
1487                let normalizes_to = ty::PredicateKind::AliasRelate(
1488                    ct.into(),
1489                    infer_ct.into(),
1490                    ty::AliasRelationDirection::Equate,
1491                );
1492                self.ecx.add_goal(
1493                    self.normalization_goal_source,
1494                    Goal::new(self.cx(), self.param_env, normalizes_to),
1495                );
1496                infer_ct
1497            }
1498            _ => ct.super_fold_with(self),
1499        }
1500    }
1501
1502    fn fold_predicate(&mut self, predicate: I::Predicate) -> I::Predicate {
1503        if predicate.allow_normalization() { predicate.super_fold_with(self) } else { predicate }
1504    }
1505}
1506
1507/// Do not call this directly, use the `tcx` query instead.
1508pub fn evaluate_root_goal_for_proof_tree_raw_provider<
1509    D: SolverDelegate<Interner = I>,
1510    I: Interner,
1511>(
1512    cx: I,
1513    canonical_goal: CanonicalInput<I>,
1514) -> (QueryResult<I>, I::Probe) {
1515    let mut inspect = inspect::ProofTreeBuilder::new();
1516    let canonical_result = SearchGraph::<D>::evaluate_root_goal_for_proof_tree(
1517        cx,
1518        cx.recursion_limit(),
1519        canonical_goal,
1520        &mut inspect,
1521    );
1522    let final_revision = inspect.unwrap();
1523    (canonical_result, cx.mk_probe(final_revision))
1524}
1525
1526/// Evaluate a goal to build a proof tree.
1527///
1528/// This is a copy of [EvalCtxt::evaluate_goal_raw] which avoids relying on the
1529/// [EvalCtxt] and uses a separate cache.
1530pub(super) fn evaluate_root_goal_for_proof_tree<D: SolverDelegate<Interner = I>, I: Interner>(
1531    delegate: &D,
1532    goal: Goal<I, I::Predicate>,
1533    origin_span: I::Span,
1534) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
1535    let opaque_types = delegate.clone_opaque_types_lookup_table();
1536    let (goal, opaque_types) = eager_resolve_vars(delegate, (goal, opaque_types));
1537
1538    let (orig_values, canonical_goal) = canonicalize_goal(delegate, goal, &opaque_types);
1539
1540    let (canonical_result, final_revision) =
1541        delegate.cx().evaluate_root_goal_for_proof_tree_raw(canonical_goal);
1542
1543    let proof_tree = inspect::GoalEvaluation {
1544        uncanonicalized_goal: goal,
1545        orig_values,
1546        final_revision,
1547        result: canonical_result,
1548    };
1549
1550    let response = match canonical_result {
1551        Err(e) => return (Err(e), proof_tree),
1552        Ok(response) => response,
1553    };
1554
1555    let (normalization_nested_goals, _certainty) = instantiate_and_apply_query_response(
1556        delegate,
1557        goal.param_env,
1558        &proof_tree.orig_values,
1559        response,
1560        VisibleForLeakCheck::Yes,
1561        origin_span,
1562    );
1563
1564    (Ok(normalization_nested_goals), proof_tree)
1565}