rustc_next_trait_solver/solve/eval_ctxt/
mod.rs

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