rust/compiler/rustc_infer/src/traits/engine.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

106 lines
3.3 KiB
Rust
Raw Normal View History

2024-06-01 14:12:34 -04:00
use std::fmt::Debug;
2020-02-22 11:44:18 +01:00
use rustc_hir::def_id::DefId;
2024-05-15 10:05:25 -04:00
use rustc_middle::ty::{self, Ty, Upcast};
2020-02-22 11:44:18 +01:00
use super::{ObligationCause, PredicateObligation, PredicateObligations};
2020-02-22 11:44:18 +01:00
use crate::infer::InferCtxt;
use crate::traits::Obligation;
2024-06-03 09:27:48 -04:00
/// A trait error with most of its information removed. This is the error
/// returned by an `ObligationCtxt` by default, and suitable if you just
/// want to see if a predicate holds, and don't particularly care about the
/// error itself (except for if it's an ambiguity or true error).
///
/// use `ObligationCtxt::new_with_diagnostics` to get a `FulfillmentError`.
#[derive(Clone, Debug)]
pub enum ScrubbedTraitError<'tcx> {
/// A real error. This goal definitely does not hold.
TrueError,
/// An ambiguity. This goal may hold if further inference is done.
Ambiguity,
/// An old-solver-style cycle error, which will fatal.
Cycle(PredicateObligations<'tcx>),
2024-06-03 09:27:48 -04:00
}
impl<'tcx> ScrubbedTraitError<'tcx> {
pub fn is_true_error(&self) -> bool {
match self {
ScrubbedTraitError::TrueError => true,
ScrubbedTraitError::Ambiguity | ScrubbedTraitError::Cycle(_) => false,
}
}
}
pub trait TraitEngine<'tcx, E: 'tcx>: 'tcx {
2020-02-22 11:44:18 +01:00
/// Requires that `ty` must implement the trait with `def_id` in
/// the given environment. This trait must not have any type
/// parameters (except for `Self`).
fn register_bound(
&mut self,
2022-09-09 13:01:06 -05:00
infcx: &InferCtxt<'tcx>,
2020-02-22 11:44:18 +01:00
param_env: ty::ParamEnv<'tcx>,
ty: Ty<'tcx>,
def_id: DefId,
cause: ObligationCause<'tcx>,
) {
let trait_ref = ty::TraitRef::new(infcx.tcx, def_id, [ty]);
2025-02-08 22:12:13 +00:00
self.register_predicate_obligation(
infcx,
Obligation {
cause,
recursion_depth: 0,
param_env,
predicate: trait_ref.upcast(infcx.tcx),
},
);
2020-02-22 11:44:18 +01:00
}
fn register_predicate_obligation(
&mut self,
2022-09-09 13:01:06 -05:00
infcx: &InferCtxt<'tcx>,
2020-02-22 11:44:18 +01:00
obligation: PredicateObligation<'tcx>,
);
fn register_predicate_obligations(
&mut self,
2022-09-09 13:01:06 -05:00
infcx: &InferCtxt<'tcx>,
obligations: PredicateObligations<'tcx>,
2020-02-22 11:44:18 +01:00
) {
for obligation in obligations {
self.register_predicate_obligation(infcx, obligation);
}
}
2024-06-01 13:22:35 -04:00
#[must_use]
2024-06-01 14:12:34 -04:00
fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E>;
2024-06-01 13:22:35 -04:00
2024-06-01 14:12:34 -04:00
fn collect_remaining_errors(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E>;
2024-06-01 13:22:35 -04:00
2024-02-16 15:07:32 +00:00
#[must_use]
2024-06-01 14:12:34 -04:00
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<E> {
let errors = self.select_where_possible(infcx);
if !errors.is_empty() {
return errors;
}
self.collect_remaining_errors(infcx)
}
2024-06-01 13:22:35 -04:00
fn has_pending_obligations(&self) -> bool;
fn pending_obligations(&self) -> PredicateObligations<'tcx>;
2024-06-01 13:22:35 -04:00
/// Among all pending obligations, collect those are stalled on a inference variable which has
/// changed since the last call to `select_where_possible`. Those obligations are marked as
/// successful and returned.
fn drain_unstalled_obligations(
&mut self,
infcx: &InferCtxt<'tcx>,
) -> PredicateObligations<'tcx>;
2020-02-22 11:44:18 +01:00
}
2024-06-01 14:12:34 -04:00
2024-06-03 09:27:48 -04:00
pub trait FromSolverError<'tcx, E>: Debug + 'tcx {
2024-06-01 14:12:34 -04:00
fn from_solver_error(infcx: &InferCtxt<'tcx>, error: E) -> Self;
}