rustc_next_trait_solver/solve/
mod.rs

1//! The next-generation trait solver, currently still WIP.
2//!
3//! As a user of rust, you can use `-Znext-solver` to enable the new trait solver.
4//!
5//! As a developer of rustc, you shouldn't be using the new trait
6//! solver without asking the trait-system-refactor-initiative, but it can
7//! be enabled with `InferCtxtBuilder::with_next_trait_solver`. This will
8//! ensure that trait solving using that inference context will be routed
9//! to the new trait solver.
10//!
11//! For a high-level overview of how this solver works, check out the relevant
12//! section of the rustc-dev-guide.
13
14mod alias_relate;
15mod assembly;
16mod effect_goals;
17mod eval_ctxt;
18pub mod inspect;
19mod normalizes_to;
20mod project_goals;
21mod search_graph;
22mod trait_goals;
23
24use derive_where::derive_where;
25use rustc_type_ir::inherent::*;
26pub use rustc_type_ir::solve::*;
27use rustc_type_ir::{self as ty, Interner, TyVid, TypingMode};
28use tracing::instrument;
29
30pub use self::eval_ctxt::{
31    EvalCtxt, GenerateProofTree, SolverDelegateEvalExt,
32    evaluate_root_goal_for_proof_tree_raw_provider,
33};
34use crate::delegate::SolverDelegate;
35use crate::solve::assembly::Candidate;
36
37/// How many fixpoint iterations we should attempt inside of the solver before bailing
38/// with overflow.
39///
40/// We previously used  `cx.recursion_limit().0.checked_ilog2().unwrap_or(0)` for this.
41/// However, it feels unlikely that uncreasing the recursion limit by a power of two
42/// to get one more itereation is every useful or desirable. We now instead used a constant
43/// here. If there ever ends up some use-cases where a bigger number of fixpoint iterations
44/// is required, we can add a new attribute for that or revert this to be dependant on the
45/// recursion limit again. However, this feels very unlikely.
46const FIXPOINT_STEP_LIMIT: usize = 8;
47
48/// Whether evaluating this goal ended up changing the
49/// inference state.
50#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
51pub enum HasChanged {
52    Yes,
53    No,
54}
55
56// FIXME(trait-system-refactor-initiative#117): we don't detect whether a response
57// ended up pulling down any universes.
58fn has_no_inference_or_external_constraints<I: Interner>(
59    response: ty::Canonical<I, Response<I>>,
60) -> bool {
61    let ExternalConstraintsData {
62        ref region_constraints,
63        ref opaque_types,
64        ref normalization_nested_goals,
65    } = *response.value.external_constraints;
66    response.value.var_values.is_identity()
67        && region_constraints.is_empty()
68        && opaque_types.is_empty()
69        && normalization_nested_goals.is_empty()
70}
71
72fn has_only_region_constraints<I: Interner>(response: ty::Canonical<I, Response<I>>) -> bool {
73    let ExternalConstraintsData {
74        region_constraints: _,
75        ref opaque_types,
76        ref normalization_nested_goals,
77    } = *response.value.external_constraints;
78    response.value.var_values.is_identity_modulo_regions()
79        && opaque_types.is_empty()
80        && normalization_nested_goals.is_empty()
81}
82
83impl<'a, D, I> EvalCtxt<'a, D>
84where
85    D: SolverDelegate<Interner = I>,
86    I: Interner,
87{
88    #[instrument(level = "trace", skip(self))]
89    fn compute_type_outlives_goal(
90        &mut self,
91        goal: Goal<I, ty::OutlivesPredicate<I, I::Ty>>,
92    ) -> QueryResult<I> {
93        let ty::OutlivesPredicate(ty, lt) = goal.predicate;
94        self.register_ty_outlives(ty, lt);
95        self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
96    }
97
98    #[instrument(level = "trace", skip(self))]
99    fn compute_region_outlives_goal(
100        &mut self,
101        goal: Goal<I, ty::OutlivesPredicate<I, I::Region>>,
102    ) -> QueryResult<I> {
103        let ty::OutlivesPredicate(a, b) = goal.predicate;
104        self.register_region_outlives(a, b);
105        self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
106    }
107
108    #[instrument(level = "trace", skip(self))]
109    fn compute_coerce_goal(&mut self, goal: Goal<I, ty::CoercePredicate<I>>) -> QueryResult<I> {
110        self.compute_subtype_goal(Goal {
111            param_env: goal.param_env,
112            predicate: ty::SubtypePredicate {
113                a_is_expected: false,
114                a: goal.predicate.a,
115                b: goal.predicate.b,
116            },
117        })
118    }
119
120    #[instrument(level = "trace", skip(self))]
121    fn compute_subtype_goal(&mut self, goal: Goal<I, ty::SubtypePredicate<I>>) -> QueryResult<I> {
122        match (goal.predicate.a.kind(), goal.predicate.b.kind()) {
123            (ty::Infer(ty::TyVar(a_vid)), ty::Infer(ty::TyVar(b_vid))) => {
124                self.sub_unify_ty_vids_raw(a_vid, b_vid);
125                self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
126            }
127            _ => {
128                self.sub(goal.param_env, goal.predicate.a, goal.predicate.b)?;
129                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
130            }
131        }
132    }
133
134    fn compute_dyn_compatible_goal(&mut self, trait_def_id: I::TraitId) -> QueryResult<I> {
135        if self.cx().trait_is_dyn_compatible(trait_def_id) {
136            self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
137        } else {
138            Err(NoSolution)
139        }
140    }
141
142    #[instrument(level = "trace", skip(self))]
143    fn compute_well_formed_goal(&mut self, goal: Goal<I, I::Term>) -> QueryResult<I> {
144        match self.well_formed_goals(goal.param_env, goal.predicate) {
145            Some(goals) => {
146                self.add_goals(GoalSource::Misc, goals);
147                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
148            }
149            None => self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS),
150        }
151    }
152
153    fn compute_unstable_feature_goal(
154        &mut self,
155        param_env: <I as Interner>::ParamEnv,
156        symbol: <I as Interner>::Symbol,
157    ) -> QueryResult<I> {
158        if self.may_use_unstable_feature(param_env, symbol) {
159            self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
160        } else {
161            self.evaluate_added_goals_and_make_canonical_response(Certainty::Maybe {
162                cause: MaybeCause::Ambiguity,
163                opaque_types_jank: OpaqueTypesJank::AllGood,
164            })
165        }
166    }
167
168    #[instrument(level = "trace", skip(self))]
169    fn compute_const_evaluatable_goal(
170        &mut self,
171        Goal { param_env, predicate: ct }: Goal<I, I::Const>,
172    ) -> QueryResult<I> {
173        match ct.kind() {
174            ty::ConstKind::Unevaluated(uv) => {
175                // We never return `NoSolution` here as `evaluate_const` emits an
176                // error itself when failing to evaluate, so emitting an additional fulfillment
177                // error in that case is unnecessary noise. This may change in the future once
178                // evaluation failures are allowed to impact selection, e.g. generic const
179                // expressions in impl headers or `where`-clauses.
180
181                // FIXME(generic_const_exprs): Implement handling for generic
182                // const expressions here.
183                if let Some(_normalized) = self.evaluate_const(param_env, uv) {
184                    self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
185                } else {
186                    self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
187                }
188            }
189            ty::ConstKind::Infer(_) => {
190                self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
191            }
192            ty::ConstKind::Placeholder(_) | ty::ConstKind::Value(_) | ty::ConstKind::Error(_) => {
193                self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
194            }
195            // We can freely ICE here as:
196            // - `Param` gets replaced with a placeholder during canonicalization
197            // - `Bound` cannot exist as we don't have a binder around the self Type
198            // - `Expr` is part of `feature(generic_const_exprs)` and is not implemented yet
199            ty::ConstKind::Param(_) | ty::ConstKind::Bound(_, _) | ty::ConstKind::Expr(_) => {
200                panic!("unexpected const kind: {:?}", ct)
201            }
202        }
203    }
204
205    #[instrument(level = "trace", skip(self), ret)]
206    fn compute_const_arg_has_type_goal(
207        &mut self,
208        goal: Goal<I, (I::Const, I::Ty)>,
209    ) -> QueryResult<I> {
210        let (ct, ty) = goal.predicate;
211        let ct = self.structurally_normalize_const(goal.param_env, ct)?;
212
213        let ct_ty = match ct.kind() {
214            ty::ConstKind::Infer(_) => {
215                return self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS);
216            }
217            ty::ConstKind::Error(_) => {
218                return self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
219            }
220            ty::ConstKind::Unevaluated(uv) => {
221                self.cx().type_of(uv.def).instantiate(self.cx(), uv.args)
222            }
223            ty::ConstKind::Expr(_) => unimplemented!(
224                "`feature(generic_const_exprs)` is not supported in the new trait solver"
225            ),
226            ty::ConstKind::Param(_) => {
227                unreachable!("`ConstKind::Param` should have been canonicalized to `Placeholder`")
228            }
229            ty::ConstKind::Bound(_, _) => panic!("escaping bound vars in {:?}", ct),
230            ty::ConstKind::Value(cv) => cv.ty(),
231            ty::ConstKind::Placeholder(placeholder) => {
232                placeholder.find_const_ty_from_env(goal.param_env)
233            }
234        };
235
236        self.eq(goal.param_env, ct_ty, ty)?;
237        self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
238    }
239}
240
241#[derive(Debug)]
242enum MergeCandidateInfo {
243    AlwaysApplicable(usize),
244    EqualResponse,
245}
246
247impl<D, I> EvalCtxt<'_, D>
248where
249    D: SolverDelegate<Interner = I>,
250    I: Interner,
251{
252    /// Try to merge multiple possible ways to prove a goal, if that is not possible returns `None`.
253    ///
254    /// In this case we tend to flounder and return ambiguity by calling `[EvalCtxt::flounder]`.
255    #[instrument(level = "trace", skip(self), ret)]
256    fn try_merge_candidates(
257        &mut self,
258        candidates: &[Candidate<I>],
259    ) -> Option<(CanonicalResponse<I>, MergeCandidateInfo)> {
260        if candidates.is_empty() {
261            return None;
262        }
263
264        let always_applicable = candidates.iter().enumerate().find(|(_, candidate)| {
265            candidate.result.value.certainty == Certainty::Yes
266                && has_no_inference_or_external_constraints(candidate.result)
267        });
268        if let Some((i, c)) = always_applicable {
269            return Some((c.result, MergeCandidateInfo::AlwaysApplicable(i)));
270        }
271
272        let one: CanonicalResponse<I> = candidates[0].result;
273        if candidates[1..].iter().all(|candidate| candidate.result == one) {
274            return Some((one, MergeCandidateInfo::EqualResponse));
275        }
276
277        None
278    }
279
280    fn bail_with_ambiguity(&mut self, candidates: &[Candidate<I>]) -> CanonicalResponse<I> {
281        debug_assert!(candidates.len() > 1);
282        let (cause, opaque_types_jank) = candidates.iter().fold(
283            (MaybeCause::Ambiguity, OpaqueTypesJank::AllGood),
284            |(c, jank), candidates| {
285                // We pull down the certainty of `Certainty::Yes` to ambiguity when combining
286                // these responses, b/c we're combining more than one response and this we
287                // don't know which one applies.
288                match candidates.result.value.certainty {
289                    Certainty::Yes => (c, jank),
290                    Certainty::Maybe { cause, opaque_types_jank } => {
291                        (c.or(cause), jank.or(opaque_types_jank))
292                    }
293                }
294            },
295        );
296        self.make_ambiguous_response_no_constraints(cause, opaque_types_jank)
297    }
298
299    /// If we fail to merge responses we flounder and return overflow or ambiguity.
300    #[instrument(level = "trace", skip(self), ret)]
301    fn flounder(&mut self, candidates: &[Candidate<I>]) -> QueryResult<I> {
302        if candidates.is_empty() {
303            return Err(NoSolution);
304        } else {
305            Ok(self.bail_with_ambiguity(candidates))
306        }
307    }
308
309    /// Normalize a type for when it is structurally matched on.
310    ///
311    /// This function is necessary in nearly all cases before matching on a type.
312    /// Not doing so is likely to be incomplete and therefore unsound during
313    /// coherence.
314    #[instrument(level = "trace", skip(self, param_env), ret)]
315    fn structurally_normalize_ty(
316        &mut self,
317        param_env: I::ParamEnv,
318        ty: I::Ty,
319    ) -> Result<I::Ty, NoSolution> {
320        self.structurally_normalize_term(param_env, ty.into()).map(|term| term.expect_ty())
321    }
322
323    /// Normalize a const for when it is structurally matched on, or more likely
324    /// when it needs `.try_to_*` called on it (e.g. to turn it into a usize).
325    ///
326    /// This function is necessary in nearly all cases before matching on a const.
327    /// Not doing so is likely to be incomplete and therefore unsound during
328    /// coherence.
329    #[instrument(level = "trace", skip(self, param_env), ret)]
330    fn structurally_normalize_const(
331        &mut self,
332        param_env: I::ParamEnv,
333        ct: I::Const,
334    ) -> Result<I::Const, NoSolution> {
335        self.structurally_normalize_term(param_env, ct.into()).map(|term| term.expect_const())
336    }
337
338    /// Normalize a term for when it is structurally matched on.
339    ///
340    /// This function is necessary in nearly all cases before matching on a ty/const.
341    /// Not doing so is likely to be incomplete and therefore unsound during coherence.
342    fn structurally_normalize_term(
343        &mut self,
344        param_env: I::ParamEnv,
345        term: I::Term,
346    ) -> Result<I::Term, NoSolution> {
347        if let Some(_) = term.to_alias_term() {
348            let normalized_term = self.next_term_infer_of_kind(term);
349            let alias_relate_goal = Goal::new(
350                self.cx(),
351                param_env,
352                ty::PredicateKind::AliasRelate(
353                    term,
354                    normalized_term,
355                    ty::AliasRelationDirection::Equate,
356                ),
357            );
358            // We normalize the self type to be able to relate it with
359            // types from candidates.
360            self.add_goal(GoalSource::TypeRelating, alias_relate_goal);
361            self.try_evaluate_added_goals()?;
362            Ok(self.resolve_vars_if_possible(normalized_term))
363        } else {
364            Ok(term)
365        }
366    }
367
368    fn opaque_type_is_rigid(&self, def_id: I::DefId) -> bool {
369        match self.typing_mode() {
370            // Opaques are never rigid outside of analysis mode.
371            TypingMode::Coherence | TypingMode::PostAnalysis => false,
372            // During analysis, opaques are rigid unless they may be defined by
373            // the current body.
374            TypingMode::Analysis { defining_opaque_types_and_generators: non_rigid_opaques }
375            | TypingMode::Borrowck { defining_opaque_types: non_rigid_opaques }
376            | TypingMode::PostBorrowckAnalysis { defined_opaque_types: non_rigid_opaques } => {
377                !def_id.as_local().is_some_and(|def_id| non_rigid_opaques.contains(&def_id))
378            }
379        }
380    }
381}
382
383/// The result of evaluating a goal.
384pub struct GoalEvaluation<I: Interner> {
385    /// The goal we've evaluated. This is the input goal, but potentially with its
386    /// inference variables resolved. This never applies any inference constraints
387    /// from evaluating the goal.
388    ///
389    /// We rely on this to check whether root goals in HIR typeck had an unresolved
390    /// type inference variable in the input. We must not resolve this after evaluating
391    /// the goal as even if the inference variable has been resolved by evaluating the
392    /// goal itself, this goal may still end up failing due to region uniquification
393    /// later on.
394    ///
395    /// This is used as a minor optimization to avoid re-resolving inference variables
396    /// when reevaluating ambiguous goals. E.g. if we've got a goal `?x: Trait` with `?x`
397    /// already being constrained to `Vec<?y>`, then the first evaluation resolves it to
398    /// `Vec<?y>: Trait`. If this goal is still ambiguous and we later resolve `?y` to `u32`,
399    /// then reevaluating this goal now only needs to resolve `?y` while it would otherwise
400    /// have to resolve both `?x` and `?y`,
401    pub goal: Goal<I, I::Predicate>,
402    pub certainty: Certainty,
403    pub has_changed: HasChanged,
404    /// If the [`Certainty`] was `Maybe`, then keep track of whether the goal has changed
405    /// before rerunning it.
406    pub stalled_on: Option<GoalStalledOn<I>>,
407}
408
409/// The conditions that must change for a goal to warrant
410#[derive_where(Clone, Debug; I: Interner)]
411pub struct GoalStalledOn<I: Interner> {
412    pub num_opaques: usize,
413    pub stalled_vars: Vec<I::GenericArg>,
414    pub sub_roots: Vec<TyVid>,
415    /// The certainty that will be returned on subsequent evaluations if this
416    /// goal remains stalled.
417    pub stalled_certainty: Certainty,
418}