diff options
author | Aleksey Kladov <[email protected]> | 2019-11-27 14:46:02 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-27 18:16:00 +0000 |
commit | a87579500a2c35597071efd0ad6983927f0c1815 (patch) | |
tree | 9805b3dcbf8d767b2fc0623f42794068f3660d44 /crates/ra_hir/src/ty/tests/coercion.rs | |
parent | 368653081558ab389c6543d6b5027859e26beb3b (diff) |
Move Ty
Diffstat (limited to 'crates/ra_hir/src/ty/tests/coercion.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests/coercion.rs | 369 |
1 files changed, 0 insertions, 369 deletions
diff --git a/crates/ra_hir/src/ty/tests/coercion.rs b/crates/ra_hir/src/ty/tests/coercion.rs deleted file mode 100644 index 1530fcc63..000000000 --- a/crates/ra_hir/src/ty/tests/coercion.rs +++ /dev/null | |||
@@ -1,369 +0,0 @@ | |||
1 | use insta::assert_snapshot; | ||
2 | use test_utils::covers; | ||
3 | |||
4 | // Infer with some common definitions and impls. | ||
5 | fn infer(source: &str) -> String { | ||
6 | let defs = r#" | ||
7 | #[lang = "sized"] | ||
8 | pub trait Sized {} | ||
9 | #[lang = "unsize"] | ||
10 | pub trait Unsize<T: ?Sized> {} | ||
11 | #[lang = "coerce_unsized"] | ||
12 | pub trait CoerceUnsized<T> {} | ||
13 | |||
14 | impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {} | ||
15 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {} | ||
16 | "#; | ||
17 | |||
18 | // Append to the end to keep positions unchanged. | ||
19 | super::infer(&format!("{}{}", source, defs)) | ||
20 | } | ||
21 | |||
22 | #[test] | ||
23 | fn infer_block_expr_type_mismatch() { | ||
24 | assert_snapshot!( | ||
25 | infer(r#" | ||
26 | fn test() { | ||
27 | let a: i32 = { 1i64 }; | ||
28 | } | ||
29 | "#), | ||
30 | @r###" | ||
31 | [11; 41) '{ ...4 }; }': () | ||
32 | [21; 22) 'a': i32 | ||
33 | [30; 38) '{ 1i64 }': i64 | ||
34 | [32; 36) '1i64': i64 | ||
35 | "###); | ||
36 | } | ||
37 | |||
38 | #[test] | ||
39 | fn coerce_places() { | ||
40 | assert_snapshot!( | ||
41 | infer(r#" | ||
42 | struct S<T> { a: T } | ||
43 | |||
44 | fn f<T>(_: &[T]) -> T { loop {} } | ||
45 | fn g<T>(_: S<&[T]>) -> T { loop {} } | ||
46 | |||
47 | fn gen<T>() -> *mut [T; 2] { loop {} } | ||
48 | fn test1<U>() -> *mut [U] { | ||
49 | gen() | ||
50 | } | ||
51 | |||
52 | fn test2() { | ||
53 | let arr: &[u8; 1] = &[1]; | ||
54 | |||
55 | let a: &[_] = arr; | ||
56 | let b = f(arr); | ||
57 | let c: &[_] = { arr }; | ||
58 | let d = g(S { a: arr }); | ||
59 | let e: [&[_]; 1] = [arr]; | ||
60 | let f: [&[_]; 2] = [arr; 2]; | ||
61 | let g: (&[_], &[_]) = (arr, arr); | ||
62 | } | ||
63 | "#), | ||
64 | @r###" | ||
65 | [31; 32) '_': &[T] | ||
66 | [45; 56) '{ loop {} }': T | ||
67 | [47; 54) 'loop {}': ! | ||
68 | [52; 54) '{}': () | ||
69 | [65; 66) '_': S<&[T]> | ||
70 | [82; 93) '{ loop {} }': T | ||
71 | [84; 91) 'loop {}': ! | ||
72 | [89; 91) '{}': () | ||
73 | [122; 133) '{ loop {} }': *mut [T;_] | ||
74 | [124; 131) 'loop {}': ! | ||
75 | [129; 131) '{}': () | ||
76 | [160; 173) '{ gen() }': *mut [U] | ||
77 | [166; 169) 'gen': fn gen<U>() -> *mut [T;_] | ||
78 | [166; 171) 'gen()': *mut [U;_] | ||
79 | [186; 420) '{ ...rr); }': () | ||
80 | [196; 199) 'arr': &[u8;_] | ||
81 | [212; 216) '&[1]': &[u8;_] | ||
82 | [213; 216) '[1]': [u8;_] | ||
83 | [214; 215) '1': u8 | ||
84 | [227; 228) 'a': &[u8] | ||
85 | [237; 240) 'arr': &[u8;_] | ||
86 | [250; 251) 'b': u8 | ||
87 | [254; 255) 'f': fn f<u8>(&[T]) -> T | ||
88 | [254; 260) 'f(arr)': u8 | ||
89 | [256; 259) 'arr': &[u8;_] | ||
90 | [270; 271) 'c': &[u8] | ||
91 | [280; 287) '{ arr }': &[u8] | ||
92 | [282; 285) 'arr': &[u8;_] | ||
93 | [297; 298) 'd': u8 | ||
94 | [301; 302) 'g': fn g<u8>(S<&[T]>) -> T | ||
95 | [301; 316) 'g(S { a: arr })': u8 | ||
96 | [303; 315) 'S { a: arr }': S<&[u8]> | ||
97 | [310; 313) 'arr': &[u8;_] | ||
98 | [326; 327) 'e': [&[u8];_] | ||
99 | [341; 346) '[arr]': [&[u8];_] | ||
100 | [342; 345) 'arr': &[u8;_] | ||
101 | [356; 357) 'f': [&[u8];_] | ||
102 | [371; 379) '[arr; 2]': [&[u8];_] | ||
103 | [372; 375) 'arr': &[u8;_] | ||
104 | [377; 378) '2': usize | ||
105 | [389; 390) 'g': (&[u8], &[u8]) | ||
106 | [407; 417) '(arr, arr)': (&[u8], &[u8]) | ||
107 | [408; 411) 'arr': &[u8;_] | ||
108 | [413; 416) 'arr': &[u8;_] | ||
109 | "### | ||
110 | ); | ||
111 | } | ||
112 | |||
113 | #[test] | ||
114 | fn infer_let_stmt_coerce() { | ||
115 | assert_snapshot!( | ||
116 | infer(r#" | ||
117 | fn test() { | ||
118 | let x: &[i32] = &[1]; | ||
119 | } | ||
120 | "#), | ||
121 | @r###" | ||
122 | [11; 40) '{ ...[1]; }': () | ||
123 | [21; 22) 'x': &[i32] | ||
124 | [33; 37) '&[1]': &[i32;_] | ||
125 | [34; 37) '[1]': [i32;_] | ||
126 | [35; 36) '1': i32 | ||
127 | "###); | ||
128 | } | ||
129 | |||
130 | #[test] | ||
131 | fn infer_custom_coerce_unsized() { | ||
132 | assert_snapshot!( | ||
133 | infer(r#" | ||
134 | struct A<T: ?Sized>(*const T); | ||
135 | struct B<T: ?Sized>(*const T); | ||
136 | struct C<T: ?Sized> { inner: *const T } | ||
137 | |||
138 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<B<U>> for B<T> {} | ||
139 | impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<C<U>> for C<T> {} | ||
140 | |||
141 | fn foo1<T>(x: A<[T]>) -> A<[T]> { x } | ||
142 | fn foo2<T>(x: B<[T]>) -> B<[T]> { x } | ||
143 | fn foo3<T>(x: C<[T]>) -> C<[T]> { x } | ||
144 | |||
145 | fn test(a: A<[u8; 2]>, b: B<[u8; 2]>, c: C<[u8; 2]>) { | ||
146 | let d = foo1(a); | ||
147 | let e = foo2(b); | ||
148 | let f = foo3(c); | ||
149 | } | ||
150 | "#), | ||
151 | @r###" | ||
152 | [258; 259) 'x': A<[T]> | ||
153 | [279; 284) '{ x }': A<[T]> | ||
154 | [281; 282) 'x': A<[T]> | ||
155 | [296; 297) 'x': B<[T]> | ||
156 | [317; 322) '{ x }': B<[T]> | ||
157 | [319; 320) 'x': B<[T]> | ||
158 | [334; 335) 'x': C<[T]> | ||
159 | [355; 360) '{ x }': C<[T]> | ||
160 | [357; 358) 'x': C<[T]> | ||
161 | [370; 371) 'a': A<[u8;_]> | ||
162 | [385; 386) 'b': B<[u8;_]> | ||
163 | [400; 401) 'c': C<[u8;_]> | ||
164 | [415; 481) '{ ...(c); }': () | ||
165 | [425; 426) 'd': A<[{unknown}]> | ||
166 | [429; 433) 'foo1': fn foo1<{unknown}>(A<[T]>) -> A<[T]> | ||
167 | [429; 436) 'foo1(a)': A<[{unknown}]> | ||
168 | [434; 435) 'a': A<[u8;_]> | ||
169 | [446; 447) 'e': B<[u8]> | ||
170 | [450; 454) 'foo2': fn foo2<u8>(B<[T]>) -> B<[T]> | ||
171 | [450; 457) 'foo2(b)': B<[u8]> | ||
172 | [455; 456) 'b': B<[u8;_]> | ||
173 | [467; 468) 'f': C<[u8]> | ||
174 | [471; 475) 'foo3': fn foo3<u8>(C<[T]>) -> C<[T]> | ||
175 | [471; 478) 'foo3(c)': C<[u8]> | ||
176 | [476; 477) 'c': C<[u8;_]> | ||
177 | "### | ||
178 | ); | ||
179 | } | ||
180 | |||
181 | #[test] | ||
182 | fn infer_if_coerce() { | ||
183 | assert_snapshot!( | ||
184 | infer(r#" | ||
185 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | ||
186 | fn test() { | ||
187 | let x = if true { | ||
188 | foo(&[1]) | ||
189 | } else { | ||
190 | &[1] | ||
191 | }; | ||
192 | } | ||
193 | "#), | ||
194 | @r###" | ||
195 | [11; 12) 'x': &[T] | ||
196 | [28; 39) '{ loop {} }': &[T] | ||
197 | [30; 37) 'loop {}': ! | ||
198 | [35; 37) '{}': () | ||
199 | [50; 126) '{ ... }; }': () | ||
200 | [60; 61) 'x': &[i32] | ||
201 | [64; 123) 'if tru... }': &[i32] | ||
202 | [67; 71) 'true': bool | ||
203 | [72; 97) '{ ... }': &[i32] | ||
204 | [82; 85) 'foo': fn foo<i32>(&[T]) -> &[T] | ||
205 | [82; 91) 'foo(&[1])': &[i32] | ||
206 | [86; 90) '&[1]': &[i32;_] | ||
207 | [87; 90) '[1]': [i32;_] | ||
208 | [88; 89) '1': i32 | ||
209 | [103; 123) '{ ... }': &[i32;_] | ||
210 | [113; 117) '&[1]': &[i32;_] | ||
211 | [114; 117) '[1]': [i32;_] | ||
212 | [115; 116) '1': i32 | ||
213 | "### | ||
214 | ); | ||
215 | } | ||
216 | |||
217 | #[test] | ||
218 | fn infer_if_else_coerce() { | ||
219 | assert_snapshot!( | ||
220 | infer(r#" | ||
221 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | ||
222 | fn test() { | ||
223 | let x = if true { | ||
224 | &[1] | ||
225 | } else { | ||
226 | foo(&[1]) | ||
227 | }; | ||
228 | } | ||
229 | "#), | ||
230 | @r###" | ||
231 | [11; 12) 'x': &[T] | ||
232 | [28; 39) '{ loop {} }': &[T] | ||
233 | [30; 37) 'loop {}': ! | ||
234 | [35; 37) '{}': () | ||
235 | [50; 126) '{ ... }; }': () | ||
236 | [60; 61) 'x': &[i32] | ||
237 | [64; 123) 'if tru... }': &[i32] | ||
238 | [67; 71) 'true': bool | ||
239 | [72; 92) '{ ... }': &[i32;_] | ||
240 | [82; 86) '&[1]': &[i32;_] | ||
241 | [83; 86) '[1]': [i32;_] | ||
242 | [84; 85) '1': i32 | ||
243 | [98; 123) '{ ... }': &[i32] | ||
244 | [108; 111) 'foo': fn foo<i32>(&[T]) -> &[T] | ||
245 | [108; 117) 'foo(&[1])': &[i32] | ||
246 | [112; 116) '&[1]': &[i32;_] | ||
247 | [113; 116) '[1]': [i32;_] | ||
248 | [114; 115) '1': i32 | ||
249 | "### | ||
250 | ); | ||
251 | } | ||
252 | |||
253 | #[test] | ||
254 | fn infer_match_first_coerce() { | ||
255 | assert_snapshot!( | ||
256 | infer(r#" | ||
257 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | ||
258 | fn test(i: i32) { | ||
259 | let x = match i { | ||
260 | 2 => foo(&[2]), | ||
261 | 1 => &[1], | ||
262 | _ => &[3], | ||
263 | }; | ||
264 | } | ||
265 | "#), | ||
266 | @r###" | ||
267 | [11; 12) 'x': &[T] | ||
268 | [28; 39) '{ loop {} }': &[T] | ||
269 | [30; 37) 'loop {}': ! | ||
270 | [35; 37) '{}': () | ||
271 | [48; 49) 'i': i32 | ||
272 | [56; 150) '{ ... }; }': () | ||
273 | [66; 67) 'x': &[i32] | ||
274 | [70; 147) 'match ... }': &[i32] | ||
275 | [76; 77) 'i': i32 | ||
276 | [88; 89) '2': i32 | ||
277 | [93; 96) 'foo': fn foo<i32>(&[T]) -> &[T] | ||
278 | [93; 102) 'foo(&[2])': &[i32] | ||
279 | [97; 101) '&[2]': &[i32;_] | ||
280 | [98; 101) '[2]': [i32;_] | ||
281 | [99; 100) '2': i32 | ||
282 | [112; 113) '1': i32 | ||
283 | [117; 121) '&[1]': &[i32;_] | ||
284 | [118; 121) '[1]': [i32;_] | ||
285 | [119; 120) '1': i32 | ||
286 | [131; 132) '_': i32 | ||
287 | [136; 140) '&[3]': &[i32;_] | ||
288 | [137; 140) '[3]': [i32;_] | ||
289 | [138; 139) '3': i32 | ||
290 | "### | ||
291 | ); | ||
292 | } | ||
293 | |||
294 | #[test] | ||
295 | fn infer_match_second_coerce() { | ||
296 | assert_snapshot!( | ||
297 | infer(r#" | ||
298 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | ||
299 | fn test(i: i32) { | ||
300 | let x = match i { | ||
301 | 1 => &[1], | ||
302 | 2 => foo(&[2]), | ||
303 | _ => &[3], | ||
304 | }; | ||
305 | } | ||
306 | "#), | ||
307 | @r###" | ||
308 | [11; 12) 'x': &[T] | ||
309 | [28; 39) '{ loop {} }': &[T] | ||
310 | [30; 37) 'loop {}': ! | ||
311 | [35; 37) '{}': () | ||
312 | [48; 49) 'i': i32 | ||
313 | [56; 150) '{ ... }; }': () | ||
314 | [66; 67) 'x': &[i32] | ||
315 | [70; 147) 'match ... }': &[i32] | ||
316 | [76; 77) 'i': i32 | ||
317 | [88; 89) '1': i32 | ||
318 | [93; 97) '&[1]': &[i32;_] | ||
319 | [94; 97) '[1]': [i32;_] | ||
320 | [95; 96) '1': i32 | ||
321 | [107; 108) '2': i32 | ||
322 | [112; 115) 'foo': fn foo<i32>(&[T]) -> &[T] | ||
323 | [112; 121) 'foo(&[2])': &[i32] | ||
324 | [116; 120) '&[2]': &[i32;_] | ||
325 | [117; 120) '[2]': [i32;_] | ||
326 | [118; 119) '2': i32 | ||
327 | [131; 132) '_': i32 | ||
328 | [136; 140) '&[3]': &[i32;_] | ||
329 | [137; 140) '[3]': [i32;_] | ||
330 | [138; 139) '3': i32 | ||
331 | "### | ||
332 | ); | ||
333 | } | ||
334 | |||
335 | #[test] | ||
336 | fn coerce_merge_one_by_one1() { | ||
337 | covers!(coerce_merge_fail_fallback); | ||
338 | |||
339 | assert_snapshot!( | ||
340 | infer(r#" | ||
341 | fn test() { | ||
342 | let t = &mut 1; | ||
343 | let x = match 1 { | ||
344 | 1 => t as *mut i32, | ||
345 | 2 => t as &i32, | ||
346 | _ => t as *const i32, | ||
347 | }; | ||
348 | } | ||
349 | "#), | ||
350 | @r###" | ||
351 | [11; 145) '{ ... }; }': () | ||
352 | [21; 22) 't': &mut i32 | ||
353 | [25; 31) '&mut 1': &mut i32 | ||
354 | [30; 31) '1': i32 | ||
355 | [41; 42) 'x': *const i32 | ||
356 | [45; 142) 'match ... }': *const i32 | ||
357 | [51; 52) '1': i32 | ||
358 | [63; 64) '1': i32 | ||
359 | [68; 69) 't': &mut i32 | ||
360 | [68; 81) 't as *mut i32': *mut i32 | ||
361 | [91; 92) '2': i32 | ||
362 | [96; 97) 't': &mut i32 | ||
363 | [96; 105) 't as &i32': &i32 | ||
364 | [115; 116) '_': i32 | ||
365 | [120; 121) 't': &mut i32 | ||
366 | [120; 135) 't as *const i32': *const i32 | ||
367 | "### | ||
368 | ); | ||
369 | } | ||