Skip to main content

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.try_resolve_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.try_resolve_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.try_resolve_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.try_resolve_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.try_resolve_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                            !#[allow(non_exhaustive_omitted_patterns)] match inner.int_unification_table().probe_value(vid)
    {
    ty::IntVarValue::Unknown if inner.int_unification_table().find(vid) == vid
        => true,
    _ => false,
}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                            !#[allow(non_exhaustive_omitted_patterns)] match inner.float_unification_table().probe_value(vid)
    {
    ty::FloatVarValue::Unknown if
        inner.float_unification_table().find(vid) == vid => true,
    _ => false,
}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                            .try_resolve_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(
260        &self,
261        sub: ty::Region<'tcx>,
262        sup: ty::Region<'tcx>,
263        vis: ty::VisibleForLeakCheck,
264        span: Span,
265    ) {
266        self.inner.borrow_mut().unwrap_region_constraints().make_subregion(
267            SubregionOrigin::RelateRegionParamBound(span, None),
268            sub,
269            sup,
270            vis,
271        );
272    }
273
274    fn equate_regions(
275        &self,
276        a: ty::Region<'tcx>,
277        b: ty::Region<'tcx>,
278        vis: ty::VisibleForLeakCheck,
279        span: Span,
280    ) {
281        self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(
282            SubregionOrigin::RelateRegionParamBound(span, None),
283            a,
284            b,
285            vis,
286        );
287    }
288
289    fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>, span: Span) {
290        self.register_type_outlives_constraint(ty, r, &ObligationCause::dummy_with_span(span));
291    }
292
293    type OpaqueTypeStorageEntries = OpaqueTypeStorageEntries;
294    fn opaque_types_storage_num_entries(&self) -> OpaqueTypeStorageEntries {
295        self.inner.borrow_mut().opaque_types().num_entries()
296    }
297    fn clone_opaque_types_lookup_table(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
298        self.inner.borrow_mut().opaque_types().iter_lookup_table().map(|(k, h)| (k, h.ty)).collect()
299    }
300    fn clone_duplicate_opaque_types(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
301        self.inner
302            .borrow_mut()
303            .opaque_types()
304            .iter_duplicate_entries()
305            .map(|(k, h)| (k, h.ty))
306            .collect()
307    }
308    fn clone_opaque_types_added_since(
309        &self,
310        prev_entries: OpaqueTypeStorageEntries,
311    ) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
312        self.inner
313            .borrow_mut()
314            .opaque_types()
315            .opaque_types_added_since(prev_entries)
316            .map(|(k, h)| (k, h.ty))
317            .collect()
318    }
319    fn opaques_with_sub_unified_hidden_type(&self, ty: ty::TyVid) -> Vec<ty::AliasTy<'tcx>> {
320        self.opaques_with_sub_unified_hidden_type(ty)
321    }
322
323    fn register_hidden_type_in_storage(
324        &self,
325        opaque_type_key: ty::OpaqueTypeKey<'tcx>,
326        hidden_ty: Ty<'tcx>,
327        span: Span,
328    ) -> Option<Ty<'tcx>> {
329        self.register_hidden_type_in_storage(
330            opaque_type_key,
331            ty::ProvisionalHiddenType { span, ty: hidden_ty },
332        )
333    }
334    fn add_duplicate_opaque_type(
335        &self,
336        opaque_type_key: ty::OpaqueTypeKey<'tcx>,
337        hidden_ty: Ty<'tcx>,
338        span: Span,
339    ) {
340        self.inner
341            .borrow_mut()
342            .opaque_types()
343            .add_duplicate(opaque_type_key, ty::ProvisionalHiddenType { span, ty: hidden_ty })
344    }
345
346    fn reset_opaque_types(&self) {
347        let _ = self.take_opaque_types();
348    }
349}