diff options
author | Marcus Klaas de Vries <[email protected]> | 2019-01-10 12:54:58 +0000 |
---|---|---|
committer | Marcus Klaas de Vries <[email protected]> | 2019-01-14 12:52:55 +0000 |
commit | a6146d35b1615cf5fb908b29f34e58bfde3bf96d (patch) | |
tree | 70613ee98eee67c1df6aff1e663be75a33c348f4 /crates/ra_hir/src/ty.rs | |
parent | 8caff4e03475c20392f13e8c6ad469bd01a4b4ce (diff) |
Implement type inference for literals (WIP)
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index fa46ddfe9..0baa205a1 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -38,7 +38,7 @@ use crate::{ | |||
38 | db::HirDatabase, | 38 | db::HirDatabase, |
39 | type_ref::{TypeRef, Mutability}, | 39 | type_ref::{TypeRef, Mutability}, |
40 | name::KnownName, | 40 | name::KnownName, |
41 | expr::{Body, Expr, ExprId, PatId, UnaryOp, BinaryOp, Statement}, | 41 | expr::{Body, Expr, Literal, ExprId, PatId, UnaryOp, BinaryOp, Statement}, |
42 | }; | 42 | }; |
43 | 43 | ||
44 | fn transpose<T>(x: Cancelable<Option<T>>) -> Option<Cancelable<T>> { | 44 | fn transpose<T>(x: Cancelable<Option<T>>) -> Option<Cancelable<T>> { |
@@ -1067,6 +1067,37 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1067 | 1067 | ||
1068 | Ty::Tuple(Arc::from(ty_vec)) | 1068 | Ty::Tuple(Arc::from(ty_vec)) |
1069 | } | 1069 | } |
1070 | Expr::Literal(lit) => match lit { | ||
1071 | Literal::Bool(..) => Ty::Bool, | ||
1072 | Literal::String(..) => Ty::Ref(Arc::new(Ty::Str), Mutability::Shared), | ||
1073 | Literal::ByteString(..) => { | ||
1074 | let byte_type = Arc::new(Ty::Uint(primitive::UintTy::U8)); | ||
1075 | let slice_type = Arc::new(Ty::Slice(byte_type)); | ||
1076 | Ty::Ref(slice_type, Mutability::Shared) | ||
1077 | } | ||
1078 | Literal::Byte(..) => Ty::Uint(primitive::UintTy::U8), | ||
1079 | Literal::Char(..) => Ty::Char, | ||
1080 | Literal::Tuple { values } => { | ||
1081 | let mut inner_tys = Vec::new(); | ||
1082 | for &expr in values { | ||
1083 | let inner_ty = self.infer_expr(expr, &Expectation::none())?; | ||
1084 | inner_tys.push(inner_ty); | ||
1085 | } | ||
1086 | Ty::Tuple(Arc::from(inner_tys)) | ||
1087 | } | ||
1088 | Literal::Array { values } => { | ||
1089 | // simply take the type of the first element for now | ||
1090 | let inner_ty = match values.get(0) { | ||
1091 | Some(&expr) => self.infer_expr(expr, &Expectation::none())?, | ||
1092 | None => Ty::Unknown, | ||
1093 | }; | ||
1094 | // TODO: we should return a Ty::Array when it becomes | ||
1095 | // available | ||
1096 | Ty::Slice(Arc::new(inner_ty)) | ||
1097 | } | ||
1098 | // TODO | ||
1099 | Literal::Int | Literal::Float => Ty::Unknown, | ||
1100 | }, | ||
1070 | }; | 1101 | }; |
1071 | // use a new type variable if we got Ty::Unknown here | 1102 | // use a new type variable if we got Ty::Unknown here |
1072 | let ty = self.insert_type_vars_shallow(ty); | 1103 | let ty = self.insert_type_vars_shallow(ty); |