Skip to content

Commit 44701e0

Browse files
committedJun 4, 2024
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))) => {

‎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),

‎compiler/rustc_middle/src/ty/error.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ pub enum TypeError<'tcx> {
4949

5050
Sorts(ExpectedFound<Ty<'tcx>>),
5151
ArgumentSorts(ExpectedFound<Ty<'tcx>>, usize),
52-
IntMismatch(ExpectedFound<ty::IntVarValue>),
53-
FloatMismatch(ExpectedFound<ty::FloatTy>),
5452
Traits(ExpectedFound<DefId>),
5553
VariadicMismatch(ExpectedFound<bool>),
5654

@@ -155,23 +153,6 @@ impl<'tcx> TypeError<'tcx> {
155153
report_maybe_different(&format!("trait `{expected}`"), &format!("trait `{found}`"))
156154
.into()
157155
}
158-
IntMismatch(ref values) => {
159-
let expected = match values.expected {
160-
ty::IntVarValue::IntType(ty) => ty.name_str(),
161-
ty::IntVarValue::UintType(ty) => ty.name_str(),
162-
};
163-
let found = match values.found {
164-
ty::IntVarValue::IntType(ty) => ty.name_str(),
165-
ty::IntVarValue::UintType(ty) => ty.name_str(),
166-
};
167-
format!("expected `{expected}`, found `{found}`").into()
168-
}
169-
FloatMismatch(ref values) => format!(
170-
"expected `{}`, found `{}`",
171-
values.expected.name_str(),
172-
values.found.name_str()
173-
)
174-
.into(),
175156
VariadicMismatch(ref values) => format!(
176157
"expected {} fn, found {} function",
177158
if values.expected { "variadic" } else { "non-variadic" },
@@ -206,8 +187,7 @@ impl<'tcx> TypeError<'tcx> {
206187
match self {
207188
CyclicTy(_) | CyclicConst(_) | SafetyMismatch(_) | ConstnessMismatch(_)
208189
| PolarityMismatch(_) | Mismatch | AbiMismatch(_) | FixedArraySize(_)
209-
| ArgumentSorts(..) | Sorts(_) | IntMismatch(_) | FloatMismatch(_)
210-
| VariadicMismatch(_) | TargetFeatureCast(_) => false,
190+
| ArgumentSorts(..) | Sorts(_) | VariadicMismatch(_) | TargetFeatureCast(_) => false,
211191

212192
Mutability
213193
| ArgumentMutability(_)

‎compiler/rustc_type_ir/src/ty_kind.rs

+70-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "nightly")]
22
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
33
#[cfg(feature = "nightly")]
4-
use rustc_data_structures::unify::{EqUnifyValue, UnifyKey};
4+
use rustc_data_structures::unify::{NoError, UnifyKey, UnifyValue};
55
#[cfg(feature = "nightly")]
66
use rustc_macros::{Decodable, Encodable, HashStable_NoContext, TyDecodable, TyEncodable};
77
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
@@ -717,14 +717,44 @@ impl FloatTy {
717717
}
718718
}
719719

720-
#[derive(Clone, Copy, PartialEq, Eq)]
720+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
721721
pub enum IntVarValue {
722+
Unknown,
722723
IntType(IntTy),
723724
UintType(UintTy),
724725
}
725726

726-
#[derive(Clone, Copy, PartialEq, Eq)]
727-
pub struct FloatVarValue(pub FloatTy);
727+
impl IntVarValue {
728+
pub fn is_known(self) -> bool {
729+
match self {
730+
IntVarValue::IntType(_) | IntVarValue::UintType(_) => true,
731+
IntVarValue::Unknown => false,
732+
}
733+
}
734+
735+
pub fn is_unknown(self) -> bool {
736+
!self.is_known()
737+
}
738+
}
739+
740+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
741+
pub enum FloatVarValue {
742+
Unknown,
743+
Known(FloatTy),
744+
}
745+
746+
impl FloatVarValue {
747+
pub fn is_known(self) -> bool {
748+
match self {
749+
FloatVarValue::Known(_) => true,
750+
FloatVarValue::Unknown => false,
751+
}
752+
}
753+
754+
pub fn is_unknown(self) -> bool {
755+
!self.is_known()
756+
}
757+
}
728758

729759
rustc_index::newtype_index! {
730760
/// A **ty**pe **v**ariable **ID**.
@@ -809,11 +839,28 @@ impl UnifyKey for TyVid {
809839
}
810840

811841
#[cfg(feature = "nightly")]
812-
impl EqUnifyValue for IntVarValue {}
842+
impl UnifyValue for IntVarValue {
843+
type Error = NoError;
844+
845+
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
846+
match (*value1, *value2) {
847+
(IntVarValue::Unknown, IntVarValue::Unknown) => Ok(IntVarValue::Unknown),
848+
(
849+
IntVarValue::Unknown,
850+
known @ (IntVarValue::UintType(_) | IntVarValue::IntType(_)),
851+
)
852+
| (
853+
known @ (IntVarValue::UintType(_) | IntVarValue::IntType(_)),
854+
IntVarValue::Unknown,
855+
) => Ok(known),
856+
_ => panic!("differing ints should have been resolved first"),
857+
}
858+
}
859+
}
813860

814861
#[cfg(feature = "nightly")]
815862
impl UnifyKey for IntVid {
816-
type Value = Option<IntVarValue>;
863+
type Value = IntVarValue;
817864
#[inline] // make this function eligible for inlining - it is quite hot.
818865
fn index(&self) -> u32 {
819866
self.as_u32()
@@ -828,11 +875,26 @@ impl UnifyKey for IntVid {
828875
}
829876

830877
#[cfg(feature = "nightly")]
831-
impl EqUnifyValue for FloatVarValue {}
878+
impl UnifyValue for FloatVarValue {
879+
type Error = NoError;
880+
881+
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
882+
match (*value1, *value2) {
883+
(FloatVarValue::Unknown, FloatVarValue::Unknown) => Ok(FloatVarValue::Unknown),
884+
(FloatVarValue::Unknown, FloatVarValue::Known(known))
885+
| (FloatVarValue::Known(known), FloatVarValue::Unknown) => {
886+
Ok(FloatVarValue::Known(known))
887+
}
888+
(FloatVarValue::Known(_), FloatVarValue::Known(_)) => {
889+
panic!("differing floats should have been resolved first")
890+
}
891+
}
892+
}
893+
}
832894

833895
#[cfg(feature = "nightly")]
834896
impl UnifyKey for FloatVid {
835-
type Value = Option<FloatVarValue>;
897+
type Value = FloatVarValue;
836898
#[inline]
837899
fn index(&self) -> u32 {
838900
self.as_u32()
@@ -860,21 +922,6 @@ impl<CTX> HashStable<CTX> for InferTy {
860922
}
861923
}
862924

863-
impl fmt::Debug for IntVarValue {
864-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
865-
match *self {
866-
IntVarValue::IntType(ref v) => v.fmt(f),
867-
IntVarValue::UintType(ref v) => v.fmt(f),
868-
}
869-
}
870-
}
871-
872-
impl fmt::Debug for FloatVarValue {
873-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
874-
self.0.fmt(f)
875-
}
876-
}
877-
878925
impl fmt::Display for InferTy {
879926
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
880927
use InferTy::*;

‎tests/ui/parser/recover/recover-range-pats.stderr

-9
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,6 @@ LL | if let X.. .0 = 0 {}
316316
| | |
317317
| | expected `u8`, found floating-point number
318318
| this is of type `u8`
319-
|
320-
= note: expected type `u8`
321-
found type `{float}`
322319

323320
error[E0029]: only `char` and numeric types are allowed in range patterns
324321
--> $DIR/recover-range-pats.rs:31:12
@@ -353,9 +350,6 @@ LL | if let X..=.0 = 0 {}
353350
| | |
354351
| | expected `u8`, found floating-point number
355352
| this is of type `u8`
356-
|
357-
= note: expected type `u8`
358-
found type `{float}`
359353

360354
error[E0029]: only `char` and numeric types are allowed in range patterns
361355
--> $DIR/recover-range-pats.rs:52:12
@@ -390,9 +384,6 @@ LL | if let X... .0 = 0 {}
390384
| | |
391385
| | expected `u8`, found floating-point number
392386
| this is of type `u8`
393-
|
394-
= note: expected type `u8`
395-
found type `{float}`
396387

397388
error[E0029]: only `char` and numeric types are allowed in range patterns
398389
--> $DIR/recover-range-pats.rs:71:12

0 commit comments

Comments
 (0)
Please sign in to comment.