2019-02-05 17:20:45 +00:00
|
|
|
use crate::infer::InferCtxt;
|
|
|
|
use crate::traits::Obligation;
|
2020-01-07 21:07:22 +00:00
|
|
|
use rustc::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
|
2020-01-05 01:37:57 +00:00
|
|
|
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};
|
2018-04-19 05:13:20 +00:00
|
|
|
use super::{ObligationCause, PredicateObligation};
|
2018-03-11 02:29:22 +00:00
|
|
|
|
2018-03-22 16:21:38 +00:00
|
|
|
pub trait TraitEngine<'tcx>: 'tcx {
|
2018-06-27 20:04:32 +00:00
|
|
|
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`).
|
2018-06-27 20:04:32 +00:00
|
|
|
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,
|
2020-01-14 04:30:32 +00:00
|
|
|
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
|
|
|
|
2018-06-27 20:04:32 +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>,
|
|
|
|
);
|
|
|
|
|
2018-06-27 20:04:32 +00:00
|
|
|
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>>>;
|
|
|
|
|
2018-06-27 20:04:32 +00:00
|
|
|
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>>>;
|
|
|
|
|
2018-04-19 05:13:20 +00:00
|
|
|
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
|
2018-03-11 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 20:04:32 +00:00
|
|
|
pub trait TraitEngineExt<'tcx> {
|
|
|
|
fn register_predicate_obligations(
|
|
|
|
&mut self,
|
2019-06-13 21:48:52 +00:00
|
|
|
infcx: &InferCtxt<'_, 'tcx>,
|
2018-06-27 20:04:32 +00:00
|
|
|
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
|
|
|
|
);
|
|
|
|
}
|
2018-03-11 14:01:01 +00:00
|
|
|
|
2018-06-27 20:04:32 +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>,
|
2018-06-27 20:04:32 +00:00
|
|
|
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
|
|
|
}
|
2018-06-27 20:04:32 +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())
|
2018-06-27 20:04:32 +00:00
|
|
|
}
|
|
|
|
}
|