Skip to content

Commit 7c2dd85

Browse files
committed
Use chalk_ir::Scalar directly
1 parent 5183c9f commit 7c2dd85

File tree

6 files changed

+78
-197
lines changed

6 files changed

+78
-197
lines changed

crates/hir_ty/src/display.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
use std::{borrow::Cow, fmt};
44

55
use crate::{
6-
db::HirDatabase, utils::generics, ApplicationTy, CallableDefId, FnSig, GenericPredicate,
7-
Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty,
8-
TypeCtor,
6+
db::HirDatabase, primitive, utils::generics, ApplicationTy, CallableDefId, FnSig,
7+
GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs,
8+
TraitRef, Ty, TypeCtor,
99
};
1010
use arrayvec::ArrayVec;
1111
use hir_def::{
@@ -244,9 +244,11 @@ impl HirDisplay for ApplicationTy {
244244
match self.ctor {
245245
TypeCtor::Scalar(Scalar::Bool) => write!(f, "bool")?,
246246
TypeCtor::Scalar(Scalar::Char) => write!(f, "char")?,
247-
TypeCtor::Scalar(Scalar::Float(t)) => write!(f, "{}", t)?,
248-
TypeCtor::Scalar(Scalar::Int(t)) => write!(f, "{}", t)?,
249-
TypeCtor::Scalar(Scalar::Uint(t)) => write!(f, "{}", t)?,
247+
TypeCtor::Scalar(Scalar::Float(t)) => {
248+
write!(f, "{}", primitive::float_ty_to_string(t))?
249+
}
250+
TypeCtor::Scalar(Scalar::Int(t)) => write!(f, "{}", primitive::int_ty_to_string(t))?,
251+
TypeCtor::Scalar(Scalar::Uint(t)) => write!(f, "{}", primitive::uint_ty_to_string(t))?,
250252
TypeCtor::Str => write!(f, "str")?,
251253
TypeCtor::Slice => {
252254
let t = self.parameters.as_single();

crates/hir_ty/src/infer/expr.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use test_utils::mark;
1515

1616
use crate::{
1717
autoderef, method_resolution, op,
18-
primitive::UintTy,
18+
primitive::{self, UintTy},
1919
traits::{FnTrait, InEnvironment},
2020
utils::{generics, variant_data, Generics},
2121
ApplicationTy, Binders, CallableDefId, InferTy, Mutability, Obligation, OpaqueTyId, Rawness,
@@ -730,17 +730,21 @@ impl<'a> InferenceContext<'a> {
730730
}
731731
Literal::Char(..) => Ty::simple(TypeCtor::Scalar(Scalar::Char)),
732732
Literal::Int(_v, ty) => match ty {
733-
Some(int_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Int((*int_ty).into()))),
733+
Some(int_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Int(
734+
primitive::int_ty_from_builtin(*int_ty),
735+
))),
734736
None => self.table.new_integer_var(),
735737
},
736738
Literal::Uint(_v, ty) => match ty {
737-
Some(int_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Uint((*int_ty).into()))),
739+
Some(int_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Uint(
740+
primitive::uint_ty_from_builtin(*int_ty),
741+
))),
738742
None => self.table.new_integer_var(),
739743
},
740744
Literal::Float(_v, ty) => match ty {
741-
Some(float_ty) => {
742-
Ty::simple(TypeCtor::Scalar(Scalar::Float((*float_ty).into())))
743-
}
745+
Some(float_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Float(
746+
primitive::float_ty_from_builtin(*float_ty),
747+
))),
744748
None => self.table.new_float_var(),
745749
},
746750
},

crates/hir_ty/src/lib.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use itertools::Itertools;
3838
use crate::{
3939
db::HirDatabase,
4040
display::HirDisplay,
41-
primitive::{FloatTy, IntTy, UintTy},
4241
utils::{generics, make_mut_slice, Generics},
4342
};
4443

@@ -50,25 +49,14 @@ pub use lower::{
5049
};
5150
pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
5251

53-
pub use chalk_ir::{BoundVar, DebruijnIndex};
52+
pub use chalk_ir::{BoundVar, DebruijnIndex, Scalar};
5453

5554
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
5655
pub enum Lifetime {
5756
Parameter(LifetimeParamId),
5857
Static,
5958
}
6059

