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.

84 lines
2.6 KiB
Rust
Raw Normal View History

2020-02-22 11:44:18 +01:00
use crate::infer::InferCtxt;
use crate::traits::Obligation;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::{self, ToPredicate, Ty};
2020-02-22 11:44:18 +01:00
use super::FulfillmentError;
use super::{ObligationCause, PredicateObligation};
pub trait TraitEngine<'tcx>: 'tcx {
/// 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 = infcx.tcx.mk_trait_ref(def_id, [ty]);
2020-02-22 11:44:18 +01:00
self.register_predicate_obligation(
infcx,
Obligation {
cause,
recursion_depth: 0,
param_env,
predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(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>,
);
2022-09-09 13:01:06 -05:00
fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
2021-07-26 10:52:17 +08:00
fn collect_remaining_errors(&mut self) -> Vec<FulfillmentError<'tcx>>;
2020-02-22 11:44:18 +01:00
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
2022-10-01 15:57:22 +02: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>,
) -> Vec<PredicateObligation<'tcx>>;
2020-02-22 11:44:18 +01:00
}
pub trait TraitEngineExt<'tcx> {
fn register_predicate_obligations(
&mut self,
2022-09-09 13:01:06 -05:00
infcx: &InferCtxt<'tcx>,
2020-02-22 11:44:18 +01:00
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
);
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
2020-02-22 11:44:18 +01:00
}
impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
2020-02-22 11:44:18 +01:00
fn register_predicate_obligations(
&mut self,
2022-09-09 13:01:06 -05:00
infcx: &InferCtxt<'tcx>,
2020-02-22 11:44:18 +01:00
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
) {
for obligation in obligations {
self.register_predicate_obligation(infcx, obligation);
}
}
fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
let errors = self.select_where_possible(infcx);
if !errors.is_empty() {
return errors;
}
self.collect_remaining_errors()
}
2020-02-22 11:44:18 +01:00
}