Skip to content

Commit 9288981

Browse files
compiler-errorslcnr
authored andcommitted
Simplify IntVarValue/FloatVarValue
1 parent abea5e5 commit 9288981

File tree

9 files changed

+145
-172
lines changed

9 files changed

+145
-172
lines changed

compiler/rustc_infer/src/infer/freshen.rs

Lines changed: 15 additions & 11 deletions
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

Lines changed: 47 additions & 29 deletions
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;
@@ -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.
@@ -1260,19 +1274,18 @@ impl<'tcx> InferCtxt<'tcx> {
12601274
known.map(|t| self.shallow_resolve(t))
12611275
}
12621276

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)),
1277+
ty::IntVar(v) => match self.inner.borrow_mut().int_unification_table().probe_value(v) {
1278+
ty::IntVarValue::Unknown => None,
1279+
ty::IntVarValue::IntType(ty) => Some(Ty::new_int(self.tcx, ty)),
1280+
ty::IntVarValue::UintType(ty) => Some(Ty::new_uint(self.tcx, ty)),
1281+
},
12691282

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)),
1283+
ty::FloatVar(v) => {
1284+
match self.inner.borrow_mut().float_unification_table().probe_value(v) {
1285+
ty::FloatVarValue::Unknown => None,
1286+
ty::FloatVarValue::Known(ty) => Some(Ty::new_float(self.tcx, ty)),
1287+
}
1288+
}
12761289

12771290
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
12781291
}
@@ -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_unknown()
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_unknown()
16401658
}
16411659

16421660
TyOrConstInferVar::Const(v) => {

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

Lines changed: 21 additions & 60 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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))) => {

compiler/rustc_middle/src/infer/unify_key.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,6 @@ impl<'tcx> UnifyValue for RegionVariableValue<'tcx> {
8686
}
8787
}
8888

89-
impl ToType for ty::IntVarValue {
90-
fn to_type<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
91-
match *self {
92-
ty::IntType(i) => Ty::new_int(tcx, i),
93-
ty::UintType(i) => Ty::new_uint(tcx, i),
94-
}
95-
}
96-
}
97-
98-
impl ToType for ty::FloatVarValue {
99-
fn to_type<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
100-
Ty::new_float(tcx, self.0)
101-
}
102-
}
103-
10489
// Generic consts.
10590

10691
#[derive(Copy, Clone, Debug)]
@@ -211,6 +196,7 @@ impl<'tcx> EffectVarValue<'tcx> {
211196

212197
impl<'tcx> UnifyValue for EffectVarValue<'tcx> {
213198
type Error = NoError;
199+
214200
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
215201
match (*value1, *value2) {
216202
(EffectVarValue::Unknown, EffectVarValue::Unknown) => Ok(EffectVarValue::Unknown),

0 commit comments

Comments
 (0)