Skip to content

Commit 14a0335

Browse files
committed
Auto merge of #123536 - compiler-errors:simplify-int-float, r=<try>
Simplify `IntVarValue`/`FloatVarValue` r? `@ghost`
2 parents 2a2c29a + c623ae1 commit 14a0335

File tree

9 files changed

+171
-198
lines changed

9 files changed

+171
-198
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

+73-55
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;
@@ -811,13 +811,13 @@ impl<'tcx> InferCtxt<'tcx> {
811811
vars.extend(
812812
(0..inner.int_unification_table().len())
813813
.map(|i| ty::IntVid::from_u32(i as u32))
814-
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
814+
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
815815
.map(|v| Ty::new_int_var(self.tcx, v)),
816816
);
817817
vars.extend(
818818
(0..inner.float_unification_table().len())
819819
.map(|i| ty::FloatVid::from_u32(i as u32))
820-
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
820+
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
821821
.map(|v| Ty::new_float_var(self.tcx, v)),
822822
);
823823
vars
@@ -1025,14 +1025,28 @@ impl<'tcx> InferCtxt<'tcx> {
10251025
ty::Const::new_var(self.tcx, vid, ty)
10261026
}
10271027

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

10331048
pub fn next_float_var(&self) -> Ty<'tcx> {
1034-
let vid = self.inner.borrow_mut().float_unification_table().new_key(None);
1035-
Ty::new_float_var(self.tcx, vid)
1049+
Ty::new_float_var(self.tcx, self.next_float_var_id())
10361050
}
10371051

10381052
/// Creates a fresh region variable with the next available index.
@@ -1234,45 +1248,44 @@ impl<'tcx> InferCtxt<'tcx> {
12341248
}
12351249

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

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

@@ -1321,21 +1334,26 @@ impl<'tcx> InferCtxt<'tcx> {
13211334
/// or else the root int var in the unification table.
13221335
pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
13231336
let mut inner = self.inner.borrow_mut();
1324-
if let Some(value) = inner.int_unification_table().probe_value(vid) {
1325-
value.to_type(self.tcx)
1326-
} else {
1327-
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1337+
let value = inner.int_unification_table().probe_value(vid);
1338+
match value {
1339+
ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1340+
ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1341+
ty::IntVarValue::Unknown => {
1342+
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1343+
}
13281344
}
13291345
}
13301346

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

@@ -1626,15 +1644,15 @@ impl<'tcx> InferCtxt<'tcx> {
16261644
// If `inlined_probe_value` returns a value it's always a
16271645
// `ty::Int(_)` or `ty::UInt(_)`, which never matches a
16281646
// `ty::Infer(_)`.
1629-
self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_some()
1647+
!self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_unknown()
16301648
}
16311649

16321650
TyOrConstInferVar::TyFloat(v) => {
16331651
// If `probe_value` returns a value it's always a
16341652
// `ty::Float(_)`, which never matches a `ty::Infer(_)`.
16351653
//
16361654
// Not `inlined_probe_value(v)` because this call site is colder.
1637-
self.inner.borrow_mut().float_unification_table().probe_value(v).is_some()
1655+
!self.inner.borrow_mut().float_unification_table().probe_value(v).is_unknown()
16381656
}
16391657

16401658
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)