]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/engine.rs
Rollup merge of #100462 - zohnannor:master, r=thomcc
[rust.git] / compiler / rustc_trait_selection / src / traits / engine.rs
1 use std::cell::RefCell;
2
3 use super::TraitEngine;
4 use super::{ChalkFulfillmentContext, FulfillmentContext};
5 use crate::infer::InferCtxtExt;
6 use rustc_data_structures::fx::FxHashSet;
7 use rustc_hir::def_id::{DefId, LocalDefId};
8 use rustc_infer::infer::{InferCtxt, InferOk};
9 use rustc_infer::traits::{
10     FulfillmentError, Obligation, ObligationCause, PredicateObligation, TraitEngineExt as _,
11 };
12 use rustc_middle::ty::error::TypeError;
13 use rustc_middle::ty::ToPredicate;
14 use rustc_middle::ty::TypeFoldable;
15 use rustc_middle::ty::{self, Ty, TyCtxt};
16 use rustc_span::Span;
17
18 pub trait TraitEngineExt<'tcx> {
19     fn new(tcx: TyCtxt<'tcx>) -> Box<Self>;
20     fn new_in_snapshot(tcx: TyCtxt<'tcx>) -> Box<Self>;
21 }
22
23 impl<'tcx> TraitEngineExt<'tcx> for dyn TraitEngine<'tcx> {
24     fn new(tcx: TyCtxt<'tcx>) -> Box<Self> {
25         if tcx.sess.opts.unstable_opts.chalk {
26             Box::new(ChalkFulfillmentContext::new())
27         } else {
28             Box::new(FulfillmentContext::new())
29         }
30     }
31
32     fn new_in_snapshot(tcx: TyCtxt<'tcx>) -> Box<Self> {
33         if tcx.sess.opts.unstable_opts.chalk {
34             Box::new(ChalkFulfillmentContext::new())
35         } else {
36             Box::new(FulfillmentContext::new_in_snapshot())
37         }
38     }
39 }
40
41 /// Used if you want to have pleasant experience when dealing
42 /// with obligations outside of hir or mir typeck.
43 pub struct ObligationCtxt<'a, 'tcx> {
44     pub infcx: &'a InferCtxt<'tcx>,
45     engine: RefCell<Box<dyn TraitEngine<'tcx>>>,
46 }
47
48 impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
49     pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
50         Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new(infcx.tcx)) }
51     }
52
53     pub fn new_in_snapshot(infcx: &'a InferCtxt<'tcx>) -> Self {
54         Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new_in_snapshot(infcx.tcx)) }
55     }
56
57     pub fn register_obligation(&self, obligation: PredicateObligation<'tcx>) {
58         self.engine.borrow_mut().register_predicate_obligation(self.infcx, obligation);
59     }
60
61     pub fn register_obligations(
62         &self,
63         obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
64     ) {
65         // Can't use `register_predicate_obligations` because the iterator
66         // may also use this `ObligationCtxt`.
67         for obligation in obligations {
68             self.engine.borrow_mut().register_predicate_obligation(self.infcx, obligation)
69         }
70     }
71
72     pub fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
73         let InferOk { value, obligations } = infer_ok;
74         self.engine.borrow_mut().register_predicate_obligations(self.infcx, obligations);
75         value
76     }
77
78     /// Requires that `ty` must implement the trait with `def_id` in
79     /// the given environment. This trait must not have any type
80     /// parameters (except for `Self`).
81     pub fn register_bound(
82         &self,
83         cause: ObligationCause<'tcx>,
84         param_env: ty::ParamEnv<'tcx>,
85         ty: Ty<'tcx>,
86         def_id: DefId,
87     ) {
88         let tcx = self.infcx.tcx;
89         let trait_ref = ty::TraitRef { def_id, substs: tcx.mk_substs_trait(ty, &[]) };
90         self.register_obligation(Obligation {
91             cause,
92             recursion_depth: 0,
93             param_env,
94             predicate: ty::Binder::dummy(trait_ref).without_const().to_predicate(tcx),
95         });
96     }
97
98     pub fn normalize<T: TypeFoldable<'tcx>>(
99         &self,
100         cause: ObligationCause<'tcx>,
101         param_env: ty::ParamEnv<'tcx>,
102         value: T,
103     ) -> T {
104         let infer_ok = self.infcx.partially_normalize_associated_types_in(cause, param_env, value);
105         self.register_infer_ok_obligations(infer_ok)
106     }
107
108     pub fn equate_types(
109         &self,
110         cause: &ObligationCause<'tcx>,
111         param_env: ty::ParamEnv<'tcx>,
112         expected: Ty<'tcx>,
113         actual: Ty<'tcx>,
114     ) -> Result<(), TypeError<'tcx>> {
115         match self.infcx.at(cause, param_env).eq(expected, actual) {
116             Ok(InferOk { obligations, value: () }) => {
117                 self.register_obligations(obligations);
118                 Ok(())
119             }
120             Err(e) => Err(e),
121         }
122     }
123
124     pub fn select_all_or_error(&self) -> Vec<FulfillmentError<'tcx>> {
125         self.engine.borrow_mut().select_all_or_error(self.infcx)
126     }
127
128     pub fn assumed_wf_types(
129         &self,
130         param_env: ty::ParamEnv<'tcx>,
131         span: Span,
132         def_id: LocalDefId,
133     ) -> FxHashSet<Ty<'tcx>> {
134         let tcx = self.infcx.tcx;
135         let assumed_wf_types = tcx.assumed_wf_types(def_id);
136         let mut implied_bounds = FxHashSet::default();
137         let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
138         let cause = ObligationCause::misc(span, hir_id);
139         for ty in assumed_wf_types {
140             // FIXME(@lcnr): rustc currently does not check wf for types
141             // pre-normalization, meaning that implied bounds are sometimes
142             // incorrect. See #100910 for more details.
143             //
144             // Not adding the unnormalized types here mostly fixes that, except
145             // that there are projections which are still ambiguous in the item definition
146             // but do normalize successfully when using the item, see #98543.
147             //
148             // Anyways, I will hopefully soon change implied bounds to make all of this
149             // sound and then uncomment this line again.
150
151             // implied_bounds.insert(ty);
152             let normalized = self.normalize(cause.clone(), param_env, ty);
153             implied_bounds.insert(normalized);
154         }
155         implied_bounds
156     }
157 }