diff options
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 103 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/backwards.txt (renamed from crates/ra_hir/src/ty/tests/data/0006_backwards.txt) | 30 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/basics.txt (renamed from crates/ra_hir/src/ty/tests/data/0001_basics.txt) | 16 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/boolean_op.txt | 31 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/let.txt (renamed from crates/ra_hir/src/ty/tests/data/0002_let.txt) | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/paths.txt (renamed from crates/ra_hir/src/ty/tests/data/0003_paths.txt) | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/refs_and_ptrs.txt (renamed from crates/ra_hir/src/ty/tests/data/0005_refs.txt) | 32 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/self.txt (renamed from crates/ra_hir/src/ty/tests/data/0007_self.txt) | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/struct.txt (renamed from crates/ra_hir/src/ty/tests/data/0004_struct.txt) | 22 |
9 files changed, 182 insertions, 66 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index fb53fcf0b..83aedaa00 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -1,8 +1,11 @@ | |||
1 | use std::sync::Arc; | ||
1 | use std::fmt::Write; | 2 | use std::fmt::Write; |
2 | use std::path::{PathBuf, Path}; | 3 | use std::path::{PathBuf, Path}; |
3 | use std::fs; | 4 | use std::fs; |
4 | 5 | ||
5 | use ra_db::{SyntaxDatabase}; | 6 | use salsa::Database; |
7 | |||
8 | use ra_db::SyntaxDatabase; | ||
6 | use ra_syntax::ast::{self, AstNode}; | 9 | use ra_syntax::ast::{self, AstNode}; |
7 | use test_utils::{project_dir, assert_eq_text, read_text}; | 10 | use test_utils::{project_dir, assert_eq_text, read_text}; |
8 | 11 | ||
@@ -31,7 +34,7 @@ fn test(a: u32, b: isize, c: !, d: &str) { | |||
31 | "test"; | 34 | "test"; |
32 | 1.0f32; | 35 | 1.0f32; |
33 | }"#, | 36 | }"#, |
34 | "0001_basics.txt", | 37 | "basics.txt", |
35 | ); | 38 | ); |
36 | } | 39 | } |
37 | 40 | ||
@@ -45,7 +48,7 @@ fn test() { | |||
45 | let c = b; | 48 | let c = b; |
46 | } | 49 | } |
47 | }"#, | 50 | }"#, |
48 | "0002_let.txt", | 51 | "let.txt", |
49 | ); | 52 | ); |
50 | } | 53 | } |
51 | 54 | ||
@@ -64,7 +67,7 @@ fn test() { | |||
64 | b::c(); | 67 | b::c(); |
65 | } | 68 | } |
66 | }"#, | 69 | }"#, |
67 | "0003_paths.txt", | 70 | "paths.txt", |
68 | ); | 71 | ); |
69 | } | 72 | } |
70 | 73 | ||
@@ -87,7 +90,7 @@ fn test() { | |||
87 | a.c; | 90 | a.c; |
88 | } | 91 | } |
89 | "#, | 92 | "#, |
90 | "0004_struct.txt", | 93 | "struct.txt", |
91 | ); | 94 | ); |
92 | } | 95 | } |
93 | 96 | ||
@@ -109,7 +112,7 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { | |||
109 | *d; | 112 | *d; |
110 | } | 113 | } |
111 | "#, | 114 | "#, |
112 | "0005_refs.txt", | 115 | "refs_and_ptrs.txt", |
113 | ); | 116 | ); |
114 | } | 117 | } |
115 | 118 | ||
@@ -130,7 +133,7 @@ fn test() -> &mut &f64 { | |||
130 | &mut &c | 133 | &mut &c |
131 | } | 134 | } |
132 | "#, | 135 | "#, |
133 | "0006_backwards.txt", | 136 | "backwards.txt", |
134 | ); | 137 | ); |
135 | } | 138 | } |
136 | 139 | ||
@@ -149,7 +152,31 @@ impl S { | |||
149 | } | 152 | } |
150 | } | 153 | } |
151 | "#, | 154 | "#, |
152 | "0007_self.txt", | 155 | "self.txt", |
156 | ); | ||
157 | } | ||
158 | |||
159 | #[test] | ||
160 | fn infer_boolean_op() { | ||
161 | check_inference( | ||
162 | r#" | ||
163 | fn f(x: bool) -> i32 { | ||
164 | 0i32 | ||
165 | } | ||
166 | |||
167 | fn test() { | ||
168 | let x = a && b; | ||
169 | let y = true || false; | ||
170 | let z = x == y; | ||
171 | let h = CONST_1 <= CONST_2; | ||
172 | let c = f(z || y) + 5; | ||
173 | let d = b; | ||
174 | let e = 3i32 && "hello world"; | ||
175 | |||
176 | 10 < 3 | ||
177 | } | ||
178 | "#, | ||
179 | "boolean_op.txt", | ||
153 | ); | 180 | ); |
154 | } | 181 | } |
155 | 182 | ||
@@ -166,7 +193,25 @@ fn infer(content: &str) -> String { | |||
166 | .unwrap() | 193 | .unwrap() |
167 | .unwrap(); | 194 | .unwrap(); |
168 | let inference_result = func.infer(&db).unwrap(); | 195 | let inference_result = func.infer(&db).unwrap(); |
169 | for (syntax_ptr, ty) in &inference_result.type_of { | 196 | let body_syntax_mapping = func.body_syntax_mapping(&db).unwrap(); |
197 | let mut types = Vec::new(); | ||
198 | for (pat, ty) in inference_result.type_of_pat.iter() { | ||
199 | let syntax_ptr = match body_syntax_mapping.pat_syntax(pat) { | ||
200 | Some(sp) => sp, | ||
201 | None => continue, | ||
202 | }; | ||
203 | types.push((syntax_ptr, ty)); | ||
204 | } | ||
205 | for (expr, ty) in inference_result.type_of_expr.iter() { | ||
206 | let syntax_ptr = match body_syntax_mapping.expr_syntax(expr) { | ||
207 | Some(sp) => sp, | ||
208 | None => continue, | ||
209 | }; | ||
210 | types.push((syntax_ptr, ty)); | ||
211 | } | ||
212 | // sort ranges for consistency | ||
213 | types.sort_by_key(|(ptr, _)| (ptr.range().start(), ptr.range().end())); | ||
214 | for (syntax_ptr, ty) in &types { | ||
170 | let node = syntax_ptr.resolve(&source_file); | 215 | let node = syntax_ptr.resolve(&source_file); |
171 | write!( | 216 | write!( |
172 | acc, | 217 | acc, |
@@ -217,3 +262,43 @@ fn ellipsize(mut text: String, max_len: usize) -> String { | |||
217 | fn test_data_dir() -> PathBuf { | 262 | fn test_data_dir() -> PathBuf { |
218 | project_dir().join("crates/ra_hir/src/ty/tests/data") | 263 | project_dir().join("crates/ra_hir/src/ty/tests/data") |
219 | } | 264 | } |
265 | |||
266 | #[test] | ||
267 | fn typing_whitespace_inside_a_function_should_not_invalidate_types() { | ||
268 | let (mut db, pos) = MockDatabase::with_position( | ||
269 | " | ||
270 | //- /lib.rs | ||
271 | fn foo() -> i32 { | ||
272 | <|>1 + 1 | ||
273 | } | ||
274 | ", | ||
275 | ); | ||
276 | let func = source_binder::function_from_position(&db, pos) | ||
277 | .unwrap() | ||
278 | .unwrap(); | ||
279 | { | ||
280 | let events = db.log_executed(|| { | ||
281 | func.infer(&db).unwrap(); | ||
282 | }); | ||
283 | assert!(format!("{:?}", events).contains("infer")) | ||
284 | } | ||
285 | |||
286 | let new_text = " | ||
287 | fn foo() -> i32 { | ||
288 | 1 | ||
289 | + | ||
290 | 1 | ||
291 | } | ||
292 | " | ||
293 | .to_string(); | ||
294 | |||
295 | db.query_mut(ra_db::FileTextQuery) | ||
296 | .set(pos.file_id, Arc::new(new_text)); | ||
297 | |||
298 | { | ||
299 | let events = db.log_executed(|| { | ||
300 | func.infer(&db).unwrap(); | ||
301 | }); | ||
302 | assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events) | ||
303 | } | ||
304 | } | ||
diff --git a/crates/ra_hir/src/ty/tests/data/0006_backwards.txt b/crates/ra_hir/src/ty/tests/data/backwards.txt index 120069401..b6807fb2a 100644 --- a/crates/ra_hir/src/ty/tests/data/0006_backwards.txt +++ b/crates/ra_hir/src/ty/tests/data/backwards.txt | |||
@@ -1,20 +1,20 @@ | |||
1 | [22; 24) '{}': () | ||
2 | [14; 15) 'x': u32 | 1 | [14; 15) 'x': u32 |
3 | [142; 158) 'unknow...nction': [unknown] | 2 | [22; 24) '{}': () |
4 | [126; 127) 'a': u32 | ||
5 | [198; 216) 'unknow...tion()': f64 | ||
6 | [228; 229) 'c': f64 | ||
7 | [198; 214) 'unknow...nction': [unknown] | ||
8 | [166; 184) 'S { i3...d: b }': S | ||
9 | [222; 229) '&mut &c': &mut &f64 | ||
10 | [194; 195) 'c': f64 | ||
11 | [92; 110) 'unknow...tion()': u32 | ||
12 | [142; 160) 'unknow...tion()': i32 | ||
13 | [92; 108) 'unknow...nction': [unknown] | ||
14 | [116; 128) 'takes_u32(a)': () | ||
15 | [78; 231) '{ ...t &c }': &mut &f64 | 3 | [78; 231) '{ ...t &c }': &mut &f64 |
16 | [227; 229) '&c': &f64 | ||
17 | [88; 89) 'a': u32 | 4 | [88; 89) 'a': u32 |
18 | [181; 182) 'b': i32 | 5 | [92; 108) 'unknow...nction': [unknown] |
6 | [92; 110) 'unknow...tion()': u32 | ||
19 | [116; 125) 'takes_u32': fn(u32,) -> () | 7 | [116; 125) 'takes_u32': fn(u32,) -> () |
8 | [116; 128) 'takes_u32(a)': () | ||
9 | [126; 127) 'a': u32 | ||
20 | [138; 139) 'b': i32 | 10 | [138; 139) 'b': i32 |
11 | [142; 158) 'unknow...nction': [unknown] | ||
12 | [142; 160) 'unknow...tion()': i32 | ||
13 | [166; 184) 'S { i3...d: b }': S | ||
14 | [181; 182) 'b': i32 | ||
15 | [194; 195) 'c': f64 | ||
16 | [198; 214) 'unknow...nction': [unknown] | ||
17 | [198; 216) 'unknow...tion()': f64 | ||
18 | [222; 229) '&mut &c': &mut &f64 | ||
19 | [227; 229) '&c': &f64 | ||
20 | [228; 229) 'c': f64 | ||
diff --git a/crates/ra_hir/src/ty/tests/data/0001_basics.txt b/crates/ra_hir/src/ty/tests/data/basics.txt index 212e92e00..8ea244ba8 100644 --- a/crates/ra_hir/src/ty/tests/data/0001_basics.txt +++ b/crates/ra_hir/src/ty/tests/data/basics.txt | |||
@@ -1,13 +1,13 @@ | |||
1 | [9; 10) 'a': u32 | ||
2 | [17; 18) 'b': isize | ||
3 | [27; 28) 'c': ! | ||
1 | [33; 34) 'd': &[unknown] | 4 | [33; 34) 'd': &[unknown] |
2 | [88; 94) '1isize': [unknown] | 5 | [42; 121) '{ ...f32; }': () |
3 | [48; 49) 'a': u32 | 6 | [48; 49) 'a': u32 |
4 | [55; 56) 'b': isize | 7 | [55; 56) 'b': isize |
5 | [112; 118) '1.0f32': [unknown] | ||
6 | [76; 82) '1usize': [unknown] | ||
7 | [9; 10) 'a': u32 | ||
8 | [27; 28) 'c': ! | ||
9 | [62; 63) 'c': ! | 8 | [62; 63) 'c': ! |
10 | [17; 18) 'b': isize | ||
11 | [100; 106) '"test"': [unknown] | ||
12 | [42; 121) '{ ...f32; }': () | ||
13 | [69; 70) 'd': &[unknown] | 9 | [69; 70) 'd': &[unknown] |
10 | [76; 82) '1usize': [unknown] | ||
11 | [88; 94) '1isize': [unknown] | ||
12 | [100; 106) '"test"': [unknown] | ||
13 | [112; 118) '1.0f32': [unknown] | ||
diff --git a/crates/ra_hir/src/ty/tests/data/boolean_op.txt b/crates/ra_hir/src/ty/tests/data/boolean_op.txt new file mode 100644 index 000000000..cce8d68fb --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/boolean_op.txt | |||
@@ -0,0 +1,31 @@ | |||
1 | [6; 7) 'x': [unknown] | ||
2 | [22; 34) '{ 0i32 }': i32 | ||
3 | [28; 32) '0i32': i32 | ||
4 | [46; 237) '{ ... < 3 }': bool | ||
5 | [56; 57) 'x': bool | ||
6 | [60; 61) 'a': bool | ||
7 | [60; 66) 'a && b': bool | ||
8 | [65; 66) 'b': bool | ||
9 | [76; 77) 'y': bool | ||
10 | [80; 84) 'true': bool | ||
11 | [80; 93) 'true || false': bool | ||
12 | [88; 93) 'false': bool | ||
13 | [103; 104) 'z': bool | ||
14 | [107; 108) 'x': bool | ||
15 | [107; 113) 'x == y': bool | ||
16 | [112; 113) 'y': bool | ||
17 | [123; 124) 'h': bool | ||
18 | [127; 134) 'CONST_1': [unknown] | ||
19 | [127; 145) 'CONST_...ONST_2': bool | ||
20 | [138; 145) 'CONST_2': [unknown] | ||
21 | [155; 156) 'c': [unknown] | ||
22 | [159; 172) 'f(z || y) + 5': [unknown] | ||
23 | [182; 183) 'd': [unknown] | ||
24 | [186; 187) 'b': [unknown] | ||
25 | [197; 198) 'e': bool | ||
26 | [201; 205) '3i32': bool | ||
27 | [201; 222) '3i32 &...world"': bool | ||
28 | [209; 222) '"hello world"': bool | ||
29 | [229; 231) '10': [unknown] | ||
30 | [229; 235) '10 < 3': bool | ||
31 | [234; 235) '3': [unknown] | ||
diff --git a/crates/ra_hir/src/ty/tests/data/0002_let.txt b/crates/ra_hir/src/ty/tests/data/let.txt index 916ca25a1..30f4a2cf5 100644 --- a/crates/ra_hir/src/ty/tests/data/0002_let.txt +++ b/crates/ra_hir/src/ty/tests/data/let.txt | |||
@@ -1,7 +1,7 @@ | |||
1 | [21; 22) 'a': [unknown] | ||
2 | [52; 53) '1': usize | ||
3 | [11; 71) '{ ...= b; }': () | 1 | [11; 71) '{ ...= b; }': () |
4 | [63; 64) 'c': usize | 2 | [21; 22) 'a': [unknown] |
5 | [25; 31) '1isize': [unknown] | 3 | [25; 31) '1isize': [unknown] |
6 | [41; 42) 'b': usize | 4 | [41; 42) 'b': usize |
5 | [52; 53) '1': usize | ||
6 | [63; 64) 'c': usize | ||
7 | [67; 68) 'b': usize | 7 | [67; 68) 'b': usize |
diff --git a/crates/ra_hir/src/ty/tests/data/0003_paths.txt b/crates/ra_hir/src/ty/tests/data/paths.txt index 2a12d264f..b22f4d4a5 100644 --- a/crates/ra_hir/src/ty/tests/data/0003_paths.txt +++ b/crates/ra_hir/src/ty/tests/data/paths.txt | |||
@@ -1,9 +1,9 @@ | |||
1 | [15; 20) '{ 1 }': u32 | 1 | [15; 20) '{ 1 }': u32 |
2 | [17; 18) '1': u32 | 2 | [17; 18) '1': u32 |
3 | [50; 51) '1': u32 | ||
4 | [48; 53) '{ 1 }': u32 | 3 | [48; 53) '{ 1 }': u32 |
5 | [82; 88) 'b::c()': u32 | 4 | [50; 51) '1': u32 |
6 | [67; 91) '{ ...c(); }': () | 5 | [67; 91) '{ ...c(); }': () |
7 | [73; 74) 'a': fn() -> u32 | 6 | [73; 74) 'a': fn() -> u32 |
8 | [73; 76) 'a()': u32 | 7 | [73; 76) 'a()': u32 |
9 | [82; 86) 'b::c': fn() -> u32 | 8 | [82; 86) 'b::c': fn() -> u32 |
9 | [82; 88) 'b::c()': u32 | ||
diff --git a/crates/ra_hir/src/ty/tests/data/0005_refs.txt b/crates/ra_hir/src/ty/tests/data/refs_and_ptrs.txt index 296e955c1..afab343ea 100644 --- a/crates/ra_hir/src/ty/tests/data/0005_refs.txt +++ b/crates/ra_hir/src/ty/tests/data/refs_and_ptrs.txt | |||
@@ -1,23 +1,23 @@ | |||
1 | [115; 117) '&b': &&mut u32 | 1 | [9; 10) 'a': &u32 |
2 | [88; 94) '&mut a': &mut &u32 | 2 | [18; 19) 'b': &mut u32 |
3 | [146; 147) 'd': *mut u32 | 3 | [31; 32) 'c': *const u32 |
4 | [145; 147) '*d': u32 | ||
5 | [65; 66) 'a': &u32 | ||
6 | [46; 47) 'd': *mut u32 | 4 | [46; 47) 'd': *mut u32 |
7 | [59; 150) '{ ... *d; }': () | 5 | [59; 150) '{ ... *d; }': () |
8 | [116; 117) 'b': &mut u32 | 6 | [65; 66) 'a': &u32 |
9 | [131; 132) 'c': *const u32 | ||
10 | [130; 132) '*c': u32 | ||
11 | [72; 74) '*a': u32 | 7 | [72; 74) '*a': u32 |
12 | [107; 109) '*b': u32 | 8 | [73; 74) 'a': &u32 |
13 | [108; 109) 'b': &mut u32 | 9 | [80; 82) '&a': &&u32 |
14 | [9; 10) 'a': &u32 | 10 | [81; 82) 'a': &u32 |
15 | [18; 19) 'b': &mut u32 | 11 | [88; 94) '&mut a': &mut &u32 |
16 | [93; 94) 'a': &u32 | 12 | [93; 94) 'a': &u32 |
17 | [100; 101) 'b': &mut u32 | 13 | [100; 101) 'b': &mut u32 |
18 | [81; 82) 'a': &u32 | 14 | [107; 109) '*b': u32 |
19 | [80; 82) '&a': &&u32 | 15 | [108; 109) 'b': &mut u32 |
20 | [73; 74) 'a': &u32 | 16 | [115; 117) '&b': &&mut u32 |
17 | [116; 117) 'b': &mut u32 | ||
21 | [123; 124) 'c': *const u32 | 18 | [123; 124) 'c': *const u32 |
22 | [31; 32) 'c': *const u32 | 19 | [130; 132) '*c': u32 |
20 | [131; 132) 'c': *const u32 | ||
23 | [138; 139) 'd': *mut u32 | 21 | [138; 139) 'd': *mut u32 |
22 | [145; 147) '*d': u32 | ||
23 | [146; 147) 'd': *mut u32 | ||
diff --git a/crates/ra_hir/src/ty/tests/data/0007_self.txt b/crates/ra_hir/src/ty/tests/data/self.txt index db4ba17d0..c38029f97 100644 --- a/crates/ra_hir/src/ty/tests/data/0007_self.txt +++ b/crates/ra_hir/src/ty/tests/data/self.txt | |||
@@ -1,6 +1,6 @@ | |||
1 | [50; 54) 'self': &S | ||
2 | [34; 38) 'self': &S | 1 | [34; 38) 'self': &S |
3 | [40; 61) '{ ... }': () | 2 | [40; 61) '{ ... }': () |
3 | [50; 54) 'self': &S | ||
4 | [75; 79) 'self': &S | ||
4 | [88; 109) '{ ... }': () | 5 | [88; 109) '{ ... }': () |
5 | [98; 102) 'self': &S | 6 | [98; 102) 'self': &S |
6 | [75; 79) 'self': &S | ||
diff --git a/crates/ra_hir/src/ty/tests/data/0004_struct.txt b/crates/ra_hir/src/ty/tests/data/struct.txt index b4af18b87..7b324c82f 100644 --- a/crates/ra_hir/src/ty/tests/data/0004_struct.txt +++ b/crates/ra_hir/src/ty/tests/data/struct.txt | |||
@@ -1,16 +1,16 @@ | |||
1 | [86; 90) 'C(1)': [unknown] | 1 | [72; 154) '{ ...a.c; }': () |
2 | [121; 122) 'B': B | 2 | [82; 83) 'c': [unknown] |
3 | [86; 87) 'C': [unknown] | 3 | [86; 87) 'C': [unknown] |
4 | [129; 130) '1': [unknown] | 4 | [86; 90) 'C(1)': [unknown] |
5 | [88; 89) '1': [unknown] | ||
6 | [96; 97) 'B': [unknown] | ||
5 | [107; 108) 'a': A | 7 | [107; 108) 'a': A |
8 | [114; 133) 'A { b:...C(1) }': A | ||
9 | [121; 122) 'B': B | ||
6 | [127; 128) 'C': [unknown] | 10 | [127; 128) 'C': [unknown] |
11 | [127; 131) 'C(1)': C | ||
12 | [129; 130) '1': [unknown] | ||
13 | [139; 140) 'a': A | ||
7 | [139; 142) 'a.b': B | 14 | [139; 142) 'a.b': B |
8 | [114; 133) 'A { b:...C(1) }': A | ||
9 | [148; 151) 'a.c': C | ||
10 | [148; 149) 'a': A | 15 | [148; 149) 'a': A |
11 | [139; 140) 'a': A | 16 | [148; 151) 'a.c': C |
12 | [72; 154) '{ ...a.c; }': () | ||
13 | [96; 97) 'B': [unknown] | ||
14 | [88; 89) '1': [unknown] | ||
15 | [82; 83) 'c': [unknown] | ||
16 | [127; 131) 'C(1)': C | ||