Auto merge of #123536 - compiler-errors:simplify-int-float, r=lcnr

Simplify `IntVarValue`/`FloatVarValue`

r? `@ghost`
This commit is contained in:
bors 2024-06-04 17:07:13 +00:00
commit 44701e070c
9 changed files with 185 additions and 198 deletions

View File

@ -33,7 +33,6 @@
use super::InferCtxt; use super::InferCtxt;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::infer::unify_key::ToType;
use rustc_middle::ty::fold::TypeFolder; use rustc_middle::ty::fold::TypeFolder;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitableExt}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable, TypeVisitableExt};
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
@ -204,22 +203,27 @@ fn fold_infer_ty(&mut self, v: ty::InferTy) -> Option<Ty<'tcx>> {
ty::IntVar(v) => { ty::IntVar(v) => {
let mut inner = self.infcx.inner.borrow_mut(); let mut inner = self.infcx.inner.borrow_mut();
let input = inner let value = inner.int_unification_table().probe_value(v);
.int_unification_table() let input = match value {
.probe_value(v) ty::IntVarValue::IntType(ty) => Ok(Ty::new_int(self.infcx.tcx, ty)),
.map(|v| v.to_type(self.infcx.tcx)) ty::IntVarValue::UintType(ty) => Ok(Ty::new_uint(self.infcx.tcx, ty)),
.ok_or_else(|| ty::IntVar(inner.int_unification_table().find(v))); ty::IntVarValue::Unknown => {
Err(ty::IntVar(inner.int_unification_table().find(v)))
}
};
drop(inner); drop(inner);
Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n))) Some(self.freshen_ty(input, |n| Ty::new_fresh_int(self.infcx.tcx, n)))
} }
ty::FloatVar(v) => { ty::FloatVar(v) => {
let mut inner = self.infcx.inner.borrow_mut(); let mut inner = self.infcx.inner.borrow_mut();
let input = inner let value = inner.float_unification_table().probe_value(v);
.float_unification_table() let input = match value {
.probe_value(v) ty::FloatVarValue::Known(ty) => Ok(Ty::new_float(self.infcx.tcx, ty)),
.map(|v| v.to_type(self.infcx.tcx)) ty::FloatVarValue::Unknown => {
.ok_or_else(|| ty::FloatVar(inner.float_unification_table().find(v))); Err(ty::FloatVar(inner.float_unification_table().find(v)))
}
};
drop(inner); drop(inner);
Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n))) Some(self.freshen_ty(input, |n| Ty::new_fresh_float(self.infcx.tcx, n)))
} }

View File

