Skip to content

Commit 44701e0

Browse files
committed
Auto merge of #123536 - compiler-errors:simplify-int-float, r=lcnr
Simplify `IntVarValue`/`FloatVarValue` r? `@ghost`
2 parents 30ea1a2 + 208c316 commit 44701e0

File tree

9 files changed

+186
-199
lines changed

9 files changed

+186
-199
lines changed

compiler/rustc_infer/src/infer/freshen.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use super::InferCtxt;
3434
use rustc_data_structures::fx::FxHashMap;
3535
use rustc_middle::bug;
36-
use rustc_middle::infer::unify_key::ToType;
3736
use rustc_middle::ty::fold::TypeFolder;
3837
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitableExt};
3938
use std::collections::hash_map::Entry;
@@ -204,22 +203,27 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
204203

205204
ty::IntVar(v) => {
206205
let mut inner = self.infcx.inner.borrow_mut();
207-
let input = inner
208-
.int_unification_table()
209-
.probe_value(v)
210-
.map(|v| v.to_type(self.infcx.tcx))
211-
.ok_or_else(|| ty::IntVar(inner.int_unification_table().find(v)));
206+
let value = inner.int_unification_table().probe_value(v);
207+
let input = match value {
208+
ty::IntVarValue::IntType(ty) => Ok(Ty::new_int(self.infcx.tcx, ty)),
209+
ty::IntVarValue::UintType(ty) => Ok(Ty::new_uint(self.infcx.tcx, ty)),
210+
ty::IntVarValue::Unknown => {
211+
Err(ty::IntVar(inner.int_unification_table().find(v)))
212+
}
213+
};
212214
drop(inner);
213215
Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n)))
214216
}
215217

216218
ty::FloatVar(v) => {
217219
let mut inner = self.infcx.inner.borrow_mut();
218-
let input = inner
219-
.float_unification_table()
220-
.probe_value(v)
221-
.map(|v| v.to_type(self.infcx.tcx))
222-
.ok_or_else(|| ty::FloatVar(inner.float_unification_table().find(v)));
220+
let value = inner.float_unification_table().probe_value(v);
221+
let input = match value {
222+
ty::FloatVarValue::Known(ty) => Ok(Ty::new_float(self.infcx.tcx, ty)),
223+
ty::FloatVarValue::Unknown => {
224+
Err(ty::FloatVar(inner.float_unification_table().find(v)))
225+
}
226+
};
223227
drop(inner);
224228
Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n)))
225229
}

compiler/rustc_infer/src/infer/mod.rs

+74-56
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use rustc_errors::{Diag, DiagCtxt, ErrorGuaranteed};
2929
use rustc_hir::def_id::{DefId, LocalDefId};
3030
use rustc_macros::extension;
3131
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
32+
use rustc_middle::infer::unify_key::ConstVariableOrigin;
3233
use rustc_middle::infer::unify_key::ConstVariableValue;
3334
use rustc_middle::infer::unify_key::EffectVarValue;
34-
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ToType};
3535
use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};
3636
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
3737
use rustc_middle::mir::ConstraintCategory;
@@ -41,7 +41,7 @@ use rustc_middle::ty::fold::BoundVarReplacerDelegate;
4141
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
4242
use rustc_middle::ty::relate::RelateResult;
4343
use rustc_middle::ty::visit::TypeVisitableExt;
44-
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, InferTy, Ty, TyCtxt};
44+
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, Ty, TyCtxt};
4545
use rustc_middle::ty::{ConstVid, EffectVid, FloatVid, IntVid, TyVid};
4646
use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgs, GenericArgsRef};
4747
use rustc_middle::{bug, span_bug};
@@ -813,13 +813,13 @@ impl<'tcx> InferCtxt<'tcx> {
813813
vars.extend(
814814
(0..inner.int_unification_table().len())
815815
.map(|i| ty::IntVid::from_u32(i as u32))
816-
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
816+
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
817817
.map(|v| Ty::new_int_var(self.tcx, v)),
818818
);
819819
vars.extend(
820820
(0..inner.float_unification_table().len())
821821
.map(|i| ty::FloatVid::from_u32(i as u32))
822-
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
822+
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
823823
.map(|v| Ty::new_float_var(self.tcx, v)),
824824
);
825825
vars
@@ -1027,14 +1027,28 @@ impl<'tcx> InferCtxt<'tcx> {
10271027
ty::Const::new_var(self.tcx, vid, ty)
10281028
}
10291029

1030+
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid {
1031+
self.inner
1032+
.borrow_mut()
1033+
.const_unification_table()
1034+
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
1035+
.vid
1036+
}
1037+
1038+
fn next_int_var_id(&self) -> IntVid {
1039+
self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown)
1040+
}
1041+
10301042
pub fn next_int_var(&self) -> Ty<'tcx> {
1031-
let vid = self.inner.borrow_mut().int_unification_table().new_key(None);
1032-
Ty::new_int_var(self.tcx, vid)
1043+
Ty::new_int_var(self.tcx, self.next_int_var_id())
1044+
}
1045+
1046+
fn next_float_var_id(&self) -> FloatVid {
1047+
self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown)
10331048
}
10341049

