Skip to main content

rustc_trait_selection/solve/
fulfill.rs

1use std::marker::PhantomData;
2use std::mem;
3
4use rustc_infer::infer::InferCtxt;
5use rustc_infer::traits::query::NoSolution;
6use rustc_infer::traits::{
7    FromSolverError, PredicateObligation, PredicateObligations, TraitEngine,
8};
9use rustc_middle::ty::{self, TyCtxt, TyVid, TypeVisitableExt, TypingMode};
10use rustc_next_trait_solver::delegate::SolverDelegate as _;
11use rustc_next_trait_solver::solve::{
12    GoalEvaluation, GoalStalledOn, HasChanged, MaybeInfo, SolverDelegateEvalExt as _,
13    StalledOnCoroutines,
14};
15use thin_vec::ThinVec;
16use tracing::instrument;
17
18use self::derive_errors::*;
19use super::Certainty;
20use super::delegate::SolverDelegate;
21use crate::traits::{FulfillmentError, ScrubbedTraitError};
22
23mod derive_errors;
24
25// FIXME: Do we need to use a `ThinVec` here?
26type PendingObligations<'tcx> =
27    ThinVec<(PredicateObligation<'tcx>, Option<GoalStalledOn<TyCtxt<'tcx>>>)>;
28
29/// A trait engine using the new trait solver.
30///
31/// This is mostly identical to how `evaluate_all` works inside of the
32/// solver, except that the requirements are slightly different.
33///
34/// Unlike `evaluate_all` it is possible to add new obligations later on
35/// and we also have to track diagnostics information by using `Obligation`
36/// instead of `Goal`.
37///
38/// It is also likely that we want to use slightly different datastructures
39/// here as this will have to deal with far more root goals than `evaluate_all`.
40pub struct FulfillmentCtxt<'tcx, E: 'tcx> {
41    obligations: ObligationStorage<'tcx>,
42
43    /// The snapshot in which this context was created. Using the context
44    /// outside of this snapshot leads to subtle bugs if the snapshot
45    /// gets rolled back. Because of this we explicitly check that we only
46    /// use the context in exactly this snapshot.
47    usable_in_snapshot: usize,
48    _errors: PhantomData<E>,
49}
50
51#[derive(#[automatically_derived]
impl<'tcx> ::core::default::Default for ObligationStorage<'tcx> {
    #[inline]
    fn default() -> ObligationStorage<'tcx> {
        ObligationStorage {
            overflowed: ::core::default::Default::default(),
            pending: ::core::default::Default::default(),
        }
    }
}Default, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ObligationStorage<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "ObligationStorage", "overflowed", &self.overflowed, "pending",
            &&self.pending)
    }
}Debug)]
52struct ObligationStorage<'tcx> {
53    /// Obligations which resulted in an overflow in fulfillment itself.
54    ///
55    /// We cannot eagerly return these as error so we instead store them here
56    /// to avoid recomputing them each time `try_evaluate_obligations` is called.
57    /// This also allows us to return the correct `FulfillmentError` for them.
58    overflowed: Vec<PredicateObligation<'tcx>>,
59    pending: PendingObligations<'tcx>,
60}
61
62impl<'tcx> ObligationStorage<'tcx> {
63    fn register(
64        &mut self,
65        obligation: PredicateObligation<'tcx>,
66        stalled_on: Option<GoalStalledOn<TyCtxt<'tcx>>>,
67    ) {
68        self.pending.push((obligation, stalled_on));
69    }
70
71    fn has_pending_obligations(&self) -> bool {
72        !self.pending.is_empty() || !self.overflowed.is_empty()
73    }
74
75    fn clone_pending(&self) -> PredicateObligations<'tcx> {
76        let mut obligations: PredicateObligations<'tcx> =
77            self.pending.iter().map(|(o, _)| o.clone()).collect();
78        obligations.extend(self.overflowed.iter().cloned());
79        obligations
80    }
81
82    fn clone_pending_potentially_referencing_sub_root(
83        &self,
84        infcx: &InferCtxt<'tcx>,
85        vid: TyVid,
86    ) -> PredicateObligations<'tcx> {
87        let mut obligations: PredicateObligations<'tcx> = self
88            .pending
89            .iter()
90            .filter(|(_, stalled_on)| {
91                let Some(stalled_on) = stalled_on else { return true };
92                // Don't reuse the sub-unification roots cached on `stalled_on`:
93                // a later sub-unification merge can have changed which root
94                // each stalled var belongs to, so the cached info can be stale.
95                // Walk `stalled_vars` and recompute the current root instead.
96                //
97                // Conservative here: if a stalled var no longer resolves to an
98                // infer var, some unification happened, so the goal is no longer
99                // stalled. Include it to be re-evaluated downstream.
100                stalled_on.stalled_vars.iter().filter_map(|arg| arg.as_type()).any(
101                    |ty| match *infcx.shallow_resolve(ty).kind() {
102                        ty::Infer(ty::TyVar(tv)) => infcx.sub_unification_table_root_var(tv) == vid,
103                        _ => true,
104                    },
105                )
106            })
107            .map(|(o, _)| o.clone())
108            .collect();
109        obligations.extend(self.overflowed.iter().cloned());
110        obligations
111    }
112
113    fn drain_pending(
114        &mut self,
115        cond: impl Fn(&PredicateObligation<'tcx>, &Option<GoalStalledOn<TyCtxt<'tcx>>>) -> bool,
116    ) -> PendingObligations<'tcx> {
117        let (unstalled, pending) =
118            mem::take(&mut self.pending).into_iter().partition(|(o, s)| cond(o, s));
119        self.pending = pending;
120        unstalled
121    }
122
123    fn on_fulfillment_overflow(&mut self, infcx: &InferCtxt<'tcx>) {
124        infcx.probe(|_| {
125            // IMPORTANT: we must not use solve any inference variables in the obligations
126            // as this is all happening inside of a probe. We use a probe to make sure
127            // we get all obligations involved in the overflow. We pretty much check: if
128            // we were to do another step of `try_evaluate_obligations`, which goals would
129            // change.
130            self.overflowed.extend(
131                self.pending
132                    .extract_if(.., |(o, stalled_on)| {
133                        let goal = o.as_goal();
134                        let result = <&SolverDelegate<'tcx>>::from(infcx).evaluate_root_goal(
135                            goal,
136                            o.cause.span,
137                            stalled_on.take(),
138                        );
139                        #[allow(non_exhaustive_omitted_patterns)] match result {
    Ok(GoalEvaluation { has_changed: HasChanged::Yes, .. }) => true,
    _ => false,
}matches!(result, Ok(GoalEvaluation { has_changed: HasChanged::Yes, .. }))
140                    })
141                    .map(|(o, _)| o),
142            );
143        })
144    }
145}
146
147impl<'tcx, E: 'tcx> FulfillmentCtxt<'tcx, E> {
148    pub fn new(infcx: &InferCtxt<'tcx>) -> FulfillmentCtxt<'tcx, E> {
149        if !infcx.next_trait_solver() {
    {
        ::core::panicking::panic_fmt(format_args!("new trait solver fulfillment context created when infcx is set up for old trait solver"));
    }
};assert!(
150            infcx.next_trait_solver(),
151            "new trait solver fulfillment context created when \
152            infcx is set up for old trait solver"
153        );
154        FulfillmentCtxt {
155            obligations: Default::default(),
156            usable_in_snapshot: infcx.num_open_snapshots(),
157            _errors: PhantomData,
158        }
159    }
160
161    fn inspect_evaluated_obligation(
162        &self,
163        infcx: &InferCtxt<'tcx>,
164        obligation: &PredicateObligation<'tcx>,
165        result: &Result<GoalEvaluation<TyCtxt<'tcx>>, NoSolution>,
166    ) {
167        if let Some(inspector) = infcx.obligation_inspector.get() {
168            let result = match result {
169                Ok(GoalEvaluation { certainty, .. }) => Ok(*certainty),
170                Err(NoSolution) => Err(NoSolution),
171            };
172            (inspector)(infcx, &obligation, result);
173        }
174    }
175}
176
177impl<'tcx, E> TraitEngine<'tcx, E> for FulfillmentCtxt<'tcx, E>
178where
179    E: FromSolverError<'tcx, NextSolverError<'tcx>>,
180{
181    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("register_predicate_obligation",
                                    "rustc_trait_selection::solve::fulfill",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/solve/fulfill.rs"),
                                    ::tracing_core::__macro_support::Option::Some(181u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::solve::fulfill"),
                                    ::tracing_core::field::FieldSet::new(&["obligation"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&obligation)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            match (&self.usable_in_snapshot, &infcx.num_open_snapshots()) {
                (left_val, right_val) => {
                    if !(*left_val == *right_val) {
                        let kind = ::core::panicking::AssertKind::Eq;
                        ::core::panicking::assert_failed(kind, &*left_val,
                            &*right_val, ::core::option::Option::None);
                    }
                }
            };
            self.obligations.register(obligation, None);
        }
    }
}#[instrument(level = "trace", skip(self, infcx))]
182    fn register_predicate_obligation(
183        &mut self,
184        infcx: &InferCtxt<'tcx>,
185        obligation: PredicateObligation<'tcx>,
186    ) {
187        assert_eq!(self.usable_in_snapshot, infcx.num_open_snapshots());
188        self.obligations.register(obligation, None);
189    }
190
191    fn collect_remaining_errors(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E> {
192        self.obligations
193            .pending
194            .drain(..)
195            .map(|(obligation, _)| NextSolverError::Ambiguity(obligation))
196            .chain(
197                self.obligations
198                    .overflowed
199                    .drain(..)
200                    .map(|obligation| NextSolverError::Overflow(obligation)),
201            )
202            .map(|e| E::from_solver_error(infcx, e))
203            .collect()
204    }
205
206    fn try_evaluate_obligations(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E> {
207        match (&self.usable_in_snapshot, &infcx.num_open_snapshots()) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.usable_in_snapshot, infcx.num_open_snapshots());
208        let mut errors = Vec::new();
209        loop {
210            let mut any_changed = false;
211            for (mut obligation, stalled_on) in self.obligations.drain_pending(|_, _| true) {
212                if !infcx.tcx.recursion_limit().value_within_limit(obligation.recursion_depth) {
213                    self.obligations.on_fulfillment_overflow(infcx);
214                    // Only return true errors that we have accumulated while processing.
215                    return errors;
216                }
217
218                let goal = obligation.as_goal();
219                let delegate = <&SolverDelegate<'tcx>>::from(infcx);
220                if !delegate.disable_trait_solver_fast_paths()
221                    && let Some(certainty) =
222                        delegate.compute_goal_fast_path(goal, obligation.cause.span)
223                {
224                    match certainty {
225                        // This fast path doesn't depend on region identity so it doesn't
226                        // matter if the goal contains inference variables or not, so we
227                        // don't need to call `push_hir_typeck_potentially_region_dependent_goal`
228                        // here.
229                        //
230                        // Only goals proven via the trait solver should be region dependent.
231                        Certainty::Yes => {}
232                        Certainty::Maybe(_) => {
233                            self.obligations.register(obligation, None);
234                        }
235                    }
236                    continue;
237                }
238
239                let result = delegate.evaluate_root_goal(goal, obligation.cause.span, stalled_on);
240                self.inspect_evaluated_obligation(infcx, &obligation, &result);
241                let GoalEvaluation { goal, certainty, has_changed, stalled_on } = match result {
242                    Ok(result) => result,
243                    Err(NoSolution) => {
244                        errors.push(E::from_solver_error(
245                            infcx,
246                            NextSolverError::TrueError(obligation),
247                        ));
248                        continue;
249                    }
250                };
251
252                // We've resolved the goal in `evaluate_root_goal`, avoid redoing this work
253                // in the next iteration. This does not resolve the inference variables
254                // constrained by evaluating the goal.
255                obligation.predicate = goal.predicate;
256                if has_changed == HasChanged::Yes {
257                    // We increment the recursion depth here to track the number of times
258                    // this goal has resulted in inference progress. This doesn't precisely
259                    // model the way that we track recursion depth in the old solver due
260                    // to the fact that we only process root obligations, but it is a good
261                    // approximation and should only result in fulfillment overflow in
262                    // pathological cases.
263                    obligation.recursion_depth += 1;
264                    any_changed = true;
265                }
266
267                match certainty {
268                    Certainty::Yes => {
269                        // Goals may depend on structural identity. Region uniquification at the
270                        // start of MIR borrowck may cause things to no longer be so, potentially
271                        // causing an ICE.
272                        //
273                        // While we uniquify root goals in HIR this does not handle cases where
274                        // regions are hidden inside of a type or const inference variable.
275                        //
276                        // FIXME(-Znext-solver): This does not handle inference variables hidden
277                        // inside of an opaque type, e.g. if there's `Opaque = (?x, ?x)` in the
278                        // storage, we can also rely on structural identity of `?x` even if we
279                        // later uniquify it in MIR borrowck.
280                        if infcx.in_hir_typeck
281                            && (obligation.has_non_region_infer() || obligation.has_free_regions())
282                        {
283                            infcx.push_hir_typeck_potentially_region_dependent_goal(obligation);
284                        }
285                    }
286                    Certainty::Maybe(_) => self.obligations.register(obligation, stalled_on),
287                }
288            }
289
290            if !any_changed {
291                break;
292            }
293        }
294
295        errors
296    }
297
298    fn has_pending_obligations(&self) -> bool {
299        self.obligations.has_pending_obligations()
300    }
301
302    fn pending_obligations(&self) -> PredicateObligations<'tcx> {
303        self.obligations.clone_pending()
304    }
305
306    fn pending_obligations_potentially_referencing_sub_root(
307        &self,
308        infcx: &InferCtxt<'tcx>,
309        vid: ty::TyVid,
310    ) -> PredicateObligations<'tcx> {
311        // `-Zdisable-fast-paths`: same gate as the other new-solver fast paths.
312        if infcx.tcx.disable_trait_solver_fast_paths() {
313            return self.obligations.clone_pending();
314        }
315        self.obligations.clone_pending_potentially_referencing_sub_root(infcx, vid)
316    }
317
318    fn drain_stalled_obligations_for_coroutines(
319        &mut self,
320        infcx: &InferCtxt<'tcx>,
321    ) -> PredicateObligations<'tcx> {
322        let stalled_coroutines = match infcx.typing_mode_raw().assert_not_erased() {
323            TypingMode::Analysis { defining_opaque_types_and_generators } => {
324                defining_opaque_types_and_generators
325            }
326            TypingMode::Coherence
327            | TypingMode::Borrowck { defining_opaque_types: _ }
328            | TypingMode::PostBorrowckAnalysis { defined_opaque_types: _ }
329            | TypingMode::PostAnalysis
330            | TypingMode::Codegen => return Default::default(),
331        };
332
333        if stalled_coroutines.is_empty() {
334            return Default::default();
335        }
336
337        self.obligations
338            .drain_pending(|_, stalled_on| {
339                stalled_on.as_ref().is_some_and(|s| match s.stalled_certainty {
340                    Certainty::Maybe(MaybeInfo {
341                        cause: _,
342                        opaque_types_jank: _,
343                        stalled_on_coroutines: StalledOnCoroutines::Yes,
344                    }) => true,
345                    Certainty::Maybe(_) | Certainty::Yes => false,
346                })
347            })
348            .into_iter()
349            .map(|(o, _)| o)
350            .collect()
351    }
352}
353
354pub enum NextSolverError<'tcx> {
355    TrueError(PredicateObligation<'tcx>),
356    Ambiguity(PredicateObligation<'tcx>),
357    Overflow(PredicateObligation<'tcx>),
358}
359
360impl<'tcx> FromSolverError<'tcx, NextSolverError<'tcx>> for FulfillmentError<'tcx> {
361    fn from_solver_error(infcx: &InferCtxt<'tcx>, error: NextSolverError<'tcx>) -> Self {
362        match error {
363            NextSolverError::TrueError(obligation) => {
364                fulfillment_error_for_no_solution(infcx, obligation)
365            }
366            NextSolverError::Ambiguity(obligation) => {
367                fulfillment_error_for_stalled(infcx, obligation)
368            }
369            NextSolverError::Overflow(obligation) => {
370                fulfillment_error_for_overflow(infcx, obligation)
371            }
372        }
373    }
374}
375
376impl<'tcx> FromSolverError<'tcx, NextSolverError<'tcx>> for ScrubbedTraitError<'tcx> {
377    fn from_solver_error(_infcx: &InferCtxt<'tcx>, error: NextSolverError<'tcx>) -> Self {
378        match error {
379            NextSolverError::TrueError(_) => ScrubbedTraitError::TrueError,
380            NextSolverError::Ambiguity(_) | NextSolverError::Overflow(_) => {
381                ScrubbedTraitError::Ambiguity
382            }
383        }
384    }
385}