]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/traits/engine.rs
Rollup merge of #104667 - WaffleLapkin:unfmttest, r=Dylan-DPC
[rust.git] / compiler / rustc_infer / src / traits / engine.rs
1 use crate::infer::InferCtxt;
2 use crate::traits::Obligation;
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_hir::def_id::DefId;
5 use rustc_middle::ty::{self, ToPredicate, Ty};
6
7 use super::FulfillmentError;
8 use super::{ObligationCause, PredicateObligation};
9
10 pub trait TraitEngine<'tcx>: 'tcx {
11     fn normalize_projection_type(
12         &mut self,
13         infcx: &InferCtxt<'tcx>,
14         param_env: ty::ParamEnv<'tcx>,
15         projection_ty: ty::ProjectionTy<'tcx>,
16         cause: ObligationCause<'tcx>,
17     ) -> Ty<'tcx>;
18
19     /// Requires that `ty` must implement the trait with `def_id` in
20     /// the given environment. This trait must not have any type
21     /// parameters (except for `Self`).
22     fn register_bound(
23         &mut self,
24         infcx: &InferCtxt<'tcx>,
25         param_env: ty::ParamEnv<'tcx>,
26         ty: Ty<'tcx>,
27         def_id: DefId,
28         cause: ObligationCause<'tcx>,
29     ) {
30         let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
31         self.register_predicate_obligation(
32             infcx,
33             Obligation {
34                 cause,
35                 recursion_depth: 0,
36                 param_env,
37                 predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(infcx.tcx),
38             },
39         );
40     }
41
42     fn register_predicate_obligation(
43         &mut self,
44         infcx: &InferCtxt<'tcx>,
45         obligation: PredicateObligation<'tcx>,
46     );
47
48     fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
49
50     fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
51
52     fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
53
54     fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships>;
55 }
56
57 pub trait TraitEngineExt<'tcx> {
58     fn register_predicate_obligations(
59         &mut self,
60         infcx: &InferCtxt<'tcx>,
61         obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
62     );
63 }
64
65 impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
66     fn register_predicate_obligations(
67         &mut self,
68         infcx: &InferCtxt<'tcx>,
69         obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
70     ) {
71         for obligation in obligations {
72             self.register_predicate_obligation(infcx, obligation);
73         }
74     }
75 }