rustc_infer/infer/
context.rs

1///! Definition of `InferCtxtLike` from the librarified type layer.
2use rustc_hir::def_id::DefId;
3use rustc_middle::traits::ObligationCause;
4use rustc_middle::ty::relate::RelateResult;
5use rustc_middle::ty::relate::combine::PredicateEmittingRelation;
6use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
7use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
8
9use super::{
10    BoundRegionConversionTime, InferCtxt, OpaqueTypeStorageEntries, RegionVariableOrigin,
11    SubregionOrigin,
12};
13
14impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
15    type Interner = TyCtxt<'tcx>;
16
17    fn cx(&self) -> TyCtxt<'tcx> {
18        self.tcx
19    }
20
21    fn next_trait_solver(&self) -> bool {
22        self.next_trait_solver
23    }
24
25    fn typing_mode(&self) -> ty::TypingMode<'tcx> {
26        self.typing_mode()
27    }
28
29    fn universe(&self) -> ty::UniverseIndex {
30        self.universe()
31    }
32
33    fn create_next_universe(&self) -> ty::UniverseIndex {
34        self.create_next_universe()
35    }
36
37    fn universe_of_ty(&self, vid: ty::TyVid) -> Option<ty::UniverseIndex> {
38        match self.probe_ty_var(vid) {
39            Err(universe) => Some(universe),
40            Ok(_) => None,
41        }
42    }
43
44    fn universe_of_lt(&self, lt: ty::RegionVid) -> Option<ty::UniverseIndex> {
45        match self.inner.borrow_mut().unwrap_region_constraints().probe_value(lt) {
46            Err(universe) => Some(universe),
47            Ok(_) => None,
48        }
49    }
50
51    fn universe_of_ct(&self, ct: ty::ConstVid) -> Option<ty::UniverseIndex> {
52        match self.probe_const_var(ct) {
53            Err(universe) => Some(universe),
54            Ok(_) => None,
55        }
56    }
57
58    fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid {
59        self.root_var(var)
60    }
61
62    fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
63        self.root_const_var(var)
64    }
65
66    fn opportunistic_resolve_ty_var(&self, vid: ty::TyVid) -> Ty<'tcx> {
67        match self.probe_ty_var(vid) {
68            Ok(ty) => ty,
69            Err(_) => Ty::new_var(self.tcx, self.root_var(vid)),
70        }
71    }
72
73    fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
74        self.opportunistic_resolve_int_var(vid)
75    }
76
77    fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
78        self.opportunistic_resolve_float_var(vid)
79    }
80
81    fn opportunistic_resolve_ct_var(&self, vid: ty::ConstVid) -> ty::Const<'tcx> {
82        match self.probe_const_var(vid) {
83            Ok(ct) => ct,
84            Err(_) => ty::Const::new_var(self.tcx, self.root_const_var(vid)),
85        }
86    }
87
88    fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> ty::Region<'tcx> {
89        self.inner.borrow_mut().unwrap_region_constraints().opportunistic_resolve_var(self.tcx, vid)
90    }
91
92    fn next_region_infer(&self) -> ty::Region<'tcx> {
93        self.next_region_var(RegionVariableOrigin::MiscVariable(DUMMY_SP))
94    }
95
96    fn next_ty_infer(&self) -> Ty<'tcx> {
97        self.next_ty_var(DUMMY_SP)
98    }
99
100    fn next_const_infer(&self) -> ty::Const<'tcx> {
101        self.next_const_var(DUMMY_SP)
102    }
103
104    fn fresh_args_for_item(&self, def_id: DefId) -> ty::GenericArgsRef<'tcx> {
105        self.fresh_args_for_item(DUMMY_SP, def_id)
106    }
107
108    fn instantiate_binder_with_infer<T: TypeFoldable<TyCtxt<'tcx>> + Copy>(
109        &self,
110        value: ty::Binder<'tcx, T>,
111    ) -> T {
112        self.instantiate_binder_with_fresh_vars(
113            DUMMY_SP,
114            BoundRegionConversionTime::HigherRankedType,
115            value,
116        )
117    }
118
119    fn enter_forall<T: TypeFoldable<TyCtxt<'tcx>>, U>(
120        &self,
121        value: ty::Binder<'tcx, T>,
122        f: impl FnOnce(T) -> U,
123    ) -> U {
124        self.enter_forall(value, f)
125    }
126
127    fn equate_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
128        self.inner.borrow_mut().type_variables().equate(a, b);
129    }
130
131    fn equate_int_vids_raw(&self, a: ty::IntVid, b: ty::IntVid) {
132        self.inner.borrow_mut().int_unification_table().union(a, b);
133    }
134
135    fn equate_float_vids_raw(&self, a: ty::FloatVid, b: ty::FloatVid) {
136        self.inner.borrow_mut().float_unification_table().union(a, b);
137    }
138
139    fn equate_const_vids_raw(&self, a: ty::ConstVid, b: ty::ConstVid) {
140        self.inner.borrow_mut().const_unification_table().union(a, b);
141    }
142
143    fn instantiate_ty_var_raw<R: PredicateEmittingRelation<Self>>(
144        &self,
145        relation: &mut R,
146        target_is_expected: bool,
147        target_vid: ty::TyVid,
148        instantiation_variance: ty::Variance,
149        source_ty: Ty<'tcx>,
150    ) -> RelateResult<'tcx, ()> {
151        self.instantiate_ty_var(
152            relation,
153            target_is_expected,
154            target_vid,
155            instantiation_variance,
156            source_ty,
157        )
158    }
159
160    fn instantiate_int_var_raw(&self, vid: ty::IntVid, value: ty::IntVarValue) {
161        self.inner.borrow_mut().int_unification_table().union_value(vid, value);
162    }
163
164    fn instantiate_float_var_raw(&self, vid: ty::FloatVid, value: ty::FloatVarValue) {
165        self.inner.borrow_mut().float_unification_table().union_value(vid, value);
166    }
167
168    fn instantiate_const_var_raw<R: PredicateEmittingRelation<Self>>(
169        &self,
170        relation: &mut R,
171        target_is_expected: bool,
172        target_vid: ty::ConstVid,
173        source_ct: ty::Const<'tcx>,
174    ) -> RelateResult<'tcx, ()> {
175        self.instantiate_const_var(relation, target_is_expected, target_vid, source_ct)
176    }
177
178    fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
179        self.set_tainted_by_errors(e)
180    }
181
182    fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
183        self.shallow_resolve(ty)
184    }
185    fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
186        self.shallow_resolve_const(ct)
187    }
188
189    fn resolve_vars_if_possible<T>(&self, value: T) -> T
190    where
191        T: TypeFoldable<TyCtxt<'tcx>>,
192    {
193        self.resolve_vars_if_possible(value)
194    }
195
196    fn probe<T>(&self, probe: impl FnOnce() -> T) -> T {
197        self.probe(|_| probe())
198    }
199
200    fn sub_regions(&self, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>, span: Span) {
201        self.inner.borrow_mut().unwrap_region_constraints().make_subregion(
202            SubregionOrigin::RelateRegionParamBound(span, None),
203            sub,
204            sup,
205        );
206    }
207
208    fn equate_regions(&self, a: ty::Region<'tcx>, b: ty::Region<'tcx>, span: Span) {
209        self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(
210            SubregionOrigin::RelateRegionParamBound(span, None),
211            a,
212            b,
213        );
214    }
215
216    fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>, span: Span) {
217        self.register_region_obligation_with_cause(ty, r, &ObligationCause::dummy_with_span(span));
218    }
219
220    type OpaqueTypeStorageEntries = OpaqueTypeStorageEntries;
221    fn opaque_types_storage_num_entries(&self) -> OpaqueTypeStorageEntries {
222        self.inner.borrow_mut().opaque_types().num_entries()
223    }
224    fn clone_opaque_types_lookup_table(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
225        self.inner.borrow_mut().opaque_types().iter_lookup_table().map(|(k, h)| (k, h.ty)).collect()
226    }
227    fn clone_duplicate_opaque_types(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
228        self.inner
229            .borrow_mut()
230            .opaque_types()
231            .iter_duplicate_entries()
232            .map(|(k, h)| (k, h.ty))
233            .collect()
234    }
235    fn clone_opaque_types_added_since(
236        &self,
237        prev_entries: OpaqueTypeStorageEntries,
238    ) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
239        self.inner
240            .borrow_mut()
241            .opaque_types()
242            .opaque_types_added_since(prev_entries)
243            .map(|(k, h)| (k, h.ty))
244            .collect()
245    }
246
247    fn register_hidden_type_in_storage(
248        &self,
249        opaque_type_key: ty::OpaqueTypeKey<'tcx>,
250        hidden_ty: Ty<'tcx>,
251        span: Span,
252    ) -> Option<Ty<'tcx>> {
253        self.register_hidden_type_in_storage(
254            opaque_type_key,
255            ty::OpaqueHiddenType { span, ty: hidden_ty },
256        )
257    }
258    fn add_duplicate_opaque_type(
259        &self,
260        opaque_type_key: ty::OpaqueTypeKey<'tcx>,
261        hidden_ty: Ty<'tcx>,
262        span: Span,
263    ) {
264        self.inner
265            .borrow_mut()
266            .opaque_types()
267            .add_duplicate(opaque_type_key, ty::OpaqueHiddenType { span, ty: hidden_ty })
268    }
269
270    fn reset_opaque_types(&self) {
271        let _ = self.take_opaque_types();
272    }
273}