diff options
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/primitive.rs | 50 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 30 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/basics.txt | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/binary_op.txt | 86 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/let.txt | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/literals.txt | 12 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/struct.txt | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/tuple.txt | 18 |
8 files changed, 149 insertions, 63 deletions
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs index 498d42d52..5741ca90d 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs | |||
@@ -2,6 +2,56 @@ use std::fmt; | |||
2 | 2 | ||
3 | use crate::{Name, KnownName}; | 3 | use crate::{Name, KnownName}; |
4 | 4 | ||
5 | #[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] | ||
6 | pub enum UncertainIntTy { | ||
7 | Unknown, | ||
8 | Unsigned(UintTy), | ||
9 | Signed(IntTy), | ||
10 | } | ||
11 | |||
12 | impl UncertainIntTy { | ||
13 | pub fn ty_to_string(&self) -> &'static str { | ||
14 | match *self { | ||
15 | UncertainIntTy::Unknown => "{integer}", | ||
16 | UncertainIntTy::Signed(ty) => ty.ty_to_string(), | ||
17 | UncertainIntTy::Unsigned(ty) => ty.ty_to_string(), | ||
18 | } | ||
19 | } | ||
20 | |||
21 | pub fn from_name(name: &Name) -> Option<UncertainIntTy> { | ||
22 | if let Some(ty) = IntTy::from_name(name) { | ||
23 | Some(UncertainIntTy::Signed(ty)) | ||
24 | } else if let Some(ty) = UintTy::from_name(name) { | ||
25 | Some(UncertainIntTy::Unsigned(ty)) | ||
26 | } else { | ||
27 | None | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | #[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] | ||
33 | pub enum UncertainFloatTy { | ||
34 | Unknown, | ||
35 | Known(FloatTy), | ||
36 | } | ||
37 | |||
38 | impl UncertainFloatTy { | ||
39 | pub fn ty_to_string(&self) -> &'static str { | ||
40 | match *self { | ||
41 | UncertainFloatTy::Unknown => "{float}", | ||
42 | UncertainFloatTy::Known(ty) => ty.ty_to_string(), | ||
43 | } | ||
44 | } | ||
45 | |||
46 | pub fn from_name(name: &Name) -> Option<UncertainFloatTy> { | ||
47 | if let Some(ty) = FloatTy::from_name(name) { | ||
48 | Some(UncertainFloatTy::Known(ty)) | ||
49 | } else { | ||
50 | None | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
5 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] | 55 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] |
6 | pub enum IntTy { | 56 | pub enum IntTy { |
7 | Isize, | 57 | Isize, |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 920188fc9..8aacb1a7f 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -133,6 +133,32 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { | |||
133 | } | 133 | } |
134 | 134 | ||
135 | #[test] | 135 | #[test] |
136 | fn infer_literals() { | ||
137 | check_inference( | ||
138 | r##" | ||
139 | fn test() { | ||
140 | 5i32; | ||
141 | "hello"; | ||
142 | b"bytes"; | ||
143 | 'c'; | ||
144 | b'b'; | ||
145 | 3.14; | ||
146 | 5000; | ||
147 | false; | ||
148 | true; | ||
149 | r#" | ||
150 | //! doc | ||
151 | // non-doc | ||
152 | mod foo {} | ||
153 | "#; | ||
154 | br#"yolo"#; | ||
155 | } | ||
156 | "##, | ||
157 | "literals.txt", | ||
158 | ); | ||
159 | } | ||
160 | |||
161 | #[test] | ||
136 | fn infer_backwards() { | 162 | fn infer_backwards() { |
137 | check_inference( | 163 | check_inference( |
138 | r#" | 164 | r#" |
@@ -180,7 +206,7 @@ fn f(x: bool) -> i32 { | |||
180 | 0i32 | 206 | 0i32 |
181 | } | 207 | } |
182 | 208 | ||
183 | fn test() { | 209 | fn test() -> bool { |
184 | let x = a && b; | 210 | let x = a && b; |
185 | let y = true || false; | 211 | let y = true || false; |
186 | let z = x == y; | 212 | let z = x == y; |
@@ -277,8 +303,6 @@ fn test(x: &str, y: isize) { | |||
277 | let b = (a, x); | 303 | let b = (a, x); |
278 | let c = (y, x); | 304 | let c = (y, x); |
279 | let d = (c, x); | 305 | let d = (c, x); |
280 | |||
281 | // we have not infered these case yet. | ||
282 | let e = (1, "e"); | 306 | let e = (1, "e"); |
283 | let f = (e, "d"); | 307 | let f = (e, "d"); |
284 | } | 308 | } |
diff --git a/crates/ra_hir/src/ty/tests/data/basics.txt b/crates/ra_hir/src/ty/tests/data/basics.txt index ac7faae0a..e65fe07aa 100644 --- a/crates/ra_hir/src/ty/tests/data/basics.txt +++ b/crates/ra_hir/src/ty/tests/data/basics.txt | |||
@@ -7,7 +7,7 @@ | |||
7 | [55; 56) 'b': isize | 7 | [55; 56) 'b': isize |
8 | [62; 63) 'c': ! | 8 | [62; 63) 'c': ! |
9 | [69; 70) 'd': &str | 9 | [69; 70) 'd': &str |
10 | [76; 82) '1usize': [unknown] | 10 | [76; 82) '1usize': usize |
11 | [88; 94) '1isize': [unknown] | 11 | [88; 94) '1isize': isize |
12 | [100; 106) '"test"': [unknown] | 12 | [100; 106) '"test"': &str |
13 | [112; 118) '1.0f32': [unknown] | 13 | [112; 118) '1.0f32': f32 |
diff --git a/crates/ra_hir/src/ty/tests/data/binary_op.txt b/crates/ra_hir/src/ty/tests/data/binary_op.txt index 8a515ac5e..58a727691 100644 --- a/crates/ra_hir/src/ty/tests/data/binary_op.txt +++ b/crates/ra_hir/src/ty/tests/data/binary_op.txt | |||
@@ -1,46 +1,46 @@ | |||
1 | [6; 7) 'x': bool | 1 | [6; 7) 'x': bool |
2 | [22; 34) '{ 0i32 }': i32 | 2 | [22; 34) '{ 0i32 }': i32 |
3 | [28; 32) '0i32': i32 | 3 | [28; 32) '0i32': i32 |
4 | [46; 342) '{ ... < 3 }': bool | 4 | [54; 350) '{ ... < 3 }': bool |
5 | [56; 57) 'x': bool | 5 | [64; 65) 'x': bool |
6 | [60; 61) 'a': bool | 6 | [68; 69) 'a': bool |
7 | [60; 66) 'a && b': bool | 7 | [68; 74) 'a && b': bool |
8 | [65; 66) 'b': bool | 8 | [73; 74) 'b': bool |
9 | [76; 77) 'y': bool | 9 | [84; 85) 'y': bool |
10 | [80; 84) 'true': bool | 10 | [88; 92) 'true': bool |
11 | [80; 93) 'true || false': bool | 11 | [88; 101) 'true || false': bool |
12 | [88; 93) 'false': bool | 12 | [96; 101) 'false': bool |
13 | [103; 104) 'z': bool | 13 | [111; 112) 'z': bool |
14 | [107; 108) 'x': bool | 14 | [115; 116) 'x': bool |
15 | [107; 113) 'x == y': bool | 15 | [115; 121) 'x == y': bool |
16 | [112; 113) 'y': bool | 16 | [120; 121) 'y': bool |
17 | [123; 134) 'minus_forty': isize | 17 | [131; 142) 'minus_forty': isize |
18 | [144; 152) '-40isize': isize | 18 | [152; 160) '-40isize': isize |
19 | [145; 152) '40isize': [unknown] | 19 | [153; 160) '40isize': isize |
20 | [162; 163) 'h': bool | 20 | [170; 171) 'h': bool |
21 | [166; 177) 'minus_forty': isize | 21 | [174; 185) 'minus_forty': isize |
22 | [166; 188) 'minus_...ONST_2': bool | 22 | [174; 196) 'minus_...ONST_2': bool |
23 | [181; 188) 'CONST_2': isize | 23 | [189; 196) 'CONST_2': isize |
24 | [198; 199) 'c': i32 | 24 | [206; 207) 'c': i32 |
25 | [202; 203) 'f': fn(bool) -> i32 | 25 | [210; 211) 'f': fn(bool) -> i32 |
26 | [202; 211) 'f(z || y)': i32 | 26 | [210; 219) 'f(z || y)': i32 |
27 | [202; 215) 'f(z || y) + 5': i32 | 27 | [210; 223) 'f(z || y) + 5': i32 |
28 | [204; 205) 'z': bool | 28 | [212; 213) 'z': bool |
29 | [204; 210) 'z || y': bool | 29 | [212; 218) 'z || y': bool |
30 | [209; 210) 'y': bool | 30 | [217; 218) 'y': bool |
31 | [214; 215) '5': i32 | 31 | [222; 223) '5': i32 |
32 | [225; 226) 'd': [unknown] | 32 | [233; 234) 'd': [unknown] |
33 | [229; 230) 'b': [unknown] | 33 | [237; 238) 'b': [unknown] |
34 | [240; 241) 'g': () | 34 | [248; 249) 'g': () |
35 | [244; 255) 'minus_forty': isize | 35 | [252; 263) 'minus_forty': isize |
36 | [244; 260) 'minus_...y ^= i': () | 36 | [252; 268) 'minus_...y ^= i': () |
37 | [259; 260) 'i': isize | 37 | [267; 268) 'i': isize |
38 | [270; 273) 'ten': usize | 38 | [278; 281) 'ten': usize |
39 | [283; 285) '10': usize | 39 | [291; 293) '10': usize |
40 | [295; 308) 'ten_is_eleven': bool | 40 | [303; 316) 'ten_is_eleven': bool |
41 | [311; 314) 'ten': usize | 41 | [319; 322) 'ten': usize |
42 | [311; 326) 'ten == some_num': bool | 42 | [319; 334) 'ten == some_num': bool |
43 | [318; 326) 'some_num': usize | 43 | [326; 334) 'some_num': usize |
44 | [333; 336) 'ten': usize | 44 | [341; 344) 'ten': usize |
45 | [333; 340) 'ten < 3': bool | 45 | [341; 348) 'ten < 3': bool |
46 | [339; 340) '3': usize | 46 | [347; 348) '3': usize |
diff --git a/crates/ra_hir/src/ty/tests/data/let.txt b/crates/ra_hir/src/ty/tests/data/let.txt index 30f4a2cf5..8815dba41 100644 --- a/crates/ra_hir/src/ty/tests/data/let.txt +++ b/crates/ra_hir/src/ty/tests/data/let.txt | |||
@@ -1,6 +1,6 @@ | |||
1 | [11; 71) '{ ...= b; }': () | 1 | [11; 71) '{ ...= b; }': () |
2 | [21; 22) 'a': [unknown] | 2 | [21; 22) 'a': isize |
3 | [25; 31) '1isize': [unknown] | 3 | [25; 31) '1isize': isize |
4 | [41; 42) 'b': usize | 4 | [41; 42) 'b': usize |
5 | [52; 53) '1': usize | 5 | [52; 53) '1': usize |
6 | [63; 64) 'c': usize | 6 | [63; 64) 'c': usize |
diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt new file mode 100644 index 000000000..84ee2c11b --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/literals.txt | |||
@@ -0,0 +1,12 @@ | |||
1 | [11; 201) '{ ...o"#; }': () | ||
2 | [17; 21) '5i32': i32 | ||
3 | [27; 34) '"hello"': &str | ||
4 | [40; 48) 'b"bytes"': &[u8] | ||
5 | [54; 57) ''c'': char | ||
6 | [63; 67) 'b'b'': u8 | ||
7 | [73; 77) '3.14': f64 | ||
8 | [83; 87) '5000': i32 | ||
9 | [93; 98) 'false': bool | ||
10 | [104; 108) 'true': bool | ||
11 | [114; 182) 'r#" ... "#': &str | ||
12 | [188; 198) 'br#"yolo"#': &[u8] | ||
diff --git a/crates/ra_hir/src/ty/tests/data/struct.txt b/crates/ra_hir/src/ty/tests/data/struct.txt index 7b324c82f..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': [unknown] | 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': [unknown] | 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 |
diff --git a/crates/ra_hir/src/ty/tests/data/tuple.txt b/crates/ra_hir/src/ty/tests/data/tuple.txt index 96169180d..a95d3c286 100644 --- a/crates/ra_hir/src/ty/tests/data/tuple.txt +++ b/crates/ra_hir/src/ty/tests/data/tuple.txt | |||
@@ -1,6 +1,6 @@ | |||
1 | [9; 10) 'x': &str | 1 | [9; 10) 'x': &str |
2 | [18; 19) 'y': isize | 2 | [18; 19) 'y': isize |
3 | [28; 214) '{ ...d"); }': () | 3 | [28; 170) '{ ...d"); }': () |
4 | [38; 39) 'a': (u32, &str) | 4 | [38; 39) 'a': (u32, &str) |
5 | [55; 63) '(1, "a")': (u32, &str) | 5 | [55; 63) '(1, "a")': (u32, &str) |
6 | [56; 57) '1': u32 | 6 | [56; 57) '1': u32 |
@@ -17,11 +17,11 @@ | |||
17 | [117; 123) '(c, x)': ((isize, &str), &str) | 17 | [117; 123) '(c, x)': ((isize, &str), &str) |
18 | [118; 119) 'c': (isize, &str) | 18 | [118; 119) 'c': (isize, &str) |
19 | [121; 122) 'x': &str | 19 | [121; 122) 'x': &str |
20 | [177; 178) 'e': ([unknown], [unknown]) | 20 | [133; 134) 'e': (i32, &str) |
21 | [181; 189) '(1, "e")': ([unknown], [unknown]) | 21 | [137; 145) '(1, "e")': (i32, &str) |
22 | [182; 183) '1': [unknown] | 22 | [138; 139) '1': i32 |
23 | [185; 188) '"e"': [unknown] | 23 | [141; 144) '"e"': &str |
24 | [199; 200) 'f': (([unknown], [unknown]), [unknown]) | 24 | [155; 156) 'f': ((i32, &str), &str) |
25 | [203; 211) '(e, "d")': (([unknown], [unknown]), [unknown]) | 25 | [159; 167) '(e, "d")': ((i32, &str), &str) |
26 | [204; 205) 'e': ([unknown], [unknown]) | 26 | [160; 161) 'e': (i32, &str) |
27 | [207; 210) '"d"': [unknown] | 27 | [163; 166) '"d"': &str |