rustc_infer/traits/
engine.rs

1use std::fmt::Debug;
2
3use rustc_hir::def_id::DefId;
4use rustc_middle::ty::{self, Ty, Upcast};
5
6use super::{ObligationCause, PredicateObligation, PredicateObligations};
7use crate::infer::InferCtxt;
8use crate::traits::Obligation;
9
10/// A trait error with most of its information removed. This is the error
11/// returned by an `ObligationCtxt` by default, and suitable if you just
12/// want to see if a predicate holds, and don't particularly care about the
13/// error itself (except for if it's an ambiguity or true error).
14///
15/// use `ObligationCtxt::new_with_diagnostics` to get a `FulfillmentError`.
16#[derive(Clone, Debug)]
17pub enum ScrubbedTraitError<'tcx> {
18    /// A real error. This goal definitely does not hold.
19    TrueError,
20    /// An ambiguity. This goal may hold if further inference is done.
21    Ambiguity,
22    /// An old-solver-style cycle error, which will fatal. This is not
23    /// returned by the new solver.
24    Cycle(PredicateObligations<'tcx>),
25}
26
27impl<'tcx> ScrubbedTraitError<'tcx> {
28    pub fn is_true_error(&self) -> bool {
29        match self {
30            ScrubbedTraitError::TrueError => true,
31            ScrubbedTraitError::Ambiguity | ScrubbedTraitError::Cycle(_) => false,
32        }
33    }
34}
35
36pub trait TraitEngine<'tcx, E: 'tcx>: 'tcx {
37    /// Requires that `ty` must implement the trait with `def_id` in
38    /// the given environment. This trait must not have any type
39    /// parameters (except for `Self`).
40    fn register_bound(
41        &mut self,
42        infcx: &InferCtxt<'tcx>,
43        param_env: ty::ParamEnv<'tcx>,
44        ty: Ty<'tcx>,
45        def_id: DefId,
46        cause: ObligationCause<'tcx>,
47    ) {
48        let trait_ref = ty::TraitRef::new(infcx.tcx, def_id, [ty]);
49        self.register_predicate_obligation(
50            infcx,
51            Obligation {
52                cause,
53                recursion_depth: 0,
54                param_env,
55                predicate: trait_ref.upcast(infcx.tcx),
56            },
57        );
58    }
59
60    fn register_predicate_obligation(
61        &mut self,
62        infcx: &InferCtxt<'tcx>,
63        obligation: PredicateObligation<'tcx>,
64    );
65
66    fn register_predicate_obligations(
67        &mut self,
68        infcx: &InferCtxt<'tcx>,
69        obligations: PredicateObligations<'tcx>,
70    ) {
71        for obligation in obligations {
72            self.register_predicate_obligation(infcx, obligation);
73        }
74    }
75
76    #[must_use]
77    fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E>;
78
79    fn collect_remaining_errors(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E>;
80
81    #[must_use]
82    fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E> {
83        let errors = self.select_where_possible(infcx);
84        if !errors.is_empty() {
85            return errors;
86        }
87
88        self.collect_remaining_errors(infcx)
89    }
90
91    fn has_pending_obligations(&self) -> bool;
92
93    fn pending_obligations(&self) -> PredicateObligations<'tcx>;
94
95    /// Among all pending obligations, collect those are stalled on a inference variable which has
96    /// changed since the last call to `select_where_possible`. Those obligations are marked as
97    /// successful and returned.
98    fn drain_stalled_obligations_for_coroutines(
99        &mut self,
100        infcx: &InferCtxt<'tcx>,
101    ) -> PredicateObligations<'tcx>;
102}
103
104pub trait FromSolverError<'tcx, E>: Debug + 'tcx {
105    fn from_solver_error(infcx: &InferCtxt<'tcx>, error: E) -> Self;
106}