]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_infer/src/traits/engine.rs
New upstream version 1.57.0+dfsg1
[rustc.git] / compiler / rustc_infer / src / traits / engine.rs
CommitLineData
9fa01778 1use crate::infer::InferCtxt;
9fa01778 2use crate::traits::Obligation;
c295e0f8 3use rustc_data_structures::fx::FxHashMap;
94222f64 4use rustc_hir as hir;
dfeec247 5use rustc_hir::def_id::DefId;
ba9703b0 6use rustc_middle::ty::{self, ToPredicate, Ty, WithConstness};
0531ce1d 7
ba9703b0 8use super::FulfillmentError;
94b46f34 9use super::{ObligationCause, PredicateObligation};
0531ce1d
XL
10
11pub trait TraitEngine<'tcx>: 'tcx {
8faf50e0 12 fn normalize_projection_type(
0531ce1d 13 &mut self,
dc9dc135 14 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
15 param_env: ty::ParamEnv<'tcx>,
16 projection_ty: ty::ProjectionTy<'tcx>,
17 cause: ObligationCause<'tcx>,
18 ) -> Ty<'tcx>;
19
0731742a
XL
20 /// Requires that `ty` must implement the trait with `def_id` in
21 /// the given environment. This trait must not have any type
22 /// parameters (except for `Self`).
8faf50e0 23 fn register_bound(
0531ce1d 24 &mut self,
dc9dc135 25 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
26 param_env: ty::ParamEnv<'tcx>,
27 ty: Ty<'tcx>,
28 def_id: DefId,
29 cause: ObligationCause<'tcx>,
0731742a 30 ) {
dfeec247
XL
31 let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
32 self.register_predicate_obligation(
33 infcx,
34 Obligation {
35 cause,
36 recursion_depth: 0,
37 param_env,
c295e0f8 38 predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(infcx.tcx),
dfeec247
XL
39 },
40 );
0731742a 41 }
0531ce1d 42
8faf50e0 43 fn register_predicate_obligation(
0531ce1d 44 &mut self,
dc9dc135 45 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
46 obligation: PredicateObligation<'tcx>,
47 );
48
8faf50e0 49 fn select_all_or_error(
0531ce1d 50 &mut self,
dc9dc135 51 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
52 ) -> Result<(), Vec<FulfillmentError<'tcx>>>;
53
94222f64
XL
54 fn select_all_with_constness_or_error(
55 &mut self,
56 infcx: &InferCtxt<'_, 'tcx>,
57 _constness: hir::Constness,
58 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
59 self.select_all_or_error(infcx)
60 }
61
8faf50e0 62 fn select_where_possible(
0531ce1d 63 &mut self,
dc9dc135 64 infcx: &InferCtxt<'_, 'tcx>,
0531ce1d
XL
65 ) -> Result<(), Vec<FulfillmentError<'tcx>>>;
66
94222f64
XL
67 // FIXME(fee1-dead) this should not provide a default body for chalk as chalk should be updated
68 fn select_with_constness_where_possible(
69 &mut self,
70 infcx: &InferCtxt<'_, 'tcx>,
71 _constness: hir::Constness,
72 ) -> Result<(), Vec<FulfillmentError<'tcx>>> {
73 self.select_where_possible(infcx)
74 }
75
94b46f34 76 fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
c295e0f8
XL
77
78 fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships>;
0531ce1d
XL
79}
80
8faf50e0
XL
81pub trait TraitEngineExt<'tcx> {
82 fn register_predicate_obligations(
83 &mut self,
dc9dc135 84 infcx: &InferCtxt<'_, 'tcx>,
8faf50e0
XL
85 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
86 );
87}
0531ce1d 88
8faf50e0
XL
89impl<T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
90 fn register_predicate_obligations(
0531ce1d 91 &mut self,
dc9dc135 92 infcx: &InferCtxt<'_, 'tcx>,
8faf50e0
XL
93 obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
94 ) {
0531ce1d
XL
95 for obligation in obligations {
96 self.register_predicate_obligation(infcx, obligation);
97 }
98 }
99}