aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/tests/simple.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/tests/simple.rs')
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs3398
1 files changed, 1711 insertions, 1687 deletions
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
index de63f4cce..3fd7d5cd4 100644
--- a/crates/ra_hir_ty/src/tests/simple.rs
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -1,6 +1,6 @@
1use insta::assert_snapshot; 1use expect::expect;
2 2
3use super::{check_types, infer}; 3use super::{check_infer, check_types};
4 4
5#[test] 5#[test]
6fn infer_box() { 6fn infer_box() {
@@ -45,43 +45,44 @@ fn test() {
45 45
46#[test] 46#[test]
47fn self_in_struct_lit() { 47fn self_in_struct_lit() {
48 assert_snapshot!(infer( 48 check_infer(
49 r#" 49 r#"
50//- /main.rs 50 //- /main.rs
51struct S<T> { x: T } 51 struct S<T> { x: T }
52 52
53impl S<u32> { 53 impl S<u32> {
54 fn foo() { 54 fn foo() {
55 Self { x: 1 }; 55 Self { x: 1 };
56 } 56 }
57} 57 }
58"#, 58 "#,
59 ), @r###" 59 expect![[r#"
60 49..79 '{ ... }': () 60 49..79 '{ ... }': ()
61 59..72 'Self { x: 1 }': S<u32> 61 59..72 'Self { x: 1 }': S<u32>
62 69..70 '1': u32 62 69..70 '1': u32
63 "###); 63 "#]],
64 );
64} 65}
65 66
66#[test] 67#[test]
67fn type_alias_in_struct_lit() { 68fn type_alias_in_struct_lit() {
68 assert_snapshot!(infer( 69 check_infer(
69 r#" 70 r#"
70//- /main.rs 71 //- /main.rs
71struct S<T> { x: T } 72 struct S<T> { x: T }
72 73
73type SS = S<u32>; 74 type SS = S<u32>;
74 75
75fn foo() { 76 fn foo() {
76 SS { x: 1 }; 77 SS { x: 1 };
77} 78 }
78 79 "#,
79"#, 80 expect![[r#"
80 ), @r###" 81 50..70 '{ ...1 }; }': ()
81 50..70 '{ ...1 }; }': () 82 56..67 'SS { x: 1 }': S<u32>
82 56..67 'SS { x: 1 }': S<u32> 83 64..65 '1': u32
83 64..65 '1': u32 84 "#]],
84 "###); 85 );
85} 86}
86 87
87#[test] 88#[test]
@@ -148,1526 +149,1549 @@ fn test() {
148 149
149#[test] 150#[test]
150fn infer_basics() { 151fn infer_basics() {
151 assert_snapshot!( 152 check_infer(
152 infer(r#" 153 r#"
153fn test(a: u32, b: isize, c: !, d: &str) { 154 fn test(a: u32, b: isize, c: !, d: &str) {
154 a; 155 a;
155 b; 156 b;
156 c; 157 c;
157 d; 158 d;
158 1usize; 159 1usize;
159 1isize; 160 1isize;
160 "test"; 161 "test";
161 1.0f32; 162 1.0f32;
162}"#), 163 }"#,
163 @r###" 164 expect![[r#"
164 8..9 'a': u32 165 8..9 'a': u32
165 16..17 'b': isize 166 16..17 'b': isize
166 26..27 'c': ! 167 26..27 'c': !
167 32..33 'd': &str 168 32..33 'd': &str
168 41..120 '{ ...f32; }': () 169 41..120 '{ ...f32; }': ()
169 47..48 'a': u32 170 47..48 'a': u32
170 54..55 'b': isize 171 54..55 'b': isize
171 61..62 'c': ! 172 61..62 'c': !
172 68..69 'd': &str 173 68..69 'd': &str
173 75..81 '1usize': usize 174 75..81 '1usize': usize
174 87..93 '1isize': isize 175 87..93 '1isize': isize
175 99..105 '"test"': &str 176 99..105 '"test"': &str
176 111..117 '1.0f32': f32 177 111..117 '1.0f32': f32
177 "### 178 "#]],
178 ); 179 );
179} 180}
180 181
181#[test] 182#[test]
182fn infer_let() { 183fn infer_let() {
183 assert_snapshot!( 184 check_infer(
184 infer(r#" 185 r#"
185fn test() { 186 fn test() {
186 let a = 1isize; 187 let a = 1isize;
187 let b: usize = 1; 188 let b: usize = 1;
188 let c = b; 189 let c = b;
189 let d: u32; 190 let d: u32;
190 let e; 191 let e;
191 let f: i32 = e; 192 let f: i32 = e;
192} 193 }
193"#), 194 "#,
194 @r###" 195 expect![[r#"
195 10..117 '{ ...= e; }': () 196 10..117 '{ ...= e; }': ()
196 20..21 'a': isize 197 20..21 'a': isize
197 24..30 '1isize': isize 198 24..30 '1isize': isize
198 40..41 'b': usize 199 40..41 'b': usize
199 51..52 '1': usize 200 51..52 '1': usize
200 62..63 'c': usize 201 62..63 'c': usize
201 66..67 'b': usize 202 66..67 'b': usize
202 77..78 'd': u32 203 77..78 'd': u32
203 93..94 'e': i32 204 93..94 'e': i32
204 104..105 'f': i32 205 104..105 'f': i32
205 113..114 'e': i32 206 113..114 'e': i32
206 "### 207 "#]],
207 ); 208 );
208} 209}
209 210
210#[test] 211#[test]
211fn infer_paths() { 212fn infer_paths() {
212 assert_snapshot!( 213 check_infer(
213 infer(r#" 214 r#"
214fn a() -> u32 { 1 } 215 fn a() -> u32 { 1 }
215 216
216mod b { 217 mod b {
217 fn c() -> u32 { 1 } 218 fn c() -> u32 { 1 }
218} 219 }
219 220
220fn test() { 221 fn test() {
221 a(); 222 a();
222 b::c(); 223 b::c();
223} 224 }
224"#), 225 "#,
225 @r###" 226 expect![[r#"
226 14..19 '{ 1 }': u32 227 14..19 '{ 1 }': u32
227 16..17 '1': u32 228 16..17 '1': u32
228 47..52 '{ 1 }': u32 229 47..52 '{ 1 }': u32
229 49..50 '1': u32 230 49..50 '1': u32
230 66..90 '{ ...c(); }': () 231 66..90 '{ ...c(); }': ()
231 72..73 'a': fn a() -> u32 232 72..73 'a': fn a() -> u32
232 72..75 'a()': u32 233 72..75 'a()': u32
233 81..85 'b::c': fn c() -> u32 234 81..85 'b::c': fn c() -> u32
234 81..87 'b::c()': u32 235 81..87 'b::c()': u32
235 "### 236 "#]],
236 ); 237 );
237} 238}
238 239
239#[test] 240#[test]
240fn infer_path_type() { 241fn infer_path_type() {
241 assert_snapshot!( 242 check_infer(
242 infer(r#" 243 r#"
243struct S; 244 struct S;
244 245
245impl S { 246 impl S {
246 fn foo() -> i32 { 1 } 247 fn foo() -> i32 { 1 }
247} 248 }
248 249
249fn test() { 250 fn test() {
250 S::foo(); 251 S::foo();
251 <S>::foo(); 252 <S>::foo();
252} 253 }
253"#), 254 "#,
254 @r###" 255 expect![[r#"
255 40..45 '{ 1 }': i32 256 40..45 '{ 1 }': i32
256 42..43 '1': i32 257 42..43 '1': i32
257 59..92 '{ ...o(); }': () 258 59..92 '{ ...o(); }': ()
258 65..71 'S::foo': fn foo() -> i32 259 65..71 'S::foo': fn foo() -> i32
259 65..73 'S::foo()': i32 260 65..73 'S::foo()': i32
260 79..87 '<S>::foo': fn foo() -> i32 261 79..87 '<S>::foo': fn foo() -> i32
261 79..89 '<S>::foo()': i32 262 79..89 '<S>::foo()': i32
262 "### 263 "#]],
263 ); 264 );
264} 265}
265 266
266#[test] 267#[test]
267fn infer_struct() { 268fn infer_struct() {
268 assert_snapshot!( 269 check_infer(
269 infer(r#" 270 r#"
270struct A { 271 struct A {
271 b: B, 272 b: B,
272 c: C, 273 c: C,
273} 274 }
274struct B; 275 struct B;
275struct C(usize); 276 struct C(usize);
276 277
277fn test() { 278 fn test() {
278 let c = C(1); 279 let c = C(1);
279 B; 280 B;
280 let a: A = A { b: B, c: C(1) }; 281 let a: A = A { b: B, c: C(1) };
281 a.b; 282 a.b;
282 a.c; 283 a.c;
283} 284 }
284"#), 285 "#,
285 @r###" 286 expect![[r#"
286 71..153 '{ ...a.c; }': () 287 71..153 '{ ...a.c; }': ()
287 81..82 'c': C 288 81..82 'c': C
288 85..86 'C': C(usize) -> C 289 85..86 'C': C(usize) -> C
289 85..89 'C(1)': C 290 85..89 'C(1)': C
290 87..88 '1': usize 291 87..88 '1': usize
291 95..96 'B': B 292 95..96 'B': B
292 106..107 'a': A 293 106..107 'a': A
293 113..132 'A { b:...C(1) }': A 294 113..132 'A { b:...C(1) }': A
294 120..121 'B': B 295 120..121 'B': B
295 126..127 'C': C(usize) -> C 296 126..127 'C': C(usize) -> C
296 126..130 'C(1)': C 297 126..130 'C(1)': C
297 128..129 '1': usize 298 128..129 '1': usize
298 138..139 'a': A 299 138..139 'a': A
299 138..141 'a.b': B 300 138..141 'a.b': B
300 147..148 'a': A 301 147..148 'a': A
301 147..150 'a.c': C 302 147..150 'a.c': C
302 "### 303 "#]],
303 ); 304 );
304} 305}
305 306
306#[test] 307#[test]
307fn infer_enum() { 308fn infer_enum() {
308 assert_snapshot!( 309 check_infer(
309 infer(r#" 310 r#"
310enum E { 311 enum E {
311 V1 { field: u32 }, 312 V1 { field: u32 },
312 V2 313 V2
314 }
315 fn test() {
316 E::V1 { field: 1 };
317 E::V2;
318 }"#,
319 expect![[r#"
320 51..89 '{ ...:V2; }': ()
321 57..75 'E::V1 ...d: 1 }': E
322 72..73 '1': u32
323 81..86 'E::V2': E
324 "#]],
325 );
313} 326}
314fn test() { 327
315 E::V1 { field: 1 }; 328#[test]
316 E::V2; 329fn infer_union() {
317}"#), 330 check_infer(
318 @r###" 331 r#"
319 47..81 '{ E:...:V2; }': () 332 union MyUnion {
320 51..69 'E::V1 ...d: 1 }': E 333 foo: u32,
321 66..67 '1': u32 334 bar: f32,
322 73..78 'E::V2': E 335 }
323 "### 336
337 unsafe fn baz(u: MyUnion) {
338 let inner = u.foo;
339 }
340 "#,
341 expect![[r#"
342 61..62 'u': MyUnion
343 73..99 '{ ...foo; }': ()
344 83..88 'inner': u32
345 91..92 'u': MyUnion
346 91..96 'u.foo': u32
347 "#]],
324 ); 348 );
325} 349}
326 350
327#[test] 351#[test]
328fn infer_refs() { 352fn infer_refs() {
329 assert_snapshot!( 353 check_infer(
330 infer(r#" 354 r#"
331fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { 355 fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
332 a; 356 a;
333 *a; 357 *a;
334 &a; 358 &a;
335 &mut a; 359 &mut a;
336 b; 360 b;
337 *b; 361 *b;
338 &b; 362 &b;
339 c; 363 c;
340 *c; 364 *c;
341 d; 365 d;
342 *d; 366 *d;
343} 367 }
344"#), 368 "#,
345 @r###" 369 expect![[r#"
346 8..9 'a': &u32 370 8..9 'a': &u32
347 17..18 'b': &mut u32 371 17..18 'b': &mut u32
348 30..31 'c': *const u32 372 30..31 'c': *const u32
349 45..46 'd': *mut u32 373 45..46 'd': *mut u32
350 58..149 '{ ... *d; }': () 374 58..149 '{ ... *d; }': ()
351 64..65 'a': &u32 375 64..65 'a': &u32
352 71..73 '*a': u32 376 71..73 '*a': u32
353 72..73 'a': &u32 377 72..73 'a': &u32
354 79..81 '&a': &&u32 378 79..81 '&a': &&u32
355 80..81 'a': &u32 379 80..81 'a': &u32
356 87..93 '&mut a': &mut &u32 380 87..93 '&mut a': &mut &u32
357 92..93 'a': &u32 381 92..93 'a': &u32
358 99..100 'b': &mut u32 382 99..100 'b': &mut u32
359 106..108 '*b': u32 383 106..108 '*b': u32
360 107..108 'b': &mut u32 384 107..108 'b': &mut u32
361 114..116 '&b': &&mut u32 385 114..116 '&b': &&mut u32
362 115..116 'b': &mut u32 386 115..116 'b': &mut u32
363 122..123 'c': *const u32 387 122..123 'c': *const u32
364 129..131 '*c': u32 388 129..131 '*c': u32
365 130..131 'c': *const u32 389 130..131 'c': *const u32
366 137..138 'd': *mut u32 390 137..138 'd': *mut u32
367 144..146 '*d': u32 391 144..146 '*d': u32
368 145..146 'd': *mut u32 392 145..146 'd': *mut u32
369 "### 393 "#]],
370 ); 394 );
371} 395}
372 396
373#[test] 397#[test]
374fn infer_raw_ref() { 398fn infer_raw_ref() {
375 assert_snapshot!( 399 check_infer(
376 infer(r#" 400 r#"
377fn test(a: i32) { 401 fn test(a: i32) {
378 &raw mut a; 402 &raw mut a;
379 &raw const a; 403 &raw const a;
380} 404 }
381"#), 405 "#,
382 @r###" 406 expect![[r#"
383 8..9 'a': i32 407 8..9 'a': i32
384 16..53 '{ ...t a; }': () 408 16..53 '{ ...t a; }': ()
385 22..32 '&raw mut a': *mut i32 409 22..32 '&raw mut a': *mut i32
386 31..32 'a': i32 410 31..32 'a': i32
387 38..50 '&raw const a': *const i32 411 38..50 '&raw const a': *const i32
388 49..50 'a': i32 412 49..50 'a': i32
389 "### 413 "#]],
390 ); 414 );
391} 415}
392 416
393#[test] 417#[test]
394fn infer_literals() { 418fn infer_literals() {
395 assert_snapshot!( 419 check_infer(
396 infer(r##" 420 r##"
397fn test() { 421 fn test() {
398 5i32; 422 5i32;
399 5f32; 423 5f32;
400 5f64; 424 5f64;
401 "hello"; 425 "hello";
402 b"bytes"; 426 b"bytes";
403 'c'; 427 'c';
404 b'b'; 428 b'b';
405 3.14; 429 3.14;
406 5000; 430 5000;
407 false; 431 false;
408 true; 432 true;
409 r#" 433 r#"
410 //! doc 434 //! doc
411 // non-doc 435 // non-doc
412 mod foo {} 436 mod foo {}
413 "#; 437 "#;
414 br#"yolo"#; 438 br#"yolo"#;
415} 439 }
416"##), 440 "##,
417 @r###" 441 expect![[r##"
418 10..220 '{ ...o"#; }': () 442 10..216 '{ ...o"#; }': ()
419 16..20 '5i32': i32 443 16..20 '5i32': i32
420 26..30 '5f32': f32 444 26..30 '5f32': f32
421 36..40 '5f64': f64 445 36..40 '5f64': f64
422 46..53 '"hello"': &str 446 46..53 '"hello"': &str
423 59..67 'b"bytes"': &[u8; _] 447 59..67 'b"bytes"': &[u8; _]
424 73..76 ''c'': char 448 73..76 ''c'': char
425 82..86 'b'b'': u8 449 82..86 'b'b'': u8
426 92..96 '3.14': f64 450 92..96 '3.14': f64
427 102..106 '5000': i32 451 102..106 '5000': i32
428 112..117 'false': bool 452 112..117 'false': bool
429 123..127 'true': bool 453 123..127 'true': bool
430 133..201 'r#" ... "#': &str 454 133..197 'r#" ... "#': &str
431 207..217 'br#"yolo"#': &[u8; _] 455 203..213 'br#"yolo"#': &[u8; _]
432 "### 456 "##]],
433 ); 457 );
434} 458}
435 459
436#[test] 460#[test]
437fn infer_unary_op() { 461fn infer_unary_op() {
438 assert_snapshot!( 462 check_infer(
439 infer(r#" 463 r#"
440enum SomeType {} 464 enum SomeType {}
441 465
442fn test(x: SomeType) { 466 fn test(x: SomeType) {
443 let b = false; 467 let b = false;
444 let c = !b; 468 let c = !b;
445 let a = 100; 469 let a = 100;
446 let d: i128 = -a; 470 let d: i128 = -a;
447 let e = -100; 471 let e = -100;
448 let f = !!!true; 472 let f = !!!true;
449 let g = !42; 473 let g = !42;
450 let h = !10u32; 474 let h = !10u32;
451 let j = !a; 475 let j = !a;
452 -3.14; 476 -3.14;
453 !3; 477 !3;
454 -x; 478 -x;
455 !x; 479 !x;
456 -"hello"; 480 -"hello";
457 !"hello"; 481 !"hello";
458} 482 }
459"#), 483 "#,
460 @r###" 484 expect![[r#"
461 26..27 'x': SomeType 485 26..27 'x': SomeType
462 39..271 '{ ...lo"; }': () 486 39..271 '{ ...lo"; }': ()
463 49..50 'b': bool 487 49..50 'b': bool
464 53..58 'false': bool 488 53..58 'false': bool
465 68..69 'c': bool 489 68..69 'c': bool
466 72..74 '!b': bool 490 72..74 '!b': bool
467 73..74 'b': bool 491 73..74 'b': bool
468 84..85 'a': i128 492 84..85 'a': i128
469 88..91 '100': i128 493 88..91 '100': i128
470 101..102 'd': i128 494 101..102 'd': i128
471 111..113 '-a': i128 495 111..113 '-a': i128
472 112..113 'a': i128 496 112..113 'a': i128
473 123..124 'e': i32 497 123..124 'e': i32
474 127..131 '-100': i32 498 127..131 '-100': i32
475 128..131 '100': i32 499 128..131 '100': i32
476 141..142 'f': bool 500 141..142 'f': bool
477 145..152 '!!!true': bool 501 145..152 '!!!true': bool
478 146..152 '!!true': bool 502 146..152 '!!true': bool
479 147..152 '!true': bool 503 147..152 '!true': bool
480 148..152 'true': bool 504 148..152 'true': bool
481 162..163 'g': i32 505 162..163 'g': i32
482 166..169 '!42': i32 506 166..169 '!42': i32
483 167..169 '42': i32 507 167..169 '42': i32
484 179..180 'h': u32 508 179..180 'h': u32
485 183..189 '!10u32': u32 509 183..189 '!10u32': u32
486 184..189 '10u32': u32 510 184..189 '10u32': u32
487 199..200 'j': i128 511 199..200 'j': i128
488 203..205 '!a': i128 512 203..205 '!a': i128
489 204..205 'a': i128 513 204..205 'a': i128
490 211..216 '-3.14': f64 514 211..216 '-3.14': f64
491 212..216 '3.14': f64 515 212..216 '3.14': f64
492 222..224 '!3': i32 516 222..224 '!3': i32
493 223..224 '3': i32 517 223..224 '3': i32
494 230..232 '-x': {unknown} 518 230..232 '-x': {unknown}
495 231..232 'x': SomeType 519 231..232 'x': SomeType
496 238..240 '!x': {unknown} 520 238..240 '!x': {unknown}
497 239..240 'x': SomeType 521 239..240 'x': SomeType
498 246..254 '-"hello"': {unknown} 522 246..254 '-"hello"': {unknown}
499 247..254 '"hello"': &str 523 247..254 '"hello"': &str
500 260..268 '!"hello"': {unknown} 524 260..268 '!"hello"': {unknown}
501 261..268 '"hello"': &str 525 261..268 '"hello"': &str
502 "### 526 "#]],
503 ); 527 );
504} 528}
505 529
506#[test] 530#[test]
507fn infer_backwards() { 531fn infer_backwards() {
508 assert_snapshot!( 532 check_infer(
509 infer(r#" 533 r#"
510fn takes_u32(x: u32) {} 534 fn takes_u32(x: u32) {}
511 535
512struct S { i32_field: i32 } 536 struct S { i32_field: i32 }
513 537
514fn test() -> &mut &f64 { 538 fn test() -> &mut &f64 {
515 let a = unknown_function(); 539 let a = unknown_function();
516 takes_u32(a); 540 takes_u32(a);
517 let b = unknown_function(); 541 let b = unknown_function();
518 S { i32_field: b }; 542 S { i32_field: b };
519 let c = unknown_function(); 543 let c = unknown_function();
520 &mut &c 544 &mut &c
521} 545 }
522"#), 546 "#,
523 @r###" 547 expect![[r#"
524 13..14 'x': u32 548 13..14 'x': u32
525 21..23 '{}': () 549 21..23 '{}': ()
526 77..230 '{ ...t &c }': &mut &f64 550 77..230 '{ ...t &c }': &mut &f64
527 87..88 'a': u32 551 87..88 'a': u32
528 91..107 'unknow...nction': {unknown} 552 91..107 'unknow...nction': {unknown}
529 91..109 'unknow...tion()': u32 553 91..109 'unknow...tion()': u32
530 115..124 'takes_u32': fn takes_u32(u32) 554 115..124 'takes_u32': fn takes_u32(u32)
531 115..127 'takes_u32(a)': () 555 115..127 'takes_u32(a)': ()
532 125..126 'a': u32 556 125..126 'a': u32
533 137..138 'b': i32 557 137..138 'b': i32
534 141..157 'unknow...nction': {unknown} 558 141..157 'unknow...nction': {unknown}
535 141..159 'unknow...tion()': i32 559 141..159 'unknow...tion()': i32
536 165..183 'S { i3...d: b }': S 560 165..183 'S { i3...d: b }': S
537 180..181 'b': i32 561 180..181 'b': i32
538 193..194 'c': f64 562 193..194 'c': f64
539 197..213 'unknow...nction': {unknown} 563 197..213 'unknow...nction': {unknown}
540 197..215 'unknow...tion()': f64 564 197..215 'unknow...tion()': f64
541 221..228 '&mut &c': &mut &f64 565 221..228 '&mut &c': &mut &f64
542 226..228 '&c': &f64 566 226..228 '&c': &f64
543 227..228 'c': f64 567 227..228 'c': f64
544 "### 568 "#]],
545 ); 569 );
546} 570}
547 571
548#[test] 572#[test]
549fn infer_self() { 573fn infer_self() {
550 assert_snapshot!( 574 check_infer(
551 infer(r#" 575 r#"
552struct S; 576 struct S;
553 577
554impl S { 578 impl S {
555 fn test(&self) { 579 fn test(&self) {
556 self; 580 self;
557 } 581 }
558 fn test2(self: &Self) { 582 fn test2(self: &Self) {
559 self; 583 self;
560 } 584 }
561 fn test3() -> Self { 585 fn test3() -> Self {
562 S {} 586 S {}
563 } 587 }
564 fn test4() -> Self { 588 fn test4() -> Self {
565 Self {} 589 Self {}
566 } 590 }
567} 591 }
568"#), 592 "#,
569 @r###" 593 expect![[r#"
570 33..37 'self': &S 594 33..37 'self': &S
571 39..60 '{ ... }': () 595 39..60 '{ ... }': ()
572 49..53 'self': &S 596 49..53 'self': &S
573 74..78 'self': &S 597 74..78 'self': &S
574 87..108 '{ ... }': () 598 87..108 '{ ... }': ()
575 97..101 'self': &S 599 97..101 'self': &S
576 132..152 '{ ... }': S 600 132..152 '{ ... }': S
577 142..146 'S {}': S 601 142..146 'S {}': S
578 176..199 '{ ... }': S 602 176..199 '{ ... }': S
579 186..193 'Self {}': S 603 186..193 'Self {}': S
580 "### 604 "#]],
581 ); 605 );
582} 606}
583 607
584#[test] 608#[test]
585fn infer_self_as_path() { 609fn infer_self_as_path() {
586 assert_snapshot!( 610 check_infer(
587 infer(r#" 611 r#"
588struct S1; 612 struct S1;
589struct S2(isize); 613 struct S2(isize);
590enum E { 614 enum E {
591 V1, 615 V1,
592 V2(u32), 616 V2(u32),
593} 617 }
594 618
595impl S1 { 619 impl S1 {
596 fn test() { 620 fn test() {
597 Self; 621 Self;
598 } 622 }
599} 623 }
600impl S2 { 624 impl S2 {
601 fn test() { 625 fn test() {
602 Self(1); 626 Self(1);
603 } 627 }
604} 628 }
605impl E { 629 impl E {
606 fn test() { 630 fn test() {
607 Self::V1; 631 Self::V1;
608 Self::V2(1); 632 Self::V2(1);
609 } 633 }
610} 634 }
611"#), 635 "#,
612 @r###" 636 expect![[r#"
613 86..107 '{ ... }': () 637 86..107 '{ ... }': ()
614 96..100 'Self': S1 638 96..100 'Self': S1
615 134..158 '{ ... }': () 639 134..158 '{ ... }': ()
616 144..148 'Self': S2(isize) -> S2 640 144..148 'Self': S2(isize) -> S2
617 144..151 'Self(1)': S2 641 144..151 'Self(1)': S2
618 149..150 '1': isize 642 149..150 '1': isize
619 184..230 '{ ... }': () 643 184..230 '{ ... }': ()
620 194..202 'Self::V1': E 644 194..202 'Self::V1': E
621 212..220 'Self::V2': V2(u32) -> E 645 212..220 'Self::V2': V2(u32) -> E
622 212..223 'Self::V2(1)': E 646 212..223 'Self::V2(1)': E
623 221..222 '1': u32 647 221..222 '1': u32
624 "### 648 "#]],
625 ); 649 );
626} 650}
627 651
628#[test] 652#[test]
629fn infer_binary_op() { 653fn infer_binary_op() {
630 assert_snapshot!( 654 check_infer(
631 infer(r#" 655 r#"
632fn f(x: bool) -> i32 { 656 fn f(x: bool) -> i32 {
633 0i32 657 0i32
634} 658 }
635 659
636fn test() -> bool { 660 fn test() -> bool {
637 let x = a && b; 661 let x = a && b;
638 let y = true || false; 662 let y = true || false;
639 let z = x == y; 663 let z = x == y;
640 let t = x != y; 664 let t = x != y;
641 let minus_forty: isize = -40isize; 665 let minus_forty: isize = -40isize;
642 let h = minus_forty <= CONST_2; 666 let h = minus_forty <= CONST_2;
643 let c = f(z || y) + 5; 667 let c = f(z || y) + 5;
644 let d = b; 668 let d = b;
645 let g = minus_forty ^= i; 669 let g = minus_forty ^= i;
646 let ten: usize = 10; 670 let ten: usize = 10;
647 let ten_is_eleven = ten == some_num; 671 let ten_is_eleven = ten == some_num;
648 672
649 ten < 3 673 ten < 3
650} 674 }
651"#), 675 "#,
652 @r###" 676 expect![[r#"
653 5..6 'x': bool 677 5..6 'x': bool
654 21..33 '{ 0i32 }': i32 678 21..33 '{ 0i32 }': i32
655 27..31 '0i32': i32 679 27..31 '0i32': i32
656 53..369 '{ ... < 3 }': bool 680 53..369 '{ ... < 3 }': bool
657 63..64 'x': bool 681 63..64 'x': bool
658 67..68 'a': bool 682 67..68 'a': bool
659 67..73 'a && b': bool 683 67..73 'a && b': bool
660 72..73 'b': bool 684 72..73 'b': bool
661 83..84 'y': bool 685 83..84 'y': bool
662 87..91 'true': bool 686 87..91 'true': bool
663 87..100 'true || false': bool 687 87..100 'true || false': bool
664 95..100 'false': bool 688 95..100 'false': bool
665 110..111 'z': bool 689 110..111 'z': bool
666 114..115 'x': bool 690 114..115 'x': bool
667 114..120 'x == y': bool 691 114..120 'x == y': bool
668 119..120 'y': bool 692 119..120 'y': bool
669 130..131 't': bool 693 130..131 't': bool
670 134..135 'x': bool 694 134..135 'x': bool
671 134..140 'x != y': bool 695 134..140 'x != y': bool
672 139..140 'y': bool 696 139..140 'y': bool
673 150..161 'minus_forty': isize 697 150..161 'minus_forty': isize
674 171..179 '-40isize': isize 698 171..179 '-40isize': isize
675 172..179 '40isize': isize 699 172..179 '40isize': isize
676 189..190 'h': bool 700 189..190 'h': bool
677 193..204 'minus_forty': isize 701 193..204 'minus_forty': isize
678 193..215 'minus_...ONST_2': bool 702 193..215 'minus_...ONST_2': bool
679 208..215 'CONST_2': isize 703 208..215 'CONST_2': isize
680 225..226 'c': i32 704 225..226 'c': i32
681 229..230 'f': fn f(bool) -> i32 705 229..230 'f': fn f(bool) -> i32
682 229..238 'f(z || y)': i32 706 229..238 'f(z || y)': i32
683 229..242 'f(z || y) + 5': i32 707 229..242 'f(z || y) + 5': i32
684 231..232 'z': bool 708 231..232 'z': bool
685 231..237 'z || y': bool 709 231..237 'z || y': bool
686 236..237 'y': bool 710 236..237 'y': bool
687 241..242 '5': i32 711 241..242 '5': i32
688 252..253 'd': {unknown} 712 252..253 'd': {unknown}
689 256..257 'b': {unknown} 713 256..257 'b': {unknown}
690 267..268 'g': () 714 267..268 'g': ()
691 271..282 'minus_forty': isize 715 271..282 'minus_forty': isize
692 271..287 'minus_...y ^= i': () 716 271..287 'minus_...y ^= i': ()
693 286..287 'i': isize 717 286..287 'i': isize
694 297..300 'ten': usize 718 297..300 'ten': usize
695 310..312 '10': usize 719 310..312 '10': usize
696 322..335 'ten_is_eleven': bool 720 322..335 'ten_is_eleven': bool
697 338..341 'ten': usize 721 338..341 'ten': usize
698 338..353 'ten == some_num': bool 722 338..353 'ten == some_num': bool
699 345..353 'some_num': usize 723 345..353 'some_num': usize
700 360..363 'ten': usize 724 360..363 'ten': usize
701 360..367 'ten < 3': bool 725 360..367 'ten < 3': bool
702 366..367 '3': usize 726 366..367 '3': usize
703 "### 727 "#]],
704 ); 728 );
705} 729}
706 730
707#[test] 731#[test]
708fn infer_shift_op() { 732fn infer_shift_op() {
709 assert_snapshot!( 733 check_infer(
710 infer(r#" 734 r#"
711fn test() { 735 fn test() {
712 1u32 << 5u8; 736 1u32 << 5u8;
713 1u32 >> 5u8; 737 1u32 >> 5u8;
714} 738 }
715"#), 739 "#,
716 @r###" 740 expect![[r#"
717 10..47 '{ ...5u8; }': () 741 10..47 '{ ...5u8; }': ()
718 16..20 '1u32': u32 742 16..20 '1u32': u32
719 16..27 '1u32 << 5u8': u32 743 16..27 '1u32 << 5u8': u32
720 24..27 '5u8': u8 744 24..27 '5u8': u8
721 33..37 '1u32': u32 745 33..37 '1u32': u32
722 33..44 '1u32 >> 5u8': u32 746 33..44 '1u32 >> 5u8': u32
723 41..44 '5u8': u8 747 41..44 '5u8': u8
724 "### 748 "#]],
725 ); 749 );
726} 750}
727 751
728#[test] 752#[test]
729fn infer_field_autoderef() { 753fn infer_field_autoderef() {
730 assert_snapshot!( 754 check_infer(
731 infer(r#" 755 r#"
732struct A { 756 struct A {
733 b: B, 757 b: B,
734} 758 }
735struct B; 759 struct B;
736 760
737fn test1(a: A) { 761 fn test1(a: A) {
738 let a1 = a; 762 let a1 = a;
739 a1.b; 763 a1.b;
740 let a2 = &a; 764 let a2 = &a;
741 a2.b; 765 a2.b;
742 let a3 = &mut a; 766 let a3 = &mut a;
743 a3.b; 767 a3.b;
744 let a4 = &&&&&&&a; 768 let a4 = &&&&&&&a;
745 a4.b; 769 a4.b;
746 let a5 = &mut &&mut &&mut a; 770 let a5 = &mut &&mut &&mut a;
747 a5.b; 771 a5.b;
748} 772 }
749 773
750fn test2(a1: *const A, a2: *mut A) { 774 fn test2(a1: *const A, a2: *mut A) {
751 a1.b; 775 a1.b;
752 a2.b; 776 a2.b;
753} 777 }
754"#), 778 "#,
755 @r###" 779 expect![[r#"
756 43..44 'a': A 780 43..44 'a': A
757 49..212 '{ ...5.b; }': () 781 49..212 '{ ...5.b; }': ()
758 59..61 'a1': A 782 59..61 'a1': A
759 64..65 'a': A 783 64..65 'a': A
760 71..73 'a1': A 784 71..73 'a1': A
761 71..75 'a1.b': B 785 71..75 'a1.b': B
762 85..87 'a2': &A 786 85..87 'a2': &A
763 90..92 '&a': &A 787 90..92 '&a': &A
764 91..92 'a': A 788 91..92 'a': A
765 98..100 'a2': &A 789 98..100 'a2': &A
766 98..102 'a2.b': B 790 98..102 'a2.b': B
767 112..114 'a3': &mut A 791 112..114 'a3': &mut A
768 117..123 '&mut a': &mut A 792 117..123 '&mut a': &mut A
769 122..123 'a': A 793 122..123 'a': A
770 129..131 'a3': &mut A 794 129..131 'a3': &mut A
771 129..133 'a3.b': B 795 129..133 'a3.b': B
772 143..145 'a4': &&&&&&&A 796 143..145 'a4': &&&&&&&A
773 148..156 '&&&&&&&a': &&&&&&&A 797 148..156 '&&&&&&&a': &&&&&&&A
774 149..156 '&&&&&&a': &&&&&&A 798 149..156 '&&&&&&a': &&&&&&A
775 150..156 '&&&&&a': &&&&&A 799 150..156 '&&&&&a': &&&&&A
776 151..156 '&&&&a': &&&&A 800 151..156 '&&&&a': &&&&A
777 152..156 '&&&a': &&&A 801 152..156 '&&&a': &&&A
778 153..156 '&&a': &&A 802 153..156 '&&a': &&A
779 154..156 '&a': &A 803 154..156 '&a': &A
780 155..156 'a': A 804 155..156 'a': A
781 162..164 'a4': &&&&&&&A 805 162..164 'a4': &&&&&&&A
782 162..166 'a4.b': B 806 162..166 'a4.b': B
783 176..178 'a5': &mut &&mut &&mut A 807 176..178 'a5': &mut &&mut &&mut A
784 181..199 '&mut &...&mut a': &mut &&mut &&mut A 808 181..199 '&mut &...&mut a': &mut &&mut &&mut A
785 186..199 '&&mut &&mut a': &&mut &&mut A 809 186..199 '&&mut &&mut a': &&mut &&mut A
786 187..199 '&mut &&mut a': &mut &&mut A 810 187..199 '&mut &&mut a': &mut &&mut A
787 192..199 '&&mut a': &&mut A 811 192..199 '&&mut a': &&mut A
788 193..199 '&mut a': &mut A 812 193..199 '&mut a': &mut A
789 198..199 'a': A 813 198..199 'a': A
790 205..207 'a5': &mut &&mut &&mut A 814 205..207 'a5': &mut &&mut &&mut A
791 205..209 'a5.b': B 815 205..209 'a5.b': B
792 223..225 'a1': *const A 816 223..225 'a1': *const A
793 237..239 'a2': *mut A 817 237..239 'a2': *mut A
794 249..272 '{ ...2.b; }': () 818 249..272 '{ ...2.b; }': ()
795 255..257 'a1': *const A 819 255..257 'a1': *const A
796 255..259 'a1.b': B 820 255..259 'a1.b': B
797 265..267 'a2': *mut A 821 265..267 'a2': *mut A
798 265..269 'a2.b': B 822 265..269 'a2.b': B
799 "### 823 "#]],
800 ); 824 );
801} 825}
802 826
803#[test] 827#[test]
804fn infer_argument_autoderef() { 828fn infer_argument_autoderef() {
805 assert_snapshot!( 829 check_infer(
806 infer(r#" 830 r#"
807#[lang = "deref"] 831 #[lang = "deref"]
808pub trait Deref { 832 pub trait Deref {
809 type Target; 833 type Target;
810 fn deref(&self) -> &Self::Target; 834 fn deref(&self) -> &Self::Target;
811} 835 }
812 836
813struct A<T>(T); 837 struct A<T>(T);
814 838
815impl<T> A<T> { 839 impl<T> A<T> {
816 fn foo(&self) -> &T { 840 fn foo(&self) -> &T {
817 &self.0 841 &self.0
818 } 842 }
819} 843 }
820 844
821struct B<T>(T); 845 struct B<T>(T);
822 846
823impl<T> Deref for B<T> { 847 impl<T> Deref for B<T> {
824 type Target = T; 848 type Target = T;
825 fn deref(&self) -> &Self::Target { 849 fn deref(&self) -> &Self::Target {
826 &self.0 850 &self.0
827 } 851 }
828} 852 }
829 853
830fn test() { 854 fn test() {
831 let t = A::foo(&&B(B(A(42)))); 855 let t = A::foo(&&B(B(A(42))));
832} 856 }
833"#), 857 "#,
834 @r###" 858 expect![[r#"
835 67..71 'self': &Self 859 67..71 'self': &Self
836 138..142 'self': &A<T> 860 138..142 'self': &A<T>
837 150..173 '{ ... }': &T 861 150..173 '{ ... }': &T
838 160..167 '&self.0': &T 862 160..167 '&self.0': &T
839 161..165 'self': &A<T> 863 161..165 'self': &A<T>
840 161..167 'self.0': T 864 161..167 'self.0': T
841 254..258 'self': &B<T> 865 254..258 'self': &B<T>
842 277..300 '{ ... }': &T 866 277..300 '{ ... }': &T
843 287..294 '&self.0': &T 867 287..294 '&self.0': &T
844 288..292 'self': &B<T> 868 288..292 'self': &B<T>
845 288..294 'self.0': T 869 288..294 'self.0': T
846 314..352 '{ ...))); }': () 870 314..352 '{ ...))); }': ()
847 324..325 't': &i32 871 324..325 't': &i32
848 328..334 'A::foo': fn foo<i32>(&A<i32>) -> &i32 872 328..334 'A::foo': fn foo<i32>(&A<i32>) -> &i32
849 328..349 'A::foo...42))))': &i32 873 328..349 'A::foo...42))))': &i32
850 335..348 '&&B(B(A(42)))': &&B<B<A<i32>>> 874 335..348 '&&B(B(A(42)))': &&B<B<A<i32>>>
851 336..348 '&B(B(A(42)))': &B<B<A<i32>>> 875 336..348 '&B(B(A(42)))': &B<B<A<i32>>>
852 337..338 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>> 876 337..338 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>>
853 337..348 'B(B(A(42)))': B<B<A<i32>>> 877 337..348 'B(B(A(42)))': B<B<A<i32>>>
854 339..340 'B': B<A<i32>>(A<i32>) -> B<A<i32>> 878 339..340 'B': B<A<i32>>(A<i32>) -> B<A<i32>>
855 339..347 'B(A(42))': B<A<i32>> 879 339..347 'B(A(42))': B<A<i32>>
856 341..342 'A': A<i32>(i32) -> A<i32> 880 341..342 'A': A<i32>(i32) -> A<i32>
857 341..346 'A(42)': A<i32> 881 341..346 'A(42)': A<i32>
858 343..345 '42': i32 882 343..345 '42': i32
859 "### 883 "#]],
860 ); 884 );
861} 885}
862 886
863#[test] 887#[test]
864fn infer_method_argument_autoderef() { 888fn infer_method_argument_autoderef() {
865 assert_snapshot!( 889 check_infer(
866 infer(r#" 890 r#"
867#[lang = "deref"] 891 #[lang = "deref"]
868pub trait Deref { 892 pub trait Deref {
869 type Target; 893 type Target;
870 fn deref(&self) -> &Self::Target; 894 fn deref(&self) -> &Self::Target;
871} 895 }
872 896
873struct A<T>(*mut T); 897 struct A<T>(*mut T);
874 898
875impl<T> A<T> { 899 impl<T> A<T> {
876 fn foo(&self, x: &A<T>) -> &T { 900 fn foo(&self, x: &A<T>) -> &T {
877 &*x.0 901 &*x.0
878 } 902 }
879} 903 }
880 904
881struct B<T>(T); 905 struct B<T>(T);
882 906
883impl<T> Deref for B<T> { 907 impl<T> Deref for B<T> {
884 type Target = T; 908 type Target = T;
885 fn deref(&self) -> &Self::Target { 909 fn deref(&self) -> &Self::Target {
886 &self.0 910 &self.0
887 } 911 }
888} 912 }
889 913
890fn test(a: A<i32>) { 914 fn test(a: A<i32>) {
891 let t = A(0 as *mut _).foo(&&B(B(a))); 915 let t = A(0 as *mut _).foo(&&B(B(a)));
892} 916 }
893"#), 917 "#,
894 @r###" 918 expect![[r#"
895 67..71 'self': &Self 919 67..71 'self': &Self
896 143..147 'self': &A<T> 920 143..147 'self': &A<T>
897 149..150 'x': &A<T> 921 149..150 'x': &A<T>
898 165..186 '{ ... }': &T 922 165..186 '{ ... }': &T
899 175..180 '&*x.0': &T 923 175..180 '&*x.0': &T
900 176..180 '*x.0': T 924 176..180 '*x.0': T
901 177..178 'x': &A<T> 925 177..178 'x': &A<T>
902 177..180 'x.0': *mut T 926 177..180 'x.0': *mut T
903 267..271 'self': &B<T> 927 267..271 'self': &B<T>
904 290..313 '{ ... }': &T 928 290..313 '{ ... }': &T
905 300..307 '&self.0': &T 929 300..307 '&self.0': &T
906 301..305 'self': &B<T> 930 301..305 'self': &B<T>
907 301..307 'self.0': T 931 301..307 'self.0': T
908 325..326 'a': A<i32> 932 325..326 'a': A<i32>
909 336..382 '{ ...))); }': () 933 336..382 '{ ...))); }': ()
910 346..347 't': &i32 934 346..347 't': &i32
911 350..351 'A': A<i32>(*mut i32) -> A<i32> 935 350..351 'A': A<i32>(*mut i32) -> A<i32>
912 350..364 'A(0 as *mut _)': A<i32> 936 350..364 'A(0 as *mut _)': A<i32>
913 350..379 'A(0 as...B(a)))': &i32 937 350..379 'A(0 as...B(a)))': &i32
914 352..353 '0': i32 938 352..353 '0': i32
915 352..363 '0 as *mut _': *mut i32 939 352..363 '0 as *mut _': *mut i32
916 369..378 '&&B(B(a))': &&B<B<A<i32>>> 940 369..378 '&&B(B(a))': &&B<B<A<i32>>>
917 370..378 '&B(B(a))': &B<B<A<i32>>> 941 370..378 '&B(B(a))': &B<B<A<i32>>>
918 371..372 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>> 942 371..372 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>>
919 371..378 'B(B(a))': B<B<A<i32>>> 943 371..378 'B(B(a))': B<B<A<i32>>>
920 373..374 'B': B<A<i32>>(A<i32>) -> B<A<i32>> 944 373..374 'B': B<A<i32>>(A<i32>) -> B<A<i32>>
921 373..377 'B(a)': B<A<i32>> 945 373..377 'B(a)': B<A<i32>>
922 375..376 'a': A<i32> 946 375..376 'a': A<i32>
923 "### 947 "#]],
924 ); 948 );
925} 949}
926 950
927#[test] 951#[test]
928fn infer_in_elseif() { 952fn infer_in_elseif() {
929 assert_snapshot!( 953 check_infer(
930 infer(r#" 954 r#"
931struct Foo { field: i32 } 955 struct Foo { field: i32 }
932fn main(foo: Foo) { 956 fn main(foo: Foo) {
933 if true { 957 if true {
934 958
935 } else if false { 959 } else if false {
936 foo.field 960 foo.field
937 } 961 }
938} 962 }
939"#), 963 "#,
940 @r###" 964 expect![[r#"
941 34..37 'foo': Foo 965 34..37 'foo': Foo
942 44..108 '{ ... } }': () 966 44..108 '{ ... } }': ()
943 50..106 'if tru... }': () 967 50..106 'if tru... }': ()
944 53..57 'true': bool 968 53..57 'true': bool
945 58..66 '{ }': () 969 58..66 '{ }': ()
946 72..106 'if fal... }': i32 970 72..106 'if fal... }': i32
947 75..80 'false': bool 971 75..80 'false': bool
948 81..106 '{ ... }': i32 972 81..106 '{ ... }': i32
949 91..94 'foo': Foo 973 91..94 'foo': Foo
950 91..100 'foo.field': i32 974 91..100 'foo.field': i32
951 "### 975 "#]],
952 ) 976 )
953} 977}
954 978
955#[test] 979#[test]
956fn infer_if_match_with_return() { 980fn infer_if_match_with_return() {
957 assert_snapshot!( 981 check_infer(
958 infer(r#" 982 r#"
959fn foo() { 983 fn foo() {
960 let _x1 = if true { 984 let _x1 = if true {
961 1 985 1
962 } else { 986 } else {
963 return; 987 return;
964 }; 988 };
965 let _x2 = if true { 989 let _x2 = if true {
966 2 990 2
967 } else { 991 } else {
968 return 992 return
969 }; 993 };
970 let _x3 = match true { 994 let _x3 = match true {
971 true => 3, 995 true => 3,
972 _ => { 996 _ => {
973 return; 997 return;
974 } 998 }
975 }; 999 };
976 let _x4 = match true { 1000 let _x4 = match true {
977 true => 4, 1001 true => 4,
978 _ => return 1002 _ => return
979 }; 1003 };
980}"#), 1004 }"#,
981 @r###" 1005 expect![[r#"
982 9..322 '{ ... }; }': () 1006 9..322 '{ ... }; }': ()
983 19..22 '_x1': i32 1007 19..22 '_x1': i32
984 25..79 'if tru... }': i32 1008 25..79 'if tru... }': i32
985 28..32 'true': bool 1009 28..32 'true': bool
986 33..50 '{ ... }': i32 1010 33..50 '{ ... }': i32
987 43..44 '1': i32 1011 43..44 '1': i32
988 56..79 '{ ... }': i32 1012 56..79 '{ ... }': i32
989 66..72 'return': ! 1013 66..72 'return': !
990 89..92 '_x2': i32 1014 89..92 '_x2': i32
991 95..148 'if tru... }': i32 1015 95..148 'if tru... }': i32
992 98..102 'true': bool 1016 98..102 'true': bool
993 103..120 '{ ... }': i32 1017 103..120 '{ ... }': i32
994 113..114 '2': i32 1018 113..114 '2': i32
995 126..148 '{ ... }': ! 1019 126..148 '{ ... }': !
996 136..142 'return': ! 1020 136..142 'return': !
997 158..161 '_x3': i32 1021 158..161 '_x3': i32
998 164..246 'match ... }': i32 1022 164..246 'match ... }': i32
999 170..174 'true': bool 1023 170..174 'true': bool
1000 185..189 'true': bool 1024 185..189 'true': bool
1001 185..189 'true': bool 1025 185..189 'true': bool
1002 193..194 '3': i32 1026 193..194 '3': i32
1003 204..205 '_': bool 1027 204..205 '_': bool
1004 209..240 '{ ... }': i32 1028 209..240 '{ ... }': i32
1005 223..229 'return': ! 1029 223..229 'return': !
1006 256..259 '_x4': i32 1030 256..259 '_x4': i32
1007 262..319 'match ... }': i32 1031 262..319 'match ... }': i32
1008 268..272 'true': bool 1032 268..272 'true': bool
1009 283..287 'true': bool 1033 283..287 'true': bool
1010 283..287 'true': bool 1034 283..287 'true': bool
1011 291..292 '4': i32 1035 291..292 '4': i32
1012 302..303 '_': bool 1036 302..303 '_': bool
1013 307..313 'return': ! 1037 307..313 'return': !
1014 "### 1038 "#]],
1015 ) 1039 )
1016} 1040}
1017 1041
1018#[test] 1042#[test]
1019fn infer_inherent_method() { 1043fn infer_inherent_method() {
1020 assert_snapshot!( 1044 check_infer(
1021 infer(r#" 1045 r#"
1022struct A; 1046 struct A;
1023 1047
1024impl A { 1048 impl A {
1025 fn foo(self, x: u32) -> i32 {} 1049 fn foo(self, x: u32) -> i32 {}
1026} 1050 }
1027 1051
1028mod b { 1052 mod b {
1029 impl super::A { 1053 impl super::A {
1030 fn bar(&self, x: u64) -> i64 {} 1054 fn bar(&self, x: u64) -> i64 {}
1031 } 1055 }
1032} 1056 }
1033 1057
1034fn test(a: A) { 1058 fn test(a: A) {
1035 a.foo(1); 1059 a.foo(1);
1036 (&a).bar(1); 1060 (&a).bar(1);
1037 a.bar(1); 1061 a.bar(1);
1038} 1062 }
1039"#), 1063 "#,
1040 @r###" 1064 expect![[r#"
1041 31..35 'self': A 1065 31..35 'self': A
1042 37..38 'x': u32 1066 37..38 'x': u32
1043 52..54 '{}': () 1067 52..54 '{}': ()
1044 102..106 'self': &A 1068 102..106 'self': &A
1045 108..109 'x': u64 1069 108..109 'x': u64
1046 123..125 '{}': () 1070 123..125 '{}': ()
1047 143..144 'a': A 1071 143..144 'a': A
1048 149..197 '{ ...(1); }': () 1072 149..197 '{ ...(1); }': ()
1049 155..156 'a': A 1073 155..156 'a': A
1050 155..163 'a.foo(1)': i32 1074 155..163 'a.foo(1)': i32
1051 161..162 '1': u32 1075 161..162 '1': u32
1052 169..180 '(&a).bar(1)': i64 1076 169..180 '(&a).bar(1)': i64
1053 170..172 '&a': &A 1077 170..172 '&a': &A
1054 171..172 'a': A 1078 171..172 'a': A
1055 178..179 '1': u64 1079 178..179 '1': u64
1056 186..187 'a': A 1080 186..187 'a': A
1057 186..194 'a.bar(1)': i64 1081 186..194 'a.bar(1)': i64
1058 192..193 '1': u64 1082 192..193 '1': u64
1059 "### 1083 "#]],
1060 ); 1084 );
1061} 1085}
1062 1086
1063#[test] 1087#[test]
1064fn infer_inherent_method_str() { 1088fn infer_inherent_method_str() {
1065 assert_snapshot!( 1089 check_infer(
1066 infer(r#" 1090 r#"
1067#[lang = "str"] 1091 #[lang = "str"]
1068impl str { 1092 impl str {
1069 fn foo(&self) -> i32 {} 1093 fn foo(&self) -> i32 {}
1070} 1094 }
1071 1095
1072fn test() { 1096 fn test() {
1073 "foo".foo(); 1097 "foo".foo();
1074} 1098 }
1075"#), 1099 "#,
1076 @r###" 1100 expect![[r#"
1077 39..43 'self': &str 1101 39..43 'self': &str
1078 52..54 '{}': () 1102 52..54 '{}': ()
1079 68..88 '{ ...o(); }': () 1103 68..88 '{ ...o(); }': ()
1080 74..79 '"foo"': &str 1104 74..79 '"foo"': &str
1081 74..85 '"foo".foo()': i32 1105 74..85 '"foo".foo()': i32
1082 "### 1106 "#]],
1083 ); 1107 );
1084} 1108}
1085 1109
1086#[test] 1110#[test]
1087fn infer_tuple() { 1111fn infer_tuple() {
1088 assert_snapshot!( 1112 check_infer(
1089 infer(r#" 1113 r#"
1090fn test(x: &str, y: isize) { 1114 fn test(x: &str, y: isize) {
1091 let a: (u32, &str) = (1, "a"); 1115 let a: (u32, &str) = (1, "a");
1092 let b = (a, x); 1116 let b = (a, x);
1093 let c = (y, x); 1117 let c = (y, x);
1094 let d = (c, x); 1118 let d = (c, x);
1095 let e = (1, "e"); 1119 let e = (1, "e");
1096 let f = (e, "d"); 1120 let f = (e, "d");
1097} 1121 }
1098"#), 1122 "#,
1099 @r###" 1123 expect![[r#"
1100 8..9 'x': &str 1124 8..9 'x': &str
1101 17..18 'y': isize 1125 17..18 'y': isize
1102 27..169 '{ ...d"); }': () 1126 27..169 '{ ...d"); }': ()
1103 37..38 'a': (u32, &str) 1127 37..38 'a': (u32, &str)
1104 54..62 '(1, "a")': (u32, &str) 1128 54..62 '(1, "a")': (u32, &str)
1105 55..56 '1': u32 1129 55..56 '1': u32
1106 58..61 '"a"': &str 1130 58..61 '"a"': &str
1107 72..73 'b': ((u32, &str), &str) 1131 72..73 'b': ((u32, &str), &str)
1108 76..82 '(a, x)': ((u32, &str), &str) 1132 76..82 '(a, x)': ((u32, &str), &str)
1109 77..78 'a': (u32, &str) 1133 77..78 'a': (u32, &str)
1110 80..81 'x': &str 1134 80..81 'x': &str
1111 92..93 'c': (isize, &str) 1135 92..93 'c': (isize, &str)
1112 96..102 '(y, x)': (isize, &str) 1136 96..102 '(y, x)': (isize, &str)
1113 97..98 'y': isize 1137 97..98 'y': isize
1114 100..101 'x': &str 1138 100..101 'x': &str
1115 112..113 'd': ((isize, &str), &str) 1139 112..113 'd': ((isize, &str), &str)
1116 116..122 '(c, x)': ((isize, &str), &str) 1140 116..122 '(c, x)': ((isize, &str), &str)
1117 117..118 'c': (isize, &str) 1141 117..118 'c': (isize, &str)
1118 120..121 'x': &str 1142 120..121 'x': &str
1119 132..133 'e': (i32, &str) 1143 132..133 'e': (i32, &str)
1120 136..144 '(1, "e")': (i32, &str) 1144 136..144 '(1, "e")': (i32, &str)
1121 137..138 '1': i32 1145 137..138 '1': i32
1122 140..143 '"e"': &str 1146 140..143 '"e"': &str
1123 154..155 'f': ((i32, &str), &str) 1147 154..155 'f': ((i32, &str), &str)
1124 158..166 '(e, "d")': ((i32, &str), &str) 1148 158..166 '(e, "d")': ((i32, &str), &str)
1125 159..160 'e': (i32, &str) 1149 159..160 'e': (i32, &str)
1126 162..165 '"d"': &str 1150 162..165 '"d"': &str
1127 "### 1151 "#]],
1128 ); 1152 );
1129} 1153}
1130 1154
1131#[test] 1155#[test]
1132fn infer_array() { 1156fn infer_array() {
1133 assert_snapshot!( 1157 check_infer(
1134 infer(r#" 1158 r#"
1135fn test(x: &str, y: isize) { 1159 fn test(x: &str, y: isize) {
1136 let a = [x]; 1160 let a = [x];
1137 let b = [a, a]; 1161 let b = [a, a];
1138 let c = [b, b]; 1162 let c = [b, b];
1139 1163
1140 let d = [y, 1, 2, 3]; 1164 let d = [y, 1, 2, 3];
1141 let d = [1, y, 2, 3]; 1165 let d = [1, y, 2, 3];
1142 let e = [y]; 1166 let e = [y];
1143 let f = [d, d]; 1167 let f = [d, d];
1144 let g = [e, e]; 1168 let g = [e, e];
1145 1169
1146 let h = [1, 2]; 1170 let h = [1, 2];
1147 let i = ["a", "b"]; 1171 let i = ["a", "b"];
1148 1172
1149 let b = [a, ["b"]]; 1173 let b = [a, ["b"]];
1150 let x: [u8; 0] = []; 1174 let x: [u8; 0] = [];
1151} 1175 }
1152"#), 1176 "#,
1153 @r###" 1177 expect![[r#"
1154 8..9 'x': &str 1178 8..9 'x': &str
1155 17..18 'y': isize 1179 17..18 'y': isize
1156 27..292 '{ ... []; }': () 1180 27..292 '{ ... []; }': ()
1157 37..38 'a': [&str; _] 1181 37..38 'a': [&str; _]
1158 41..44 '[x]': [&str; _] 1182 41..44 '[x]': [&str; _]
1159 42..43 'x': &str 1183 42..43 'x': &str
1160 54..55 'b': [[&str; _]; _] 1184 54..55 'b': [[&str; _]; _]
1161 58..64 '[a, a]': [[&str; _]; _] 1185 58..64 '[a, a]': [[&str; _]; _]
1162 59..60 'a': [&str; _] 1186 59..60 'a': [&str; _]
1163 62..63 'a': [&str; _] 1187 62..63 'a': [&str; _]
1164 74..75 'c': [[[&str; _]; _]; _] 1188 74..75 'c': [[[&str; _]; _]; _]
1165 78..84 '[b, b]': [[[&str; _]; _]; _] 1189 78..84 '[b, b]': [[[&str; _]; _]; _]
1166 79..80 'b': [[&str; _]; _] 1190 79..80 'b': [[&str; _]; _]
1167 82..83 'b': [[&str; _]; _] 1191 82..83 'b': [[&str; _]; _]
1168 95..96 'd': [isize; _] 1192 95..96 'd': [isize; _]
1169 99..111 '[y, 1, 2, 3]': [isize; _] 1193 99..111 '[y, 1, 2, 3]': [isize; _]
1170 100..101 'y': isize 1194 100..101 'y': isize
1171 103..104 '1': isize 1195 103..104 '1': isize
1172 106..107 '2': isize 1196 106..107 '2': isize
1173 109..110 '3': isize 1197 109..110 '3': isize
1174 121..122 'd': [isize; _] 1198 121..122 'd': [isize; _]
1175 125..137 '[1, y, 2, 3]': [isize; _] 1199 125..137 '[1, y, 2, 3]': [isize; _]
1176 126..127 '1': isize 1200 126..127 '1': isize
1177 129..130 'y': isize 1201 129..130 'y': isize
1178 132..133 '2': isize 1202 132..133 '2': isize
1179 135..136 '3': isize 1203 135..136 '3': isize
1180 147..148 'e': [isize; _] 1204 147..148 'e': [isize; _]
1181 151..154 '[y]': [isize; _] 1205 151..154 '[y]': [isize; _]
1182 152..153 'y': isize 1206 152..153 'y': isize
1183 164..165 'f': [[isize; _]; _] 1207 164..165 'f': [[isize; _]; _]
1184 168..174 '[d, d]': [[isize; _]; _] 1208 168..174 '[d, d]': [[isize; _]; _]
1185 169..170 'd': [isize; _] 1209 169..170 'd': [isize; _]
1186 172..173 'd': [isize; _] 1210 172..173 'd': [isize; _]
1187 184..185 'g': [[isize; _]; _] 1211 184..185 'g': [[isize; _]; _]
1188 188..194 '[e, e]': [[isize; _]; _] 1212 188..194 '[e, e]': [[isize; _]; _]
1189 189..190 'e': [isize; _] 1213 189..190 'e': [isize; _]
1190 192..193 'e': [isize; _] 1214 192..193 'e': [isize; _]
1191 205..206 'h': [i32; _] 1215 205..206 'h': [i32; _]
1192 209..215 '[1, 2]': [i32; _] 1216 209..215 '[1, 2]': [i32; _]
1193 210..211 '1': i32 1217 210..211 '1': i32
1194 213..214 '2': i32 1218 213..214 '2': i32
1195 225..226 'i': [&str; _] 1219 225..226 'i': [&str; _]
1196 229..239 '["a", "b"]': [&str; _] 1220 229..239 '["a", "b"]': [&str; _]
1197 230..233 '"a"': &str 1221 230..233 '"a"': &str
1198 235..238 '"b"': &str 1222 235..238 '"b"': &str
1199 250..251 'b': [[&str; _]; _] 1223 250..251 'b': [[&str; _]; _]
1200 254..264 '[a, ["b"]]': [[&str; _]; _] 1224 254..264 '[a, ["b"]]': [[&str; _]; _]
1201 255..256 'a': [&str; _] 1225 255..256 'a': [&str; _]
1202 258..263 '["b"]': [&str; _] 1226 258..263 '["b"]': [&str; _]
1203 259..262 '"b"': &str 1227 259..262 '"b"': &str
1204 274..275 'x': [u8; _] 1228 274..275 'x': [u8; _]
1205 287..289 '[]': [u8; _] 1229 287..289 '[]': [u8; _]
1206 "### 1230 "#]],
1207 ); 1231 );
1208} 1232}
1209 1233
1210#[test] 1234#[test]
1211fn infer_struct_generics() { 1235fn infer_struct_generics() {
1212 assert_snapshot!( 1236 check_infer(
1213 infer(r#" 1237 r#"
1214struct A<T> { 1238 struct A<T> {
1215 x: T, 1239 x: T,
1216} 1240 }
1217 1241
1218fn test(a1: A<u32>, i: i32) { 1242 fn test(a1: A<u32>, i: i32) {
1219 a1.x; 1243 a1.x;
1220 let a2 = A { x: i }; 1244 let a2 = A { x: i };
1221 a2.x; 1245 a2.x;
1222 let a3 = A::<i128> { x: 1 }; 1246 let a3 = A::<i128> { x: 1 };
1223 a3.x; 1247 a3.x;
1224} 1248 }
1225"#), 1249 "#,
1226 @r###" 1250 expect![[r#"
1227 35..37 'a1': A<u32> 1251 35..37 'a1': A<u32>
1228 47..48 'i': i32 1252 47..48 'i': i32
1229 55..146 '{ ...3.x; }': () 1253 55..146 '{ ...3.x; }': ()
1230 61..63 'a1': A<u32> 1254 61..63 'a1': A<u32>
1231 61..65 'a1.x': u32 1255 61..65 'a1.x': u32
1232 75..77 'a2': A<i32> 1256 75..77 'a2': A<i32>
1233 80..90 'A { x: i }': A<i32> 1257 80..90 'A { x: i }': A<i32>
1234 87..88 'i': i32 1258 87..88 'i': i32
1235 96..98 'a2': A<i32> 1259 96..98 'a2': A<i32>
1236 96..100 'a2.x': i32 1260 96..100 'a2.x': i32
1237 110..112 'a3': A<i128> 1261 110..112 'a3': A<i128>
1238 115..133 'A::<i1...x: 1 }': A<i128> 1262 115..133 'A::<i1...x: 1 }': A<i128>
1239 130..131 '1': i128 1263 130..131 '1': i128
1240 139..141 'a3': A<i128> 1264 139..141 'a3': A<i128>
1241 139..143 'a3.x': i128 1265 139..143 'a3.x': i128
1242 "### 1266 "#]],
1243 ); 1267 );
1244} 1268}
1245 1269
1246#[test] 1270#[test]
1247fn infer_tuple_struct_generics() { 1271fn infer_tuple_struct_generics() {
1248 assert_snapshot!( 1272 check_infer(
1249 infer(r#" 1273 r#"
1250struct A<T>(T); 1274 struct A<T>(T);
1251enum Option<T> { Some(T), None } 1275 enum Option<T> { Some(T), None }
1252use Option::*; 1276 use Option::*;
1253 1277
1254fn test() { 1278 fn test() {
1255 A(42); 1279 A(42);
1256 A(42u128); 1280 A(42u128);
1257 Some("x"); 1281 Some("x");
1258 Option::Some("x"); 1282 Option::Some("x");
1259 None; 1283 None;
1260 let x: Option<i64> = None; 1284 let x: Option<i64> = None;
1261} 1285 }
1262"#), 1286 "#,
1263 @r###" 1287 expect![[r#"
1264 75..183 '{ ...one; }': () 1288 75..183 '{ ...one; }': ()
1265 81..82 'A': A<i32>(i32) -> A<i32> 1289 81..82 'A': A<i32>(i32) -> A<i32>
1266 81..86 'A(42)': A<i32> 1290 81..86 'A(42)': A<i32>
1267 83..85 '42': i32 1291 83..85 '42': i32
1268 92..93 'A': A<u128>(u128) -> A<u128> 1292 92..93 'A': A<u128>(u128) -> A<u128>
1269 92..101 'A(42u128)': A<u128> 1293 92..101 'A(42u128)': A<u128>
1270 94..100 '42u128': u128 1294 94..100 '42u128': u128
1271 107..111 'Some': Some<&str>(&str) -> Option<&str> 1295 107..111 'Some': Some<&str>(&str) -> Option<&str>
1272 107..116 'Some("x")': Option<&str> 1296 107..116 'Some("x")': Option<&str>
1273 112..115 '"x"': &str 1297 112..115 '"x"': &str
1274 122..134 'Option::Some': Some<&str>(&str) -> Option<&str> 1298 122..134 'Option::Some': Some<&str>(&str) -> Option<&str>
1275 122..139 'Option...e("x")': Option<&str> 1299 122..139 'Option...e("x")': Option<&str>
1276 135..138 '"x"': &str 1300 135..138 '"x"': &str
1277 145..149 'None': Option<{unknown}> 1301 145..149 'None': Option<{unknown}>
1278 159..160 'x': Option<i64> 1302 159..160 'x': Option<i64>
1279 176..180 'None': Option<i64> 1303 176..180 'None': Option<i64>
1280 "### 1304 "#]],
1281 ); 1305 );
1282} 1306}
1283 1307
1284#[test] 1308#[test]
1285fn infer_function_generics() { 1309fn infer_function_generics() {
1286 assert_snapshot!( 1310 check_infer(
1287 infer(r#" 1311 r#"
1288fn id<T>(t: T) -> T { t } 1312 fn id<T>(t: T) -> T { t }
1289 1313
1290fn test() { 1314 fn test() {
1291 id(1u32); 1315 id(1u32);
1292 id::<i128>(1); 1316 id::<i128>(1);
1293 let x: u64 = id(1); 1317 let x: u64 = id(1);
1294} 1318 }
1295"#), 1319 "#,
1296 @r###" 1320 expect![[r#"
1297 9..10 't': T 1321 9..10 't': T
1298 20..25 '{ t }': T 1322 20..25 '{ t }': T
1299 22..23 't': T 1323 22..23 't': T
1300 37..97 '{ ...(1); }': () 1324 37..97 '{ ...(1); }': ()
1301 43..45 'id': fn id<u32>(u32) -> u32 1325 43..45 'id': fn id<u32>(u32) -> u32
1302 43..51 'id(1u32)': u32 1326 43..51 'id(1u32)': u32
1303 46..50 '1u32': u32 1327 46..50 '1u32': u32
1304 57..67 'id::<i128>': fn id<i128>(i128) -> i128 1328 57..67 'id::<i128>': fn id<i128>(i128) -> i128
1305 57..70 'id::<i128>(1)': i128 1329 57..70 'id::<i128>(1)': i128
1306 68..69 '1': i128 1330 68..69 '1': i128
1307 80..81 'x': u64 1331 80..81 'x': u64
1308 89..91 'id': fn id<u64>(u64) -> u64 1332 89..91 'id': fn id<u64>(u64) -> u64
1309 89..94 'id(1)': u64 1333 89..94 'id(1)': u64
1310 92..93 '1': u64 1334 92..93 '1': u64
1311 "### 1335 "#]],
1312 ); 1336 );
1313} 1337}
1314 1338
1315#[test] 1339#[test]
1316fn infer_impl_generics_basic() { 1340fn infer_impl_generics_basic() {
1317 assert_snapshot!( 1341 check_infer(
1318 infer(r#" 1342 r#"
1319struct A<T1, T2> { 1343 struct A<T1, T2> {
1320 x: T1, 1344 x: T1,
1321 y: T2, 1345 y: T2,
1322} 1346 }
1323impl<Y, X> A<X, Y> { 1347 impl<Y, X> A<X, Y> {
1324 fn x(self) -> X { 1348 fn x(self) -> X {
1325 self.x 1349 self.x
1326 } 1350 }
1327 fn y(self) -> Y { 1351 fn y(self) -> Y {
1328 self.y 1352 self.y
1329 } 1353 }
1330 fn z<T>(self, t: T) -> (X, Y, T) { 1354 fn z<T>(self, t: T) -> (X, Y, T) {
1331 (self.x, self.y, t) 1355 (self.x, self.y, t)
1332 } 1356 }
1333} 1357 }
1334 1358
1335fn test() -> i128 { 1359 fn test() -> i128 {
1336 let a = A { x: 1u64, y: 1i64 }; 1360 let a = A { x: 1u64, y: 1i64 };
1337 a.x(); 1361 a.x();
1338 a.y(); 1362 a.y();
1339 a.z(1i128); 1363 a.z(1i128);
1340 a.z::<u128>(1); 1364 a.z::<u128>(1);
1341} 1365 }
1342"#), 1366 "#,
1343 @r###" 1367 expect![[r#"
1344 73..77 'self': A<X, Y> 1368 73..77 'self': A<X, Y>
1345 84..106 '{ ... }': X 1369 84..106 '{ ... }': X
1346 94..98 'self': A<X, Y> 1370 94..98 'self': A<X, Y>
1347 94..100 'self.x': X 1371 94..100 'self.x': X
1348 116..120 'self': A<X, Y> 1372 116..120 'self': A<X, Y>
1349 127..149 '{ ... }': Y 1373 127..149 '{ ... }': Y
1350 137..141 'self': A<X, Y> 1374 137..141 'self': A<X, Y>
1351 137..143 'self.y': Y 1375 137..143 'self.y': Y
1352 162..166 'self': A<X, Y> 1376 162..166 'self': A<X, Y>
1353 168..169 't': T 1377 168..169 't': T
1354 187..222 '{ ... }': (X, Y, T) 1378 187..222 '{ ... }': (X, Y, T)
1355 197..216 '(self.....y, t)': (X, Y, T) 1379 197..216 '(self.....y, t)': (X, Y, T)
1356 198..202 'self': A<X, Y> 1380 198..202 'self': A<X, Y>
1357 198..204 'self.x': X 1381 198..204 'self.x': X
1358 206..210 'self': A<X, Y> 1382 206..210 'self': A<X, Y>
1359 206..212 'self.y': Y 1383 206..212 'self.y': Y
1360 214..215 't': T 1384 214..215 't': T
1361 244..341 '{ ...(1); }': () 1385 244..341 '{ ...(1); }': ()
1362 254..255 'a': A<u64, i64> 1386 254..255 'a': A<u64, i64>
1363 258..280 'A { x:...1i64 }': A<u64, i64> 1387 258..280 'A { x:...1i64 }': A<u64, i64>
1364 265..269 '1u64': u64 1388 265..269 '1u64': u64
1365 274..278 '1i64': i64 1389 274..278 '1i64': i64
1366 286..287 'a': A<u64, i64> 1390 286..287 'a': A<u64, i64>
1367 286..291 'a.x()': u64 1391 286..291 'a.x()': u64
1368 297..298 'a': A<u64, i64> 1392 297..298 'a': A<u64, i64>
1369 297..302 'a.y()': i64 1393 297..302 'a.y()': i64
1370 308..309 'a': A<u64, i64> 1394 308..309 'a': A<u64, i64>
1371 308..318 'a.z(1i128)': (u64, i64, i128) 1395 308..318 'a.z(1i128)': (u64, i64, i128)
1372 312..317 '1i128': i128 1396 312..317 '1i128': i128
1373 324..325 'a': A<u64, i64> 1397 324..325 'a': A<u64, i64>
1374 324..338 'a.z::<u128>(1)': (u64, i64, u128) 1398 324..338 'a.z::<u128>(1)': (u64, i64, u128)
1375 336..337 '1': u128 1399 336..337 '1': u128
1376 "### 1400 "#]],
1377 ); 1401 );
1378} 1402}
1379 1403
1380#[test] 1404#[test]
1381fn infer_impl_generics_with_autoderef() { 1405fn infer_impl_generics_with_autoderef() {
1382 assert_snapshot!( 1406 check_infer(
1383 infer(r#" 1407 r#"
1384enum Option<T> { 1408 enum Option<T> {
1385 Some(T), 1409 Some(T),
1386 None, 1410 None,
1387} 1411 }
1388impl<T> Option<T> { 1412 impl<T> Option<T> {
1389 fn as_ref(&self) -> Option<&T> {} 1413 fn as_ref(&self) -> Option<&T> {}
1390} 1414 }
1391fn test(o: Option<u32>) { 1415 fn test(o: Option<u32>) {
1392 (&o).as_ref(); 1416 (&o).as_ref();
1393 o.as_ref(); 1417 o.as_ref();
1394} 1418 }
1395"#), 1419 "#,
1396 @r###" 1420 expect![[r#"
1397 77..81 'self': &Option<T> 1421 77..81 'self': &Option<T>
1398 97..99 '{}': () 1422 97..99 '{}': ()
1399 110..111 'o': Option<u32> 1423 110..111 'o': Option<u32>
1400 126..164 '{ ...f(); }': () 1424 126..164 '{ ...f(); }': ()
1401 132..145 '(&o).as_ref()': Option<&u32> 1425 132..145 '(&o).as_ref()': Option<&u32>
1402 133..135 '&o': &Option<u32> 1426 133..135 '&o': &Option<u32>
1403 134..135 'o': Option<u32> 1427 134..135 'o': Option<u32>
1404 151..152 'o': Option<u32> 1428 151..152 'o': Option<u32>
1405 151..161 'o.as_ref()': Option<&u32> 1429 151..161 'o.as_ref()': Option<&u32>
1406 "### 1430 "#]],
1407 ); 1431 );
1408} 1432}
1409 1433
1410#[test] 1434#[test]
1411fn infer_generic_chain() { 1435fn infer_generic_chain() {
1412 assert_snapshot!( 1436 check_infer(
1413 infer(r#" 1437 r#"
1414struct A<T> { 1438 struct A<T> {
1415 x: T, 1439 x: T,
1416} 1440 }
1417impl<T2> A<T2> { 1441 impl<T2> A<T2> {
1418 fn x(self) -> T2 { 1442 fn x(self) -> T2 {
1419 self.x 1443 self.x
1420 } 1444 }
1421} 1445 }
1422fn id<T>(t: T) -> T { t } 1446 fn id<T>(t: T) -> T { t }
1423 1447
1424fn test() -> i128 { 1448 fn test() -> i128 {
1425 let x = 1; 1449 let x = 1;
1426 let y = id(x); 1450 let y = id(x);
1427 let a = A { x: id(y) }; 1451 let a = A { x: id(y) };
1428 let z = id(a.x); 1452 let z = id(a.x);
1429 let b = A { x: z }; 1453 let b = A { x: z };
1430 b.x() 1454 b.x()
1431} 1455 }
1432"#), 1456 "#,
1433 @r###" 1457 expect![[r#"
1434 52..56 'self': A<T2> 1458 52..56 'self': A<T2>
1435 64..86 '{ ... }': T2 1459 64..86 '{ ... }': T2
1436 74..78 'self': A<T2> 1460 74..78 'self': A<T2>
1437 74..80 'self.x': T2 1461 74..80 'self.x': T2
1438 98..99 't': T 1462 98..99 't': T
1439 109..114 '{ t }': T 1463 109..114 '{ t }': T
1440 111..112 't': T 1464 111..112 't': T
1441 134..260 '{ ....x() }': i128 1465 134..254 '{ ....x() }': i128
1442 145..146 'x': i128 1466 144..145 'x': i128
1443 149..150 '1': i128 1467 148..149 '1': i128
1444 161..162 'y': i128 1468 159..160 'y': i128
1445 165..167 'id': fn id<i128>(i128) -> i128 1469 163..165 'id': fn id<i128>(i128) -> i128
1446 165..170 'id(x)': i128 1470 163..168 'id(x)': i128
1447 168..169 'x': i128 1471 166..167 'x': i128
1448 181..182 'a': A<i128> 1472 178..179 'a': A<i128>
1449 185..199 'A { x: id(y) }': A<i128> 1473 182..196 'A { x: id(y) }': A<i128>
1450 192..194 'id': fn id<i128>(i128) -> i128 1474 189..191 'id': fn id<i128>(i128) -> i128
1451 192..197 'id(y)': i128 1475 189..194 'id(y)': i128
1452 195..196 'y': i128 1476 192..193 'y': i128
1453 210..211 'z': i128 1477 206..207 'z': i128
1454 214..216 'id': fn id<i128>(i128) -> i128 1478 210..212 'id': fn id<i128>(i128) -> i128
1455 214..221 'id(a.x)': i128 1479 210..217 'id(a.x)': i128
1456 217..218 'a': A<i128> 1480 213..214 'a': A<i128>
1457 217..220 'a.x': i128 1481 213..216 'a.x': i128
1458 232..233 'b': A<i128> 1482 227..228 'b': A<i128>
1459 236..246 'A { x: z }': A<i128> 1483 231..241 'A { x: z }': A<i128>
1460 243..244 'z': i128 1484 238..239 'z': i128
1461 253..254 'b': A<i128> 1485 247..248 'b': A<i128>
1462 253..258 'b.x()': i128 1486 247..252 'b.x()': i128
1463 "### 1487 "#]],
1464 ); 1488 );
1465} 1489}
1466 1490
1467#[test] 1491#[test]
1468fn infer_associated_const() { 1492fn infer_associated_const() {
1469 assert_snapshot!( 1493 check_infer(
1470 infer(r#" 1494 r#"
1471struct Struct; 1495 struct Struct;
1472 1496
1473impl Struct { 1497 impl Struct {
1474 const FOO: u32 = 1; 1498 const FOO: u32 = 1;
1475} 1499 }
1476 1500
1477enum Enum {} 1501 enum Enum {}
1478 1502
1479impl Enum { 1503 impl Enum {
1480 const BAR: u32 = 2; 1504 const BAR: u32 = 2;
1481} 1505 }
1482 1506
1483trait Trait { 1507 trait Trait {
1484 const ID: u32; 1508 const ID: u32;
1485} 1509 }
1486 1510
1487struct TraitTest; 1511 struct TraitTest;
1488 1512
1489impl Trait for TraitTest { 1513 impl Trait for TraitTest {
1490 const ID: u32 = 5; 1514 const ID: u32 = 5;
1491} 1515 }
1492 1516
1493fn test() { 1517 fn test() {
1494 let x = Struct::FOO; 1518 let x = Struct::FOO;
1495 let y = Enum::BAR; 1519 let y = Enum::BAR;
1496 let z = TraitTest::ID; 1520 let z = TraitTest::ID;
1497} 1521 }
1498"#), 1522 "#,
1499 @r###" 1523 expect![[r#"
1500 51..52 '1': u32 1524 51..52 '1': u32
1501 104..105 '2': u32 1525 104..105 '2': u32
1502 212..213 '5': u32 1526 212..213 '5': u32
1503 228..306 '{ ...:ID; }': () 1527 228..306 '{ ...:ID; }': ()
1504 238..239 'x': u32 1528 238..239 'x': u32
1505 242..253 'Struct::FOO': u32 1529 242..253 'Struct::FOO': u32
1506 263..264 'y': u32 1530 263..264 'y': u32
1507 267..276 'Enum::BAR': u32 1531 267..276 'Enum::BAR': u32
1508 286..287 'z': u32 1532 286..287 'z': u32
1509 290..303 'TraitTest::ID': u32 1533 290..303 'TraitTest::ID': u32
1510 "### 1534 "#]],
1511 ); 1535 );
1512} 1536}
1513 1537
1514#[test] 1538#[test]
1515fn infer_type_alias() { 1539fn infer_type_alias() {
1516 assert_snapshot!( 1540 check_infer(
1517 infer(r#" 1541 r#"
1518struct A<X, Y> { x: X, y: Y } 1542 struct A<X, Y> { x: X, y: Y }
1519type Foo = A<u32, i128>; 1543 type Foo = A<u32, i128>;
1520type Bar<T> = A<T, u128>; 1544 type Bar<T> = A<T, u128>;
1521type Baz<U, V> = A<V, U>; 1545 type Baz<U, V> = A<V, U>;
1522fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) { 1546 fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
1523 x.x; 1547 x.x;
1524 x.y; 1548 x.y;
1525 y.x; 1549 y.x;
1526 y.y; 1550 y.y;
1527 z.x; 1551 z.x;
1528 z.y; 1552 z.y;
1529} 1553 }
1530"#), 1554 "#,
1531 @r###" 1555 expect![[r#"
1532 115..116 'x': A<u32, i128> 1556 115..116 'x': A<u32, i128>
1533 123..124 'y': A<&str, u128> 1557 123..124 'y': A<&str, u128>
1534 137..138 'z': A<u8, i8> 1558 137..138 'z': A<u8, i8>
1535 153..210 '{ ...z.y; }': () 1559 153..210 '{ ...z.y; }': ()
1536 159..160 'x': A<u32, i128> 1560 159..160 'x': A<u32, i128>
1537 159..162 'x.x': u32 1561 159..162 'x.x': u32
1538 168..169 'x': A<u32, i128> 1562 168..169 'x': A<u32, i128>
1539 168..171 'x.y': i128 1563 168..171 'x.y': i128
1540 177..178 'y': A<&str, u128> 1564 177..178 'y': A<&str, u128>
1541 177..180 'y.x': &str 1565 177..180 'y.x': &str
1542 186..187 'y': A<&str, u128> 1566 186..187 'y': A<&str, u128>
1543 186..189 'y.y': u128 1567 186..189 'y.y': u128
1544 195..196 'z': A<u8, i8> 1568 195..196 'z': A<u8, i8>
1545 195..198 'z.x': u8 1569 195..198 'z.x': u8
1546 204..205 'z': A<u8, i8> 1570 204..205 'z': A<u8, i8>
1547 204..207 'z.y': i8 1571 204..207 'z.y': i8
1548 "### 1572 "#]],
1549 ) 1573 )
1550} 1574}
1551 1575
1552#[test] 1576#[test]
1553fn recursive_type_alias() { 1577fn recursive_type_alias() {
1554 assert_snapshot!( 1578 check_infer(
1555 infer(r#" 1579 r#"
1556struct A<X> {} 1580 struct A<X> {}
1557type Foo = Foo; 1581 type Foo = Foo;
1558type Bar = A<Bar>; 1582 type Bar = A<Bar>;
1559fn test(x: Foo) {} 1583 fn test(x: Foo) {}
1560"#), 1584 "#,
1561 @r###" 1585 expect![[r#"
1562 58..59 'x': {unknown} 1586 58..59 'x': {unknown}
1563 66..68 '{}': () 1587 66..68 '{}': ()
1564 "### 1588 "#]],
1565 ) 1589 )
1566} 1590}
1567 1591
1568#[test] 1592#[test]
1569fn infer_type_param() { 1593fn infer_type_param() {
1570 assert_snapshot!( 1594 check_infer(
1571 infer(r#" 1595 r#"
1572fn id<T>(x: T) -> T { 1596 fn id<T>(x: T) -> T {
1573 x 1597 x
1574} 1598 }
1575 1599
1576fn clone<T>(x: &T) -> T { 1600 fn clone<T>(x: &T) -> T {
1577 *x 1601 *x
1578} 1602 }
1579 1603
1580fn test() { 1604 fn test() {
1581 let y = 10u32; 1605 let y = 10u32;
1582 id(y); 1606 id(y);
1583 let x: bool = clone(z); 1607 let x: bool = clone(z);
1584 id::<i128>(1); 1608 id::<i128>(1);
1585} 1609 }
1586"#), 1610 "#,
1587 @r###" 1611 expect![[r#"
1588 9..10 'x': T 1612 9..10 'x': T
1589 20..29 '{ x }': T 1613 20..29 '{ x }': T
1590 26..27 'x': T 1614 26..27 'x': T
1591 43..44 'x': &T 1615 43..44 'x': &T
1592 55..65 '{ *x }': T 1616 55..65 '{ *x }': T
1593 61..63 '*x': T 1617 61..63 '*x': T
1594 62..63 'x': &T 1618 62..63 'x': &T
1595 77..157 '{ ...(1); }': () 1619 77..157 '{ ...(1); }': ()
1596 87..88 'y': u32 1620 87..88 'y': u32
1597 91..96 '10u32': u32 1621 91..96 '10u32': u32
1598 102..104 'id': fn id<u32>(u32) -> u32 1622 102..104 'id': fn id<u32>(u32) -> u32
1599 102..107 'id(y)': u32 1623 102..107 'id(y)': u32
1600 105..106 'y': u32 1624 105..106 'y': u32
1601 117..118 'x': bool 1625 117..118 'x': bool
1602 127..132 'clone': fn clone<bool>(&bool) -> bool 1626 127..132 'clone': fn clone<bool>(&bool) -> bool
1603 127..135 'clone(z)': bool 1627 127..135 'clone(z)': bool
1604 133..134 'z': &bool 1628 133..134 'z': &bool
1605 141..151 'id::<i128>': fn id<i128>(i128) -> i128 1629 141..151 'id::<i128>': fn id<i128>(i128) -> i128
1606 141..154 'id::<i128>(1)': i128 1630 141..154 'id::<i128>(1)': i128
1607 152..153 '1': i128 1631 152..153 '1': i128
1608 "### 1632 "#]],
1609 ); 1633 );
1610} 1634}
1611 1635
1612#[test] 1636#[test]
1613fn infer_const() { 1637fn infer_const() {
1614 assert_snapshot!( 1638 check_infer(
1615 infer(r#" 1639 r#"
1616struct Foo; 1640 struct Foo;
1617impl Foo { const ASSOC_CONST: u32 = 0; } 1641 impl Foo { const ASSOC_CONST: u32 = 0; }
1618const GLOBAL_CONST: u32 = 101; 1642 const GLOBAL_CONST: u32 = 101;
1619fn test() { 1643 fn test() {
1620 const LOCAL_CONST: u32 = 99; 1644 const LOCAL_CONST: u32 = 99;
1621 let x = LOCAL_CONST; 1645 let x = LOCAL_CONST;
1622 let z = GLOBAL_CONST; 1646 let z = GLOBAL_CONST;
1623 let id = Foo::ASSOC_CONST; 1647 let id = Foo::ASSOC_CONST;
1624} 1648 }
1625"#), 1649 "#,
1626 @r###" 1650 expect![[r#"
1627 48..49 '0': u32 1651 48..49 '0': u32
1628 79..82 '101': u32 1652 79..82 '101': u32
1629 94..212 '{ ...NST; }': () 1653 94..212 '{ ...NST; }': ()
1630 137..138 'x': u32 1654 137..138 'x': u32
1631 141..152 'LOCAL_CONST': u32 1655 141..152 'LOCAL_CONST': u32
1632 162..163 'z': u32 1656 162..163 'z': u32
1633 166..178 'GLOBAL_CONST': u32 1657 166..178 'GLOBAL_CONST': u32
1634 188..190 'id': u32 1658 188..190 'id': u32
1635 193..209 'Foo::A..._CONST': u32 1659 193..209 'Foo::A..._CONST': u32
1636 125..127 '99': u32 1660 125..127 '99': u32
1637 "### 1661 "#]],
1638 ); 1662 );
1639} 1663}
1640 1664
1641#[test] 1665#[test]
1642fn infer_static() { 1666fn infer_static() {
1643 assert_snapshot!( 1667 check_infer(
1644 infer(r#" 1668 r#"
1645static GLOBAL_STATIC: u32 = 101; 1669 static GLOBAL_STATIC: u32 = 101;
1646static mut GLOBAL_STATIC_MUT: u32 = 101; 1670 static mut GLOBAL_STATIC_MUT: u32 = 101;
1647fn test() { 1671 fn test() {
1648 static LOCAL_STATIC: u32 = 99; 1672 static LOCAL_STATIC: u32 = 99;
1649 static mut LOCAL_STATIC_MUT: u32 = 99; 1673 static mut LOCAL_STATIC_MUT: u32 = 99;
1650 let x = LOCAL_STATIC; 1674 let x = LOCAL_STATIC;
1651 let y = LOCAL_STATIC_MUT; 1675 let y = LOCAL_STATIC_MUT;
1652 let z = GLOBAL_STATIC; 1676 let z = GLOBAL_STATIC;
1653 let w = GLOBAL_STATIC_MUT; 1677 let w = GLOBAL_STATIC_MUT;
1654} 1678 }
1655"#), 1679 "#,
1656 @r###" 1680 expect![[r#"
1657 28..31 '101': u32 1681 28..31 '101': u32
1658 69..72 '101': u32 1682 69..72 '101': u32
1659 84..279 '{ ...MUT; }': () 1683 84..279 '{ ...MUT; }': ()
1660 172..173 'x': u32 1684 172..173 'x': u32
1661 176..188 'LOCAL_STATIC': u32 1685 176..188 'LOCAL_STATIC': u32
1662 198..199 'y': u32 1686 198..199 'y': u32
1663 202..218 'LOCAL_...IC_MUT': u32 1687 202..218 'LOCAL_...IC_MUT': u32
1664 228..229 'z': u32 1688 228..229 'z': u32
1665 232..245 'GLOBAL_STATIC': u32 1689 232..245 'GLOBAL_STATIC': u32
1666 255..256 'w': u32 1690 255..256 'w': u32
1667 259..276 'GLOBAL...IC_MUT': u32 1691 259..276 'GLOBAL...IC_MUT': u32
1668 117..119 '99': u32 1692 117..119 '99': u32
1669 160..162 '99': u32 1693 160..162 '99': u32
1670 "### 1694 "#]],
1671 ); 1695 );
1672} 1696}
1673 1697
@@ -1754,413 +1778,413 @@ fn main() {
1754 1778
1755#[test] 1779#[test]
1756fn closure_return() { 1780fn closure_return() {
1757 assert_snapshot!( 1781 check_infer(
1758 infer(r#" 1782 r#"
1759fn foo() -> u32 { 1783 fn foo() -> u32 {
1760 let x = || -> usize { return 1; }; 1784 let x = || -> usize { return 1; };
1761} 1785 }
1762"#), 1786 "#,
1763 @r###" 1787 expect![[r#"
1764 16..58 '{ ...; }; }': () 1788 16..58 '{ ...; }; }': ()
1765 26..27 'x': || -> usize 1789 26..27 'x': || -> usize
1766 30..55 '|| -> ...n 1; }': || -> usize 1790 30..55 '|| -> ...n 1; }': || -> usize
1767 42..55 '{ return 1; }': usize 1791 42..55 '{ return 1; }': usize
1768 44..52 'return 1': ! 1792 44..52 'return 1': !
1769 51..52 '1': usize 1793 51..52 '1': usize
1770 "### 1794 "#]],
1771 ); 1795 );
1772} 1796}
1773 1797
1774#[test] 1798#[test]
1775fn closure_return_unit() { 1799fn closure_return_unit() {
1776 assert_snapshot!( 1800 check_infer(
1777 infer(r#" 1801 r#"
1778fn foo() -> u32 { 1802 fn foo() -> u32 {
1779 let x = || { return; }; 1803 let x = || { return; };
1780} 1804 }
1781"#), 1805 "#,
1782 @r###" 1806 expect![[r#"
1783 16..47 '{ ...; }; }': () 1807 16..47 '{ ...; }; }': ()
1784 26..27 'x': || -> () 1808 26..27 'x': || -> ()
1785 30..44 '|| { return; }': || -> () 1809 30..44 '|| { return; }': || -> ()
1786 33..44 '{ return; }': () 1810 33..44 '{ return; }': ()
1787 35..41 'return': ! 1811 35..41 'return': !
1788 "### 1812 "#]],
1789 ); 1813 );
1790} 1814}
1791 1815
1792#[test] 1816#[test]
1793fn closure_return_inferred() { 1817fn closure_return_inferred() {
1794 assert_snapshot!( 1818 check_infer(
1795 infer(r#" 1819 r#"
1796fn foo() -> u32 { 1820 fn foo() -> u32 {
1797 let x = || { "test" }; 1821 let x = || { "test" };
1798} 1822 }
1799"#), 1823 "#,
1800 @r###" 1824 expect![[r#"
1801 16..46 '{ ..." }; }': () 1825 16..46 '{ ..." }; }': ()
1802 26..27 'x': || -> &str 1826 26..27 'x': || -> &str
1803 30..43 '|| { "test" }': || -> &str 1827 30..43 '|| { "test" }': || -> &str
1804 33..43 '{ "test" }': &str 1828 33..43 '{ "test" }': &str
1805 35..41 '"test"': &str 1829 35..41 '"test"': &str
1806 "### 1830 "#]],
1807 ); 1831 );
1808} 1832}
1809 1833
1810#[test] 1834#[test]
1811fn fn_pointer_return() { 1835fn fn_pointer_return() {
1812 assert_snapshot!( 1836 check_infer(
1813 infer(r#" 1837 r#"
1814struct Vtable { 1838 struct Vtable {
1815 method: fn(), 1839 method: fn(),
1816} 1840 }
1817 1841
1818fn main() { 1842 fn main() {
1819 let vtable = Vtable { method: || {} }; 1843 let vtable = Vtable { method: || {} };
1820 let m = vtable.method; 1844 let m = vtable.method;
1821} 1845 }
1822"#), 1846 "#,
1823 @r###" 1847 expect![[r#"
1824 47..120 '{ ...hod; }': () 1848 47..120 '{ ...hod; }': ()
1825 57..63 'vtable': Vtable 1849 57..63 'vtable': Vtable
1826 66..90 'Vtable...| {} }': Vtable 1850 66..90 'Vtable...| {} }': Vtable
1827 83..88 '|| {}': || -> () 1851 83..88 '|| {}': || -> ()
1828 86..88 '{}': () 1852 86..88 '{}': ()
1829 100..101 'm': fn() 1853 100..101 'm': fn()
1830 104..110 'vtable': Vtable 1854 104..110 'vtable': Vtable
1831 104..117 'vtable.method': fn() 1855 104..117 'vtable.method': fn()
1832 "### 1856 "#]],
1833 ); 1857 );
1834} 1858}
1835 1859
1836#[test] 1860#[test]
1837fn effects_smoke_test() { 1861fn effects_smoke_test() {
1838 assert_snapshot!( 1862 check_infer(
1839 infer(r#" 1863 r#"
1840fn main() { 1864 fn main() {
1841 let x = unsafe { 92 }; 1865 let x = unsafe { 92 };
1842 let y = async { async { () }.await }; 1866 let y = async { async { () }.await };
1843 let z = try { () }; 1867 let z = try { () };
1844 let t = 'a: { 92 }; 1868 let t = 'a: { 92 };
1845} 1869 }
1846"#), 1870 "#,
1847 @r###" 1871 expect![[r#"
1848 10..130 '{ ...2 }; }': () 1872 10..130 '{ ...2 }; }': ()
1849 20..21 'x': i32 1873 20..21 'x': i32
1850 24..37 'unsafe { 92 }': i32 1874 24..37 'unsafe { 92 }': i32
1851 31..37 '{ 92 }': i32 1875 31..37 '{ 92 }': i32
1852 33..35 '92': i32 1876 33..35 '92': i32
1853 47..48 'y': {unknown} 1877 47..48 'y': {unknown}
1854 57..79 '{ asyn...wait }': {unknown} 1878 57..79 '{ asyn...wait }': {unknown}
1855 59..77 'async ....await': {unknown} 1879 59..77 'async ....await': {unknown}
1856 65..71 '{ () }': () 1880 65..71 '{ () }': ()
1857 67..69 '()': () 1881 67..69 '()': ()
1858 89..90 'z': {unknown} 1882 89..90 'z': {unknown}
1859 93..103 'try { () }': {unknown} 1883 93..103 'try { () }': {unknown}
1860 97..103 '{ () }': () 1884 97..103 '{ () }': ()
1861 99..101 '()': () 1885 99..101 '()': ()
1862 113..114 't': i32 1886 113..114 't': i32
1863 121..127 '{ 92 }': i32 1887 121..127 '{ 92 }': i32
1864 123..125 '92': i32 1888 123..125 '92': i32
1865 "### 1889 "#]],
1866 ) 1890 )
1867} 1891}
1868 1892
1869#[test] 1893#[test]
1870fn infer_generic_from_later_assignment() { 1894fn infer_generic_from_later_assignment() {
1871 assert_snapshot!( 1895 check_infer(
1872 infer(r#" 1896 r#"
1873enum Option<T> { Some(T), None } 1897 enum Option<T> { Some(T), None }
1874use Option::*; 1898 use Option::*;
1875 1899
1876fn test() { 1900 fn test() {
1877 let mut end = None; 1901 let mut end = None;
1878 loop { 1902 loop {
1879 end = Some(true); 1903 end = Some(true);
1880 } 1904 }
1881} 1905 }
1882"#), 1906 "#,
1883 @r###" 1907 expect![[r#"
1884 59..129 '{ ... } }': () 1908 59..129 '{ ... } }': ()
1885 69..76 'mut end': Option<bool> 1909 69..76 'mut end': Option<bool>
1886 79..83 'None': Option<bool> 1910 79..83 'None': Option<bool>
1887 89..127 'loop {... }': ! 1911 89..127 'loop {... }': !
1888 94..127 '{ ... }': () 1912 94..127 '{ ... }': ()
1889 104..107 'end': Option<bool> 1913 104..107 'end': Option<bool>
1890 104..120 'end = ...(true)': () 1914 104..120 'end = ...(true)': ()
1891 110..114 'Some': Some<bool>(bool) -> Option<bool> 1915 110..114 'Some': Some<bool>(bool) -> Option<bool>
1892 110..120 'Some(true)': Option<bool> 1916 110..120 'Some(true)': Option<bool>
1893 115..119 'true': bool 1917 115..119 'true': bool
1894 "### 1918 "#]],
1895 ); 1919 );
1896} 1920}
1897 1921
1898#[test] 1922#[test]
1899fn infer_loop_break_with_val() { 1923fn infer_loop_break_with_val() {
1900 assert_snapshot!( 1924 check_infer(
1901 infer(r#" 1925 r#"
1902enum Option<T> { Some(T), None } 1926 enum Option<T> { Some(T), None }
1903use Option::*; 1927 use Option::*;
1904 1928
1905fn test() { 1929 fn test() {
1906 let x = loop { 1930 let x = loop {
1907 if false { 1931 if false {
1908 break None; 1932 break None;
1909 } 1933 }
1910 1934
1911 break Some(true); 1935 break Some(true);
1912 }; 1936 };
1913} 1937 }
1914"#), 1938 "#,
1915 @r###" 1939 expect![[r#"
1916 59..168 '{ ... }; }': () 1940 59..168 '{ ... }; }': ()
1917 69..70 'x': Option<bool> 1941 69..70 'x': Option<bool>
1918 73..165 'loop {... }': Option<bool> 1942 73..165 'loop {... }': Option<bool>
1919 78..165 '{ ... }': () 1943 78..165 '{ ... }': ()
1920 88..132 'if fal... }': () 1944 88..132 'if fal... }': ()
1921 91..96 'false': bool 1945 91..96 'false': bool
1922 97..132 '{ ... }': () 1946 97..132 '{ ... }': ()
1923 111..121 'break None': ! 1947 111..121 'break None': !
1924 117..121 'None': Option<bool> 1948 117..121 'None': Option<bool>
1925 142..158 'break ...(true)': ! 1949 142..158 'break ...(true)': !
1926 148..152 'Some': Some<bool>(bool) -> Option<bool> 1950 148..152 'Some': Some<bool>(bool) -> Option<bool>
1927 148..158 'Some(true)': Option<bool> 1951 148..158 'Some(true)': Option<bool>
1928 153..157 'true': bool 1952 153..157 'true': bool
1929 "### 1953 "#]],
1930 ); 1954 );
1931} 1955}
1932 1956
1933#[test] 1957#[test]
1934fn infer_loop_break_without_val() { 1958fn infer_loop_break_without_val() {
1935 assert_snapshot!( 1959 check_infer(
1936 infer(r#" 1960 r#"
1937enum Option<T> { Some(T), None } 1961 enum Option<T> { Some(T), None }
1938use Option::*; 1962 use Option::*;
1939 1963
1940fn test() { 1964 fn test() {
1941 let x = loop { 1965 let x = loop {
1942 if false { 1966 if false {
1943 break; 1967 break;
1944 } 1968 }
1945 }; 1969 };
1946} 1970 }
1947"#), 1971 "#,
1948 @r###" 1972 expect![[r#"
1949 59..136 '{ ... }; }': () 1973 59..136 '{ ... }; }': ()
1950 69..70 'x': () 1974 69..70 'x': ()
1951 73..133 'loop {... }': () 1975 73..133 'loop {... }': ()
1952 78..133 '{ ... }': () 1976 78..133 '{ ... }': ()
1953 88..127 'if fal... }': () 1977 88..127 'if fal... }': ()
1954 91..96 'false': bool 1978 91..96 'false': bool
1955 97..127 '{ ... }': () 1979 97..127 '{ ... }': ()
1956 111..116 'break': ! 1980 111..116 'break': !
1957 "### 1981 "#]],
1958 ); 1982 );
1959} 1983}
1960 1984
1961#[test] 1985#[test]
1962fn infer_labelled_break_with_val() { 1986fn infer_labelled_break_with_val() {
1963 assert_snapshot!( 1987 check_infer(
1964 infer(r#" 1988 r#"
1965fn foo() { 1989 fn foo() {
1966 let _x = || 'outer: loop { 1990 let _x = || 'outer: loop {
1967 let inner = 'inner: loop { 1991 let inner = 'inner: loop {
1968 let i = Default::default(); 1992 let i = Default::default();
1969 if (break 'outer i) { 1993 if (break 'outer i) {
1970 loop { break 'inner 5i8; }; 1994 loop { break 'inner 5i8; };
1971 } else if true { 1995 } else if true {
1972 break 'inner 6; 1996 break 'inner 6;
1973 } 1997 }
1974 break 7; 1998 break 7;
1975 }; 1999 };
1976 break inner < 8; 2000 break inner < 8;
1977 }; 2001 };
1978} 2002 }
1979"#), 2003 "#,
1980 @r###" 2004 expect![[r#"
1981 9..335 '{ ... }; }': () 2005 9..335 '{ ... }; }': ()
1982 19..21 '_x': || -> bool 2006 19..21 '_x': || -> bool
1983 24..332 '|| 'ou... }': || -> bool 2007 24..332 '|| 'ou... }': || -> bool
1984 27..332 ''outer... }': bool 2008 27..332 ''outer... }': bool
1985 40..332 '{ ... }': () 2009 40..332 '{ ... }': ()
1986 54..59 'inner': i8 2010 54..59 'inner': i8
1987 62..300 ''inner... }': i8 2011 62..300 ''inner... }': i8
1988 75..300 '{ ... }': () 2012 75..300 '{ ... }': ()
1989 93..94 'i': bool 2013 93..94 'i': bool
1990 97..113 'Defaul...efault': {unknown} 2014 97..113 'Defaul...efault': {unknown}
1991 97..115 'Defaul...ault()': bool 2015 97..115 'Defaul...ault()': bool
1992 129..269 'if (br... }': () 2016 129..269 'if (br... }': ()
1993 133..147 'break 'outer i': ! 2017 133..147 'break 'outer i': !
1994 146..147 'i': bool 2018 146..147 'i': bool
1995 149..208 '{ ... }': () 2019 149..208 '{ ... }': ()
1996 167..193 'loop {...5i8; }': ! 2020 167..193 'loop {...5i8; }': !
1997 172..193 '{ brea...5i8; }': () 2021 172..193 '{ brea...5i8; }': ()
1998 174..190 'break ...er 5i8': ! 2022 174..190 'break ...er 5i8': !
1999 187..190 '5i8': i8 2023 187..190 '5i8': i8
2000 214..269 'if tru... }': () 2024 214..269 'if tru... }': ()
2001 217..221 'true': bool 2025 217..221 'true': bool
2002 222..269 '{ ... }': () 2026 222..269 '{ ... }': ()
2003 240..254 'break 'inner 6': ! 2027 240..254 'break 'inner 6': !
2004 253..254 '6': i8 2028 253..254 '6': i8
2005 282..289 'break 7': ! 2029 282..289 'break 7': !
2006 288..289 '7': i8 2030 288..289 '7': i8
2007 310..325 'break inner < 8': ! 2031 310..325 'break inner < 8': !
2008 316..321 'inner': i8 2032 316..321 'inner': i8
2009 316..325 'inner < 8': bool 2033 316..325 'inner < 8': bool
2010 324..325 '8': i8 2034 324..325 '8': i8
2011 "### 2035 "#]],
2012 ); 2036 );
2013} 2037}
2014 2038
2015#[test] 2039#[test]
2016fn generic_default() { 2040fn generic_default() {
2017 assert_snapshot!( 2041 check_infer(
2018 infer(r#" 2042 r#"
2019struct Thing<T = ()> { t: T } 2043 struct Thing<T = ()> { t: T }
2020enum OtherThing<T = ()> { 2044 enum OtherThing<T = ()> {
2021 One { t: T }, 2045 One { t: T },
2022 Two(T), 2046 Two(T),
2023} 2047 }
2024 2048
2025fn test(t1: Thing, t2: OtherThing, t3: Thing<i32>, t4: OtherThing<i32>) { 2049 fn test(t1: Thing, t2: OtherThing, t3: Thing<i32>, t4: OtherThing<i32>) {
2026 t1.t; 2050 t1.t;
2027 t3.t; 2051 t3.t;
2028 match t2 { 2052 match t2 {
2029 OtherThing::One { t } => { t; }, 2053 OtherThing::One { t } => { t; },
2030 OtherThing::Two(t) => { t; }, 2054 OtherThing::Two(t) => { t; },
2031 } 2055 }
2032 match t4 { 2056 match t4 {
2033 OtherThing::One { t } => { t; }, 2057 OtherThing::One { t } => { t; },
2034 OtherThing::Two(t) => { t; }, 2058 OtherThing::Two(t) => { t; },
2035 } 2059 }
2036} 2060 }
2037"#), 2061 "#,
2038 @r###" 2062 expect![[r#"
2039 97..99 't1': Thing<()> 2063 97..99 't1': Thing<()>
2040 108..110 't2': OtherThing<()> 2064 108..110 't2': OtherThing<()>
2041 124..126 't3': Thing<i32> 2065 124..126 't3': Thing<i32>
2042 140..142 't4': OtherThing<i32> 2066 140..142 't4': OtherThing<i32>
2043 161..384 '{ ... } }': () 2067 161..384 '{ ... } }': ()
2044 167..169 't1': Thing<()> 2068 167..169 't1': Thing<()>
2045 167..171 't1.t': () 2069 167..171 't1.t': ()
2046 177..179 't3': Thing<i32> 2070 177..179 't3': Thing<i32>
2047 177..181 't3.t': i32 2071 177..181 't3.t': i32
2048 187..282 'match ... }': () 2072 187..282 'match ... }': ()
2049 193..195 't2': OtherThing<()> 2073 193..195 't2': OtherThing<()>
2050 206..227 'OtherT... { t }': OtherThing<()> 2074 206..227 'OtherT... { t }': OtherThing<()>
2051 224..225 't': () 2075 224..225 't': ()
2052 231..237 '{ t; }': () 2076 231..237 '{ t; }': ()
2053 233..234 't': () 2077 233..234 't': ()
2054 247..265 'OtherT...Two(t)': OtherThing<()> 2078 247..265 'OtherT...Two(t)': OtherThing<()>
2055 263..264 't': () 2079 263..264 't': ()
2056 269..275 '{ t; }': () 2080 269..275 '{ t; }': ()
2057 271..272 't': () 2081 271..272 't': ()
2058 287..382 'match ... }': () 2082 287..382 'match ... }': ()
2059 293..295 't4': OtherThing<i32> 2083 293..295 't4': OtherThing<i32>
2060 306..327 'OtherT... { t }': OtherThing<i32> 2084 306..327 'OtherT... { t }': OtherThing<i32>
2061 324..325 't': i32 2085 324..325 't': i32
2062 331..337 '{ t; }': () 2086 331..337 '{ t; }': ()
2063 333..334 't': i32 2087 333..334 't': i32
2064 347..365 'OtherT...Two(t)': OtherThing<i32> 2088 347..365 'OtherT...Two(t)': OtherThing<i32>
2065 363..364 't': i32 2089 363..364 't': i32
2066 369..375 '{ t; }': () 2090 369..375 '{ t; }': ()
2067 371..372 't': i32 2091 371..372 't': i32
2068 "### 2092 "#]],
2069 ); 2093 );
2070} 2094}
2071 2095
2072#[test] 2096#[test]
2073fn generic_default_in_struct_literal() { 2097fn generic_default_in_struct_literal() {
2074 assert_snapshot!( 2098 check_infer(
2075 infer(r#" 2099 r#"
2076struct Thing<T = ()> { t: T } 2100 struct Thing<T = ()> { t: T }
2077enum OtherThing<T = ()> { 2101 enum OtherThing<T = ()> {
2078 One { t: T }, 2102 One { t: T },
2079 Two(T), 2103 Two(T),
2080} 2104 }
2081 2105
2082fn test() { 2106 fn test() {
2083 let x = Thing { t: loop {} }; 2107 let x = Thing { t: loop {} };
2084 let y = Thing { t: () }; 2108 let y = Thing { t: () };
2085 let z = Thing { t: 1i32 }; 2109 let z = Thing { t: 1i32 };
2086 if let Thing { t } = z { 2110 if let Thing { t } = z {
2087 t; 2111 t;
2088 } 2112 }
2089 2113
2090 let a = OtherThing::One { t: 1i32 }; 2114 let a = OtherThing::One { t: 1i32 };
2091 let b = OtherThing::Two(1i32); 2115 let b = OtherThing::Two(1i32);
2092} 2116 }
2093"#), 2117 "#,
2094 @r###" 2118 expect![[r#"
2095 99..319 '{ ...32); }': () 2119 99..319 '{ ...32); }': ()
2096 109..110 'x': Thing<!> 2120 109..110 'x': Thing<!>
2097 113..133 'Thing ...p {} }': Thing<!> 2121 113..133 'Thing ...p {} }': Thing<!>
2098 124..131 'loop {}': ! 2122 124..131 'loop {}': !
2099 129..131 '{}': () 2123 129..131 '{}': ()
2100 143..144 'y': Thing<()> 2124 143..144 'y': Thing<()>
2101 147..162 'Thing { t: () }': Thing<()> 2125 147..162 'Thing { t: () }': Thing<()>
2102 158..160 '()': () 2126 158..160 '()': ()
2103 172..173 'z': Thing<i32> 2127 172..173 'z': Thing<i32>
2104 176..193 'Thing ...1i32 }': Thing<i32> 2128 176..193 'Thing ...1i32 }': Thing<i32>
2105 187..191 '1i32': i32 2129 187..191 '1i32': i32
2106 199..240 'if let... }': () 2130 199..240 'if let... }': ()
2107 206..217 'Thing { t }': Thing<i32> 2131 206..217 'Thing { t }': Thing<i32>
2108 214..215 't': i32 2132 214..215 't': i32
2109 220..221 'z': Thing<i32> 2133 220..221 'z': Thing<i32>
2110 222..240 '{ ... }': () 2134 222..240 '{ ... }': ()
2111 232..233 't': i32 2135 232..233 't': i32
2112 250..251 'a': OtherThing<i32> 2136 250..251 'a': OtherThing<i32>
2113 254..281 'OtherT...1i32 }': OtherThing<i32> 2137 254..281 'OtherT...1i32 }': OtherThing<i32>
2114 275..279 '1i32': i32 2138 275..279 '1i32': i32
2115 291..292 'b': OtherThing<i32> 2139 291..292 'b': OtherThing<i32>
2116 295..310 'OtherThing::Two': Two<i32>(i32) -> OtherThing<i32> 2140 295..310 'OtherThing::Two': Two<i32>(i32) -> OtherThing<i32>
2117 295..316 'OtherT...(1i32)': OtherThing<i32> 2141 295..316 'OtherT...(1i32)': OtherThing<i32>
2118 311..315 '1i32': i32 2142 311..315 '1i32': i32
2119 "### 2143 "#]],
2120 ); 2144 );
2121} 2145}
2122 2146
2123#[test] 2147#[test]
2124fn generic_default_depending_on_other_type_arg() { 2148fn generic_default_depending_on_other_type_arg() {
2125 assert_snapshot!( 2149 // FIXME: the {unknown} is a bug
2126 infer(r#" 2150 check_infer(
2127struct Thing<T = u128, F = fn() -> T> { t: T } 2151 r#"
2128 2152 struct Thing<T = u128, F = fn() -> T> { t: T }
2129fn test(t1: Thing<u32>, t2: Thing) { 2153
2130 t1; 2154 fn test(t1: Thing<u32>, t2: Thing) {
2131 t2; 2155 t1;
2132 Thing::<_> { t: 1u32 }; 2156 t2;
2133} 2157 Thing::<_> { t: 1u32 };
2134"#), 2158 }
2135 // FIXME: the {unknown} is a bug 2159 "#,
2136 @r###" 2160 expect![[r#"
2137 56..58 't1': Thing<u32, fn() -> u32> 2161 56..58 't1': Thing<u32, fn() -> u32>
2138 72..74 't2': Thing<u128, fn() -> u128> 2162 72..74 't2': Thing<u128, fn() -> u128>
2139 83..130 '{ ...2 }; }': () 2163 83..130 '{ ...2 }; }': ()
2140 89..91 't1': Thing<u32, fn() -> u32> 2164 89..91 't1': Thing<u32, fn() -> u32>
2141 97..99 't2': Thing<u128, fn() -> u128> 2165 97..99 't2': Thing<u128, fn() -> u128>
2142 105..127 'Thing:...1u32 }': Thing<u32, fn() -> {unknown}> 2166 105..127 'Thing:...1u32 }': Thing<u32, fn() -> {unknown}>
2143 121..125 '1u32': u32 2167 121..125 '1u32': u32
2144 "### 2168 "#]],
2145 ); 2169 );
2146} 2170}
2147 2171
2148#[test] 2172#[test]
2149fn generic_default_depending_on_other_type_arg_forward() { 2173fn generic_default_depending_on_other_type_arg_forward() {
2150 assert_snapshot!( 2174 // the {unknown} here is intentional, as defaults are not allowed to
2151 infer(r#" 2175 // refer to type parameters coming later
2152struct Thing<F = fn() -> T, T = u128> { t: T } 2176 check_infer(
2153 2177 r#"
2154fn test(t1: Thing) { 2178 struct Thing<F = fn() -> T, T = u128> { t: T }
2155 t1; 2179
2156} 2180 fn test(t1: Thing) {
2157"#), 2181 t1;
2158 // the {unknown} here is intentional, as defaults are not allowed to 2182 }
2159 // refer to type parameters coming later 2183 "#,
2160 @r###" 2184 expect![[r#"
2161 56..58 't1': Thing<fn() -> {unknown}, u128> 2185 56..58 't1': Thing<fn() -> {unknown}, u128>
2162 67..78 '{ t1; }': () 2186 67..78 '{ t1; }': ()
2163 73..75 't1': Thing<fn() -> {unknown}, u128> 2187 73..75 't1': Thing<fn() -> {unknown}, u128>
2164 "### 2188 "#]],
2165 ); 2189 );
2166} 2190}