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 sub_unification_table_root_var(&self, var: ty::TyVid) -> ty::TyVid {
63        self.sub_unification_table_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 sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
187        self.sub_unify_ty_vids_raw(a, b);
188    }
189
190    fn equate_int_vids_raw(&self, a: ty::IntVid, b: ty::IntVid) {
191        self.inner.borrow_mut().int_unification_table().union(a, b);
192    }
193
194    fn equate_float_vids_raw(&self, a: ty::FloatVid, b: ty::FloatVid) {
195        self.inner.borrow_mut().float_unification_table().union(a, b);
196    }
197
198    fn equate_const_vids_raw(&self, a: ty::ConstVid, b: ty::ConstVid) {
199        self.inner.borrow_mut().const_unification_table().union(a, b);
200    }
201
202    fn instantiate_ty_var_raw<R: PredicateEmittingRelation<Self>>(
203        &self,
204        relation: &mut R,
205        target_is_expected: bool,
206        target_vid: ty::TyVid,
207        instantiation_variance: ty::Variance,
208        source_ty: Ty<'tcx>,
209    ) -> RelateResult<'tcx, ()> {
210        self.instantiate_ty_var(
211            relation,
212            target_is_expected,
213            target_vid,
214            instantiation_variance,
215            source_ty,
216        )
217    }
218
219    fn instantiate_int_var_raw(&self, vid: ty::IntVid, value: ty::IntVarValue) {
220        self.inner.borrow_mut().int_unification_table().union_value(vid, value);
221    }
222
223    fn instantiate_float_var_raw(&self, vid: ty::FloatVid, value: ty::FloatVarValue) {
224        self.inner.borrow_mut().float_unification_table().union_value(vid, value);
225    }
226
227    fn instantiate_const_var_raw<R: PredicateEmittingRelation<Self>>(
228        &self,
229        relation: &mut R,
230        target_is_expected: bool,
231        target_vid: ty::ConstVid,
232        source_ct: ty::Const<'tcx>,
233    ) -> RelateResult<'tcx, ()> {
234        self.instantiate_const_var(relation, target_is_expected, target_vid, source_ct)
235    }
236
237    fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
238        self.set_tainted_by_errors(e)
239    }
240
241    fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
242        self.shallow_resolve(ty)
243    }
244    fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
245        self.shallow_resolve_const(ct)
246    }
247
248    fn resolve_vars_if_possible<T>(&self, value: T) -> T
249    where
250        T: TypeFoldable<TyCtxt<'tcx>>,
251    {
252        self.resolve_vars_if_possible(value)
253    }
254
255    fn probe<T>(&self, probe: impl FnOnce() -> T) -> T {
256        self.probe(|_| probe())
257    }
258
259    fn sub_regions(&self, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>, span: Span) {
260        self.inner.borrow_mut().unwrap_region_constraints().make_subregion(
261            SubregionOrigin::RelateRegionParamBound(span, None),
262            sub,
263            sup,
264        );
265    }
266
267    fn equate_regions(&self, a: ty::Region<'tcx>, b: ty::Region<'tcx>, span: Span) {
268        self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(
269            SubregionOrigin::RelateRegionParamBound(span, None),
270            a,
271            b,
272        );
273    }
274
275    fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>, span: Span) {
276        self.register_type_outlives_constraint(ty, r, &ObligationCause::dummy_with_span(span));
277    }
278
279    type OpaqueTypeStorageEntries = OpaqueTypeStorageEntries;
280    fn opaque_types_storage_num_entries(&self) -> OpaqueTypeStorageEntries {
281        self.inner.borrow_mut().opaque_types().num_entries()
282    }
283    fn clone_opaque_types_lookup_table(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
284        self.inner.borrow_mut().opaque_types().iter_lookup_table().map(|(k, h)| (k, h.ty)).collect()
285    }
286    fn clone_duplicate_opaque_types(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
287        self.inner
288            .borrow_mut()
289            .opaque_types()
290            .iter_duplicate_entries()
291            .map(|(k, h)| (k, h.ty))
292            .collect()
293    }
294    fn clone_opaque_types_added_since(
295        &self,
296        prev_entries: OpaqueTypeStorageEntries,
297    ) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
298        self.inner
299            .borrow_mut()
300            .opaque_types()
301            .opaque_types_added_since(prev_entries)
302            .map(|(k, h)| (k, h.ty))
303            .collect()
304    }
305
306    fn register_hidden_type_in_storage(
307        &self,
308        opaque_type_key: ty::OpaqueTypeKey<'tcx>,
309        hidden_ty: Ty<'tcx>,
310        span: Span,
311    ) -> Option<Ty<'tcx>> {
312        self.register_hidden_type_in_storage(
313            opaque_type_key,
314            ty::OpaqueHiddenType { span, ty: hidden_ty },
315        )
316    }
317    fn add_duplicate_opaque_type(
318        &self,
319        opaque_type_key: ty::OpaqueTypeKey<'tcx>,
320        hidden_ty: Ty<'tcx>,
321        span: Span,
322    ) {
323        self.inner
324            .borrow_mut()
325            .opaque_types()
326            .add_duplicate(opaque_type_key, ty::OpaqueHiddenType { span, ty: hidden_ty })
327    }
328
329    fn reset_opaque_types(&self) {
330        let _ = self.take_opaque_types();
331    }
332}