aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/expr.rs19
-rw-r--r--crates/ra_hir/src/ty.rs49
-rw-r--r--crates/ra_hir/src/ty/tests.rs2
-rw-r--r--crates/ra_hir/src/ty/tests/data/literals.txt10
-rw-r--r--crates/ra_hir/src/ty/tests/data/struct.txt4
5 files changed, 27 insertions, 57 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 2798937a6..52af2af45 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -115,8 +115,6 @@ pub enum Literal {
115 Bool(bool), 115 Bool(bool),
116 Int(u64, UncertainIntTy), 116 Int(u64, UncertainIntTy),
117 Float(u64, UncertainFloatTy), // FIXME: f64 is not Eq 117 Float(u64, UncertainFloatTy), // FIXME: f64 is not Eq
118 Tuple { values: Vec<ExprId> },
119 Array { values: Vec<ExprId> },
120} 118}
121 119
122#[derive(Debug, Clone, Eq, PartialEq)] 120#[derive(Debug, Clone, Eq, PartialEq)]
@@ -322,14 +320,7 @@ impl Expr {
322 f(*expr); 320 f(*expr);
323 } 321 }
324 } 322 }
325 Expr::Literal(l) => match l { 323 Expr::Literal(_) => {}
326 Literal::Array { values } | Literal::Tuple { values } => {
327 for &val in values {
328 f(val);
329 }
330 }
331 _ => {}
332 },
333 } 324 }
334 } 325 }
335} 326}
@@ -720,14 +711,6 @@ impl ExprCollector {
720 let text = c.text().to_string(); 711 let text = c.text().to_string();
721 Literal::String(text) 712 Literal::String(text)
722 } 713 }
723 SyntaxKind::ARRAY_EXPR => {
724 // TODO: recursively call to self
725 Literal::Array { values: vec![] }
726 }
727 SyntaxKind::PAREN_EXPR => {
728 // TODO: recursively call to self
729 Literal::Tuple { values: vec![] }
730 }
731 SyntaxKind::TRUE_KW => Literal::Bool(true), 714 SyntaxKind::TRUE_KW => Literal::Bool(true),
732 SyntaxKind::FALSE_KW => Literal::Bool(false), 715 SyntaxKind::FALSE_KW => Literal::Bool(false),
733 SyntaxKind::BYTE_STRING => { 716 SyntaxKind::BYTE_STRING => {
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index b4b338874..de5ec5b46 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -107,9 +107,9 @@ impl UnifyValue for TypeVarValue {
107 } 107 }
108} 108}
109 109
110/// The kinds of placeholders we need during type inference. Currently, we only 110/// The kinds of placeholders we need during type inference. There's seperate
111/// have type variables; in the future, we will probably also need int and float 111/// values for general types, and for integer and float variables. The latter
112/// variables, for inference of literal values (e.g. `100` could be one of 112/// two are used for inference of literal values (e.g. `100` could be one of
113/// several integer types). 113/// several integer types).
114#[derive(Clone, PartialEq, Eq, Hash, Debug)] 114#[derive(Clone, PartialEq, Eq, Hash, Debug)]
115pub enum InferTy { 115pub enum InferTy {
@@ -118,6 +118,20 @@ pub enum InferTy {
118 FloatVar(TypeVarId), 118 FloatVar(TypeVarId),
119} 119}
120 120
121impl InferTy {
122 fn fallback_value(self) -> Ty {
123 match self {
124 InferTy::TypeVar(..) => Ty::Unknown,
125 InferTy::IntVar(..) => {
126 Ty::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32))
127 }
128 InferTy::FloatVar(..) => {
129 Ty::Float(primitive::UncertainFloatTy::Known(primitive::FloatTy::F64))
130 }
131 }
132 }
133}
134
121/// When inferring an expression, we propagate downward whatever type hint we 135/// When inferring an expression, we propagate downward whatever type hint we
122/// are able in the form of an `Expectation`. 136/// are able in the form of an `Expectation`.
123#[derive(Clone, PartialEq, Eq, Debug)] 137#[derive(Clone, PartialEq, Eq, Debug)]
@@ -156,8 +170,6 @@ pub enum Ty {
156 /// A primitive integer type. For example, `i32`. 170 /// A primitive integer type. For example, `i32`.
157 Int(primitive::UncertainIntTy), 171 Int(primitive::UncertainIntTy),
158 172
159 // /// A primitive unsigned integer type. For example, `u32`.
160 // Uint(primitive::UintTy),
161 /// A primitive floating-point type. For example, `f64`. 173 /// A primitive floating-point type. For example, `f64`.
162 Float(primitive::UncertainFloatTy), 174 Float(primitive::UncertainFloatTy),
163 175
@@ -199,8 +211,9 @@ pub enum Ty {
199 // above function pointer type. Once we implement generics, we will probably 211 // above function pointer type. Once we implement generics, we will probably
200 // need this as well. 212 // need this as well.
201 213
202 // A trait, defined with `dyn trait`. 214 // A trait, defined with `dyn Trait`.
203 // Dynamic(), 215 // Dynamic(),
216
204 // The anonymous type of a closure. Used to represent the type of 217 // The anonymous type of a closure. Used to represent the type of
205 // `|a| a`. 218 // `|a| a`.
206 // Closure(DefId, ClosureSubsts<'tcx>), 219 // Closure(DefId, ClosureSubsts<'tcx>),
@@ -824,11 +837,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
824 // known_ty may contain other variables that are known by now 837 // known_ty may contain other variables that are known by now
825 self.resolve_ty_completely(known_ty.clone()) 838 self.resolve_ty_completely(known_ty.clone())
826 } else { 839 } else {
827 match i { 840 i.fallback_value()
828 InferTy::TypeVar(..) => Ty::Unknown,
829 InferTy::IntVar(..) => Ty::Int(primitive::UncertainIntTy::Unknown),
830 InferTy::FloatVar(..) => Ty::Float(primitive::UncertainFloatTy::Unknown),
831 }
832 } 841 }
833 } 842 }
834 _ => ty, 843 _ => ty,
@@ -1111,24 +1120,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1111 Ty::Ref(slice_type, Mutability::Shared) 1120 Ty::Ref(slice_type, Mutability::Shared)
1112 } 1121 }
1113 Literal::Char(..) => Ty::Char, 1122 Literal::Char(..) => Ty::Char,
1114 Literal::Tuple { values } => {
1115 let mut inner_tys = Vec::new();
1116 for &expr in values {
1117 let inner_ty = self.infer_expr(expr, &Expectation::none())?;
1118 inner_tys.push(inner_ty);
1119 }
1120 Ty::Tuple(Arc::from(inner_tys))
1121 }
1122 Literal::Array { values } => {
1123 // simply take the type of the first element for now
1124 let inner_ty = match values.get(0) {
1125 Some(&expr) => self.infer_expr(expr, &Expectation::none())?,
1126 None => Ty::Unknown,
1127 };
1128 // TODO: we should return a Ty::Array when it becomes
1129 // available
1130 Ty::Slice(Arc::new(inner_ty))
1131 }
1132 Literal::Int(_v, ty) => Ty::Int(*ty), 1123 Literal::Int(_v, ty) => Ty::Int(*ty),
1133 Literal::Float(_v, ty) => Ty::Float(*ty), 1124 Literal::Float(_v, ty) => Ty::Float(*ty),
1134 }, 1125 },
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 0c43415a6..53ea99874 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -144,9 +144,7 @@ fn test() {
144 b'b'; 144 b'b';
145 3.14; 145 3.14;
146 5000; 146 5000;
147 (0u32, -5isize);
148 false; 147 false;
149 [true, true, false]
150} 148}
151"#, 149"#,
152 "literals.txt", 150 "literals.txt",
diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt
index df435edd7..8d6079c98 100644
--- a/crates/ra_hir/src/ty/tests/data/literals.txt
+++ b/crates/ra_hir/src/ty/tests/data/literals.txt
@@ -1,11 +1,9 @@
1[11; 146) '{ ...lse] }': () 1[11; 101) '{ ...lse; }': ()
2[17; 21) '5i32': i32 2[17; 21) '5i32': i32
3[27; 34) '"hello"': &str 3[27; 34) '"hello"': &str
4[40; 48) 'b"bytes"': &[u8] 4[40; 48) 'b"bytes"': &[u8]
5[54; 57) ''c'': char 5[54; 57) ''c'': char
6[63; 67) 'b'b'': u8 6[63; 67) 'b'b'': u8
7[73; 77) '3.14': {float} 7[73; 77) '3.14': f64
8[83; 87) '5000': {integer} 8[83; 87) '5000': i32
9[93; 108) '(0u32, -5isize)': [unknown] 9[93; 98) 'false': bool
10[114; 119) 'false': bool
11[125; 144) '[true,...false]': ()
diff --git a/crates/ra_hir/src/ty/tests/data/struct.txt b/crates/ra_hir/src/ty/tests/data/struct.txt
index dcdf61363..be9e12d02 100644
--- a/crates/ra_hir/src/ty/tests/data/struct.txt
+++ b/crates/ra_hir/src/ty/tests/data/struct.txt
@@ -2,14 +2,14 @@
2[82; 83) 'c': [unknown] 2[82; 83) 'c': [unknown]
3[86; 87) 'C': [unknown] 3[86; 87) 'C': [unknown]
4[86; 90) 'C(1)': [unknown] 4[86; 90) 'C(1)': [unknown]
5[88; 89) '1': {integer} 5[88; 89) '1': i32
6[96; 97) 'B': [unknown] 6[96; 97) 'B': [unknown]
7[107; 108) 'a': A 7[107; 108) 'a': A
8[114; 133) 'A { b:...C(1) }': A 8[114; 133) 'A { b:...C(1) }': A
9[121; 122) 'B': B 9[121; 122) 'B': B
10[127; 128) 'C': [unknown] 10[127; 128) 'C': [unknown]
11[127; 131) 'C(1)': C 11[127; 131) 'C(1)': C
12[129; 130) '1': {integer} 12[129; 130) '1': i32
13[139; 140) 'a': A 13[139; 140) 'a': A
14[139; 142) 'a.b': B 14[139; 142) 'a.b': B
15[148; 149) 'a': A 15[148; 149) 'a': A