Skip to content

Commit cc9de3b

Browse files
Simplify IntVarValue/FloatVarValue
1 parent 11853ec commit cc9de3b

File tree

9 files changed

+129
-173
lines changed

9 files changed

+129
-173
lines changed

compiler/rustc_infer/src/infer/freshen.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
//! inferencer knows "so far".
3333
use super::InferCtxt;
3434
use rustc_data_structures::fx::FxHashMap;
35-
use rustc_middle::infer::unify_key::ToType;
3635
use rustc_middle::ty::fold::TypeFolder;
3736
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitableExt};
3837
use std::collections::hash_map::Entry;
@@ -203,22 +202,27 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
203202

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

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

compiler/rustc_infer/src/infer/mod.rs

+33-30
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub use lexical_region_resolve::RegionResolutionError;
44
pub use relate::combine::CombineFields;
55
pub use relate::combine::ObligationEmittingRelation;
66
pub use relate::StructurallyRelateAliases;
7-
pub use rustc_middle::ty::IntVarValue;
87
pub use BoundRegionConversionTime::*;
98
pub use RegionVariableOrigin::*;
109
pub use SubregionOrigin::*;
@@ -30,7 +29,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
3029
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
3130
use rustc_middle::infer::unify_key::ConstVariableValue;
3231
use rustc_middle::infer::unify_key::EffectVarValue;
33-
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
32+
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
3433
use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};
3534
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
3635
use rustc_middle::mir::ConstraintCategory;
@@ -797,13 +796,13 @@ impl<'tcx> InferCtxt<'tcx> {
797796
vars.extend(
798797
(0..inner.int_unification_table().len())
799798
.map(|i| ty::IntVid::from_u32(i as u32))
800-
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
799+
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
801800
.map(|v| Ty::new_int_var(self.tcx, v)),
802801
);
803802
vars.extend(
804803
(0..inner.float_unification_table().len())
805804
.map(|i| ty::FloatVid::from_u32(i as u32))
806-
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
805+
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
807806
.map(|v| Ty::new_float_var(self.tcx, v)),
808807
);
809808
vars
@@ -1026,15 +1025,15 @@ impl<'tcx> InferCtxt<'tcx> {
10261025
}
10271026

10281027
fn next_int_var_id(&self) -> IntVid {
1029-
self.inner.borrow_mut().int_unification_table().new_key(None)
1028+
self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown)
10301029
}
10311030

10321031
pub fn next_int_var(&self) -> Ty<'tcx> {
10331032
Ty::new_int_var(self.tcx, self.next_int_var_id())
10341033
}
10351034

10361035
fn next_float_var_id(&self) -> FloatVid {
1037-
self.inner.borrow_mut().float_unification_table().new_key(None)
1036+
self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown)
10381037
}
10391038

