aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-01-06 15:47:59 +0000
committerFlorian Diebold <[email protected]>2019-01-06 23:05:19 +0000
commit6210e82041849bad6129331b9e45ac0bae6fe569 (patch)
tree43656452729519e45f4aa036372526b2934a230c /crates/ra_hir/src/ty
parent3c945ceb5e0dc287139de0589cc9a4b285911f17 (diff)
Use HIR Expr for type inference
Now we can reuse the type inference inside a function when typing whitespace etc. :)
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r--crates/ra_hir/src/ty/tests.rs24
-rw-r--r--crates/ra_hir/src/ty/tests/data/0001_basics.txt2
-rw-r--r--crates/ra_hir/src/ty/tests/data/0004_struct.txt2
-rw-r--r--crates/ra_hir/src/ty/tests/data/0005_refs.txt4
-rw-r--r--crates/ra_hir/src/ty/tests/data/0006_backwards.txt6
-rw-r--r--crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt14
6 files changed, 35 insertions, 17 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index 25a354947..e46f309ae 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -4,8 +4,9 @@ use std::path::{PathBuf, Path};
4use std::fs; 4use std::fs;
5 5
6use salsa::Database; 6use salsa::Database;
7use rustc_hash::FxHashMap;
7 8
8use ra_db::{SyntaxDatabase}; 9use ra_db::SyntaxDatabase;
9use ra_syntax::ast::{self, AstNode}; 10use ra_syntax::ast::{self, AstNode};
10use test_utils::{project_dir, assert_eq_text, read_text}; 11use test_utils::{project_dir, assert_eq_text, read_text};
11 12
@@ -193,7 +194,25 @@ fn infer(content: &str) -> String {
193 .unwrap() 194 .unwrap()
194 .unwrap(); 195 .unwrap();
195 let inference_result = func.infer(&db).unwrap(); 196 let inference_result = func.infer(&db).unwrap();
196 for (syntax_ptr, ty) in &inference_result.type_of { 197 let body_syntax_mapping = func.body_syntax_mapping(&db).unwrap();
198 let mut types = FxHashMap::default();
199 for (pat, ty) in &inference_result.type_of_pat {
200 let syntax_ptr = if let Some(sp) = body_syntax_mapping.pat_syntax(*pat) {
201 sp
202 } else {
203 continue;
204 };
205 types.insert(syntax_ptr, ty);
206 }
207 for (expr, ty) in &inference_result.type_of_expr {
208 let syntax_ptr = if let Some(sp) = body_syntax_mapping.expr_syntax(*expr) {
209 sp
210 } else {
211 continue;
212 };
213 types.insert(syntax_ptr, ty);
214 }
215 for (syntax_ptr, ty) in &types {
197 let node = syntax_ptr.resolve(&source_file); 216 let node = syntax_ptr.resolve(&source_file);
198 write!( 217 write!(
199 acc, 218 acc,
@@ -246,7 +265,6 @@ fn test_data_dir() -> PathBuf {
246} 265}
247 266
248#[test] 267#[test]
249#[should_panic] // TODO this should work once hir::Expr is used
250fn typing_whitespace_inside_a_function_should_not_invalidate_types() { 268fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
251 let (mut db, pos) = MockDatabase::with_position( 269 let (mut db, pos) = MockDatabase::with_position(
252 " 270 "
diff --git a/crates/ra_hir/src/ty/tests/data/0001_basics.txt b/crates/ra_hir/src/ty/tests/data/0001_basics.txt
index 212e92e00..4df6b42c9 100644
--- a/crates/ra_hir/src/ty/tests/data/0001_basics.txt
+++ b/crates/ra_hir/src/ty/tests/data/0001_basics.txt
@@ -8,6 +8,6 @@
8[27; 28) 'c': ! 8[27; 28) 'c': !
9[62; 63) 'c': ! 9[62; 63) 'c': !
10[17; 18) 'b': isize 10[17; 18) 'b': isize
11[100; 106) '"test"': [unknown]
12[42; 121) '{ ...f32; }': () 11[42; 121) '{ ...f32; }': ()
12[100; 106) '"test"': [unknown]
13[69; 70) 'd': &[unknown] 13[69; 70) 'd': &[unknown]
diff --git a/crates/ra_hir/src/ty/tests/data/0004_struct.txt b/crates/ra_hir/src/ty/tests/data/0004_struct.txt
index b4af18b87..6f919b332 100644
--- a/crates/ra_hir/src/ty/tests/data/0004_struct.txt
+++ b/crates/ra_hir/src/ty/tests/data/0004_struct.txt
@@ -1,8 +1,8 @@
1[86; 90) 'C(1)': [unknown] 1[86; 90) 'C(1)': [unknown]
2[121; 122) 'B': B 2[121; 122) 'B': B
3[86; 87) 'C': [unknown] 3[86; 87) 'C': [unknown]
4[129; 130) '1': [unknown]
5[107; 108) 'a': A 4[107; 108) 'a': A
5[129; 130) '1': [unknown]
6[127; 128) 'C': [unknown] 6[127; 128) 'C': [unknown]
7[139; 142) 'a.b': B 7[139; 142) 'a.b': B
8[114; 133) 'A { b:...C(1) }': A 8[114; 133) 'A { b:...C(1) }': A
diff --git a/crates/ra_hir/src/ty/tests/data/0005_refs.txt b/crates/ra_hir/src/ty/tests/data/0005_refs.txt
index 296e955c1..cc32162a1 100644
--- a/crates/ra_hir/src/ty/tests/data/0005_refs.txt
+++ b/crates/ra_hir/src/ty/tests/data/0005_refs.txt
@@ -6,9 +6,9 @@
6[46; 47) 'd': *mut u32 6[46; 47) 'd': *mut u32
7[59; 150) '{ ... *d; }': () 7[59; 150) '{ ... *d; }': ()
8[116; 117) 'b': &mut u32 8[116; 117) 'b': &mut u32
9[72; 74) '*a': u32
9[131; 132) 'c': *const u32 10[131; 132) 'c': *const u32
10[130; 132) '*c': u32 11[130; 132) '*c': u32
11[72; 74) '*a': u32
12[107; 109) '*b': u32 12[107; 109) '*b': u32
13[108; 109) 'b': &mut u32 13[108; 109) 'b': &mut u32
14[9; 10) 'a': &u32 14[9; 10) 'a': &u32
@@ -17,7 +17,7 @@
17[100; 101) 'b': &mut u32 17[100; 101) 'b': &mut u32
18[81; 82) 'a': &u32 18[81; 82) 'a': &u32
19[80; 82) '&a': &&u32 19[80; 82) '&a': &&u32
20[73; 74) 'a': &u32
21[123; 124) 'c': *const u32 20[123; 124) 'c': *const u32
21[73; 74) 'a': &u32
22[31; 32) 'c': *const u32 22[31; 32) 'c': *const u32
23[138; 139) 'd': *mut u32 23[138; 139) 'd': *mut u32
diff --git a/crates/ra_hir/src/ty/tests/data/0006_backwards.txt b/crates/ra_hir/src/ty/tests/data/0006_backwards.txt
index 120069401..0efae598e 100644
--- a/crates/ra_hir/src/ty/tests/data/0006_backwards.txt
+++ b/crates/ra_hir/src/ty/tests/data/0006_backwards.txt
@@ -1,12 +1,12 @@
1[22; 24) '{}': () 1[22; 24) '{}': ()
2[14; 15) 'x': u32 2[14; 15) 'x': u32
3[142; 158) 'unknow...nction': [unknown] 3[142; 158) 'unknow...nction': [unknown]
4[126; 127) 'a': u32
5[198; 216) 'unknow...tion()': f64
6[228; 229) 'c': f64 4[228; 229) 'c': f64
5[198; 216) 'unknow...tion()': f64
6[126; 127) 'a': u32
7[198; 214) 'unknow...nction': [unknown] 7[198; 214) 'unknow...nction': [unknown]
8[166; 184) 'S { i3...d: b }': S
9[222; 229) '&mut &c': &mut &f64 8[222; 229) '&mut &c': &mut &f64
9[166; 184) 'S { i3...d: b }': S
10[194; 195) 'c': f64 10[194; 195) 'c': f64
11[92; 110) 'unknow...tion()': u32 11[92; 110) 'unknow...tion()': u32
12[142; 160) 'unknow...tion()': i32 12[142; 160) 'unknow...tion()': i32
diff --git a/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt
index ca01ad159..0ae172914 100644
--- a/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt
+++ b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt
@@ -1,31 +1,31 @@
1[28; 32) '0i32': i32 1[28; 32) '0i32': i32
2[22; 34) '{ 0i32 }': i32 2[22; 34) '{ 0i32 }': i32
3[6; 7) 'x': [unknown] 3[6; 7) 'x': [unknown]
4[127; 134) 'CONST_1': [unknown]
5[201; 205) '3i32': bool 4[201; 205) '3i32': bool
5[127; 134) 'CONST_1': [unknown]
6[76; 77) 'y': bool 6[76; 77) 'y': bool
7[65; 66) 'b': bool
8[60; 66) 'a && b': bool 7[60; 66) 'a && b': bool
8[65; 66) 'b': bool
9[229; 231) '10': [unknown]
9[127; 145) 'CONST_...ONST_2': bool 10[127; 145) 'CONST_...ONST_2': bool
10[182; 183) 'd': [unknown] 11[182; 183) 'd': [unknown]
11[229; 231) '10': [unknown]
12[209; 222) '"hello world"': bool 12[209; 222) '"hello world"': bool
13[229; 235) '10 < 3': bool 13[229; 235) '10 < 3': bool
14[186; 187) 'b': [unknown] 14[186; 187) 'b': [unknown]
15[159; 172) 'f(z || y) + 5': [unknown]
16[56; 57) 'x': bool 15[56; 57) 'x': bool
16[159; 172) 'f(z || y) + 5': [unknown]
17[112; 113) 'y': bool 17[112; 113) 'y': bool
18[201; 222) '3i32 &...world"': bool
19[234; 235) '3': [unknown] 18[234; 235) '3': [unknown]
19[201; 222) '3i32 &...world"': bool
20[138; 145) 'CONST_2': [unknown] 20[138; 145) 'CONST_2': [unknown]
21[80; 93) 'true || false': bool 21[80; 93) 'true || false': bool
22[46; 237) '{ ... < 3 }': bool 22[46; 237) '{ ... < 3 }': bool
23[197; 198) 'e': bool 23[197; 198) 'e': bool
24[107; 113) 'x == y': bool 24[107; 113) 'x == y': bool
25[88; 93) 'false': bool 25[88; 93) 'false': bool
26[80; 84) 'true': bool
27[123; 124) 'h': bool
28[155; 156) 'c': [unknown] 26[155; 156) 'c': [unknown]
27[123; 124) 'h': bool
28[80; 84) 'true': bool
29[103; 104) 'z': bool 29[103; 104) 'z': bool
30[60; 61) 'a': bool 30[60; 61) 'a': bool
31[107; 108) 'x': bool 31[107; 108) 'x': bool