rust/src/librustc_trait_selection/traits/engine.rs

85 lines
2.4 KiB
Rust
Raw Normal View History

2019-02-05 17:20:45 +00:00
use crate::infer::InferCtxt;
use crate::traits::Obligation;
use rustc::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
use rustc_hir::def_id::DefId;
2018-03-11 02:29:22 +00:00
2020-02-17 20:32:37 +00:00
use super::{FulfillmentContext, FulfillmentError};
use super::{ObligationCause, PredicateObligation};
2018-03-11 02:29:22 +00:00
pub trait TraitEngine<'tcx>: 'tcx {
fn normalize_projection_type(
2018-03-11 02:29:22 +00:00
&mut self,
2019-06-13 21:48:52 +00:00
infcx: &InferCtxt<'_, 'tcx>,
2018-03-11 02:29:22 +00:00
param_env: ty::ParamEnv<'tcx>,
projection_ty: ty::ProjectionTy<'tcx>,
cause: ObligationCause<'tcx>,
) -> Ty<'tcx>;
2018-11-24 19:18:16 +00: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(
2018-03-11 02:29:22 +00:00
&mut self,
2019-06-13 21:48:52 +00:00
infcx: &InferCtxt<'_, 'tcx>,
2018-03-11 02:29:22 +00:00
param_env: ty::ParamEnv<'tcx>,
ty: Ty<'tcx>,
def_id: DefId,
cause: ObligationCause<'tcx>,
2018-11-24 19:18:16 +00:00
) {
2019-12-22 22:42:04 +00:00
let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
self.register_predicate_obligation(
infcx,
Obligation {
cause,
recursion_depth: 0,
param_env,
predicate: trait_ref.without_const().to_predicate(),
2019-12-22 22:42:04 +00:00
},
);
2018-11-24 19:18:16 +00:00
}
2018-03-11 02:29:22 +00:00
fn register_predicate_obligation(
2018-03-11 02:29:22 +00:00
&mut self,
2019-06-13 21:48:52 +00:00
infcx: &InferCtxt<'_, 'tcx>,
2018-03-11 02:29:22 +00:00
obligation: PredicateObligation<'tcx>,
);
fn select_all_or_error(
2018-03-11 02:29:22 +00:00
&mut self,
2019-06-13 21:48:52 +00:00
infcx: &InferCtxt<'_, 'tcx>,
2018-03-11 02:29:22 +00:00
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
fn select_where_possible(
2018-03-11 02:29:22 +00:00
&mut self,
2019-06-13 21:48:52 +00:00
infcx: &InferCtxt<'_, 'tcx>,
2018-03-11 02:29:22 +00:00
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
2018-03-11 02:29:22 +00:00
}
pub trait TraitEngineExt<'tcx> {
fn register_predicate_obligations(
&mut self,
2019-06-13 21:48:52 +00:00
infcx: &InferCtxt<'_, 'tcx>,
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
);
}
2018-03-11 14:01:01 +00:00
impl<T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
fn register_predicate_obligations(
2018-03-22 16:06:47 +00:00
&mut self,
2019-06-13 21:48:52 +00:00
infcx: &InferCtxt<'_, 'tcx>,
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
) {
2018-03-22 16:06:47 +00:00
for obligation in obligations {
self.register_predicate_obligation(infcx, obligation);
}
}
2018-03-11 02:29:22 +00:00
}
impl dyn TraitEngine<'tcx> {
2020-02-17 20:32:37 +00:00
pub fn new(_tcx: TyCtxt<'tcx>) -> Box<Self> {
Box::new(FulfillmentContext::new())
}
}