Skip to content

Commit 6e5acfb

Browse files
Simplify IntVarValue/FloatVarValue
1 parent 3fba278 commit 6e5acfb

File tree

9 files changed

+127
-171
lines changed

9 files changed

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

+31-28
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::*;
@@ -28,9 +27,9 @@ use rustc_data_structures::unify as ut;
2827
use rustc_errors::{Diag, DiagCtxt, ErrorGuaranteed};
2928
use rustc_hir::def_id::{DefId, LocalDefId};
3029
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
30+
use rustc_middle::infer::unify_key::ConstVariableOrigin;
3131
use rustc_middle::infer::unify_key::ConstVariableValue;
3232
use rustc_middle::infer::unify_key::EffectVarValue;
33-
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ToType};
3433
use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};
3534
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
3635
use rustc_middle::mir::ConstraintCategory;
@@ -799,13 +798,13 @@ impl<'tcx> InferCtxt<'tcx> {
799798
vars.extend(
800799
(0..inner.int_unification_table().len())
801800
.map(|i| ty::IntVid::from_u32(i as u32))
802-
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
801+
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
803802
.map(|v| Ty::new_int_var(self.tcx, v)),
804803
);
805804
vars.extend(
806805
(0..inner.float_unification_table().len())
807806
.map(|i| ty::FloatVid::from_u32(i as u32))
808-
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
807+
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
809808
.map(|v| Ty::new_float_var(self.tcx, v)),
810809
);
811810
vars
@@ -1041,15 +1040,15 @@ impl<'tcx> InferCtxt<'tcx> {
10411040
}
10421041

10431042
fn next_int_var_id(&self) -> IntVid {
1044-
self.inner.borrow_mut().int_unification_table().new_key(None)
1043+
self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown)
10451044
}
10461045

10471046
pub fn next_int_var(&self) -> Ty<'tcx> {
10481047
Ty::new_int_var(self.tcx, self.next_int_var_id())
10491048
}
10501049

10511050
fn next_float_var_id(&self) -> FloatVid {
1052-
self.inner.borrow_mut().float_unification_table().new_key(None)
1051+
self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown)
10531052
}
10541053

10551054
pub fn next_float_var(&self) -> Ty<'tcx> {
@@ -1279,19 +1278,18 @@ impl<'tcx> InferCtxt<'tcx> {
12791278
known.map(|t| self.shallow_resolve(t))
12801279
}
12811280

1282-
ty::IntVar(v) => self
1283-
.inner
1284-
.borrow_mut()
1285-
.int_unification_table()
1286-
.probe_value(v)
1287-
.map(|v| v.to_type(self.tcx)),
1281+
ty::IntVar(v) => match self.inner.borrow_mut().int_unification_table().probe_value(v) {
1282+
ty::IntVarValue::Unknown => None,
1283+
ty::IntVarValue::IntType(ty) => Some(Ty::new_int(self.tcx, ty)),
1284+
ty::IntVarValue::UintType(ty) => Some(Ty::new_uint(self.tcx, ty)),
1285+
},
12881286

1289-
ty::FloatVar(v) => self
1290-
.inner
1291-
.borrow_mut()
1292-
.float_unification_table()
1293-
.probe_value(v)
1294-
.map(|v| v.to_type(self.tcx)),
1287+
ty::FloatVar(v) => {
1288+
match self.inner.borrow_mut().float_unification_table().probe_value(v) {
1289+
ty::FloatVarValue::Unknown => None,
1290+
ty::FloatVarValue::Known(ty) => Some(Ty::new_float(self.tcx, ty)),
1291+
}
1292+
}
12951293

12961294
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None,
12971295
}
@@ -1342,21 +1340,26 @@ impl<'tcx> InferCtxt<'tcx> {
13421340
/// or else the root int var in the unification table.
13431341
pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
13441342
let mut inner = self.inner.borrow_mut();
1345-
if let Some(value) = inner.int_unification_table().probe_value(vid) {
1346-
value.to_type(self.tcx)
1347-
} else {
1348-
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1343+
let value = inner.int_unification_table().probe_value(vid);
1344+
match value {
1345+
ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1346+
ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1347+
ty::IntVarValue::Unknown => {
1348+
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1349+
}
13491350
}
13501351
}
13511352

13521353
/// Resolves a float var to a rigid int type, if it was constrained to one,
13531354
/// or else the root float var in the unification table.
13541355
pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
13551356
let mut inner = self.inner.borrow_mut();
1356-
if let Some(value) = inner.float_unification_table().probe_value(vid) {
1357-
value.to_type(self.tcx)
1358-
} else {
1359-
Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1357+
let value = inner.float_unification_table().probe_value(vid);
1358+
match value {
1359+
ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1360+
ty::FloatVarValue::Unknown => {
1361+
Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1362+
}
13601363
}
13611364
}
13621365

@@ -1667,15 +1670,15 @@ impl<'tcx> InferCtxt<'tcx> {
16671670
// If `inlined_probe_value` returns a value it's always a
16681671
// `ty::Int(_)` or `ty::UInt(_)`, which never matches a
16691672
// `ty::Infer(_)`.
1670-
self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_some()
1673+
!self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_unknown()
16711674
}
16721675

16731676
TyOrConstInferVar::TyFloat(v) => {
16741677
// If `probe_value` returns a value it's always a
16751678
// `ty::Float(_)`, which never matches a `ty::Infer(_)`.
16761679
//
16771680
// Not `inlined_probe_value(v)` because this call site is colder.
1678-
self.inner.borrow_mut().float_unification_table().probe_value(v).is_some()
1681+
!self.inner.borrow_mut().float_unification_table().probe_value(v).is_unknown()
16791682
}
16801683

16811684
TyOrConstInferVar::Const(v) => {

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