|
1 | 1 | //! There are four type combiners: [TypeRelating], `Lub`, and `Glb`, |
2 | 2 | //! and `NllTypeRelating` in rustc_borrowck, which is only used for NLL. |
3 | 3 | //! |
4 | | -//! Each implements the trait [TypeRelation] and contains methods for |
5 | | -//! combining two instances of various things and yielding a new instance. |
6 | | -//! These combiner methods always yield a `Result<T>`. To relate two |
7 | | -//! types, you can use `infcx.at(cause, param_env)` which then allows |
8 | | -//! you to use the relevant methods of [At](crate::infer::at::At). |
| 4 | +//! Each implements the trait [TypeRelation](ty::relate::TypeRelation) and |
| 5 | +//! contains methods for combining two instances of various things and |
| 6 | +//! yielding a new instance. These combiner methods always yield a `Result<T>`. |
| 7 | +//! To relate two types, you can use `infcx.at(cause, param_env)` which |
| 8 | +//! then allows you to use the relevant methods of [At](crate::infer::at::At). |
9 | 9 | //! |
10 | 10 | //! Combiners mostly do their specific behavior and then hand off the |
11 | | -//! bulk of the work to [InferCtxt::super_combine_tys] and |
12 | | -//! [InferCtxt::super_combine_consts]. |
| 11 | +//! bulk of the work to [InferCtxtCombineExt::super_combine_tys] and |
| 12 | +//! [InferCtxtCombineExt::super_combine_consts]. |
13 | 13 | //! |
14 | 14 | //! Combining two types may have side-effects on the inference contexts |
15 | 15 | //! which can be undone by using snapshots. You probably want to use |
|
18 | 18 | //! On success, the LUB/GLB operations return the appropriate bound. The |
19 | 19 | //! return value of `Equate` or `Sub` shouldn't really be used. |
20 | 20 |
|
21 | | -use rustc_middle::bug; |
22 | | -use rustc_middle::infer::unify_key::EffectVarValue; |
23 | 21 | use rustc_middle::traits::solve::Goal; |
24 | | -use rustc_middle::ty::error::{ExpectedFound, TypeError}; |
25 | | -use rustc_middle::ty::{self, InferConst, IntType, Ty, TyCtxt, TypeVisitableExt, UintType, Upcast}; |
26 | | -pub use rustc_next_trait_solver::relate::combine::*; |
27 | | -use tracing::debug; |
| 22 | +use rustc_middle::ty::relate::StructurallyRelateAliases; |
| 23 | +pub use rustc_middle::ty::relate::combine::*; |
| 24 | +use rustc_middle::ty::{self, TyCtxt, Upcast}; |
28 | 25 |
|
29 | 26 | use super::lattice::{LatticeOp, LatticeOpKind}; |
30 | 27 | use super::type_relating::TypeRelating; |
31 | | -use super::{RelateResult, StructurallyRelateAliases}; |
32 | | -use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace, relate}; |
| 28 | +use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace}; |
33 | 29 | use crate::traits::{Obligation, PredicateObligation}; |
34 | 30 |
|
35 | 31 | #[derive(Clone)] |
@@ -71,222 +67,6 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { |
71 | 67 | } |
72 | 68 | } |
73 | 69 |
|
74 | | -impl<'tcx> InferCtxt<'tcx> { |
75 | | - pub fn super_combine_tys<R>( |
76 | | - &self, |
77 | | - relation: &mut R, |
78 | | - a: Ty<'tcx>, |
79 | | - b: Ty<'tcx>, |
80 | | - ) -> RelateResult<'tcx, Ty<'tcx>> |
81 | | - where |
82 | | - R: PredicateEmittingRelation<InferCtxt<'tcx>>, |
83 | | - { |
84 | | - debug!("super_combine_tys::<{}>({:?}, {:?})", std::any::type_name::<R>(), a, b); |
85 | | - debug_assert!(!a.has_escaping_bound_vars()); |
86 | | - debug_assert!(!b.has_escaping_bound_vars()); |
87 | | - |
88 | | - match (a.kind(), b.kind()) { |
89 | | - (&ty::Error(e), _) | (_, &ty::Error(e)) => { |
90 | | - self.set_tainted_by_errors(e); |
91 | | - return Ok(Ty::new_error(self.tcx, e)); |
92 | | - } |
93 | | - |
94 | | - // Relate integral variables to other types |
95 | | - (&ty::Infer(ty::IntVar(a_id)), &ty::Infer(ty::IntVar(b_id))) => { |
96 | | - self.inner.borrow_mut().int_unification_table().union(a_id, b_id); |
97 | | - Ok(a) |
98 | | - } |
99 | | - (&ty::Infer(ty::IntVar(v_id)), &ty::Int(v)) => { |
100 | | - self.unify_integral_variable(v_id, IntType(v)); |
101 | | - Ok(b) |
102 | | - } |
103 | | - (&ty::Int(v), &ty::Infer(ty::IntVar(v_id))) => { |
104 | | - self.unify_integral_variable(v_id, IntType(v)); |
105 | | - Ok(a) |
106 | | - } |
107 | | - (&ty::Infer(ty::IntVar(v_id)), &ty::Uint(v)) => { |
108 | | - self.unify_integral_variable(v_id, UintType(v)); |
109 | | - Ok(b) |
110 | | - } |
111 | | - (&ty::Uint(v), &ty::Infer(ty::IntVar(v_id))) => { |
112 | | - self.unify_integral_variable(v_id, UintType(v)); |
113 | | - Ok(a) |
114 | | - } |
115 | | - |
116 | | - // Relate floating-point variables to other types |
117 | | - (&ty::Infer(ty::FloatVar(a_id)), &ty::Infer(ty::FloatVar(b_id))) => { |
118 | | - self.inner.borrow_mut().float_unification_table().union(a_id, b_id); |
119 | | - Ok(a) |
120 | | - } |
121 | | - (&ty::Infer(ty::FloatVar(v_id)), &ty::Float(v)) => { |
122 | | - self.unify_float_variable(v_id, ty::FloatVarValue::Known(v)); |
123 | | - Ok(b) |
124 | | - } |
125 | | - (&ty::Float(v), &ty::Infer(ty::FloatVar(v_id))) => { |
126 | | - self.unify_float_variable(v_id, ty::FloatVarValue::Known(v)); |
127 | | - Ok(a) |
128 | | - } |
129 | | - |
130 | | - // We don't expect `TyVar` or `Fresh*` vars at this point with lazy norm. |
131 | | - (ty::Alias(..), ty::Infer(ty::TyVar(_))) | (ty::Infer(ty::TyVar(_)), ty::Alias(..)) |
132 | | - if self.next_trait_solver() => |
133 | | - { |
134 | | - bug!( |
135 | | - "We do not expect to encounter `TyVar` this late in combine \ |
136 | | - -- they should have been handled earlier" |
137 | | - ) |
138 | | - } |
139 | | - (_, ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))) |
140 | | - | (ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)), _) |
141 | | - if self.next_trait_solver() => |
142 | | - { |
143 | | - bug!("We do not expect to encounter `Fresh` variables in the new solver") |
144 | | - } |
145 | | - |
146 | | - (_, ty::Alias(..)) | (ty::Alias(..), _) if self.next_trait_solver() => { |
147 | | - match relation.structurally_relate_aliases() { |
148 | | - StructurallyRelateAliases::Yes => { |
149 | | - relate::structurally_relate_tys(relation, a, b) |
150 | | - } |
151 | | - StructurallyRelateAliases::No => { |
152 | | - relation.register_alias_relate_predicate(a, b); |
153 | | - Ok(a) |
154 | | - } |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - // All other cases of inference are errors |
159 | | - (&ty::Infer(_), _) | (_, &ty::Infer(_)) => { |
160 | | - Err(TypeError::Sorts(ExpectedFound::new(true, a, b))) |
161 | | - } |
162 | | - |
163 | | - // During coherence, opaque types should be treated as *possibly* |
164 | | - // equal to any other type (except for possibly itself). This is an |
165 | | - // extremely heavy hammer, but can be relaxed in a forwards-compatible |
166 | | - // way later. |
167 | | - (&ty::Alias(ty::Opaque, _), _) | (_, &ty::Alias(ty::Opaque, _)) if self.intercrate => { |
168 | | - relation.register_predicates([ty::Binder::dummy(ty::PredicateKind::Ambiguous)]); |
169 | | - Ok(a) |
170 | | - } |
171 | | - |
172 | | - _ => relate::structurally_relate_tys(relation, a, b), |
173 | | - } |
174 | | - } |
175 | | - |
176 | | - pub fn super_combine_consts<R>( |
177 | | - &self, |
178 | | - relation: &mut R, |
179 | | - a: ty::Const<'tcx>, |
180 | | - b: ty::Const<'tcx>, |
181 | | - ) -> RelateResult<'tcx, ty::Const<'tcx>> |
182 | | - where |
183 | | - R: PredicateEmittingRelation<InferCtxt<'tcx>>, |
184 | | - { |
185 | | - debug!("super_combine_consts::<{}>({:?}, {:?})", std::any::type_name::<R>(), a, b); |
186 | | - debug_assert!(!a.has_escaping_bound_vars()); |
187 | | - debug_assert!(!b.has_escaping_bound_vars()); |
188 | | - |
189 | | - if a == b { |
190 | | - return Ok(a); |
191 | | - } |
192 | | - |
193 | | - let a = self.shallow_resolve_const(a); |
194 | | - let b = self.shallow_resolve_const(b); |
195 | | - |
196 | | - match (a.kind(), b.kind()) { |
197 | | - ( |
198 | | - ty::ConstKind::Infer(InferConst::Var(a_vid)), |
199 | | - ty::ConstKind::Infer(InferConst::Var(b_vid)), |
200 | | - ) => { |
201 | | - self.inner.borrow_mut().const_unification_table().union(a_vid, b_vid); |
202 | | - Ok(a) |
203 | | - } |
204 | | - |
205 | | - ( |
206 | | - ty::ConstKind::Infer(InferConst::EffectVar(a_vid)), |
207 | | - ty::ConstKind::Infer(InferConst::EffectVar(b_vid)), |
208 | | - ) => { |
209 | | - self.inner.borrow_mut().effect_unification_table().union(a_vid, b_vid); |
210 | | - Ok(a) |
211 | | - } |
212 | | - |
213 | | - // All other cases of inference with other variables are errors. |
214 | | - ( |
215 | | - ty::ConstKind::Infer(InferConst::Var(_) | InferConst::EffectVar(_)), |
216 | | - ty::ConstKind::Infer(_), |
217 | | - ) |
218 | | - | ( |
219 | | - ty::ConstKind::Infer(_), |
220 | | - ty::ConstKind::Infer(InferConst::Var(_) | InferConst::EffectVar(_)), |
221 | | - ) => { |
222 | | - bug!( |
223 | | - "tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var): {a:?} and {b:?}" |
224 | | - ) |
225 | | - } |
226 | | - |
227 | | - (ty::ConstKind::Infer(InferConst::Var(vid)), _) => { |
228 | | - self.instantiate_const_var(relation, true, vid, b)?; |
229 | | - Ok(b) |
230 | | - } |
231 | | - |
232 | | - (_, ty::ConstKind::Infer(InferConst::Var(vid))) => { |
233 | | - self.instantiate_const_var(relation, false, vid, a)?; |
234 | | - Ok(a) |
235 | | - } |
236 | | - |
237 | | - (ty::ConstKind::Infer(InferConst::EffectVar(vid)), _) => { |
238 | | - Ok(self.unify_effect_variable(vid, b)) |
239 | | - } |
240 | | - |
241 | | - (_, ty::ConstKind::Infer(InferConst::EffectVar(vid))) => { |
242 | | - Ok(self.unify_effect_variable(vid, a)) |
243 | | - } |
244 | | - |
245 | | - (ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..)) |
246 | | - if self.tcx.features().generic_const_exprs || self.next_trait_solver() => |
247 | | - { |
248 | | - match relation.structurally_relate_aliases() { |
249 | | - StructurallyRelateAliases::No => { |
250 | | - relation.register_predicates([if self.next_trait_solver() { |
251 | | - ty::PredicateKind::AliasRelate( |
252 | | - a.into(), |
253 | | - b.into(), |
254 | | - ty::AliasRelationDirection::Equate, |
255 | | - ) |
256 | | - } else { |
257 | | - ty::PredicateKind::ConstEquate(a, b) |
258 | | - }]); |
259 | | - |
260 | | - Ok(b) |
261 | | - } |
262 | | - StructurallyRelateAliases::Yes => { |
263 | | - relate::structurally_relate_consts(relation, a, b) |
264 | | - } |
265 | | - } |
266 | | - } |
267 | | - _ => relate::structurally_relate_consts(relation, a, b), |
268 | | - } |
269 | | - } |
270 | | - |
271 | | - #[inline(always)] |
272 | | - fn unify_integral_variable(&self, vid: ty::IntVid, val: ty::IntVarValue) { |
273 | | - self.inner.borrow_mut().int_unification_table().union_value(vid, val); |
274 | | - } |
275 | | - |
276 | | - #[inline(always)] |
277 | | - fn unify_float_variable(&self, vid: ty::FloatVid, val: ty::FloatVarValue) { |
278 | | - self.inner.borrow_mut().float_unification_table().union_value(vid, val); |
279 | | - } |
280 | | - |
281 | | - fn unify_effect_variable(&self, vid: ty::EffectVid, val: ty::Const<'tcx>) -> ty::Const<'tcx> { |
282 | | - self.inner |
283 | | - .borrow_mut() |
284 | | - .effect_unification_table() |
285 | | - .union_value(vid, EffectVarValue::Known(val)); |
286 | | - val |
287 | | - } |
288 | | -} |
289 | | - |
290 | 70 | impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> { |
291 | 71 | pub fn tcx(&self) -> TyCtxt<'tcx> { |
292 | 72 | self.infcx.tcx |
|
0 commit comments