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