aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-02-28 00:20:04 +0000
committerLukas Wirth <[email protected]>2021-02-28 00:20:04 +0000
commit5183c9f08345c664237ae138e86f96ff46714f15 (patch)
treea0e660cb49fd67951ee2209d9fc75a2108243df7 /crates/hir_ty/src
parent2a4076c14d0e3f7ae03908c2b9cd1a52851d401c (diff)
Introduce TypeCtor::Scalar
Diffstat (limited to 'crates/hir_ty/src')
-rw-r--r--crates/hir_ty/src/display.rs12
-rw-r--r--crates/hir_ty/src/infer.rs6
-rw-r--r--crates/hir_ty/src/infer/expr.rs63
-rw-r--r--crates/hir_ty/src/infer/unify.rs26
-rw-r--r--crates/hir_ty/src/lib.rs51
-rw-r--r--crates/hir_ty/src/method_resolution.rs90
-rw-r--r--crates/hir_ty/src/op.rs24
-rw-r--r--crates/hir_ty/src/primitive.rs157
-rw-r--r--crates/hir_ty/src/traits/chalk/mapping.rs140
9 files changed, 271 insertions, 298 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index 271fcbfaf..cfe9cedb0 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -4,7 +4,8 @@ use std::{borrow::Cow, fmt};
4 4
5use crate::{ 5use crate::{
6 db::HirDatabase, utils::generics, ApplicationTy, CallableDefId, FnSig, GenericPredicate, 6 db::HirDatabase, utils::generics, ApplicationTy, CallableDefId, FnSig, GenericPredicate,
7 Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, 7 Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, TraitRef, Ty,
8 TypeCtor,
8}; 9};
9use arrayvec::ArrayVec; 10use arrayvec::ArrayVec;
10use hir_def::{ 11use hir_def::{
@@ -241,10 +242,11 @@ impl HirDisplay for ApplicationTy {
241 } 242 }
242 243
243 match self.ctor { 244 match self.ctor {
244 TypeCtor::Bool => write!(f, "bool")?, 245 TypeCtor::Scalar(Scalar::Bool) => write!(f, "bool")?,
245 TypeCtor::Char => write!(f, "char")?, 246 TypeCtor::Scalar(Scalar::Char) => write!(f, "char")?,
246 TypeCtor::Int(t) => write!(f, "{}", t)?, 247 TypeCtor::Scalar(Scalar::Float(t)) => write!(f, "{}", t)?,
247 TypeCtor::Float(t) => write!(f, "{}", t)?, 248 TypeCtor::Scalar(Scalar::Int(t)) => write!(f, "{}", t)?,
249 TypeCtor::Scalar(Scalar::Uint(t)) => write!(f, "{}", t)?,
248 TypeCtor::Str => write!(f, "str")?, 250 TypeCtor::Str => write!(f, "str")?,
249 TypeCtor::Slice => { 251 TypeCtor::Slice => {
250 let t = self.parameters.as_single(); 252 let t = self.parameters.as_single();
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index 4b683c5a7..657f011d2 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -41,7 +41,7 @@ use super::{
41 InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, 41 InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk,
42}; 42};
43use crate::{ 43use crate::{
44 db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, 44 db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, Scalar,
45}; 45};
46 46
47pub(crate) use unify::unify; 47pub(crate) use unify::unify;
@@ -684,8 +684,8 @@ impl InferTy {
684 fn fallback_value(self) -> Ty { 684 fn fallback_value(self) -> Ty {
685 match self { 685 match self {
686 InferTy::TypeVar(..) => Ty::Unknown, 686 InferTy::TypeVar(..) => Ty::Unknown,
687 InferTy::IntVar(..) => Ty::simple(TypeCtor::Int(IntTy::i32())), 687 InferTy::IntVar(..) => Ty::simple(TypeCtor::Scalar(Scalar::Int(IntTy::I32))),
688 InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(FloatTy::f64())), 688 InferTy::FloatVar(..) => Ty::simple(TypeCtor::Scalar(Scalar::Float(FloatTy::F64))),
689 InferTy::MaybeNeverTypeVar(..) => Ty::simple(TypeCtor::Never), 689 InferTy::MaybeNeverTypeVar(..) => Ty::simple(TypeCtor::Never),
690 } 690 }
691 } 691 }
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index cb59a6937..c25f3f34b 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -4,7 +4,6 @@ use std::iter::{repeat, repeat_with};
4use std::{mem, sync::Arc}; 4use std::{mem, sync::Arc};
5 5
6use hir_def::{ 6use hir_def::{
7 builtin_type::Signedness,
8 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, 7 expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
9 path::{GenericArg, GenericArgs}, 8 path::{GenericArg, GenericArgs},
10 resolver::resolver_for_expr, 9 resolver::resolver_for_expr,
@@ -16,10 +15,11 @@ use test_utils::mark;
16 15
17use crate::{ 16use crate::{
18 autoderef, method_resolution, op, 17 autoderef, method_resolution, op,
18 primitive::UintTy,
19 traits::{FnTrait, InEnvironment}, 19 traits::{FnTrait, InEnvironment},
20 utils::{generics, variant_data, Generics}, 20 utils::{generics, variant_data, Generics},
21 ApplicationTy, Binders, CallableDefId, InferTy, IntTy, Mutability, Obligation, OpaqueTyId, 21 ApplicationTy, Binders, CallableDefId, InferTy, Mutability, Obligation, OpaqueTyId, Rawness,
22 Rawness, Substs, TraitRef, Ty, TypeCtor, 22 Scalar, Substs, TraitRef, Ty, TypeCtor,
23}; 23};
24 24
25use super::{ 25use super::{
@@ -120,7 +120,10 @@ impl<'a> InferenceContext<'a> {
120 Expr::Missing => Ty::Unknown, 120 Expr::Missing => Ty::Unknown,
121 Expr::If { condition, then_branch, else_branch } => { 121 Expr::If { condition, then_branch, else_branch } => {
122 // if let is desugared to match, so this is always simple if 122 // if let is desugared to match, so this is always simple if
123 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); 123 self.infer_expr(
124 *condition,
125 &Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Bool))),
126 );
124 127
125 let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe); 128 let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
126 let mut both_arms_diverge = Diverges::Always; 129 let mut both_arms_diverge = Diverges::Always;
@@ -203,7 +206,10 @@ impl<'a> InferenceContext<'a> {
203 label: label.map(|label| self.body[label].name.clone()), 206 label: label.map(|label| self.body[label].name.clone()),
204 }); 207 });
205 // while let is desugared to a match loop, so this is always simple while 208 // while let is desugared to a match loop, so this is always simple while
206 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); 209 self.infer_expr(
210 *condition,
211 &Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Bool))),
212 );
207 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 213 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
208 let _ctxt = self.breakables.pop().expect("breakable stack broken"); 214 let _ctxt = self.breakables.pop().expect("breakable stack broken");
209 // the body may not run, so it diverging doesn't mean we diverge 215 // the body may not run, so it diverging doesn't mean we diverge
@@ -321,7 +327,7 @@ impl<'a> InferenceContext<'a> {
321 if let Some(guard_expr) = arm.guard { 327 if let Some(guard_expr) = arm.guard {
322 self.infer_expr( 328 self.infer_expr(
323 guard_expr, 329 guard_expr,
324 &Expectation::has_type(Ty::simple(TypeCtor::Bool)), 330 &Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Bool))),
325 ); 331 );
326 } 332 }
327 333
@@ -534,10 +540,13 @@ impl<'a> InferenceContext<'a> {
534 match &inner_ty { 540 match &inner_ty {
535 // Fast path for builtins 541 // Fast path for builtins
536 Ty::Apply(ApplicationTy { 542 Ty::Apply(ApplicationTy {
537 ctor: TypeCtor::Int(IntTy { signedness: Signedness::Signed, .. }), 543 ctor: TypeCtor::Scalar(Scalar::Int(_)),
544 ..
545 })
546 | Ty::Apply(ApplicationTy {
547 ctor: TypeCtor::Scalar(Scalar::Float(_)),
538 .. 548 ..
539 }) 549 })
540 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Float(_), .. })
541 | Ty::Infer(InferTy::IntVar(..)) 550 | Ty::Infer(InferTy::IntVar(..))
542 | Ty::Infer(InferTy::FloatVar(..)) => inner_ty, 551 | Ty::Infer(InferTy::FloatVar(..)) => inner_ty,
543 // Otherwise we resolve via the std::ops::Neg trait 552 // Otherwise we resolve via the std::ops::Neg trait
@@ -548,8 +557,18 @@ impl<'a> InferenceContext<'a> {
548 UnaryOp::Not => { 557 UnaryOp::Not => {
549 match &inner_ty { 558 match &inner_ty {
550 // Fast path for builtins 559 // Fast path for builtins
551 Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. }) 560 Ty::Apply(ApplicationTy {
552 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Int(_), .. }) 561 ctor: TypeCtor::Scalar(Scalar::Bool),
562 ..
563 })
564 | Ty::Apply(ApplicationTy {
565 ctor: TypeCtor::Scalar(Scalar::Int(_)),
566 ..
567 })
568 | Ty::Apply(ApplicationTy {
569 ctor: TypeCtor::Scalar(Scalar::Uint(_)),
570 ..
571 })
553 | Ty::Infer(InferTy::IntVar(..)) => inner_ty, 572 | Ty::Infer(InferTy::IntVar(..)) => inner_ty,
554 // Otherwise we resolve via the std::ops::Not trait 573 // Otherwise we resolve via the std::ops::Not trait
555 _ => self 574 _ => self
@@ -561,7 +580,9 @@ impl<'a> InferenceContext<'a> {
561 Expr::BinaryOp { lhs, rhs, op } => match op { 580 Expr::BinaryOp { lhs, rhs, op } => match op {
562 Some(op) => { 581 Some(op) => {
563 let lhs_expectation = match op { 582 let lhs_expectation = match op {
564 BinaryOp::LogicOp(..) => Expectation::has_type(Ty::simple(TypeCtor::Bool)), 583 BinaryOp::LogicOp(..) => {
584 Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Bool)))
585 }
565 _ => Expectation::none(), 586 _ => Expectation::none(),
566 }; 587 };
567 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); 588 let lhs_ty = self.infer_expr(*lhs, &lhs_expectation);
@@ -688,7 +709,9 @@ impl<'a> InferenceContext<'a> {
688 ); 709 );
689 self.infer_expr( 710 self.infer_expr(
690 *repeat, 711 *repeat,
691 &Expectation::has_type(Ty::simple(TypeCtor::Int(IntTy::usize()))), 712 &Expectation::has_type(Ty::simple(TypeCtor::Scalar(Scalar::Uint(
713 UintTy::Usize,
714 )))),
692 ); 715 );
693 } 716 }
694 } 717 }
@@ -696,22 +719,28 @@ impl<'a> InferenceContext<'a> {
696 Ty::apply_one(TypeCtor::Array, elem_ty) 719 Ty::apply_one(TypeCtor::Array, elem_ty)
697 } 720 }
698 Expr::Literal(lit) => match lit { 721 Expr::Literal(lit) => match lit {
699 Literal::Bool(..) => Ty::simple(TypeCtor::Bool), 722 Literal::Bool(..) => Ty::simple(TypeCtor::Scalar(Scalar::Bool)),
700 Literal::String(..) => { 723 Literal::String(..) => {
701 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str)) 724 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
702 } 725 }
703 Literal::ByteString(..) => { 726 Literal::ByteString(..) => {
704 let byte_type = Ty::simple(TypeCtor::Int(IntTy::u8())); 727 let byte_type = Ty::simple(TypeCtor::Scalar(Scalar::Uint(UintTy::U8)));
705 let array_type = Ty::apply_one(TypeCtor::Array, byte_type); 728 let array_type = Ty::apply_one(TypeCtor::Array, byte_type);
706 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), array_type) 729 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), array_type)
707 } 730 }
708 Literal::Char(..) => Ty::simple(TypeCtor::Char), 731 Literal::Char(..) => Ty::simple(TypeCtor::Scalar(Scalar::Char)),
709 Literal::Int(_v, ty) => match ty { 732 Literal::Int(_v, ty) => match ty {
710 Some(int_ty) => Ty::simple(TypeCtor::Int((*int_ty).into())), 733 Some(int_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Int((*int_ty).into()))),
734 None => self.table.new_integer_var(),
735 },
736 Literal::Uint(_v, ty) => match ty {
737 Some(int_ty) => Ty::simple(TypeCtor::Scalar(Scalar::Uint((*int_ty).into()))),
711 None => self.table.new_integer_var(), 738 None => self.table.new_integer_var(),
712 }, 739 },
713 Literal::Float(_v, ty) => match ty { 740 Literal::Float(_v, ty) => match ty {
714 Some(float_ty) => Ty::simple(TypeCtor::Float((*float_ty).into())), 741 Some(float_ty) => {
742 Ty::simple(TypeCtor::Scalar(Scalar::Float((*float_ty).into())))
743 }
715 None => self.table.new_float_var(), 744 None => self.table.new_float_var(),
716 }, 745 },
717 }, 746 },
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs
index 76984242e..57eb8cede 100644
--- a/crates/hir_ty/src/infer/unify.rs
+++ b/crates/hir_ty/src/infer/unify.rs
@@ -8,8 +8,8 @@ use test_utils::mark;
8 8
9use super::{InferenceContext, Obligation}; 9use super::{InferenceContext, Obligation};
10use crate::{ 10use crate::{
11 BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferTy, Substs, Ty, 11 BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferTy, Scalar, Substs,
12 TyKind, TypeCtor, TypeWalk, 12 Ty, TyKind, TypeCtor, TypeWalk,
13}; 13};
14 14
15impl<'a> InferenceContext<'a> { 15impl<'a> InferenceContext<'a> {
@@ -300,10 +300,24 @@ impl InferenceTable {
300 | (other, Ty::Infer(InferTy::TypeVar(tv))) 300 | (other, Ty::Infer(InferTy::TypeVar(tv)))
301 | (Ty::Infer(InferTy::MaybeNeverTypeVar(tv)), other) 301 | (Ty::Infer(InferTy::MaybeNeverTypeVar(tv)), other)
302 | (other, Ty::Infer(InferTy::MaybeNeverTypeVar(tv))) 302 | (other, Ty::Infer(InferTy::MaybeNeverTypeVar(tv)))
303 | (Ty::Infer(InferTy::IntVar(tv)), other @ ty_app!(TypeCtor::Int(_))) 303 | (Ty::Infer(InferTy::IntVar(tv)), other @ ty_app!(TypeCtor::Scalar(Scalar::Int(_))))
304 | (other @ ty_app!(TypeCtor::Int(_)), Ty::Infer(InferTy::IntVar(tv))) 304 | (other @ ty_app!(TypeCtor::Scalar(Scalar::Int(_))), Ty::Infer(InferTy::IntVar(tv)))
305 | (Ty::Infer(InferTy::FloatVar(tv)), other @ ty_app!(TypeCtor::Float(_))) 305 | (
306 | (other @ ty_app!(TypeCtor::Float(_)), Ty::Infer(InferTy::FloatVar(tv))) => { 306 Ty::Infer(InferTy::IntVar(tv)),
307 other @ ty_app!(TypeCtor::Scalar(Scalar::Uint(_))),
308 )
309 | (
310 other @ ty_app!(TypeCtor::Scalar(Scalar::Uint(_))),
311 Ty::Infer(InferTy::IntVar(tv)),
312 )
313 | (
314 Ty::Infer(InferTy::FloatVar(tv)),
315 other @ ty_app!(TypeCtor::Scalar(Scalar::Float(_))),
316 )
317 | (
318 other @ ty_app!(TypeCtor::Scalar(Scalar::Float(_))),
319 Ty::Infer(InferTy::FloatVar(tv)),
320 ) => {
307 // the type var is unknown since we tried to resolve it 321 // the type var is unknown since we tried to resolve it
308 self.var_unification_table.union_value(*tv, TypeVarValue::Known(other.clone())); 322 self.var_unification_table.union_value(*tv, TypeVarValue::Known(other.clone()));
309 true 323 true
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index 50d248674..2a45479a8 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -38,7 +38,7 @@ use itertools::Itertools;
38use crate::{ 38use crate::{
39 db::HirDatabase, 39 db::HirDatabase,
40 display::HirDisplay, 40 display::HirDisplay,
41 primitive::{FloatTy, IntTy}, 41 primitive::{FloatTy, IntTy, UintTy},
42 utils::{generics, make_mut_slice, Generics}, 42 utils::{generics, make_mut_slice, Generics},
43}; 43};
44 44
@@ -58,23 +58,24 @@ pub enum Lifetime {
58 Static, 58 Static,
59} 59}
60 60
61/// Types of scalar values.
62#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
63#[allow(missing_docs)]
64pub enum Scalar {
65 Bool,
66 Char,
67 Int(IntTy),
68 Uint(UintTy),
69 Float(FloatTy),
70}
71
61/// A type constructor or type name: this might be something like the primitive 72/// A type constructor or type name: this might be something like the primitive
62/// type `bool`, a struct like `Vec`, or things like function pointers or 73/// type `bool`, a struct like `Vec`, or things like function pointers or
63/// tuples. 74/// tuples.
64#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] 75#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
65pub enum TypeCtor { 76pub enum TypeCtor {
66 /// The primitive boolean type. Written as `bool`. 77 /// a scalar type like `bool` or `u32`
67 Bool, 78 Scalar(Scalar),
68
69 /// The primitive character type; holds a Unicode scalar value
70 /// (a non-surrogate code point). Written as `char`.
71 Char,
72
73 /// A primitive integer type. For example, `i32`.
74 Int(IntTy),
75
76 /// A primitive floating-point type. For example, `f64`.
77 Float(FloatTy),
78 79
79 /// Structures, enumerations and unions. 80 /// Structures, enumerations and unions.
80 Adt(AdtId), 81 Adt(AdtId),
@@ -152,10 +153,7 @@ pub enum TypeCtor {
152impl TypeCtor { 153impl TypeCtor {
153 pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize { 154 pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize {
154 match self { 155 match self {
155 TypeCtor::Bool 156 TypeCtor::Scalar(_)
156 | TypeCtor::Char
157 | TypeCtor::Int(_)
158 | TypeCtor::Float(_)
159 | TypeCtor::Str 157 | TypeCtor::Str
160 | TypeCtor::Never => 0, 158 | TypeCtor::Never => 0,
161 TypeCtor::Slice 159 TypeCtor::Slice
@@ -197,10 +195,7 @@ impl TypeCtor {
197 195
198 pub fn krate(self, db: &dyn HirDatabase) -> Option<CrateId> { 196 pub fn krate(self, db: &dyn HirDatabase) -> Option<CrateId> {
199 match self { 197 match self {
200 TypeCtor::Bool 198 TypeCtor::Scalar(_)
201 | TypeCtor::Char
202 | TypeCtor::Int(_)
203 | TypeCtor::Float(_)
204 | TypeCtor::Str 199 | TypeCtor::Str
205 | TypeCtor::Never 200 | TypeCtor::Never
206 | TypeCtor::Slice 201 | TypeCtor::Slice
@@ -232,10 +227,7 @@ impl TypeCtor {
232 227
233 pub fn as_generic_def(self) -> Option<GenericDefId> { 228 pub fn as_generic_def(self) -> Option<GenericDefId> {
234 match self { 229 match self {
235 TypeCtor::Bool 230 TypeCtor::Scalar(_)
236 | TypeCtor::Char
237 | TypeCtor::Int(_)
238 | TypeCtor::Float(_)
239 | TypeCtor::Str 231 | TypeCtor::Str
240 | TypeCtor::Never 232 | TypeCtor::Never
241 | TypeCtor::Slice 233 | TypeCtor::Slice
@@ -741,11 +733,12 @@ impl Ty {
741 } 733 }
742 pub fn builtin(builtin: BuiltinType) -> Self { 734 pub fn builtin(builtin: BuiltinType) -> Self {
743 Ty::simple(match builtin { 735 Ty::simple(match builtin {
744 BuiltinType::Char => TypeCtor::Char, 736 BuiltinType::Char => TypeCtor::Scalar(Scalar::Char),
745 BuiltinType::Bool => TypeCtor::Bool, 737 BuiltinType::Bool => TypeCtor::Scalar(Scalar::Bool),
746 BuiltinType::Str => TypeCtor::Str, 738 BuiltinType::Str => TypeCtor::Str,
747 BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()), 739 BuiltinType::Int(t) => TypeCtor::Scalar(Scalar::Int(t.into())),
748 BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()), 740 BuiltinType::Uint(t) => TypeCtor::Scalar(Scalar::Uint(t.into())),
741 BuiltinType::Float(t) => TypeCtor::Scalar(Scalar::Float(t.into())),
749 }) 742 })
750 } 743 }
751 744
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs
index b3d1fe9a4..3c817701d 100644
--- a/crates/hir_ty/src/method_resolution.rs
+++ b/crates/hir_ty/src/method_resolution.rs
@@ -7,11 +7,8 @@ use std::{iter, sync::Arc};
7use arrayvec::ArrayVec; 7use arrayvec::ArrayVec;
8use base_db::CrateId; 8use base_db::CrateId;
9use hir_def::{ 9use hir_def::{
10 builtin_type::{IntBitness, Signedness}, 10 lang_item::LangItemTarget, type_ref::Mutability, AssocContainerId, AssocItemId, FunctionId,
11 lang_item::LangItemTarget, 11 GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId,
12 type_ref::Mutability,
13 AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule, ImplId, Lookup, ModuleId,
14 TraitId,
15}; 12};
16use hir_expand::name::Name; 13use hir_expand::name::Name;
17use rustc_hash::{FxHashMap, FxHashSet}; 14use rustc_hash::{FxHashMap, FxHashSet};
@@ -19,10 +16,10 @@ use rustc_hash::{FxHashMap, FxHashSet};
19use crate::{ 16use crate::{
20 autoderef, 17 autoderef,
21 db::HirDatabase, 18 db::HirDatabase,
22 primitive::{FloatBitness, FloatTy, IntTy}, 19 primitive::{FloatTy, IntTy, UintTy},
23 utils::all_super_traits, 20 utils::all_super_traits,
24 ApplicationTy, Canonical, DebruijnIndex, InEnvironment, Substs, TraitEnvironment, TraitRef, Ty, 21 ApplicationTy, Canonical, DebruijnIndex, InEnvironment, Scalar, Substs, TraitEnvironment,
25 TyKind, TypeCtor, TypeWalk, 22 TraitRef, Ty, TyKind, TypeCtor, TypeWalk,
26}; 23};
27 24
28/// This is used as a key for indexing impls. 25/// This is used as a key for indexing impls.
@@ -46,59 +43,23 @@ impl TyFingerprint {
46} 43}
47 44
48pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [ 45pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [
49 TyFingerprint::Apply(TypeCtor::Int(IntTy { 46 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I8))),
50 signedness: Signedness::Unsigned, 47 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I16))),
51 bitness: IntBitness::X8, 48 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I32))),
52 })), 49 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I64))),
53 TyFingerprint::Apply(TypeCtor::Int(IntTy { 50 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::I128))),
54 signedness: Signedness::Unsigned, 51 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Int(IntTy::Isize))),
55 bitness: IntBitness::X16, 52 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U8))),
56 })), 53 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U16))),
57 TyFingerprint::Apply(TypeCtor::Int(IntTy { 54 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U32))),
58 signedness: Signedness::Unsigned, 55 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U64))),
59 bitness: IntBitness::X32, 56 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::U128))),
60 })), 57 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Uint(UintTy::Usize))),
61 TyFingerprint::Apply(TypeCtor::Int(IntTy {
62 signedness: Signedness::Unsigned,
63 bitness: IntBitness::X64,
64 })),
65 TyFingerprint::Apply(TypeCtor::Int(IntTy {
66 signedness: Signedness::Unsigned,
67 bitness: IntBitness::X128,
68 })),
69 TyFingerprint::Apply(TypeCtor::Int(IntTy {
70 signedness: Signedness::Unsigned,
71 bitness: IntBitness::Xsize,
72 })),
73 TyFingerprint::Apply(TypeCtor::Int(IntTy {
74 signedness: Signedness::Signed,
75 bitness: IntBitness::X8,
76 })),
77 TyFingerprint::Apply(TypeCtor::Int(IntTy {
78 signedness: Signedness::Signed,
79 bitness: IntBitness::X16,
80 })),
81 TyFingerprint::Apply(TypeCtor::Int(IntTy {
82 signedness: Signedness::Signed,
83 bitness: IntBitness::X32,
84 })),
85 TyFingerprint::Apply(TypeCtor::Int(IntTy {
86 signedness: Signedness::Signed,
87 bitness: IntBitness::X64,
88 })),
89 TyFingerprint::Apply(TypeCtor::Int(IntTy {
90 signedness: Signedness::Signed,
91 bitness: IntBitness::X128,
92 })),
93 TyFingerprint::Apply(TypeCtor::Int(IntTy {
94 signedness: Signedness::Signed,
95 bitness: IntBitness::Xsize,
96 })),
97]; 58];
98 59
99pub(crate) const ALL_FLOAT_FPS: [TyFingerprint; 2] = [ 60pub(crate) const ALL_FLOAT_FPS: [TyFingerprint; 2] = [
100 TyFingerprint::Apply(TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 })), 61 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Float(FloatTy::F32))),
101 TyFingerprint::Apply(TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 })), 62 TyFingerprint::Apply(TypeCtor::Scalar(Scalar::Float(FloatTy::F64))),
102]; 63];
103 64
104/// Trait impls defined or available in some crate. 65/// Trait impls defined or available in some crate.
@@ -257,14 +218,15 @@ impl Ty {
257 TypeCtor::ForeignType(type_alias_id) => { 218 TypeCtor::ForeignType(type_alias_id) => {
258 return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast())); 219 return mod_to_crate_ids(type_alias_id.lookup(db.upcast()).module(db.upcast()));
259 } 220 }
260 TypeCtor::Bool => lang_item_crate!("bool"), 221 TypeCtor::Scalar(Scalar::Bool) => lang_item_crate!("bool"),
261 TypeCtor::Char => lang_item_crate!("char"), 222 TypeCtor::Scalar(Scalar::Char) => lang_item_crate!("char"),
262 TypeCtor::Float(f) => match f.bitness { 223 TypeCtor::Scalar(Scalar::Float(f)) => match f {
263 // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime) 224 // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
264 FloatBitness::X32 => lang_item_crate!("f32", "f32_runtime"), 225 FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"),
265 FloatBitness::X64 => lang_item_crate!("f64", "f64_runtime"), 226 FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"),
266 }, 227 },
267 TypeCtor::Int(i) => lang_item_crate!(i.ty_to_string()), 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()),
268 TypeCtor::Str => lang_item_crate!("str_alloc", "str"), 230 TypeCtor::Str => lang_item_crate!("str_alloc", "str"),
269 TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"), 231 TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"),
270 TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"), 232 TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"),
diff --git a/crates/hir_ty/src/op.rs b/crates/hir_ty/src/op.rs
index 0870874fc..a4999c51d 100644
--- a/crates/hir_ty/src/op.rs
+++ b/crates/hir_ty/src/op.rs
@@ -2,15 +2,17 @@
2use hir_def::expr::{ArithOp, BinaryOp, CmpOp}; 2use hir_def::expr::{ArithOp, BinaryOp, CmpOp};
3 3
4use super::{InferTy, Ty, TypeCtor}; 4use super::{InferTy, Ty, TypeCtor};
5use crate::ApplicationTy; 5use crate::{ApplicationTy, Scalar};
6 6
7pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty { 7pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
8 match op { 8 match op {
9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Bool), 9 BinaryOp::LogicOp(_) | BinaryOp::CmpOp(_) => Ty::simple(TypeCtor::Scalar(Scalar::Bool)),
10 BinaryOp::Assignment { .. } => Ty::unit(), 10 BinaryOp::Assignment { .. } => Ty::unit(),
11 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty { 11 BinaryOp::ArithOp(ArithOp::Shl) | BinaryOp::ArithOp(ArithOp::Shr) => match lhs_ty {
12 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 12 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
13 TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, 13 TypeCtor::Scalar(Scalar::Int(_))
14 | TypeCtor::Scalar(Scalar::Uint(_))
15 | TypeCtor::Scalar(Scalar::Float(_)) => lhs_ty,
14 _ => Ty::Unknown, 16 _ => Ty::Unknown,
15 }, 17 },
16 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, 18 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
@@ -18,7 +20,9 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
18 }, 20 },
19 BinaryOp::ArithOp(_) => match rhs_ty { 21 BinaryOp::ArithOp(_) => match rhs_ty {
20 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 22 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
21 TypeCtor::Int(..) | TypeCtor::Float(..) => rhs_ty, 23 TypeCtor::Scalar(Scalar::Int(_))
24 | TypeCtor::Scalar(Scalar::Uint(_))
25 | TypeCtor::Scalar(Scalar::Float(_)) => rhs_ty,
22 _ => Ty::Unknown, 26 _ => Ty::Unknown,
23 }, 27 },
24 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty, 28 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => rhs_ty,
@@ -29,15 +33,11 @@ pub(super) fn binary_op_return_ty(op: BinaryOp, lhs_ty: Ty, rhs_ty: Ty) -> Ty {
29 33
30pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { 34pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
31 match op { 35 match op {
32 BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Bool), 36 BinaryOp::LogicOp(..) => Ty::simple(TypeCtor::Scalar(Scalar::Bool)),
33 BinaryOp::Assignment { op: None } => lhs_ty, 37 BinaryOp::Assignment { op: None } => lhs_ty,
34 BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty { 38 BinaryOp::CmpOp(CmpOp::Eq { .. }) => match lhs_ty {
35 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 39 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
36 TypeCtor::Int(..) 40 TypeCtor::Scalar(_) | TypeCtor::Str => lhs_ty,
37 | TypeCtor::Float(..)
38 | TypeCtor::Str
39 | TypeCtor::Char
40 | TypeCtor::Bool => lhs_ty,
41 _ => Ty::Unknown, 41 _ => Ty::Unknown,
42 }, 42 },
43 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, 43 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
@@ -48,7 +48,9 @@ pub(super) fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty {
48 | BinaryOp::Assignment { op: Some(_) } 48 | BinaryOp::Assignment { op: Some(_) }
49 | BinaryOp::ArithOp(_) => match lhs_ty { 49 | BinaryOp::ArithOp(_) => match lhs_ty {
50 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor { 50 Ty::Apply(ApplicationTy { ctor, .. }) => match ctor {
51 TypeCtor::Int(..) | TypeCtor::Float(..) => lhs_ty, 51 TypeCtor::Scalar(Scalar::Int(_))
52 | TypeCtor::Scalar(Scalar::Uint(_))
53 | TypeCtor::Scalar(Scalar::Float(_)) => lhs_ty,
52 _ => Ty::Unknown, 54 _ => Ty::Unknown,
53 }, 55 },
54 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty, 56 Ty::Infer(InferTy::IntVar(..)) | Ty::Infer(InferTy::FloatVar(..)) => lhs_ty,
diff --git a/crates/hir_ty/src/primitive.rs b/crates/hir_ty/src/primitive.rs
index 37966b709..e727c9581 100644
--- a/crates/hir_ty/src/primitive.rs
+++ b/crates/hir_ty/src/primitive.rs
@@ -5,18 +5,28 @@
5 5
6use std::fmt; 6use std::fmt;
7 7
8pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, FloatBitness, IntBitness, Signedness}; 8pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint};
9 9
10#[derive(Copy, Clone, Eq, PartialEq, Hash)] 10/// Different signed int types.
11pub struct IntTy { 11#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12 pub signedness: Signedness, 12pub enum IntTy {
13 pub bitness: IntBitness, 13 Isize,
14 I8,
15 I16,
16 I32,
17 I64,
18 I128,
14} 19}
15 20
16impl fmt::Debug for IntTy { 21/// Different unsigned int types.
17 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 22#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
18 fmt::Display::fmt(self, f) 23pub enum UintTy {
19 } 24 Usize,
25 U8,
26 U16,
27 U32,
28 U64,
29 U128,
20} 30}
21 31
22impl fmt::Display for IntTy { 32impl fmt::Display for IntTy {
@@ -26,75 +36,41 @@ impl fmt::Display for IntTy {
26} 36}
27 37
28impl IntTy { 38impl IntTy {
29 pub fn isize() -> IntTy { 39 pub fn ty_to_string(self) -> &'static str {
30 IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize } 40 match self {
31 } 41 IntTy::Isize => "isize",
32 42 IntTy::I8 => "i8",
33 pub fn i8() -> IntTy { 43 IntTy::I16 => "i16",
34 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 } 44 IntTy::I32 => "i32",
35 } 45 IntTy::I64 => "i64",
36 46 IntTy::I128 => "i128",
37 pub fn i16() -> IntTy { 47 }
38 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 }
39 }
40
41 pub fn i32() -> IntTy {
42 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 }
43 }
44
45 pub fn i64() -> IntTy {
46 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 }
47 }
48
49 pub fn i128() -> IntTy {
50 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 }
51 }
52
53 pub fn usize() -> IntTy {
54 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize }
55 }
56
57 pub fn u8() -> IntTy {
58 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 }
59 }
60
61 pub fn u16() -> IntTy {
62 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 }
63 }
64
65 pub fn u32() -> IntTy {
66 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 }
67 }
68
69 pub fn u64() -> IntTy {
70 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 }
71 } 48 }
49}
72 50
73 pub fn u128() -> IntTy { 51impl fmt::Display for UintTy {
74 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 } 52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 write!(f, "{}", self.ty_to_string())
75 } 54 }
55}
76 56
57impl UintTy {
77 pub fn ty_to_string(self) -> &'static str { 58 pub fn ty_to_string(self) -> &'static str {
78 match (self.signedness, self.bitness) { 59 match self {
79 (Signedness::Signed, IntBitness::Xsize) => "isize", 60 UintTy::Usize => "usize",
80 (Signedness::Signed, IntBitness::X8) => "i8", 61 UintTy::U8 => "u8",
81 (Signedness::Signed, IntBitness::X16) => "i16", 62 UintTy::U16 => "u16",
82 (Signedness::Signed, IntBitness::X32) => "i32", 63 UintTy::U32 => "u32",
83 (Signedness::Signed, IntBitness::X64) => "i64", 64 UintTy::U64 => "u64",
84 (Signedness::Signed, IntBitness::X128) => "i128", 65 UintTy::U128 => "u128",
85 (Signedness::Unsigned, IntBitness::Xsize) => "usize",
86 (Signedness::Unsigned, IntBitness::X8) => "u8",
87 (Signedness::Unsigned, IntBitness::X16) => "u16",
88 (Signedness::Unsigned, IntBitness::X32) => "u32",
89 (Signedness::Unsigned, IntBitness::X64) => "u64",
90 (Signedness::Unsigned, IntBitness::X128) => "u128",
91 } 66 }
92 } 67 }
93} 68}
94 69
95#[derive(Copy, Clone, PartialEq, Eq, Hash)] 70#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
96pub struct FloatTy { 71pub enum FloatTy {
97 pub bitness: FloatBitness, 72 F32,
73 F64,
98} 74}
99 75
100impl fmt::Debug for FloatTy { 76impl fmt::Debug for FloatTy {
@@ -110,30 +86,45 @@ impl fmt::Display for FloatTy {
110} 86}
111 87
112impl FloatTy { 88impl FloatTy {
113 pub fn f32() -> FloatTy {
114 FloatTy { bitness: FloatBitness::X32 }
115 }
116
117 pub fn f64() -> FloatTy {
118 FloatTy { bitness: FloatBitness::X64 }
119 }
120
121 pub fn ty_to_string(self) -> &'static str { 89 pub fn ty_to_string(self) -> &'static str {
122 match self.bitness { 90 match self {
123 FloatBitness::X32 => "f32", 91 FloatTy::F32 => "f32",
124 FloatBitness::X64 => "f64", 92 FloatTy::F64 => "f64",
125 } 93 }
126 } 94 }
127} 95}
128 96
129impl From<BuiltinInt> for IntTy { 97impl From<BuiltinInt> for IntTy {
130 fn from(t: BuiltinInt) -> Self { 98 fn from(t: BuiltinInt) -> Self {
131 IntTy { signedness: t.signedness, bitness: t.bitness } 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 }
107 }
108}
109
110impl 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 }
132 } 120 }
133} 121}
134 122
135impl From<BuiltinFloat> for FloatTy { 123impl From<BuiltinFloat> for FloatTy {
136 fn from(t: BuiltinFloat) -> Self { 124 fn from(t: BuiltinFloat) -> Self {
137 FloatTy { bitness: t.bitness } 125 match t {
126 BuiltinFloat::F32 => Self::F32,
127 BuiltinFloat::F64 => Self::F64,
128 }
138 } 129 }
139} 130}
diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs
index 8700d664e..0e9fc3265 100644
--- a/crates/hir_ty/src/traits/chalk/mapping.rs
+++ b/crates/hir_ty/src/traits/chalk/mapping.rs
@@ -4,7 +4,7 @@
4//! conversions. 4//! conversions.
5 5
6use chalk_ir::{ 6use chalk_ir::{
7 cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex, Scalar, 7 cast::Cast, fold::shift::Shift, interner::HasInterner, LifetimeData, PlaceholderIndex,
8 UniverseIndex, 8 UniverseIndex,
9}; 9};
10use chalk_solve::rust_ir; 10use chalk_solve::rust_ir;
@@ -14,10 +14,11 @@ use hir_def::{type_ref::Mutability, AssocContainerId, GenericDefId, Lookup, Type
14 14
15use crate::{ 15use crate::{
16 db::HirDatabase, 16 db::HirDatabase,
17 primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, 17 primitive::{FloatTy, IntTy, UintTy},
18 traits::{Canonical, Obligation}, 18 traits::{Canonical, Obligation},
19 ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId, 19 ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId,
20 ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor, 20 ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment, TraitRef, Ty, TyKind,
21 TypeCtor,
21}; 22};
22 23
23use super::interner::*; 24use super::interner::*;
@@ -63,19 +64,31 @@ impl ToChalk for Ty {
63 chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner) 64 chalk_ir::TyKind::Foreign(foreign_type_id).intern(&Interner)
64 } 65 }
65 66
66 TypeCtor::Bool => chalk_ir::TyKind::Scalar(Scalar::Bool).intern(&Interner), 67 TypeCtor::Scalar(scalar) => chalk_ir::TyKind::Scalar(match scalar {
67 TypeCtor::Char => chalk_ir::TyKind::Scalar(Scalar::Char).intern(&Interner), 68 Scalar::Bool => chalk_ir::Scalar::Bool,
68 TypeCtor::Int(int_ty) => { 69 Scalar::Char => chalk_ir::Scalar::Char,
69 chalk_ir::TyKind::Scalar(int_ty_to_chalk(int_ty)).intern(&Interner) 70 Scalar::Int(it) => chalk_ir::Scalar::Int(match it {
70 } 71 IntTy::Isize => chalk_ir::IntTy::Isize,
71 TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 }) => { 72 IntTy::I8 => chalk_ir::IntTy::I8,
72 chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) 73 IntTy::I16 => chalk_ir::IntTy::I16,
73 .intern(&Interner) 74 IntTy::I32 => chalk_ir::IntTy::I32,
74 } 75 IntTy::I64 => chalk_ir::IntTy::I64,
75 TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 }) => { 76 IntTy::I128 => chalk_ir::IntTy::I128,
76 chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) 77 }),
77 .intern(&Interner) 78 Scalar::Uint(it) => chalk_ir::Scalar::Uint(match it {
78 } 79 UintTy::Usize => chalk_ir::UintTy::Usize,
80 UintTy::U8 => chalk_ir::UintTy::U8,
81 UintTy::U16 => chalk_ir::UintTy::U16,
82 UintTy::U32 => chalk_ir::UintTy::U32,
83 UintTy::U64 => chalk_ir::UintTy::U64,
84 UintTy::U128 => chalk_ir::UintTy::U128,
85 }),
86 Scalar::Float(it) => chalk_ir::Scalar::Float(match it {
87 FloatTy::F32 => chalk_ir::FloatTy::F32,
88 FloatTy::F64 => chalk_ir::FloatTy::F64,
89 }),
90 })
91 .intern(&Interner),
79 92
80 TypeCtor::Tuple { cardinality } => { 93 TypeCtor::Tuple { cardinality } => {
81 let substitution = apply_ty.parameters.to_chalk(db); 94 let substitution = apply_ty.parameters.to_chalk(db);
@@ -219,21 +232,37 @@ impl ToChalk for Ty {
219 apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst) 232 apply_ty_from_chalk(db, TypeCtor::OpaqueType(from_chalk(db, opaque_type_id)), subst)
220 } 233 }
221 234
222 chalk_ir::TyKind::Scalar(Scalar::Bool) => Ty::simple(TypeCtor::Bool), 235 chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Bool) => {
223 chalk_ir::TyKind::Scalar(Scalar::Char) => Ty::simple(TypeCtor::Char), 236 Ty::simple(TypeCtor::Scalar(Scalar::Bool))
224 chalk_ir::TyKind::Scalar(Scalar::Int(int_ty)) => Ty::simple(TypeCtor::Int(IntTy { 237 }
225 signedness: Signedness::Signed, 238 chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Char) => {
226 bitness: bitness_from_chalk_int(int_ty), 239 Ty::simple(TypeCtor::Scalar(Scalar::Char))
227 })), 240 }
228 chalk_ir::TyKind::Scalar(Scalar::Uint(uint_ty)) => Ty::simple(TypeCtor::Int(IntTy { 241 chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Int(int_ty)) => {
229 signedness: Signedness::Unsigned, 242 Ty::simple(TypeCtor::Scalar(Scalar::Int(match int_ty {
230 bitness: bitness_from_chalk_uint(uint_ty), 243 chalk_ir::IntTy::Isize => IntTy::Isize,
231 })), 244 chalk_ir::IntTy::I8 => IntTy::I8,
232 chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F32)) => { 245 chalk_ir::IntTy::I16 => IntTy::I16,
233 Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X32 })) 246 chalk_ir::IntTy::I32 => IntTy::I32,
247 chalk_ir::IntTy::I64 => IntTy::I64,
248 chalk_ir::IntTy::I128 => IntTy::I128,
249 })))
234 } 250 }
235 chalk_ir::TyKind::Scalar(Scalar::Float(chalk_ir::FloatTy::F64)) => { 251 chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Uint(int_ty)) => {
236 Ty::simple(TypeCtor::Float(FloatTy { bitness: FloatBitness::X64 })) 252 Ty::simple(TypeCtor::Scalar(Scalar::Uint(match int_ty {
253 chalk_ir::UintTy::Usize => UintTy::Usize,
254 chalk_ir::UintTy::U8 => UintTy::U8,
255 chalk_ir::UintTy::U16 => UintTy::U16,
256 chalk_ir::UintTy::U32 => UintTy::U32,
257 chalk_ir::UintTy::U64 => UintTy::U64,
258 chalk_ir::UintTy::U128 => UintTy::U128,
259 })))
260 }
261 chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Float(float_ty)) => {
262 Ty::simple(TypeCtor::Scalar(Scalar::Float(match float_ty {
263 chalk_ir::FloatTy::F32 => FloatTy::F32,
264 chalk_ir::FloatTy::F64 => FloatTy::F64,
265 })))
237 } 266 }
238 chalk_ir::TyKind::Tuple(cardinality, subst) => { 267 chalk_ir::TyKind::Tuple(cardinality, subst) => {
239 apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst) 268 apply_ty_from_chalk(db, TypeCtor::Tuple { cardinality: cardinality as u16 }, subst)
@@ -293,7 +322,7 @@ fn ref_to_chalk(
293fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> { 322fn array_to_chalk(db: &dyn HirDatabase, subst: Substs) -> chalk_ir::Ty<Interner> {
294 let arg = subst[0].clone().to_chalk(db); 323 let arg = subst[0].clone().to_chalk(db);
295 let usize_ty = 324 let usize_ty =
296 chalk_ir::TyKind::Scalar(Scalar::Uint(chalk_ir::UintTy::Usize)).intern(&Interner); 325 chalk_ir::TyKind::Scalar(chalk_ir::Scalar::Uint(chalk_ir::UintTy::Usize)).intern(&Interner);
297 let const_ = chalk_ir::ConstData { 326 let const_ = chalk_ir::ConstData {
298 ty: usize_ty, 327 ty: usize_ty,
299 value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }), 328 value: chalk_ir::ConstValue::Concrete(chalk_ir::ConcreteConst { interned: () }),
@@ -364,55 +393,6 @@ impl ToChalk for OpaqueTyId {
364 } 393 }
365} 394}
366 395
367fn bitness_from_chalk_uint(uint_ty: chalk_ir::UintTy) -> IntBitness {
368 use chalk_ir::UintTy;
369
370 match uint_ty {
371 UintTy::Usize => IntBitness::Xsize,
372 UintTy::U8 => IntBitness::X8,
373 UintTy::U16 => IntBitness::X16,
374 UintTy::U32 => IntBitness::X32,
375 UintTy::U64 => IntBitness::X64,
376 UintTy::U128 => IntBitness::X128,
377 }
378}
379
380fn bitness_from_chalk_int(int_ty: chalk_ir::IntTy) -> IntBitness {
381 use chalk_ir::IntTy;
382
383 match int_ty {
384 IntTy::Isize => IntBitness::Xsize,
385 IntTy::I8 => IntBitness::X8,
386 IntTy::I16 => IntBitness::X16,
387 IntTy::I32 => IntBitness::X32,
388 IntTy::I64 => IntBitness::X64,
389 IntTy::I128 => IntBitness::X128,
390 }
391}
392
393fn int_ty_to_chalk(int_ty: IntTy) -> Scalar {
394 use chalk_ir::{IntTy, UintTy};
395
396 match int_ty.signedness {
397 Signedness::Signed => Scalar::Int(match int_ty.bitness {
398 IntBitness::Xsize => IntTy::Isize,
399 IntBitness::X8 => IntTy::I8,
400 IntBitness::X16 => IntTy::I16,
401 IntBitness::X32 => IntTy::I32,
402 IntBitness::X64 => IntTy::I64,
403 IntBitness::X128 => IntTy::I128,
404 }),
405 Signedness::Unsigned => Scalar::Uint(match int_ty.bitness {
406 IntBitness::Xsize => UintTy::Usize,
407 IntBitness::X8 => UintTy::U8,
408 IntBitness::X16 => UintTy::U16,
409 IntBitness::X32 => UintTy::U32,
410 IntBitness::X64 => UintTy::U64,
411 IntBitness::X128 => UintTy::U128,
412 }),
413 }
414}
415
416impl ToChalk for Mutability { 396impl ToChalk for Mutability {
417 type Chalk = chalk_ir::Mutability; 397 type Chalk = chalk_ir::Mutability;
418 fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk { 398 fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {