Skip to content

Commit bb0d460

Browse files
Merge #4770
4770: Clean up handling of int/float literal types r=matklad a=flodiebold 'Unknown' int/float types actually never exist as such, they get replaced by type variables immediately. So the whole `Uncertain<IntTy>` thing was unnecessary and just led to a bunch of match branches that were never hit. Co-authored-by: Florian Diebold <flodiebold@gmail.com>
2 parents b366b98 + d66daee commit bb0d460

File tree

6 files changed

+33
-104
lines changed

6 files changed

+33
-104
lines changed

crates/ra_hir_ty/src/infer.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ use ra_syntax::SmolStr;
3939
use super::{
4040
primitive::{FloatTy, IntTy},
4141
traits::{Guidance, Obligation, ProjectionPredicate, Solution},
42-
ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor,
43-
TypeWalk, Uncertain,
42+
InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk,
4443
};
4544
use crate::{
4645
db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
@@ -312,12 +311,6 @@ impl<'a> InferenceContext<'a> {
312311
fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty {
313312
match ty {
314313
Ty::Unknown => self.table.new_type_var(),
315-
Ty::Apply(ApplicationTy { ctor: TypeCtor::Int(Uncertain::Unknown), .. }) => {
316-
self.table.new_integer_var()
317-
}
318-
Ty::Apply(ApplicationTy { ctor: TypeCtor::Float(Uncertain::Unknown), .. }) => {
319-
self.table.new_float_var()
320-
}
321314
_ => ty,
322315
}
323316
}
@@ -664,8 +657,8 @@ impl InferTy {
664657
fn fallback_value(self) -> Ty {
665658
match self {
666659
InferTy::TypeVar(..) => Ty::Unknown,
667-
InferTy::IntVar(..) => Ty::simple(TypeCtor::Int(Uncertain::Known(IntTy::i32()))),
668-
InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(Uncertain::Known(FloatTy::f64()))),
660+
InferTy::IntVar(..) => Ty::simple(TypeCtor::Int(IntTy::i32())),
661+
InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(FloatTy::f64())),
669662
InferTy::MaybeNeverTypeVar(..) => Ty::simple(TypeCtor::Never),
670663
}
671664
}

crates/ra_hir_ty/src/infer/expr.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
traits::InEnvironment,
1919
utils::{generics, variant_data, Generics},
2020
ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Rawness, Substs,
21-
TraitRef, Ty, TypeCtor, Uncertain,
21+
TraitRef, Ty, TypeCtor,
2222
};
2323

2424
use super::{
@@ -426,15 +426,7 @@ impl<'a> InferenceContext<'a> {
426426
match &inner_ty {
427427
// Fast path for builtins
428428
Ty::Apply(ApplicationTy {
429-
ctor:
430-
TypeCtor::Int(Uncertain::Known(IntTy {
431-
signedness: Signedness::Signed,
432-
..
433-
})),
434-
..
435-
})
436-
| Ty::Apply(ApplicationTy {
437-
ctor: TypeCtor::Int(Uncertain::Unknown),
429+
ctor: TypeCtor::Int(IntTy { signedness: Signedness::Signed, .. }),
438430
..
439431
})
440432
| Ty::Apply(ApplicationTy { ctor: TypeCtor::Float(_), .. })
@@ -577,9 +569,7 @@ impl<'a> InferenceContext<'a> {
577569
);
578570
self.infer_expr(
579571
*repeat,
580-
&Expectation::has_type(Ty::simple(TypeCtor::Int(Uncertain::Known(
581-
IntTy::usize(),
582-
)))),
572+
&Expectation::has_type(Ty::simple(TypeCtor::Int(IntTy::usize()))),
583573
);
584574
}
585575
}
@@ -592,13 +582,19 @@ impl<'a> InferenceContext<'a> {
592582
Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
593583
}
594584
Literal::ByteString(..) => {
595-
let byte_type = Ty::simple(TypeCtor::Int(Uncertain::Known(IntTy::u8())));
585+
let byte_type = Ty::simple(TypeCtor::Int(IntTy::u8()));
596586
let array_type = Ty::apply_one(TypeCtor::Array, byte_type);
597587
Ty::apply_one(TypeCtor::Ref(Mutability::Shared), array_type)
598588
}
599589
Literal::Char(..) => Ty::simple(TypeCtor::Char),
600-
Literal::Int(_v, ty) => Ty::simple(TypeCtor::Int((*ty).into())),
601-
Literal::Float(_v, ty) => Ty::simple(TypeCtor::Float((*ty).into())),
590+
Literal::Int(_v, ty) => match ty {
591+
Some(int_ty) => Ty::simple(TypeCtor::Int((*int_ty).into())),
592+
None => self.table.new_integer_var(),
593+
},
594+
Literal::Float(_v, ty) => match ty {
595+
Some(float_ty) => Ty::simple(TypeCtor::Float((*float_ty).into())),
596+
None => self.table.new_float_var(),
597+
},
602598
},
603599
};
604600
// use a new type variable if we got Ty::Unknown here

crates/ra_hir_ty/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use ra_db::{impl_intern_key, salsa, CrateId};
5858

5959
use crate::{
6060
db::HirDatabase,
61-
primitive::{FloatTy, IntTy, Uncertain},
61+
primitive::{FloatTy, IntTy},
6262
utils::{generics, make_mut_slice, Generics},
6363
};
6464
use display::HirDisplay;
@@ -87,10 +87,10 @@ pub enum TypeCtor {
8787
Char,
8888

8989
/// A primitive integer type. For example, `i32`.
90-
Int(Uncertain<IntTy>),
90+
Int(IntTy),
9191

9292
/// A primitive floating-point type. For example, `f64`.
93-
Float(Uncertain<FloatTy>),
93+
Float(FloatTy),
9494

9595
/// Structures, enumerations and unions.
9696
Adt(AdtId),

crates/ra_hir_ty/src/method_resolution.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ use rustc_hash::{FxHashMap, FxHashSet};
1616

1717
use super::Substs;
1818
use crate::{
19-
autoderef,
20-
db::HirDatabase,
21-
primitive::{FloatBitness, Uncertain},
22-
utils::all_super_traits,
23-
ApplicationTy, Canonical, DebruijnIndex, InEnvironment, TraitEnvironment, TraitRef, Ty,
24-
TypeCtor, TypeWalk,
19+
autoderef, db::HirDatabase, primitive::FloatBitness, utils::all_super_traits, ApplicationTy,
20+
Canonical, DebruijnIndex, InEnvironment, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk,
2521
};
2622

2723
/// This is used as a key for indexing impls.
@@ -147,12 +143,12 @@ impl Ty {
147143
}
148144
TypeCtor::Bool => lang_item_crate!("bool"),
149145
TypeCtor::Char => lang_item_crate!("char"),
150-
TypeCtor::Float(Uncertain::Known(f)) => match f.bitness {
146+
TypeCtor::Float(f) => match f.bitness {
151147
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
152148
FloatBitness::X32 => lang_item_crate!("f32", "f32_runtime"),
153149
FloatBitness::X64 => lang_item_crate!("f64", "f64_runtime"),
154150
},
155-
TypeCtor::Int(Uncertain::Known(i)) => lang_item_crate!(i.ty_to_string()),
151+
TypeCtor::Int(i) => lang_item_crate!(i.ty_to_string()),
156152
TypeCtor::Str => lang_item_crate!("str_alloc", "str"),
157153
TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"),
158154
TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"),

crates/ra_hir_ty/src/primitive.rs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,6 @@ use std::fmt;
77

88
pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, FloatBitness, IntBitness, Signedness};
99

10-
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
11-
pub enum Uncertain<T> {
12-
Unknown,
13-
Known(T),
14-
}
15-
16-
impl From<IntTy> for Uncertain<IntTy> {
17-
fn from(ty: IntTy) -> Self {
18-
Uncertain::Known(ty)
19-
}
20-
}
21-
22-
impl fmt::Display for Uncertain<IntTy> {
23-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24-
match *self {
25-
Uncertain::Unknown => write!(f, "{{integer}}"),
26-
Uncertain::Known(ty) => write!(f, "{}", ty),
27-
}
28-
}
29-
}
30-
31-
impl From<FloatTy> for Uncertain<FloatTy> {
32-
fn from(ty: FloatTy) -> Self {
33-
Uncertain::Known(ty)
34-
}
35-
}
36-
37-
impl fmt::Display for Uncertain<FloatTy> {
38-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39-
match *self {
40-
Uncertain::Unknown => write!(f, "{{float}}"),
41-
Uncertain::Known(ty) => write!(f, "{}", ty),
42-
}
43-
}
44-
}
45-
4610
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
4711
pub struct IntTy {
4812
pub signedness: Signedness,
@@ -173,21 +137,3 @@ impl From<BuiltinFloat> for FloatTy {
173137
FloatTy { bitness: t.bitness }
174138
}
175139
}
176-
177-
impl From<Option<BuiltinInt>> for Uncertain<IntTy> {
178-
fn from(t: Option<BuiltinInt>) -> Self {
179-
match t {
180-
None => Uncertain::Unknown,
181-
Some(t) => Uncertain::Known(t.into()),
182-
}
183-
}
184-
}
185-
186-
impl From<Option<BuiltinFloat>> for Uncertain<FloatTy> {
187-
fn from(t: Option<BuiltinFloat>) -> Self {
188-
match t {
189-
None => Uncertain::Unknown,
190-
Some(t) => Uncertain::Known(t.into()),
191-
}
192-
}
193-
}

crates/ra_hir_ty/src/traits/chalk/mapping.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ra_db::salsa::InternKey;
1414

1515
use crate::{
1616
db::HirDatabase,
17-
primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain},
17+
primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness},
1818
traits::{builtin, AssocTyValue, Canonical, Impl, Obligation},
1919
ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId,
2020
ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor,
@@ -249,11 +249,11 @@ impl ToChalk for TypeCtor {
249249

250250
TypeCtor::Bool => TypeName::Scalar(Scalar::Bool),
251251
TypeCtor::Char => TypeName::Scalar(Scalar::Char),
252-
TypeCtor::Int(Uncertain::Known(int_ty)) => TypeName::Scalar(int_ty_to_chalk(int_ty)),
253-
TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X32 })) => {
252+
TypeCtor::Int(int_ty) => TypeName::Scalar(int_ty_to_chalk(int_ty)),
253+
TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) => {
254254
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32))
255255
}
256-
TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X64 })) => {
256+
TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) => {
257257
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64))
258258
}
259259

@@ -268,9 +268,7 @@ impl ToChalk for TypeCtor {
268268
}
269269
TypeCtor::Never => TypeName::Never,
270270

271-
TypeCtor::Int(Uncertain::Unknown)
272-
| TypeCtor::Float(Uncertain::Unknown)
273-
| TypeCtor::Adt(_)
271+
TypeCtor::Adt(_)
274272
| TypeCtor::Array
275273
| TypeCtor::FnPtr { .. }
276274
| TypeCtor::Closure { .. } => {
@@ -291,19 +289,19 @@ impl ToChalk for TypeCtor {
291289

292290
TypeName::Scalar(Scalar::Bool) => TypeCtor::Bool,
293291
TypeName::Scalar(Scalar::Char) => TypeCtor::Char,
294-
TypeName::Scalar(Scalar::Int(int_ty)) => TypeCtor::Int(Uncertain::Known(IntTy {
292+
TypeName::Scalar(Scalar::Int(int_ty)) => TypeCtor::Int(IntTy {
295293
signedness: Signedness::Signed,
296294
bitness: bitness_from_chalk_int(int_ty),
297-
})),
298-
TypeName::Scalar(Scalar::Uint(uint_ty)) => TypeCtor::Int(Uncertain::Known(IntTy {
295+
}),
296+
TypeName::Scalar(Scalar::Uint(uint_ty)) => TypeCtor::Int(IntTy {
299297
signedness: Signedness::Unsigned,
300298
bitness: bitness_from_chalk_uint(uint_ty),
301-
})),
299+
}),
302300
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => {
303-
TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X32 }))
301+
TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 })
304302
}
305303
TypeName::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => {
306-
TypeCtor::Float(Uncertain::Known(FloatTy { bitness: FloatBitness::X64 }))
304+
TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 })
307305
}
308306
TypeName::Tuple(cardinality) => TypeCtor::Tuple { cardinality: cardinality as u16 },
309307
TypeName::Raw(mutability) => TypeCtor::RawPtr(from_chalk(db, mutability)),

0 commit comments

Comments
 (0)