@ -29,9 +29,9 @@
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_macros::extension; use rustc_macros::extension;
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues}; use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
use rustc_middle::infer::unify_key::ConstVariableOrigin;
use rustc_middle::infer::unify_key::ConstVariableValue; use rustc_middle::infer::unify_key::ConstVariableValue;
use rustc_middle::infer::unify_key::EffectVarValue; use rustc_middle::infer::unify_key::EffectVarValue;
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ToType};
use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey}; use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult}; use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
use rustc_middle::mir::ConstraintCategory; use rustc_middle::mir::ConstraintCategory;
@ -41,7 +41,7 @@
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::relate::RelateResult; use rustc_middle::ty::relate::RelateResult;
use rustc_middle::ty::visit::TypeVisitableExt; use rustc_middle::ty::visit::TypeVisitableExt;
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, InferTy, Ty, TyCtxt}; use rustc_middle::ty::{self, GenericParamDefKind, InferConst, Ty, TyCtxt};
use rustc_middle::ty::{ConstVid, EffectVid, FloatVid, IntVid, TyVid}; use rustc_middle::ty::{ConstVid, EffectVid, FloatVid, IntVid, TyVid};
use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgs, GenericArgsRef}; use rustc_middle::ty::{GenericArg, GenericArgKind, GenericArgs, GenericArgsRef};
use rustc_middle::{bug, span_bug}; use rustc_middle::{bug, span_bug};
@ -813,13 +813,13 @@ pub fn unresolved_variables(&self) -> Vec<Ty<'tcx>> {
vars.extend( vars.extend(
(0..inner.int_unification_table().len()) (0..inner.int_unification_table().len())
.map(|i| ty::IntVid::from_u32(i as u32)) .map(|i| ty::IntVid::from_u32(i as u32))
.filter(|&vid| inner.int_unification_table().probe_value(vid).is_none()) .filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
.map(|v| Ty::new_int_var(self.tcx, v)), .map(|v| Ty::new_int_var(self.tcx, v)),
); );
vars.extend( vars.extend(
(0..inner.float_unification_table().len()) (0..inner.float_unification_table().len())
.map(|i| ty::FloatVid::from_u32(i as u32)) .map(|i| ty::FloatVid::from_u32(i as u32))
.filter(|&vid| inner.float_unification_table().probe_value(vid).is_none()) .filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
.map(|v| Ty::new_float_var(self.tcx, v)), .map(|v| Ty::new_float_var(self.tcx, v)),
); );
vars vars
@ -1027,14 +1027,28 @@ pub fn next_const_var_in_universe(
ty::Const::new_var(self.tcx, vid, ty) ty::Const::new_var(self.tcx, vid, ty)
} }
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid {
self.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
.vid
}
fn next_int_var_id(&self) -> IntVid {
self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown)
}
pub fn next_int_var(&self) -> Ty<'tcx> { pub fn next_int_var(&self) -> Ty<'tcx> {
let vid = self.inner.borrow_mut().int_unification_table().new_key(None); Ty::new_int_var(self.tcx, self.next_int_var_id())
Ty::new_int_var(self.tcx, vid) }
fn next_float_var_id(&self) -> FloatVid {
self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown)
} }
pub fn next_float_var(&self) -> Ty<'tcx> { pub fn next_float_var(&self) -> Ty<'tcx> {
let vid = self.inner.borrow_mut().float_unification_table().new_key(None); Ty::new_float_var(self.tcx, self.next_float_var_id())
Ty::new_float_var(self.tcx, vid)
} }
/// Creates a fresh region variable with the next available index. /// Creates a fresh region variable with the next available index.
@ -1236,12 +1250,7 @@ pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
} }
pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> { pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
if let ty::Infer(v) = ty.kind() { self.fold_infer_ty(*v).unwrap_or(ty) } else { ty } if let ty::Infer(v) = *ty.kind() {
}
// This is separate from `shallow_resolve` to keep that method small and inlinable.
#[inline(never)]
fn fold_infer_ty(&self, v: InferTy) -> Option<Ty<'tcx>> {
match v { match v {
ty::TyVar(v) => { ty::TyVar(v) => {
// Not entirely obvious: if `typ` is a type variable, // Not entirely obvious: if `typ` is a type variable,
@ -1257,24 +1266,28 @@ fn fold_infer_ty(&self, v: InferTy) -> Option<Ty<'tcx>> {
// Note: if these two lines are combined into one we get // Note: if these two lines are combined into one we get
// dynamic borrow errors on `self.inner`. // dynamic borrow errors on `self.inner`.
let known = self.inner.borrow_mut().type_variables().probe(v).known(); let known = self.inner.borrow_mut().type_variables().probe(v).known();
known.map(|t| self.shallow_resolve(t)) known.map_or(ty, |t| self.shallow_resolve(t))
} }
ty::IntVar(v) => self ty::IntVar(v) => {
.inner match self.inner.borrow_mut().int_unification_table().probe_value(v) {
.borrow_mut() ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
.int_unification_table() ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
.probe_value(v) ty::IntVarValue::Unknown => ty,
.map(|v| v.to_type(self.tcx)), }
}
ty::FloatVar(v) => self ty::FloatVar(v) => {
.inner match self.inner.borrow_mut().float_unification_table().probe_value(v) {
.borrow_mut() ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
.float_unification_table() ty::FloatVarValue::Unknown => ty,
.probe_value(v) }
.map(|v| v.to_type(self.tcx)), }
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => None, ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => ty,
}
} else {
ty
} }
} }
@ -1323,23 +1336,28 @@ pub fn root_effect_var(&self, var: ty::EffectVid) -> ty::EffectVid {
/// or else the root int var in the unification table. /// or else the root int var in the unification table.
pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> { pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
if let Some(value) = inner.int_unification_table().probe_value(vid) { let value = inner.int_unification_table().probe_value(vid);
value.to_type(self.tcx) match value {
} else { ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
ty::IntVarValue::Unknown => {
Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid)) Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
} }
} }
}
/// Resolves a float var to a rigid int type, if it was constrained to one, /// Resolves a float var to a rigid int type, if it was constrained to one,
/// or else the root float var in the unification table. /// or else the root float var in the unification table.
pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> { pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
if let Some(value) = inner.float_unification_table().probe_value(vid) { let value = inner.float_unification_table().probe_value(vid);
value.to_type(self.tcx) match value {
} else { ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
ty::FloatVarValue::Unknown => {
Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid)) Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
} }
} }
}
/// Where possible, replaces type/const variables in /// Where possible, replaces type/const variables in
/// `value` with their final value. Note that region variables /// `value` with their final value. Note that region variables
@ -1628,7 +1646,7 @@ pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar) -> boo
// If `inlined_probe_value` returns a value it's always a // If `inlined_probe_value` returns a value it's always a
// `ty::Int(_)` or `ty::UInt(_)`, which never matches a // `ty::Int(_)` or `ty::UInt(_)`, which never matches a
// `ty::Infer(_)`. // `ty::Infer(_)`.
self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_some() self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_known()
} }
TyOrConstInferVar::TyFloat(v) => { TyOrConstInferVar::TyFloat(v) => {
@ -1636,7 +1654,7 @@ pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar) -> boo
// `ty::Float(_)`, which never matches a `ty::Infer(_)`. // `ty::Float(_)`, which never matches a `ty::Infer(_)`.
// //
// Not `inlined_probe_value(v)` because this call site is colder. // Not `inlined_probe_value(v)` because this call site is colder.
self.inner.borrow_mut().float_unification_table().probe_value(v).is_some() self.inner.borrow_mut().float_unification_table().probe_value(v).is_known()
} }
TyOrConstInferVar::Const(v) => { TyOrConstInferVar::Const(v) => {

View File

@ -26,7 +26,7 @@
use crate::traits::{Obligation, PredicateObligations}; use crate::traits::{Obligation, PredicateObligations};
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::infer::unify_key::EffectVarValue; use rustc_middle::infer::unify_key::EffectVarValue;
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::relate::{RelateResult, TypeRelation}; use rustc_middle::ty::relate::{RelateResult, TypeRelation};
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt, TypeVisitableExt, Upcast}; use rustc_middle::ty::{self, InferConst, Ty, TyCtxt, TypeVisitableExt, Upcast};
use rustc_middle::ty::{IntType, UintType}; use rustc_middle::ty::{IntType, UintType};
@ -68,40 +68,38 @@ pub fn super_combine_tys<R>(
match (a.kind(), b.kind()) { match (a.kind(), b.kind()) {
// Relate integral variables to other types // Relate integral variables to other types
(&ty::Infer(ty::IntVar(a_id)), &ty::Infer(ty::IntVar(b_id))) => { (&ty::Infer(ty::IntVar(a_id)), &ty::Infer(ty::IntVar(b_id))) => {
self.inner self.inner.borrow_mut().int_unification_table().union(a_id, b_id);
.borrow_mut()
.int_unification_table()
.unify_var_var(a_id, b_id)
.map_err(|e| int_unification_error(true, e))?;
Ok(a) Ok(a)
} }
(&ty::Infer(ty::IntVar(v_id)), &ty::Int(v)) => { (&ty::Infer(ty::IntVar(v_id)), &ty::Int(v)) => {
self.unify_integral_variable(true, v_id, IntType(v)) self.unify_integral_variable(v_id, IntType(v));
Ok(b)
} }
(&ty::Int(v), &ty::Infer(ty::IntVar(v_id))) => { (&ty::Int(v), &ty::Infer(ty::IntVar(v_id))) => {
self.unify_integral_variable(false, v_id, IntType(v)) self.unify_integral_variable(v_id, IntType(v));
Ok(a)
} }
(&ty::Infer(ty::IntVar(v_id)), &ty::Uint(v)) => { (&ty::Infer(ty::IntVar(v_id)), &ty::Uint(v)) => {
self.unify_integral_variable(true, v_id, UintType(v)) self.unify_integral_variable(v_id, UintType(v));
Ok(b)
} }
(&ty::Uint(v), &ty::Infer(ty::IntVar(v_id))) => { (&ty::Uint(v), &ty::Infer(ty::IntVar(v_id))) => {
self.unify_integral_variable(false, v_id, UintType(v)) self.unify_integral_variable(v_id, UintType(v));
Ok(a)
} }
// Relate floating-point variables to other types // Relate floating-point variables to other types
(&ty::Infer(ty::FloatVar(a_id)), &ty::Infer(ty::FloatVar(b_id))) => { (&ty::Infer(ty::FloatVar(a_id)), &ty::Infer(ty::FloatVar(b_id))) => {
self.inner self.inner.borrow_mut().float_unification_table().union(a_id, b_id);
.borrow_mut()
.float_unification_table()
.unify_var_var(a_id, b_id)
.map_err(|e| float_unification_error(true, e))?;
Ok(a) Ok(a)
} }
(&ty::Infer(ty::FloatVar(v_id)), &ty::Float(v)) => { (&ty::Infer(ty::FloatVar(v_id)), &ty::Float(v)) => {
self.unify_float_variable(true, v_id, v) self.unify_float_variable(v_id, ty::FloatVarValue::Known(v));
Ok(b)
} }
(&ty::Float(v), &ty::Infer(ty::FloatVar(v_id))) => { (&ty::Float(v), &ty::Infer(ty::FloatVar(v_id))) => {
self.unify_float_variable(false, v_id, v) self.unify_float_variable(v_id, ty::FloatVarValue::Known(v));
Ok(a)
} }
// We don't expect `TyVar` or `Fresh*` vars at this point with lazy norm. // We don't expect `TyVar` or `Fresh*` vars at this point with lazy norm.
@ -244,35 +242,14 @@ pub fn super_combine_consts<R>(
} }
} }
fn unify_integral_variable( #[inline(always)]
&self, fn unify_integral_variable(&self, vid: ty::IntVid, val: ty::IntVarValue) {
vid_is_expected: bool, self.inner.borrow_mut().int_unification_table().union_value(vid, val);
vid: ty::IntVid,
val: ty::IntVarValue,
) -> RelateResult<'tcx, Ty<'tcx>> {
self.inner
.borrow_mut()
.int_unification_table()
.unify_var_value(vid, Some(val))
.map_err(|e| int_unification_error(vid_is_expected, e))?;
match val {
IntType(v) => Ok(Ty::new_int(self.tcx, v)),
UintType(v) => Ok(Ty::new_uint(self.tcx, v)),
}
} }
fn unify_float_variable( #[inline(always)]
&self, fn unify_float_variable(&self, vid: ty::FloatVid, val: ty::FloatVarValue) {
vid_is_expected: bool, self.inner.borrow_mut().float_unification_table().union_value(vid, val);
vid: ty::FloatVid,
val: ty::FloatTy,
) -> RelateResult<'tcx, Ty<'tcx>> {
self.inner
.borrow_mut()
.float_unification_table()
.unify_var_value(vid, Some(ty::FloatVarValue(val)))
.map_err(|e| float_unification_error(vid_is_expected, e))?;
Ok(Ty::new_float(self.tcx, val))
} }
fn unify_effect_variable(&self, vid: ty::EffectVid, val: ty::Const<'tcx>) -> ty::Const<'tcx> { fn unify_effect_variable(&self, vid: ty::EffectVid, val: ty::Const<'tcx>) -> ty::Const<'tcx> {
@ -350,19 +327,3 @@ fn register_predicates(
/// Register `AliasRelate` obligation(s) that both types must be related to each other. /// Register `AliasRelate` obligation(s) that both types must be related to each other.
fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>); fn register_type_relate_obligation(&mut self, a: Ty<'tcx>, b: Ty<'tcx>);
} }
fn int_unification_error<'tcx>(
a_is_expected: bool,
v: (ty::IntVarValue, ty::IntVarValue),
) -> TypeError<'tcx> {
let (a, b) = v;
TypeError::IntMismatch(ExpectedFound::new(a_is_expected, a, b))
}
fn float_unification_error<'tcx>(
a_is_expected: bool,
v: (ty::FloatVarValue, ty::FloatVarValue),
) -> TypeError<'tcx> {
let (ty::FloatVarValue(a), ty::FloatVarValue(b)) = v;
TypeError::FloatMismatch(ExpectedFound::new(a_is_expected, a, b))
}

