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