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 in_hir_typeck(&self) -> bool {
26        self.in_hir_typeck
27    }
28
29    fn typing_mode(&self) -> ty::TypingMode<'tcx> {
30        self.typing_mode()
31    }
32
33    fn universe(&self) -> ty::UniverseIndex {
34        self.universe()
35    }
36
37    fn create_next_universe(&self) -> ty::UniverseIndex {
38        self.create_next_universe()
39    }
40
41    fn universe_of_ty(&self, vid: ty::TyVid) -> Option<ty::UniverseIndex> {
42        match self.probe_ty_var(vid) {
43            Err(universe) => Some(universe),
44            Ok(_) => None,
45        }
46    }
47
48    fn universe_of_lt(&self, lt: ty::RegionVid) -> Option<ty::UniverseIndex> {
49        match self.inner.borrow_mut().unwrap_region_constraints().probe_value(lt) {
50            Err(universe) => Some(universe),
51            Ok(_) => None,
52        }
53    }
54
55    fn universe_of_ct(&self, ct: ty::ConstVid) -> Option<ty::UniverseIndex> {
56        match self.probe_const_var(ct) {
57            Err(universe) => Some(universe),
58            Ok(_) => None,
59        }
60    }
61
62    fn root_ty_var(&self, var: ty::TyVid) -> ty::TyVid {
63        self.root_var(var)
64    }
65
66    fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
67        self.root_const_var(var)
68    }
69
70    fn opportunistic_resolve_ty_var(&self, vid: ty::TyVid) -> Ty<'tcx> {
71        match self.probe_ty_var(vid) {
72            Ok(ty) => ty,
73            Err(_) => Ty::new_var(self.tcx, self.root_var(vid)),
74        }
75    }
76
77    fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
78        self.opportunistic_resolve_int_var(vid)
79    }
80
81    fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
82        self.opportunistic_resolve_float_var(vid)
83    }
84
85    fn opportunistic_resolve_ct_var(&self, vid: ty::ConstVid) -> ty::Const<'tcx> {
86        match self.probe_const_var(vid) {
87            Ok(ct) => ct,
88            Err(_) => ty::Const::new_var(self.tcx, self.root_const_var(vid)),
89        }
90    }
91
92    fn opportunistic_resolve_lt_var(&self, vid: ty::RegionVid) -> ty::Region<'tcx> {
93        self.inner.borrow_mut().unwrap_region_constraints().opportunistic_resolve_var(self.tcx, vid)
94    }
95
96    fn is_changed_arg(&self, arg: ty::GenericArg<'tcx>) -> bool {
97        match arg.kind() {
98            ty::GenericArgKind::Lifetime(_) => {
99                // Lifetimes should not change affect trait selection.
100                false
101            }
102            ty::GenericArgKind::Type(ty) => {
103                if let ty::Infer(infer_ty) = *ty.kind() {
104                    match infer_ty {
105                        ty::InferTy::TyVar(vid) => {
106                            !self.probe_ty_var(vid).is_err_and(|_| self.root_var(vid) == vid)
107                        }
108                        ty::InferTy::IntVar(vid) => {
109                            let mut inner = self.inner.borrow_mut();
110                            !matches!(
111                                inner.int_unification_table().probe_value(vid),
112                                ty::IntVarValue::Unknown
113                                    if inner.int_unification_table().find(vid) == vid
114                            )
115                        }
116                        ty::InferTy::FloatVar(vid) => {
117                            let mut inner = self.inner.borrow_mut();
118                            !matches!(
119                                inner.float_unification_table().probe_value(vid),
120                                ty::FloatVarValue::Unknown
121                                    if inner.float_unification_table().find(vid) == vid
122                            )
123                        }
124                        ty::InferTy::FreshTy(_)
125                        | ty::InferTy::FreshIntTy(_)
126                        | ty::InferTy::FreshFloatTy(_) => true,
127                    }
128                } else {
129                    true
130                }
131            }
132            ty::GenericArgKind::Const(ct) => {
133                if let ty::ConstKind::Infer(infer_ct) = ct.kind() {
134                    match infer_ct {
135                        ty::InferConst::Var(vid) => !self
136                            .probe_const_var(vid)
137                            .is_err_and(|_| self.root_const_var(vid) == vid),
138                        ty::InferConst::Fresh(_) => true,
139                    }
140                } else {
141                    true
142                }
143            }
144        }
145    }
146
147    fn next_region_infer(&self) -> ty::Region<'tcx> {
148        self.next_region_var(RegionVariableOrigin::Misc(DUMMY_SP))
149    }
150
151    fn next_ty_infer(&self) -> Ty<'tcx> {
152        self.next_ty_var(DUMMY_SP)
153    }
154
155    fn next_const_infer(&self) -> ty::Const<'tcx> {
156        self.next_const_var(DUMMY_SP)
157    }
158
159    fn fresh_args_for_item(&self, def_id: DefId) -> ty::GenericArgsRef<'tcx> {
160        self.fresh_args_for_item(DUMMY_SP, def_id)
161    }
162
163    fn instantiate_binder_with_infer<T: TypeFoldable<TyCtxt<'tcx>> + Copy>(
164        &self,
165        value: ty::Binder<'tcx, T>,
166    ) -> T {
167        self.instantiate_binder_with_fresh_vars(
168            DUMMY_SP,
169            BoundRegionConversionTime::HigherRankedType,
170            value,
171        )
172    }
173
174    fn enter_forall<T: TypeFoldable<TyCtxt<'tcx>>, U>(
175        &self,
176        value: ty::Binder<'tcx, T>,
177        f: impl FnOnce(T) -> U,
178    ) -> U {
179        self.enter_forall(value, f)
180    }
181
182    fn equate_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
183        self.inner.borrow_mut().type_variables().equate(a, b);
184    }
185
186    fn equate_int_vids_raw(&self, a: ty::IntVid, b: ty::IntVid) {
187        self.inner.borrow_mut().int_unification_table().union(a, b);
188    }
189
190    fn equate_float_vids_raw(&self, a: ty::FloatVid, b: ty::FloatVid) {
191        self.inner.borrow_mut().float_unification_table().union(a, b);
192    }
193
194    fn equate_const_vids_raw(&self, a: ty::ConstVid, b: ty::ConstVid) {
195        self.inner.borrow_mut().const_unification_table().union(a, b);
196    }
197
198    fn instantiate_ty_var_raw<R: PredicateEmittingRelation<Self>>(
199        &self,
200        relation: &mut R,
201        target_is_expected: bool,
202        target_vid: ty::TyVid,
203        instantiation_variance: ty::Variance,
204        source_ty: Ty<'tcx>,
205    ) -> RelateResult<'tcx, ()> {
206        self.instantiate_ty_var(
207            relation,
208            target_is_expected,
209            target_vid,
210            instantiation_variance,
211            source_ty,
212        )
213    }
214
215    fn instantiate_int_var_raw(&self, vid: ty::IntVid, value: ty::IntVarValue) {
216        self.inner.borrow_mut().int_unification_table().union_value(vid, value);
217    }
218
219    fn instantiate_float_var_raw(&self, vid: ty::FloatVid, value: ty::FloatVarValue) {
220        self.inner.borrow_mut().float_unification_table().union_value(vid, value);
221    }
222
223    fn instantiate_const_var_raw<R: PredicateEmittingRelation<Self>>(
224        &self,
225        relation: &mut R,
226        target_is_expected: bool,
227        target_vid: ty::ConstVid,
228        source_ct: ty::Const<'tcx>,
229    ) -> RelateResult<'tcx, ()> {
230        self.instantiate_const_var(relation, target_is_expected, target_vid, source_ct)
231    }
232
233    fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
234        self.set_tainted_by_errors(e)
235    }
236
237    fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
238        self.shallow_resolve(ty)
239    }
240    fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
241        self.shallow_resolve_const(ct)
242    }
243
244    fn resolve_vars_if_possible<T>(&self, value: T) -> T
245    where
246        T: TypeFoldable<TyCtxt<'tcx>>,
247    {
248        self.resolve_vars_if_possible(value)
249    }
250
251    fn probe<T>(&self, probe: impl FnOnce() -> T) -> T {
252        self.probe(|_| probe())
253    }
254
255    fn sub_regions(&self, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>, span: Span) {
256        self.inner.borrow_mut().unwrap_region_constraints().make_subregion(
257            SubregionOrigin::RelateRegionParamBound(span, None),
258            sub,
259            sup,
260        );
261    }
262
263    fn equate_regions(&self, a: ty::Region<'tcx>, b: ty::Region<'tcx>, span: Span) {
264        self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(
265            SubregionOrigin::RelateRegionParamBound(span, None),
266            a,
267            b,
268        );
269    }
270
271    fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>, span: Span) {
272        self.register_type_outlives_constraint(ty, r, &ObligationCause::dummy_with_span(span));
273    }
274
275    type OpaqueTypeStorageEntries = OpaqueTypeStorageEntries;
276    fn opaque_types_storage_num_entries(&self) -> OpaqueTypeStorageEntries {
277        self.inner.borrow_mut().opaque_types().num_entries()
278    }
279    fn clone_opaque_types_lookup_table(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
280        self.inner.borrow_mut().opaque_types().iter_lookup_table().map(|(k, h)| (k, h.ty)).collect()
281    }
282    fn clone_duplicate_opaque_types(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
283        self.inner
284            .borrow_mut()
285            .opaque_types()
286            .iter_duplicate_entries()
287            .map(|(k, h)| (k, h.ty))
288            .collect()
289    }
290    fn clone_opaque_types_added_since(
291        &self,
292        prev_entries: OpaqueTypeStorageEntries,
293    ) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
294        self.inner
295            .borrow_mut()
296            .opaque_types()
297            .opaque_types_added_since(prev_entries)
298            .map(|(k, h)| (k, h.ty))
299            .collect()
300    }
301
302    fn register_hidden_type_in_storage(
303        &self,
304        opaque_type_key: ty::OpaqueTypeKey<'tcx>,
305        hidden_ty: Ty<'tcx>,
306        span: Span,
307    ) -> Option<Ty<'tcx>> {
308        self.register_hidden_type_in_storage(
309            opaque_type_key,
310            ty::OpaqueHiddenType { span, ty: hidden_ty },
311        )
312    }
313    fn add_duplicate_opaque_type(
314        &self,
315        opaque_type_key: ty::OpaqueTypeKey<'tcx>,
316        hidden_ty: Ty<'tcx>,
317        span: Span,
318    ) {
319        self.inner
320            .borrow_mut()
321            .opaque_types()
322            .add_duplicate(opaque_type_key, ty::OpaqueHiddenType { span, ty: hidden_ty })
323    }
324
325    fn reset_opaque_types(&self) {
326        let _ = self.take_opaque_types();
327    }
328}