1 use crate::infer::InferCtxt;
2 use crate::traits::Obligation;
3 use rustc_hir::def_id::DefId;
4 use rustc_middle::ty::{self, ToPredicate, Ty};
6 use super::FulfillmentError;
7 use super::{ObligationCause, PredicateObligation};
9 pub trait TraitEngine<'tcx>: 'tcx {
10 /// Requires that `ty` must implement the trait with `def_id` in
11 /// the given environment. This trait must not have any type
12 /// parameters (except for `Self`).
15 infcx: &InferCtxt<'tcx>,
16 param_env: ty::ParamEnv<'tcx>,
19 cause: ObligationCause<'tcx>,
21 let trait_ref = infcx.tcx.mk_trait_ref(def_id, [ty]);
22 self.register_predicate_obligation(
28 predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(infcx.tcx),
33 fn register_predicate_obligation(
35 infcx: &InferCtxt<'tcx>,
36 obligation: PredicateObligation<'tcx>,
39 fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
41 fn collect_remaining_errors(&mut self) -> Vec<FulfillmentError<'tcx>>;
43 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
45 /// Among all pending obligations, collect those are stalled on a inference variable which has
46 /// changed since the last call to `select_where_possible`. Those obligations are marked as
47 /// successful and returned.
48 fn drain_unstalled_obligations(
50 infcx: &InferCtxt<'tcx>,
51 ) -> Vec<PredicateObligation<'tcx>>;
54 pub trait TraitEngineExt<'tcx> {
55 fn register_predicate_obligations(
57 infcx: &InferCtxt<'tcx>,
58 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
61 fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
64 impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
65 fn register_predicate_obligations(
67 infcx: &InferCtxt<'tcx>,
68 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
70 for obligation in obligations {
71 self.register_predicate_obligation(infcx, obligation);
75 fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
76 let errors = self.select_where_possible(infcx);
77 if !errors.is_empty() {
81 self.collect_remaining_errors()