10401039
pub fn next_float_var(&self) -> Ty<'tcx> {
@@ -1276,21 +1275,26 @@ impl<'tcx> InferCtxt<'tcx> {
12761275
/// or else the root int var in the unification table.
12771276
pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
12781277
let mut inner = self.inner.borrow_mut();
1279-
if let Some(value) = inner.int_unification_table().probe_value(vid) {
1280-
value.to_type(self.tcx)
1281-
} else {
1282-
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1278+
let value = inner.int_unification_table().probe_value(vid);
1279+
match value {
1280+
ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1281+
ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1282+
ty::IntVarValue::Unknown => {
1283+
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1284+
}
12831285
}
12841286
}
12851287

12861288
/// Resolves a float var to a rigid int type, if it was constrained to one,
12871289
/// or else the root float var in the unification table.
12881290
pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
12891291
let mut inner = self.inner.borrow_mut();
1290-
if let Some(value) = inner.float_unification_table().probe_value(vid) {
1291-
value.to_type(self.tcx)
1292-
} else {
1293-
Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1292+
let value = inner.float_unification_table().probe_value(vid);
1293+
match value {
1294+
ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1295+
ty::FloatVarValue::Unknown => {
1296+
Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1297+
}
12941298
}
12951299
}
12961300

@@ -1607,15 +1611,15 @@ impl<'tcx> InferCtxt<'tcx> {
16071611
// If `inlined_probe_value` returns a value it's always a
16081612
// `ty::Int(_)` or `ty::UInt(_)`, which never matches a
16091613
// `ty::Infer(_)`.
1610-
self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_some()
1614+
!self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_unknown()
16111615
}
16121616

16131617
TyOrConstInferVar::TyFloat(v) => {
16141618
// If `probe_value` returns a value it's always a
16151619
// `ty::Float(_)`, which never matches a `ty::Infer(_)`.
16161620
//
16171621
// Not `inlined_probe_value(v)` because this call site is colder.
1618-
self.inner.borrow_mut().float_unification_table().probe_value(v).is_some()
1622+
!self.inner.borrow_mut().float_unification_table().probe_value(v).is_unknown()
16191623
}
16201624

16211625
TyOrConstInferVar::Const(v) => {
@@ -1836,21 +1840,20 @@ impl<'a, 'tcx> ShallowResolver<'a, 'tcx> {
18361840
known.map(|t| self.fold_ty(t))
18371841
}
18381842

1839-
ty::IntVar(v) => self
1840-
.infcx
1841-
.inner
1842-
.borrow_mut()
1843-
.int_unification_table()
1844-
.probe_value(v)
1845-
.map(|v| v.to_type(self.infcx.tcx)),
1843+
ty::IntVar(v) => {
1844+
match self.infcx.inner.borrow_mut().int_unification_table().probe_value(v) {
1845+
ty::IntVarValue::Unknown => None,
1846+
ty::IntVarValue::IntType(ty) => Some(Ty::new_int(self.infcx.tcx, ty)),
1847+
ty::IntVarValue::UintType(ty) => Some(Ty::new_uint(self.infcx.tcx, ty)),
1848+
}
1849+
}
18461850

1847-
ty::FloatVar(v) => self
1848-
.infcx
1849-
.inner
1850-
.borrow_mut()
1851-
.float_unification_table()
1852-
.probe_value(v)
1853-
.map(|v| v.to_type(self.infcx.tcx)),
1851+
ty::FloatVar(v) => {
1852+
match self.infcx.inner.borrow_mut().float_unification_table().probe_value(v) {
1853+
ty::FloatVarValue::Unknown => None,
1854+
ty::FloatVarValue::Known(ty) => Some(Ty::new_float(self.infcx.tcx, ty)),
1855+
}
1856+
}
18541857

18551858
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
18561859
}

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

+19-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::infer::canonical::OriginalQueryValues;
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, ToPredicate, Ty, TyCtxt, TypeVisitableExt};
3232
use rustc_middle::ty::{IntType, UintType};
@@ -57,40 +57,38 @@ impl<'tcx> InferCtxt<'tcx> {
5757
match (a.kind(), b.kind()) {
5858
// Relate integral variables to other types
5959
(&ty::Infer(ty::IntVar(a_id)), &ty::Infer(ty::IntVar(b_id))) => {
60-
self.inner
61-
.borrow_mut()
62-
.int_unification_table()
63-
.unify_var_var(a_id, b_id)
64-
.map_err(|e| int_unification_error(true, e))?;
60+
self.inner.borrow_mut().int_unification_table().union(a_id, b_id);
6561
Ok(a)
6662
}
6763
(&ty::Infer(ty::IntVar(v_id)), &ty::Int(v)) => {
68-
self.unify_integral_variable(true, v_id, IntType(v))
64+
self.unify_integral_variable(v_id, IntType(v));
65+
Ok(b)
6966
}
7067
(&ty::Int(v), &ty::Infer(ty::IntVar(v_id))) => {
71-
self.unify_integral_variable(false, v_id, IntType(v))
68+
self.unify_integral_variable(v_id, IntType(v));
69+
Ok(a)
7270
}
7371
(&ty::Infer(ty::IntVar(v_id)), &ty::Uint(v)) => {
74-
self.unify_integral_variable(true, v_id, UintType(v))
72+
self.unify_integral_variable(v_id, UintType(v));
73+
Ok(b)
7574
}
7675
(&ty::Uint(v), &ty::Infer(ty::IntVar(v_id))) => {
77-
self.unify_integral_variable(false, v_id, UintType(v))
76+
self.unify_integral_variable(v_id, UintType(v));
77+
Ok(a)
7878
}
7979

8080
// Relate floating-point variables to other types
8181
(&ty::Infer(ty::FloatVar(a_id)), &ty::Infer(ty::FloatVar(b_id))) => {
82-
self.inner
83-
.borrow_mut()
84-
.float_unification_table()
85-
.unify_var_var(a_id, b_id)
86-
.map_err(|e| float_unification_error(true, e))?;
82+
self.inner.borrow_mut().float_unification_table().union(a_id, b_id);
8783
Ok(a)
8884
}
8985
(&ty::Infer(ty::FloatVar(v_id)), &ty::Float(v)) => {
90-
self.unify_float_variable(true, v_id, v)
86+
self.unify_float_variable(v_id, ty::FloatVarValue::Known(v));
87+
Ok(b)
9188
}
9289
(&ty::Float(v), &ty::Infer(ty::FloatVar(v_id))) => {
93-
self.unify_float_variable(false, v_id, v)
90+
self.unify_float_variable(v_id, ty::FloatVarValue::Known(v));
91+
Ok(a)
9492
}
9593

9694
// We don't expect `TyVar` or `Fresh*` vars at this point with lazy norm.
@@ -264,35 +262,12 @@ impl<'tcx> InferCtxt<'tcx> {
264262
}
265263
}
266264

267-
fn unify_integral_variable(
268-
&self,
269-
vid_is_expected: bool,
270-
vid: ty::IntVid,
271-
val: ty::IntVarValue,
272-
) -> RelateResult<'tcx, Ty<'tcx>> {
273-
self.inner
274-
.borrow_mut()
275-
.int_unification_table()
276-
.unify_var_value(vid, Some(val))
277-
.map_err(|e| int_unification_error(vid_is_expected, e))?;
278-
match val {
279-
IntType(v) => Ok(Ty::new_int(self.tcx, v)),
280-
UintType(v) => Ok(Ty::new_uint(self.tcx, v)),
281-
}
265+
fn unify_integral_variable(&self, vid: ty::IntVid, val: ty::IntVarValue) {
266+
self.inner.borrow_mut().int_unification_table().union_value(vid, val);
282267
}
283268

284-
fn unify_float_variable(
285-
&self,
286-
vid_is_expected: bool,
287-
vid: ty::FloatVid,
288-
val: ty::FloatTy,
289-
) -> RelateResult<'tcx, Ty<'tcx>> {
290-
self.inner
291-
.borrow_mut()
292-
.float_unification_table()
293-
.unify_var_value(vid, Some(ty::FloatVarValue(val)))
294-
.map_err(|e| float_unification_error(vid_is_expected, e))?;
295-
Ok(Ty::new_float(self.tcx, val))
269+
fn unify_float_variable(&self, vid: ty::FloatVid, val: ty::FloatVarValue) {
270+
self.inner.borrow_mut().float_unification_table().union_value(vid, val);
296271
}
297272

298273
fn unify_effect_variable(&self, vid: ty::EffectVid, val: ty::Const<'tcx>) -> ty::Const<'tcx> {
@@ -364,19 +339,3 @@ pub trait ObligationEmittingRelation<'tcx>: TypeRelation<'tcx> {
364339
/// Register `AliasRelate` obligation(s) that both types must be related to each other.
365340
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>);
366341
}
367-
368-
fn int_unification_error<'tcx>(
369-
a_is_expected: bool,
370-
v: (ty::IntVarValue, ty::IntVarValue),
371-
) -> TypeError<'tcx> {
372-
let (a, b) = v;
373-
TypeError::IntMismatch(ExpectedFound::new(a_is_expected, a, b))
374-
}
375-
376-
fn float_unification_error<'tcx>(
377-
a_is_expected: bool,
378-
v: (ty::FloatVarValue, ty::FloatVarValue),
379-
) -> TypeError<'tcx> {
380-
let (ty::FloatVarValue(a), ty::FloatVarValue(b)) = v;
381-
TypeError::FloatMismatch(ExpectedFound::new(a_is_expected, a, b))
382-
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ where
6565

6666
let infcx = this.infcx();
6767

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

7171
match (a.kind(), b.kind()) {
7272
// 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))) => {

compiler/rustc_middle/src/infer/unify_key.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,6 @@ impl<'tcx> UnifyValue for RegionVariableValue<'tcx> {
8787
}
8888
}
8989

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

10792
#[derive(Copy, Clone, Debug)]
@@ -217,6 +202,7 @@ impl<'tcx> EffectVarValue<'tcx> {
217202

218203
impl<'tcx> UnifyValue for EffectVarValue<'tcx> {
219204
type Error = NoError;
205+
220206
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
221207
match (*value1, *value2) {
222208
(EffectVarValue::Unknown, EffectVarValue::Unknown) => Ok(EffectVarValue::Unknown),

0 commit comments

Comments
 (0)