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