]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/traits/engine.rs
Reformat everything
[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>)
51     -> Vec<FulfillmentError<'tcx>>;
52
53     fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
54
55     fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships>;
56 }
57
58 pub trait TraitEngineExt<'tcx> {
59     fn register_predicate_obligations(
60         &mut self,
61         infcx: &InferCtxt<'_, 'tcx>,
62         obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
63     );
64 }
65
66 impl<T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
67     fn register_predicate_obligations(
68         &mut self,
69         infcx: &InferCtxt<'_, 'tcx>,
70         obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
71     ) {
72         for obligation in obligations {
73             self.register_predicate_obligation(infcx, obligation);
74         }
75     }
76 }