]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/traits/engine.rs
Auto merge of #105924 - TimNN:ui-remap, r=Mark-Simulacrum
[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     /// Requires that `ty` must implement the trait with `def_id` in
12     /// the given environment. This trait must not have any type
13     /// parameters (except for `Self`).
14     fn register_bound(
15         &mut self,
16         infcx: &InferCtxt<'tcx>,
17         param_env: ty::ParamEnv<'tcx>,
18         ty: Ty<'tcx>,
19         def_id: DefId,
20         cause: ObligationCause<'tcx>,
21     ) {
22         let trait_ref = infcx.tcx.mk_trait_ref(def_id, [ty]);
23         self.register_predicate_obligation(
24             infcx,
25             Obligation {
26                 cause,
27                 recursion_depth: 0,
28                 param_env,
29                 predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(infcx.tcx),
30             },
31         );
32     }
33
34     fn register_predicate_obligation(
35         &mut self,
36         infcx: &InferCtxt<'tcx>,
37         obligation: PredicateObligation<'tcx>,
38     );
39
40     fn select_all_or_error(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
41
42     fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
43
44     fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
45
46     fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships>;
47 }
48
49 pub trait TraitEngineExt<'tcx> {
50     fn register_predicate_obligations(
51         &mut self,
52         infcx: &InferCtxt<'tcx>,
53         obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
54     );
55 }
56
57 impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
58     fn register_predicate_obligations(
59         &mut self,
60         infcx: &InferCtxt<'tcx>,
61         obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
62     ) {
63         for obligation in obligations {
64             self.register_predicate_obligation(infcx, obligation);
65         }
66     }
67 }