61-
/// Types of scalar values.
62-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
63-
#[allow(missing_docs)]
64-
pub enum Scalar {
65-
Bool,
66-
Char,
67-
Int(IntTy),
68-
Uint(UintTy),
69-
Float(FloatTy),
70-
}
71-
7260
/// A type constructor or type name: this might be something like the primitive
7361
/// type `bool`, a struct like `Vec`, or things like function pointers or
7462
/// tuples.
@@ -736,9 +724,13 @@ impl Ty {
736724
BuiltinType::Char => TypeCtor::Scalar(Scalar::Char),
737725
BuiltinType::Bool => TypeCtor::Scalar(Scalar::Bool),
738726
BuiltinType::Str => TypeCtor::Str,
739-
BuiltinType::Int(t) => TypeCtor::Scalar(Scalar::Int(t.into())),
740-
BuiltinType::Uint(t) => TypeCtor::Scalar(Scalar::Uint(t.into())),
741-
BuiltinType::Float(t) => TypeCtor::Scalar(Scalar::Float(t.into())),
727+
BuiltinType::Int(t) => TypeCtor::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))),
728+
BuiltinType::Uint(t) => {
729+
TypeCtor::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t)))
730+
}
731+
BuiltinType::Float(t) => {
732+
TypeCtor::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t)))
733+
}
742734
})
743735
}
744736

crates/hir_ty/src/method_resolution.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
1616
use crate::{
1717
autoderef,
1818
db::HirDatabase,
19-
primitive::{FloatTy, IntTy, UintTy},
19+
primitive::{self, FloatTy, IntTy, UintTy},
2020
utils::all_super_traits,
2121
ApplicationTy, Canonical, DebruijnIndex, InEnvironment, Scalar, Substs, TraitEnvironment,
2222
TraitRef, Ty, TyKind, TypeCtor, TypeWalk,
@@ -225,8 +225,12 @@ impl Ty {
225225
FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"),
226226
FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"),
227227
},
228-
TypeCtor::Scalar(Scalar::Int(t)) => lang_item_crate!(t.ty_to_string()),
229-
TypeCtor::Scalar(Scalar::Uint(t)) => lang_item_crate!(t.ty_to_string()),
228+
TypeCtor::Scalar(Scalar::Int(t)) => {
229+
lang_item_crate!(primitive::int_ty_to_string(t))
230+
}
231+
TypeCtor::Scalar(Scalar::Uint(t)) => {
232+
lang_item_crate!(primitive::uint_ty_to_string(t))
233+
}
230234
TypeCtor::Str => lang_item_crate!("str_alloc", "str"),
231235
TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"),
232236
TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"),

crates/hir_ty/src/primitive.rs

Lines changed: 41 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -3,128 +3,63 @@
33
//! * during type inference, they can be uncertain (ie, `let x = 92;`)
44
//! * they don't belong to any particular crate.
55
6-
use std::fmt;
7-
6+
pub use chalk_ir::{FloatTy, IntTy, UintTy};
87
pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint};
98

10-
/// Different signed int types.
11-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12-
pub enum IntTy {
13-
Isize,
14-
I8,
15-
I16,
16-
I32,
17-
I64,
18-
I128,
19-
}
20-
21-
/// Different unsigned int types.
22-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
23-
pub enum UintTy {
24-
Usize,
25-
U8,
26-
U16,
27-
U32,
28-
U64,
29-
U128,
30-
}
31-
32-
impl fmt::Display for IntTy {
33-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34-
write!(f, "{}", self.ty_to_string())
35-
}
36-
}
37-
38-
impl IntTy {
39-
pub fn ty_to_string(self) -> &'static str {
40-
match self {
41-
IntTy::Isize => "isize",
42-
IntTy::I8 => "i8",
43-
IntTy::I16 => "i16",
44-
IntTy::I32 => "i32",
45-
IntTy::I64 => "i64",
46-
IntTy::I128 => "i128",
47-
}
48-
}
49-
}
50-
51-
impl fmt::Display for UintTy {
52-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53-
write!(f, "{}", self.ty_to_string())
54-
}
55-
}
56-
57-
impl UintTy {
58-
pub fn ty_to_string(self) -> &'static str {
59-
match self {
60-
UintTy::Usize => "usize",
61-
UintTy::U8 => "u8",
62-
UintTy::U16 => "u16",
63-
UintTy::U32 => "u32",
64-
UintTy::U64 => "u64",
65-
UintTy::U128 => "u128",
66-
}
67-
}
68-
}
69-
70-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
71-
pub enum FloatTy {
72-
F32,
73-
F64,
74-
}
75-
76-
impl fmt::Debug for FloatTy {
77-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78-
fmt::Display::fmt(self, f)
9+
pub fn int_ty_to_string(ty: IntTy) -> &'static str {
10+
match ty {
11+
IntTy::Isize => "isize",
12+
IntTy::I8 => "i8",
13+
IntTy::I16 => "i16",
14+
IntTy::I32 => "i32",
15+
IntTy::I64 => "i64",
16+
IntTy::I128 => "i128",
7917
}
8018
}
8119

82-
impl fmt::Display for FloatTy {
83-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84-
write!(f, "{}", self.ty_to_string())
20+
pub fn uint_ty_to_string(ty: UintTy) -> &'static str {
21+
match ty {
22+
UintTy::Usize => "usize",
23+
UintTy::U8 => "u8",
24+
UintTy::U16 => "u16",
25+
UintTy::U32 => "u32",
26+
UintTy::U64 => "u64",
27+
UintTy::U128 => "u128",
8528
}
8629
}
8730

88-
impl FloatTy {
89-
pub fn ty_to_string(self) -> &'static str {
90-
match self {
91-
FloatTy::F32 => "f32",
92-
FloatTy::F64 => "f64",
93-
}
31+
pub fn float_ty_to_string(ty: FloatTy) -> &'static str {
32+
match ty {
33+
FloatTy::F32 => "f32",
34+
FloatTy::F64 => "f64",
9435
}
9536
}
9637

97-
impl From<BuiltinInt> for IntTy {
98-
fn from(t: BuiltinInt) -> Self {
99-
match t {
100-
BuiltinInt::Isize => Self::Isize,
101-
BuiltinInt::I8 => Self::I8,
102-
BuiltinInt::I16 => Self::I16,
103-
BuiltinInt::I32 => Self::I32,
104-
BuiltinInt::I64 => Self::I64,
105-
BuiltinInt::I128 => Self::I128,
106-
}
38+
pub(super) fn int_ty_from_builtin(t: BuiltinInt) -> IntTy {
39+
match t {
40+
BuiltinInt::Isize => IntTy::Isize,
41+
BuiltinInt::I8 => IntTy::I8,
42+
BuiltinInt::I16 => IntTy::I16,
43+
BuiltinInt::I32 => IntTy::I32,
44+
BuiltinInt::I64 => IntTy::I64,
45+
BuiltinInt::I128 => IntTy::I128,
10746
}
10847
}
10948

110-
impl From<BuiltinUint> for UintTy {
111-
fn from(t: BuiltinUint) -> Self {
112-
match t {
113-
BuiltinUint::Usize => Self::Usize,
114-
BuiltinUint::U8 => Self::U8,
115-
BuiltinUint::U16 => Self::U16,
116-
BuiltinUint::U32 => Self::U32,
117-
BuiltinUint::U64 => Self::U64,
118-
BuiltinUint::U128 => Self::U128,
119-
}
49+
pub(super) fn uint_ty_from_builtin(t: BuiltinUint) -> UintTy {
50+
match t {
51+
BuiltinUint::Usize => UintTy::Usize,
52+
BuiltinUint::U8 => UintTy::U8,
53+
BuiltinUint::U16 => UintTy::U16,
54+
BuiltinUint::U32 => UintTy::U32,
55+
BuiltinUint::U64 => UintTy::U64,
56+
BuiltinUint::U128 => UintTy::U128,
12057
}
12158
}
12259

123-
impl From<BuiltinFloat> for FloatTy {
124-
fn from(t: BuiltinFloat) -> Self {
125-
match t {
126-
BuiltinFloat::F32 => Self::F32,
127-
BuiltinFloat::F64 => Self::F64,
128-
}
60+
pub(super) fn float_ty_from_builtin(t: BuiltinFloat) -> FloatTy {
61+
match t {
62+
BuiltinFloat::F32 => FloatTy::F32,
63+
BuiltinFloat::F64 => FloatTy::F64,
12964
}
13065
}

0 commit comments

Comments
 (0)