diff options
Diffstat (limited to 'crates/ra_hir_ty/src/tests/never_type.rs')
-rw-r--r-- | crates/ra_hir_ty/src/tests/never_type.rs | 118 |
1 files changed, 46 insertions, 72 deletions
diff --git a/crates/ra_hir_ty/src/tests/never_type.rs b/crates/ra_hir_ty/src/tests/never_type.rs index ab9a990f5..64d421d40 100644 --- a/crates/ra_hir_ty/src/tests/never_type.rs +++ b/crates/ra_hir_ty/src/tests/never_type.rs | |||
@@ -1,99 +1,91 @@ | |||
1 | use insta::assert_snapshot; | 1 | use insta::assert_snapshot; |
2 | 2 | ||
3 | use super::{infer_with_mismatches, type_at}; | 3 | use super::{check_types, infer_with_mismatches}; |
4 | 4 | ||
5 | #[test] | 5 | #[test] |
6 | fn infer_never1() { | 6 | fn infer_never1() { |
7 | let t = type_at( | 7 | check_types( |
8 | r#" | 8 | r#" |
9 | //- /main.rs | ||
10 | fn test() { | 9 | fn test() { |
11 | let t = return; | 10 | let t = return; |
12 | t<|>; | 11 | t; |
13 | } | 12 | } //^ ! |
14 | "#, | 13 | "#, |
15 | ); | 14 | ); |
16 | assert_eq!(t, "!"); | ||
17 | } | 15 | } |
18 | 16 | ||
19 | #[test] | 17 | #[test] |
20 | fn infer_never2() { | 18 | fn infer_never2() { |
21 | let t = type_at( | 19 | check_types( |
22 | r#" | 20 | r#" |
23 | //- /main.rs | ||
24 | fn gen<T>() -> T { loop {} } | 21 | fn gen<T>() -> T { loop {} } |
25 | 22 | ||
26 | fn test() { | 23 | fn test() { |
27 | let a = gen(); | 24 | let a = gen(); |
28 | if false { a } else { loop {} }; | 25 | if false { a } else { loop {} }; |
29 | a<|>; | 26 | a; |
30 | } | 27 | } //^ ! |
31 | "#, | 28 | "#, |
32 | ); | 29 | ); |
33 | assert_eq!(t, "!"); | ||
34 | } | 30 | } |
35 | 31 | ||
36 | #[test] | 32 | #[test] |
37 | fn infer_never3() { | 33 | fn infer_never3() { |
38 | let t = type_at( | 34 | check_types( |
39 | r#" | 35 | r#" |
40 | //- /main.rs | ||
41 | fn gen<T>() -> T { loop {} } | 36 | fn gen<T>() -> T { loop {} } |
42 | 37 | ||
43 | fn test() { | 38 | fn test() { |
44 | let a = gen(); | 39 | let a = gen(); |
45 | if false { loop {} } else { a }; | 40 | if false { loop {} } else { a }; |
46 | a<|>; | 41 | a; |
42 | //^ ! | ||
47 | } | 43 | } |
48 | "#, | 44 | "#, |
49 | ); | 45 | ); |
50 | assert_eq!(t, "!"); | ||
51 | } | 46 | } |
52 | 47 | ||
53 | #[test] | 48 | #[test] |
54 | fn never_type_in_generic_args() { | 49 | fn never_type_in_generic_args() { |
55 | let t = type_at( | 50 | check_types( |
56 | r#" | 51 | r#" |
57 | //- /main.rs | ||
58 | enum Option<T> { None, Some(T) } | 52 | enum Option<T> { None, Some(T) } |
59 | 53 | ||
60 | fn test() { | 54 | fn test() { |
61 | let a = if true { Option::None } else { Option::Some(return) }; | 55 | let a = if true { Option::None } else { Option::Some(return) }; |
62 | a<|>; | 56 | a; |
63 | } | 57 | } //^ Option<!> |
64 | "#, | 58 | "#, |
65 | ); | 59 | ); |
66 | assert_eq!(t, "Option<!>"); | ||
67 | } | 60 | } |
68 | 61 | ||
69 | #[test] | 62 | #[test] |
70 | fn never_type_can_be_reinferred1() { | 63 | fn never_type_can_be_reinferred1() { |
71 | let t = type_at( | 64 | check_types( |
72 | r#" | 65 | r#" |
73 | //- /main.rs | ||
74 | fn gen<T>() -> T { loop {} } | 66 | fn gen<T>() -> T { loop {} } |
75 | 67 | ||
76 | fn test() { | 68 | fn test() { |
77 | let a = gen(); | 69 | let a = gen(); |
78 | if false { loop {} } else { a }; | 70 | if false { loop {} } else { a }; |
79 | a<|>; | 71 | a; |
72 | //^ () | ||
80 | if false { a }; | 73 | if false { a }; |
81 | } | 74 | } |
82 | "#, | 75 | "#, |
83 | ); | 76 | ); |
84 | assert_eq!(t, "()"); | ||
85 | } | 77 | } |
86 | 78 | ||
87 | #[test] | 79 | #[test] |
88 | fn never_type_can_be_reinferred2() { | 80 | fn never_type_can_be_reinferred2() { |
89 | let t = type_at( | 81 | check_types( |
90 | r#" | 82 | r#" |
91 | //- /main.rs | ||
92 | enum Option<T> { None, Some(T) } | 83 | enum Option<T> { None, Some(T) } |
93 | 84 | ||
94 | fn test() { | 85 | fn test() { |
95 | let a = if true { Option::None } else { Option::Some(return) }; | 86 | let a = if true { Option::None } else { Option::Some(return) }; |
96 | a<|>; | 87 | a; |
88 | //^ Option<i32> | ||
97 | match 42 { | 89 | match 42 { |
98 | 42 => a, | 90 | 42 => a, |
99 | _ => Option::Some(42), | 91 | _ => Option::Some(42), |
@@ -101,19 +93,18 @@ fn test() { | |||
101 | } | 93 | } |
102 | "#, | 94 | "#, |
103 | ); | 95 | ); |
104 | assert_eq!(t, "Option<i32>"); | ||
105 | } | 96 | } |
106 | 97 | ||
107 | #[test] | 98 | #[test] |
108 | fn never_type_can_be_reinferred3() { | 99 | fn never_type_can_be_reinferred3() { |
109 | let t = type_at( | 100 | check_types( |
110 | r#" | 101 | r#" |
111 | //- /main.rs | ||
112 | enum Option<T> { None, Some(T) } | 102 | enum Option<T> { None, Some(T) } |
113 | 103 | ||
114 | fn test() { | 104 | fn test() { |
115 | let a = if true { Option::None } else { Option::Some(return) }; | 105 | let a = if true { Option::None } else { Option::Some(return) }; |
116 | a<|>; | 106 | a; |
107 | //^ Option<&str> | ||
117 | match 42 { | 108 | match 42 { |
118 | 42 => a, | 109 | 42 => a, |
119 | _ => Option::Some("str"), | 110 | _ => Option::Some("str"), |
@@ -121,82 +112,72 @@ fn test() { | |||
121 | } | 112 | } |
122 | "#, | 113 | "#, |
123 | ); | 114 | ); |
124 | assert_eq!(t, "Option<&str>"); | ||
125 | } | 115 | } |
126 | 116 | ||
127 | #[test] | 117 | #[test] |
128 | fn match_no_arm() { | 118 | fn match_no_arm() { |
129 | let t = type_at( | 119 | check_types( |
130 | r#" | 120 | r#" |
131 | //- /main.rs | ||
132 | enum Void {} | 121 | enum Void {} |
133 | 122 | ||
134 | fn test(a: Void) { | 123 | fn test(a: Void) { |
135 | let t = match a {}; | 124 | let t = match a {}; |
136 | t<|>; | 125 | t; |
137 | } | 126 | } //^ ! |
138 | "#, | 127 | "#, |
139 | ); | 128 | ); |
140 | assert_eq!(t, "!"); | ||
141 | } | 129 | } |
142 | 130 | ||
143 | #[test] | 131 | #[test] |
144 | fn match_unknown_arm() { | 132 | fn match_unknown_arm() { |
145 | let t = type_at( | 133 | check_types( |
146 | r#" | 134 | r#" |
147 | //- /main.rs | ||
148 | fn test(a: Option) { | 135 | fn test(a: Option) { |
149 | let t = match 0 { | 136 | let t = match 0 { |
150 | _ => unknown, | 137 | _ => unknown, |
151 | }; | 138 | }; |
152 | t<|>; | 139 | t; |
153 | } | 140 | } //^ {unknown} |
154 | "#, | 141 | "#, |
155 | ); | 142 | ); |
156 | assert_eq!(t, "{unknown}"); | ||
157 | } | 143 | } |
158 | 144 | ||
159 | #[test] | 145 | #[test] |
160 | fn if_never() { | 146 | fn if_never() { |
161 | let t = type_at( | 147 | check_types( |
162 | r#" | 148 | r#" |
163 | //- /main.rs | ||
164 | fn test() { | 149 | fn test() { |
165 | let i = if true { | 150 | let i = if true { |
166 | loop {} | 151 | loop {} |
167 | } else { | 152 | } else { |
168 | 3.0 | 153 | 3.0 |
169 | }; | 154 | }; |
170 | i<|>; | 155 | i; |
171 | } | 156 | } //^ f64 |
172 | "#, | 157 | "#, |
173 | ); | 158 | ); |
174 | assert_eq!(t, "f64"); | ||
175 | } | 159 | } |
176 | 160 | ||
177 | #[test] | 161 | #[test] |
178 | fn if_else_never() { | 162 | fn if_else_never() { |
179 | let t = type_at( | 163 | check_types( |
180 | r#" | 164 | r#" |
181 | //- /main.rs | ||
182 | fn test(input: bool) { | 165 | fn test(input: bool) { |
183 | let i = if input { | 166 | let i = if input { |
184 | 2.0 | 167 | 2.0 |
185 | } else { | 168 | } else { |
186 | return | 169 | return |
187 | }; | 170 | }; |
188 | i<|>; | 171 | i; |
189 | } | 172 | } //^ f64 |
190 | "#, | 173 | "#, |
191 | ); | 174 | ); |
192 | assert_eq!(t, "f64"); | ||
193 | } | 175 | } |
194 | 176 | ||
195 | #[test] | 177 | #[test] |
196 | fn match_first_arm_never() { | 178 | fn match_first_arm_never() { |
197 | let t = type_at( | 179 | check_types( |
198 | r#" | 180 | r#" |
199 | //- /main.rs | ||
200 | fn test(a: i32) { | 181 | fn test(a: i32) { |
201 | let i = match a { | 182 | let i = match a { |
202 | 1 => return, | 183 | 1 => return, |
@@ -204,18 +185,16 @@ fn test(a: i32) { | |||
204 | 3 => loop {}, | 185 | 3 => loop {}, |
205 | _ => 3.0, | 186 | _ => 3.0, |
206 | }; | 187 | }; |
207 | i<|>; | 188 | i; |
208 | } | 189 | } //^ f64 |
209 | "#, | 190 | "#, |
210 | ); | 191 | ); |
211 | assert_eq!(t, "f64"); | ||
212 | } | 192 | } |
213 | 193 | ||
214 | #[test] | 194 | #[test] |
215 | fn match_second_arm_never() { | 195 | fn match_second_arm_never() { |
216 | let t = type_at( | 196 | check_types( |
217 | r#" | 197 | r#" |
218 | //- /main.rs | ||
219 | fn test(a: i32) { | 198 | fn test(a: i32) { |
220 | let i = match a { | 199 | let i = match a { |
221 | 1 => 3.0, | 200 | 1 => 3.0, |
@@ -223,45 +202,40 @@ fn test(a: i32) { | |||
223 | 3 => 3.0, | 202 | 3 => 3.0, |
224 | _ => return, | 203 | _ => return, |
225 | }; | 204 | }; |
226 | i<|>; | 205 | i; |
227 | } | 206 | } //^ f64 |
228 | "#, | 207 | "#, |
229 | ); | 208 | ); |
230 | assert_eq!(t, "f64"); | ||
231 | } | 209 | } |
232 | 210 | ||
233 | #[test] | 211 | #[test] |
234 | fn match_all_arms_never() { | 212 | fn match_all_arms_never() { |
235 | let t = type_at( | 213 | check_types( |
236 | r#" | 214 | r#" |
237 | //- /main.rs | ||
238 | fn test(a: i32) { | 215 | fn test(a: i32) { |
239 | let i = match a { | 216 | let i = match a { |
240 | 2 => return, | 217 | 2 => return, |
241 | _ => loop {}, | 218 | _ => loop {}, |
242 | }; | 219 | }; |
243 | i<|>; | 220 | i; |
244 | } | 221 | } //^ ! |
245 | "#, | 222 | "#, |
246 | ); | 223 | ); |
247 | assert_eq!(t, "!"); | ||
248 | } | 224 | } |
249 | 225 | ||
250 | #[test] | 226 | #[test] |
251 | fn match_no_never_arms() { | 227 | fn match_no_never_arms() { |
252 | let t = type_at( | 228 | check_types( |
253 | r#" | 229 | r#" |
254 | //- /main.rs | ||
255 | fn test(a: i32) { | 230 | fn test(a: i32) { |
256 | let i = match a { | 231 | let i = match a { |
257 | 2 => 2.0, | 232 | 2 => 2.0, |
258 | _ => 3.0, | 233 | _ => 3.0, |
259 | }; | 234 | }; |
260 | i<|>; | 235 | i; |
261 | } | 236 | } //^ f64 |
262 | "#, | 237 | "#, |
263 | ); | 238 | ); |
264 | assert_eq!(t, "f64"); | ||
265 | } | 239 | } |
266 | 240 | ||
267 | #[test] | 241 | #[test] |