View File

@ -64,8 +64,8 @@ pub fn super_lattice_tys<'a, 'tcx: 'a, L>(
let infcx = this.infcx(); let infcx = this.infcx();
let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a); let a = infcx.shallow_resolve(a);
let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b); let b = infcx.shallow_resolve(b);
match (a.kind(), b.kind()) { match (a.kind(), b.kind()) {
// If one side is known to be a variable and one is not, // If one side is known to be a variable and one is not,

View File

@ -80,8 +80,8 @@ fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
} }
let infcx = self.fields.infcx; let infcx = self.fields.infcx;
let a = infcx.inner.borrow_mut().type_variables().replace_if_possible(a); let a = infcx.shallow_resolve(a);
let b = infcx.inner.borrow_mut().type_variables().replace_if_possible(b); let b = infcx.shallow_resolve(b);
match (a.kind(), b.kind()) { match (a.kind(), b.kind()) {
(&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => { (&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {

View File

@ -86,21 +86,6 @@ fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
} }
} }
impl ToType for ty::IntVarValue {
fn to_type<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
match *self {
ty::IntType(i) => Ty::new_int(tcx, i),
ty::UintType(i) => Ty::new_uint(tcx, i),
}
}
}
impl ToType for ty::FloatVarValue {
fn to_type<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
Ty::new_float(tcx, self.0)
}
}
// Generic consts. // Generic consts.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@ -211,6 +196,7 @@ pub fn is_unknown(self) -> bool {
impl<'tcx> UnifyValue for EffectVarValue<'tcx> { impl<'tcx> UnifyValue for EffectVarValue<'tcx> {
type Error = NoError; type Error = NoError;
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> { fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
match (*value1, *value2) { match (*value1, *value2) {
(EffectVarValue::Unknown, EffectVarValue::Unknown) => Ok(EffectVarValue::Unknown), (EffectVarValue::Unknown, EffectVarValue::Unknown) => Ok(EffectVarValue::Unknown),

View File

@ -49,8 +49,6 @@ pub enum TypeError<'tcx> {
Sorts(ExpectedFound<Ty<'tcx>>), Sorts(ExpectedFound<Ty<'tcx>>),
ArgumentSorts(ExpectedFound<Ty<'tcx>>, usize), ArgumentSorts(ExpectedFound<Ty<'tcx>>, usize),
IntMismatch(ExpectedFound<ty::IntVarValue>),
FloatMismatch(ExpectedFound<ty::FloatTy>),
Traits(ExpectedFound<DefId>), Traits(ExpectedFound<DefId>),
VariadicMismatch(ExpectedFound<bool>), VariadicMismatch(ExpectedFound<bool>),
@ -155,23 +153,6 @@ fn report_maybe_different(expected: &str, found: &str) -> String {
report_maybe_different(&format!("trait `{expected}`"), &format!("trait `{found}`")) report_maybe_different(&format!("trait `{expected}`"), &format!("trait `{found}`"))
.into() .into()
} }
IntMismatch(ref values) => {
let expected = match values.expected {
ty::IntVarValue::IntType(ty) => ty.name_str(),
ty::IntVarValue::UintType(ty) => ty.name_str(),
};
let found = match values.found {
ty::IntVarValue::IntType(ty) => ty.name_str(),
ty::IntVarValue::UintType(ty) => ty.name_str(),
};
format!("expected `{expected}`, found `{found}`").into()
}
FloatMismatch(ref values) => format!(
"expected `{}`, found `{}`",
values.expected.name_str(),
values.found.name_str()
)
.into(),
VariadicMismatch(ref values) => format!( VariadicMismatch(ref values) => format!(
"expected {} fn, found {} function", "expected {} fn, found {} function",
if values.expected { "variadic" } else { "non-variadic" }, if values.expected { "variadic" } else { "non-variadic" },
@ -206,8 +187,7 @@ pub fn must_include_note(self) -> bool {
match self { match self {
CyclicTy(_) | CyclicConst(_) | SafetyMismatch(_) | ConstnessMismatch(_) CyclicTy(_) | CyclicConst(_) | SafetyMismatch(_) | ConstnessMismatch(_)
| PolarityMismatch(_) | Mismatch | AbiMismatch(_) | FixedArraySize(_) | PolarityMismatch(_) | Mismatch | AbiMismatch(_) | FixedArraySize(_)
| ArgumentSorts(..) | Sorts(_) | IntMismatch(_) | FloatMismatch(_) | ArgumentSorts(..) | Sorts(_) | VariadicMismatch(_) | TargetFeatureCast(_) => false,
| VariadicMismatch(_) | TargetFeatureCast(_) => false,
Mutability Mutability
| ArgumentMutability(_) | ArgumentMutability(_)

View File

@ -1,7 +1,7 @@
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
use rustc_data_structures::unify::{EqUnifyValue, UnifyKey}; use rustc_data_structures::unify::{NoError, UnifyKey, UnifyValue};
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
use rustc_macros::{Decodable, Encodable, HashStable_NoContext, TyDecodable, TyEncodable}; use rustc_macros::{Decodable, Encodable, HashStable_NoContext, TyDecodable, TyEncodable};
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic}; use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
@ -717,14 +717,44 @@ pub fn bit_width(self) -> u64 {
} }
} }
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum IntVarValue { pub enum IntVarValue {
Unknown,
IntType(IntTy), IntType(IntTy),
UintType(UintTy), UintType(UintTy),
} }
#[derive(Clone, Copy, PartialEq, Eq)] impl IntVarValue {
pub struct FloatVarValue(pub FloatTy); pub fn is_known(self) -> bool {
match self {
IntVarValue::IntType(_) | IntVarValue::UintType(_) => true,
IntVarValue::Unknown => false,
}
}
pub fn is_unknown(self) -> bool {
!self.is_known()
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum FloatVarValue {
Unknown,
Known(FloatTy),
}
impl FloatVarValue {
pub fn is_known(self) -> bool {
match self {
FloatVarValue::Known(_) => true,
FloatVarValue::Unknown => false,
}
}
pub fn is_unknown(self) -> bool {
!self.is_known()
}
}
rustc_index::newtype_index! { rustc_index::newtype_index! {
/// A **ty**pe **v**ariable **ID**. /// A **ty**pe **v**ariable **ID**.
@ -809,11 +839,28 @@ fn tag() -> &'static str {
} }
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
impl EqUnifyValue for IntVarValue {} impl UnifyValue for IntVarValue {
type Error = NoError;
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
match (*value1, *value2) {
(IntVarValue::Unknown, IntVarValue::Unknown) => Ok(IntVarValue::Unknown),
(
IntVarValue::Unknown,
known @ (IntVarValue::UintType(_) | IntVarValue::IntType(_)),
)
| (
known @ (IntVarValue::UintType(_) | IntVarValue::IntType(_)),
IntVarValue::Unknown,
) => Ok(known),
_ => panic!("differing ints should have been resolved first"),
}
}
}
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
impl UnifyKey for IntVid { impl UnifyKey for IntVid {
type Value = Option<IntVarValue>; type Value = IntVarValue;
#[inline] // make this function eligible for inlining - it is quite hot. #[inline] // make this function eligible for inlining - it is quite hot.
fn index(&self) -> u32 { fn index(&self) -> u32 {
self.as_u32() self.as_u32()
@ -828,11 +875,26 @@ fn tag() -> &'static str {
} }
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
impl EqUnifyValue for FloatVarValue {} impl UnifyValue for FloatVarValue {
type Error = NoError;
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
match (*value1, *value2) {
(FloatVarValue::Unknown, FloatVarValue::Unknown) => Ok(FloatVarValue::Unknown),
(FloatVarValue::Unknown, FloatVarValue::Known(known))
| (FloatVarValue::Known(known), FloatVarValue::Unknown) => {
Ok(FloatVarValue::Known(known))
}
(FloatVarValue::Known(_), FloatVarValue::Known(_)) => {
panic!("differing floats should have been resolved first")
}
}
}
}
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
impl UnifyKey for FloatVid { impl UnifyKey for FloatVid {
type Value = Option<FloatVarValue>; type Value = FloatVarValue;
#[inline] #[inline]
fn index(&self) -> u32 { fn index(&self) -> u32 {
self.as_u32() self.as_u32()
@ -860,21 +922,6 @@ fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
} }
} }
impl fmt::Debug for IntVarValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
IntVarValue::IntType(ref v) => v.fmt(f),
IntVarValue::UintType(ref v) => v.fmt(f),
}
}
}
impl fmt::Debug for FloatVarValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Display for InferTy { impl fmt::Display for InferTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use InferTy::*; use InferTy::*;

View File

@ -316,9 +316,6 @@ LL | if let X.. .0 = 0 {}
| | | | | |
| | expected `u8`, found floating-point number | | expected `u8`, found floating-point number
| this is of type `u8` | this is of type `u8`
|
= note: expected type `u8`
found type `{float}`
error[E0029]: only `char` and numeric types are allowed in range patterns error[E0029]: only `char` and numeric types are allowed in range patterns
--> $DIR/recover-range-pats.rs:31:12 --> $DIR/recover-range-pats.rs:31:12
@ -353,9 +350,6 @@ LL | if let X..=.0 = 0 {}
| | | | | |
| | expected `u8`, found floating-point number | | expected `u8`, found floating-point number
| this is of type `u8` | this is of type `u8`
|
= note: expected type `u8`
found type `{float}`
error[E0029]: only `char` and numeric types are allowed in range patterns error[E0029]: only `char` and numeric types are allowed in range patterns
--> $DIR/recover-range-pats.rs:52:12 --> $DIR/recover-range-pats.rs:52:12
@ -390,9 +384,6 @@ LL | if let X... .0 = 0 {}
| | | | | |
| | expected `u8`, found floating-point number | | expected `u8`, found floating-point number
| this is of type `u8` | this is of type `u8`
|
= note: expected type `u8`
found type `{float}`
error[E0029]: only `char` and numeric types are allowed in range patterns error[E0029]: only `char` and numeric types are allowed in range patterns
--> $DIR/recover-range-pats.rs:71:12 --> $DIR/recover-range-pats.rs:71:12