1 use crate::infer::InferCtxt;
2 use crate::ty::{self, Ty, TyCtxt, ToPredicate};
3 use crate::traits::Obligation;
4 use crate::hir::def_id::DefId;
6 use super::{ChalkFulfillmentContext, FulfillmentContext, FulfillmentError};
7 use super::{ObligationCause, PredicateObligation};
9 pub trait TraitEngine<'tcx>: 'tcx {
10 fn normalize_projection_type(
12 infcx: &InferCtxt<'_, 'tcx>,
13 param_env: ty::ParamEnv<'tcx>,
14 projection_ty: ty::ProjectionTy<'tcx>,
15 cause: ObligationCause<'tcx>,
18 /// Requires that `ty` must implement the trait with `def_id` in
19 /// the given environment. This trait must not have any type
20 /// parameters (except for `Self`).
23 infcx: &InferCtxt<'_, 'tcx>,
24 param_env: ty::ParamEnv<'tcx>,
27 cause: ObligationCause<'tcx>,
29 let trait_ref = ty::TraitRef {
31 substs: infcx.tcx.mk_substs_trait(ty, &[]),
33 self.register_predicate_obligation(infcx, Obligation {
37 predicate: trait_ref.to_predicate()
41 fn register_predicate_obligation(
43 infcx: &InferCtxt<'_, 'tcx>,
44 obligation: PredicateObligation<'tcx>,
47 fn select_all_or_error(
49 infcx: &InferCtxt<'_, 'tcx>,
50 ) -> Result<(), Vec<FulfillmentError<'tcx>>>;
52 fn select_where_possible(
54 infcx: &InferCtxt<'_, 'tcx>,
55 ) -> Result<(), Vec<FulfillmentError<'tcx>>>;
57 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
60 pub trait TraitEngineExt<'tcx> {
61 fn register_predicate_obligations(
63 infcx: &InferCtxt<'_, 'tcx>,
64 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
68 impl<T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
69 fn register_predicate_obligations(
71 infcx: &InferCtxt<'_, 'tcx>,
72 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
74 for obligation in obligations {
75 self.register_predicate_obligation(infcx, obligation);
80 impl dyn TraitEngine<'tcx> {
81 pub fn new(tcx: TyCtxt<'tcx>) -> Box<Self> {
82 if tcx.sess.opts.debugging_opts.chalk {
83 Box::new(ChalkFulfillmentContext::new())
85 Box::new(FulfillmentContext::new())