]> git.proxmox.com Git - rustc.git/blame - src/librustc_infer/traits/engine.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_infer / traits / engine.rs
CommitLineData
9fa01778 1use crate::infer::InferCtxt;
9fa01778 2use crate::traits::Obligation;
dfeec247 3use rustc_hir::def_id::DefId;
ba9703b0 4use rustc_middle::ty::{self, ToPredicate, Ty, WithConstness};
0531ce1d 5
ba9703b0 6use super::FulfillmentError;
94b46f34 7use super::{ObligationCause, PredicateObligation};
0531ce1d
XL
8
9pub trait TraitEngine<'tcx>: 'tcx {
8faf50e0 10 fn normalize_projection_type(
0531ce1d 11 &mut self,
dc9dc135 12 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
13 param_env: ty::ParamEnv<'tcx>,
14 projection_ty: ty::ProjectionTy<'tcx>,
15 cause: ObligationCause<'tcx>,
16 ) -> Ty<'tcx>;
17
0731742a
XL
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`).
8faf50e0 21 fn register_bound(
0531ce1d 22 &mut self,
dc9dc135 23 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
24 param_env: ty::ParamEnv<'tcx>,
25 ty: Ty<'tcx>,
26 def_id: DefId,
27 cause: ObligationCause<'tcx>,
0731742a 28 ) {
dfeec247
XL
29 let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
30 self.register_predicate_obligation(
31 infcx,
32 Obligation {
33 cause,
34 recursion_depth: 0,
35 param_env,
f9f354fc 36 predicate: trait_ref.without_const().to_predicate(infcx.tcx),
dfeec247
XL
37 },
38 );
0731742a 39 }
0531ce1d 40
8faf50e0 41 fn register_predicate_obligation(
0531ce1d 42 &mut self,
dc9dc135 43 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
44 obligation: PredicateObligation<'tcx>,
45 );
46
8faf50e0 47 fn select_all_or_error(
0531ce1d 48 &mut self,
dc9dc135 49 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
50 ) -> Result<(), Vec<FulfillmentError<'tcx>>>;
51
8faf50e0 52 fn select_where_possible(
0531ce1d 53 &mut self,
dc9dc135 54 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
55 ) -> Result<(), Vec<FulfillmentError<'tcx>>>;
56
94b46f34 57 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
0531ce1d
XL
58}
59
8faf50e0
XL
60pub trait TraitEngineExt<'tcx> {
61 fn register_predicate_obligations(
62 &mut self,
dc9dc135 63 infcx: &InferCtxt<'_, 'tcx>,
8faf50e0
XL
64 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
65 );
66}
0531ce1d 67
8faf50e0
XL
68impl<T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
69 fn register_predicate_obligations(
0531ce1d 70 &mut self,
dc9dc135 71 infcx: &InferCtxt<'_, 'tcx>,
8faf50e0
XL
72 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
73 ) {
0531ce1d
XL
74 for obligation in obligations {
75 self.register_predicate_obligation(infcx, obligation);
76 }
77 }
78}