2019-02-05 17:20:45 +00:00
|
|
|
use crate::infer::InferCtxt;
|
|
|
|
use crate::ty::{self, Ty, TyCtxt, ToPredicate};
|
|
|
|
use crate::traits::Obligation;
|
|
|
|
use crate::hir::def_id::DefId;
|
2018-03-11 02:29:22 +00:00
|
|
|
|
2018-11-24 19:18:16 +00:00
|
|
|
use super::{ChalkFulfillmentContext, 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,
|
2018-06-27 20:04:32 +00:00
|
|
|
infcx: &InferCtxt<'_, 'gcx, '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,
|
2018-06-27 20:04:32 +00:00
|
|
|
infcx: &InferCtxt<'_, 'gcx, '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
|
|
|
) {
|
|
|
|
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.to_predicate()
|
|
|
|
});
|
|
|
|
}
|
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,
|
2018-06-27 20:04:32 +00:00
|
|
|
infcx: &InferCtxt<'_, 'gcx, '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,
|
2018-06-27 20:04:32 +00:00
|
|
|
infcx: &InferCtxt<'_, 'gcx, '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,
|
2018-06-27 20:04:32 +00:00
|
|
|
infcx: &InferCtxt<'_, 'gcx, '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,
|
|
|
|
infcx: &InferCtxt<'_, 'gcx, 'tcx>,
|
|
|
|
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,
|
2018-06-27 20:04:32 +00:00
|
|
|
infcx: &InferCtxt<'_, 'gcx, '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
|
|
|
}
|
2018-06-27 20:04:32 +00:00
|
|
|
|
|
|
|
impl dyn TraitEngine<'tcx> {
|
2019-06-11 20:47:08 +00:00
|
|
|
pub fn new(tcx: TyCtxt<'_, 'tcx>) -> Box<Self> {
|
2018-11-24 19:18:16 +00:00
|
|
|
if tcx.sess.opts.debugging_opts.chalk {
|
|
|
|
Box::new(ChalkFulfillmentContext::new())
|
|
|
|
} else {
|
|
|
|
Box::new(FulfillmentContext::new())
|
|
|
|
}
|
2018-06-27 20:04:32 +00:00
|
|
|
}
|
|
|
|
}
|