10351050
pub fn next_float_var(&self) -> Ty<'tcx> {
1036-
let vid = self.inner.borrow_mut().float_unification_table().new_key(None);
1037-
Ty::new_float_var(self.tcx, vid)
1051+
Ty::new_float_var(self.tcx, self.next_float_var_id())
10381052
}
10391053

10401054
/// Creates a fresh region variable with the next available index.
@@ -1236,45 +1250,44 @@ impl<'tcx> InferCtxt<'tcx> {
12361250
}
12371251

12381252
pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
1239-
if let ty::Infer(v) = ty.kind() { self.fold_infer_ty(*v).unwrap_or(ty) } else { ty }
1240-
}
1241-
1242-
// This is separate from `shallow_resolve` to keep that method small and inlinable.
1243-
#[inline(never)]
1244-
fn fold_infer_ty(&self, v: InferTy) -> Option<Ty<'tcx>> {
1245-
match v {
1246-
ty::TyVar(v) => {
1247-
// Not entirely obvious: if `typ` is a type variable,
1248-
// it can be resolved to an int/float variable, which
1249-
// can then be recursively resolved, hence the
1250-
// recursion. Note though that we prevent type
1251-
// variables from unifying to other type variables
1252-
// directly (though they may be embedded
1253-
// structurally), and we prevent cycles in any case,
1254-
// so this recursion should always be of very limited
1255-
// depth.
1256-
//
1257-
// Note: if these two lines are combined into one we get
1258-
// dynamic borrow errors on `self.inner`.
1259-
let known = self.inner.borrow_mut().type_variables().probe(v).known();
1260-
known.map(|t| self.shallow_resolve(t))
1261-
}
1253+
if let ty::Infer(v) = *ty.kind() {
1254+
match v {
1255+
ty::TyVar(v) => {
1256+
// Not entirely obvious: if `typ` is a type variable,
1257+
// it can be resolved to an int/float variable, which
1258+
// can then be recursively resolved, hence the
1259+
// recursion. Note though that we prevent type
1260+
// variables from unifying to other type variables
1261+
// directly (though they may be embedded
1262+
// structurally), and we prevent cycles in any case,
1263+
// so this recursion should always be of very limited
1264+
// depth.
1265+
//
1266+
// Note: if these two lines are combined into one we get
1267+
// dynamic borrow errors on `self.inner`.
1268+
let known = self.inner.borrow_mut().type_variables().probe(v).known();
1269+
known.map_or(ty, |t| self.shallow_resolve(t))
1270+
}
1271+
1272+
ty::IntVar(v) => {
1273+
match self.inner.borrow_mut().int_unification_table().probe_value(v) {
1274+
ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1275+
ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1276+
ty::IntVarValue::Unknown => ty,
1277+
}
1278+
}
12621279

1263-
ty::IntVar(v) => self
1264-
.inner
1265-
.borrow_mut()
1266-
.int_unification_table()
1267-
.probe_value(v)
1268-
.map(|v| v.to_type(self.tcx)),
1269-
1270-
ty::FloatVar(v) => self
1271-
.inner
1272-
.borrow_mut()
1273-
.float_unification_table()
1274-
.probe_value(v)
1275-
.map(|v| v.to_type(self.tcx)),
1276-
1277-
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
1280+
ty::FloatVar(v) => {
1281+
match self.inner.borrow_mut().float_unification_table().probe_value(v) {
1282+
ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1283+
ty::FloatVarValue::Unknown => ty,
1284+
}
1285+
}
1286+
1287+
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => ty,
1288+
}
1289+
} else {
1290+
ty
12781291
}
12791292
}
12801293

@@ -1323,21 +1336,26 @@ impl<'tcx> InferCtxt<'tcx> {
13231336
/// or else the root int var in the unification table.
13241337
pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
13251338
let mut inner = self.inner.borrow_mut();
1326-
if let Some(value) = inner.int_unification_table().probe_value(vid) {
1327-
value.to_type(self.tcx)
1328-
} else {
1329-
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1339+
let value = inner.int_unification_table().probe_value(vid);
1340+
match value {
1341+
ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1342+
ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1343+
ty::IntVarValue::Unknown => {
1344+
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1345+
}
13301346
}
13311347
}
13321348

13331349
/// Resolves a float var to a rigid int type, if it was constrained to one,
13341350
/// or else the root float var in the unification table.
13351351
pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
13361352
let mut inner = self.inner.borrow_mut();
1337-
if let Some(value) = inner.float_unification_table().probe_value(vid) {
1338-
value.to_type(self.tcx)
1339-
} else {
1340-
Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1353+
let value = inner.float_unification_table().probe_value(vid);
1354+
match value {
1355+
ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1356+
ty::FloatVarValue::Unknown => {
1357+
Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1358+
}
13411359
}
13421360
}
13431361

@@ -1628,15 +1646,15 @@ impl<'tcx> InferCtxt<'tcx> {
16281646
// If `inlined_probe_value` returns a value it's always a
16291647
// `ty::Int(_)` or `ty::UInt(_)`, which never matches a
16301648
// `ty::Infer(_)`.
1631-
self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_some()
1649+
self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_known()
16321650
}
16331651

16341652
TyOrConstInferVar::TyFloat(v) => {
16351653
// If `probe_value` returns a value it's always a
16361654
// `ty::Float(_)`, which never matches a `ty::Infer(_)`.
16371655
//
16381656
// Not `inlined_probe_value(v)` because this call site is colder.
1639-
self.inner.borrow_mut().float_unification_table().probe_value(v).is_some()
1657+
self.inner.borrow_mut().float_unification_table().probe_value(v).is_known()
16401658
}
16411659

16421660
TyOrConstInferVar::Const(v) => {

compiler/rustc_infer/src/infer/relate/combine.rs

+21-60
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
2626
use crate::traits::{Obligation, PredicateObligations};
2727
use rustc_middle::bug;
2828
use rustc_middle::infer::unify_key::EffectVarValue;
29-
use rustc_middle::ty::error::{ExpectedFound, TypeError};
29+
use rustc_middle::ty::error::TypeError;
3030
use rustc_middle::ty::relate::{RelateResult, TypeRelation};
3131
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt, TypeVisitableExt, Upcast};
3232
use rustc_middle::ty::{IntType, UintType};
@@ -68,40 +68,38 @@ impl<'tcx> InferCtxt<'tcx> {
6868
match (a.kind(), b.kind()) {
6969
// Relate integral variables to other types
7070
(&ty::Infer(ty::IntVar(a_id)), &ty::Infer(ty::IntVar(b_id))) => {
71-
self.inner
72-
.borrow_mut()
73-
.int_unification_table()
74-
.unify_var_var(a_id, b_id)
75-
.map_err(|e| int_unification_error(true, e))?;
71+
self.inner.borrow_mut().int_unification_table().union(a_id, b_id);
7672
Ok(a)
7773
}
7874
(&ty::Infer(ty::IntVar(v_id)), &ty::Int(v)) => {
79-
self.unify_integral_variable(true, v_id, IntType(v))
75+
self.unify_integral_variable(v_id, IntType(v));
76+
Ok(b)
8077
}
8178
(&ty::Int(v), &ty::Infer(ty::IntVar(v_id))) => {
82-
self.unify_integral_variable(false, v_id, IntType(v))
79+
self.unify_integral_variable(v_id, IntType(v));
80+
Ok(a)
8381
}
8482
(&ty::Infer(ty::IntVar(v_id)), &ty::Uint(v)) => {
85-
self.unify_integral_variable(true, v_id, UintType(v))
83+
self.unify_integral_variable(v_id, UintType(v));
84+
Ok(b)
8685
}
8786
(&ty::Uint(v), &ty::Infer(ty::IntVar(v_id))) => {
88-
self.unify_integral_variable(false, v_id, UintType(v))
87+
self.unify_integral_variable(v_id, UintType(v));
88+
Ok(a)
8989
}
9090

9191
// Relate floating-point variables to other types
9292
(&ty::Infer(ty::FloatVar(a_id)), &ty::Infer(ty::FloatVar(b_id))) => {
93-
self.inner
94-
.borrow_mut()
95-
.float_unification_table()
96-
.unify_var_var(a_id, b_id)
97-
.map_err(|e| float_unification_error(true, e))?;
93+
self.inner.borrow_mut().float_unification_table().union(a_id, b_id);
9894
Ok(a)
9995
}
10096
(&ty::Infer(ty::FloatVar(v_id)), &ty::Float(v)) => {
101-
self.unify_float_variable(true, v_id, v)
97+
self.unify_float_variable(v_id, ty::FloatVarValue::Known(v));
98+
Ok(b)
10299
}
103100
(&ty::Float(v), &ty::Infer(ty::FloatVar(v_id))) => {
104-
self.unify_float_variable(false, v_id, v)
101+
self.unify_float_variable(v_id, ty::FloatVarValue::Known(v));
102+
Ok(a)
105103
}
106104

107105
// We don't expect `TyVar` or `Fresh*` vars at this point with lazy norm.
@@ -244,35 +242,14 @@ impl<'tcx> InferCtxt<'tcx> {
244242
}
245243
}
246244

247-
fn unify_integral_variable(
248-
&self,
249-
vid_is_expected: bool,
250-
vid: ty::IntVid,
251-
val: ty::IntVarValue,
252-
) -> RelateResult<'tcx, Ty<'tcx>> {
253-
self.inner
254-
.borrow_mut()
255-
.int_unification_table()
256-
.unify_var_value(vid, Some(val))
257-
.map_err(|e| int_unification_error(vid_is_expected, e))?;
258-
match val {
259-
IntType(v) => Ok(Ty::new_int(self.tcx, v)),
260-
UintType(v) => Ok(Ty::new_uint(self.tcx, v)),
261-
}
245+
#[inline(always)]
246+
fn unify_integral_variable(&self, vid: ty::IntVid, val: ty::IntVarValue) {
247+
self.inner.borrow_mut().int_unification_table().union_value(vid, val);
262248
}
263249

264-
fn unify_float_variable(
265-
&self,
266-
vid_is_expected: bool,
267-
vid: ty::FloatVid,
268-
val: ty::FloatTy,
269-
) -> RelateResult<'tcx, Ty<'tcx>> {
270-
self.inner
271-
.borrow_mut()
272-
.float_unification_table()
273-
.unify_var_value(vid, Some(ty::FloatVarValue(val)))
274-
.map_err(|e| float_unification_error(vid_is_expected, e))?;
275-
Ok(Ty::new_float(self.tcx, val))
250+
#[inline(always)]
251+
fn unify_float_variable(&self, vid: ty::FloatVid, val: ty::FloatVarValue) {
252+
self.inner.borrow_mut().float_unification_table().union_value(vid, val);
276253
}
277254

278255
fn unify_effect_variable(&self, vid: ty::EffectVid, val: ty::Const<'tcx>) -> ty::Const<'tcx> {
@@ -350,19 +327,3 @@ pub trait ObligationEmittingRelation<'tcx>: TypeRelation<'tcx> {
350327
/// Register `AliasRelate` obligation(s) that both types must be related to each other.
351328
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>);
352329
}
353-
354-
fn int_unification_error<'tcx>(
355-
a_is_expected: bool,
356-
v: (ty::IntVarValue, ty::IntVarValue),
357-
) -> TypeError<'tcx> {
358-
let (a, b) = v;
359-
TypeError::IntMismatch(ExpectedFound::new(a_is_expected, a, b))
360-
}
361-
362-
fn float_unification_error<'tcx>(
363-
a_is_expected: bool,
364-
v: (ty::FloatVarValue, ty::FloatVarValue),
365-
) -> TypeError<'tcx> {
366-
let (ty::FloatVarValue(a), ty::FloatVarValue(b)) = v;
367-
TypeError::FloatMismatch(ExpectedFound::new(a_is_expected, a, b))
368-
}

compiler/rustc_infer/src/infer/relate/lattice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ where
6464

6565
let infcx = this.infcx();
6666

67-
let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a);
68-
let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b);
67+
let a = infcx.shallow_resolve(a);
68+
let b = infcx.shallow_resolve(b);
6969

7070
match (a.kind(), b.kind()) {
7171
// If one side is known to be a variable and one is not,

compiler/rustc_infer/src/infer/relate/type_relating.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ impl<'tcx> TypeRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
8080
}
8181

8282
let infcx = self.fields.infcx;
83-
let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a);
84-
let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b);
83+
let a = infcx.shallow_resolve(a);
84+
let b = infcx.shallow_resolve(b);
8585

8686
match (a.kind(), b.kind()) {
8787
(&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {

0 commit comments

Comments
 (0)