aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir_ty/src/tests.rs4908
-rw-r--r--crates/ra_hir_ty/src/tests/macros.rs268
-rw-r--r--crates/ra_hir_ty/src/tests/method_resolution.rs1005
-rw-r--r--crates/ra_hir_ty/src/tests/patterns.rs238
-rw-r--r--crates/ra_hir_ty/src/tests/regression.rs333
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs1608
-rw-r--r--crates/ra_hir_ty/src/tests/traits.rs1424
7 files changed, 4882 insertions, 4902 deletions
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs
index 2ea9e261c..c520bb375 100644
--- a/crates/ra_hir_ty/src/tests.rs
+++ b/crates/ra_hir_ty/src/tests.rs
@@ -1,5 +1,11 @@
1mod never_type; 1mod never_type;
2mod coercion; 2mod coercion;
3mod regression;
4mod simple;
5mod patterns;
6mod traits;
7mod method_resolution;
8mod macros;
3 9
4use std::fmt::Write; 10use std::fmt::Write;
5use std::sync::Arc; 11use std::sync::Arc;
@@ -15,7 +21,6 @@ use ra_syntax::{
15 algo, 21 algo,
16 ast::{self, AstNode}, 22 ast::{self, AstNode},
17}; 23};
18use test_utils::covers;
19 24
20use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResult}; 25use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResult};
21 26
@@ -23,4850 +28,6 @@ use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResu
23// against snapshots of the expected results using insta. Use cargo-insta to 28// against snapshots of the expected results using insta. Use cargo-insta to
24// update the snapshots. 29// update the snapshots.
25 30
26#[test]
27fn cfg_impl_block() {
28 let (db, pos) = TestDB::with_position(
29 r#"
30//- /main.rs crate:main deps:foo cfg:test
31use foo::S as T;
32struct S;
33
34#[cfg(test)]
35impl S {
36 fn foo1(&self) -> i32 { 0 }
37}
38
39#[cfg(not(test))]
40impl S {
41 fn foo2(&self) -> i32 { 0 }
42}
43
44fn test() {
45 let t = (S.foo1(), S.foo2(), T.foo3(), T.foo4());
46 t<|>;
47}
48
49//- /foo.rs crate:foo
50struct S;
51
52#[cfg(not(test))]
53impl S {
54 fn foo3(&self) -> i32 { 0 }
55}
56
57#[cfg(test)]
58impl S {
59 fn foo4(&self) -> i32 { 0 }
60}
61"#,
62 );
63 assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos));
64}
65
66#[test]
67fn infer_await() {
68 let (db, pos) = TestDB::with_position(
69 r#"
70//- /main.rs crate:main deps:std
71
72struct IntFuture;
73
74impl Future for IntFuture {
75 type Output = u64;
76}
77
78fn test() {
79 let r = IntFuture;
80 let v = r.await;
81 v<|>;
82}
83
84//- /std.rs crate:std
85#[prelude_import] use future::*;
86mod future {
87 trait Future {
88 type Output;
89 }
90}
91
92"#,
93 );
94 assert_eq!("u64", type_at_pos(&db, pos));
95}
96
97#[test]
98fn infer_box() {
99 let (db, pos) = TestDB::with_position(
100 r#"
101//- /main.rs crate:main deps:std
102
103fn test() {
104 let x = box 1;
105 let t = (x, box x, box &1, box [1]);
106 t<|>;
107}
108
109//- /std.rs crate:std
110#[prelude_import] use prelude::*;
111mod prelude {}
112
113mod boxed {
114 pub struct Box<T: ?Sized> {
115 inner: *mut T,
116 }
117}
118
119"#,
120 );
121 assert_eq!("(Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32;_]>)", type_at_pos(&db, pos));
122}
123
124#[test]
125fn infer_adt_self() {
126 let (db, pos) = TestDB::with_position(
127 r#"
128//- /main.rs
129enum Nat { Succ(Self), Demo(Nat), Zero }
130
131fn test() {
132 let foo: Nat = Nat::Zero;
133 if let Nat::Succ(x) = foo {
134 x<|>
135 }
136}
137
138"#,
139 );
140 assert_eq!("Nat", type_at_pos(&db, pos));
141}
142
143#[test]
144fn infer_try() {
145 let (db, pos) = TestDB::with_position(
146 r#"
147//- /main.rs crate:main deps:std
148
149fn test() {
150 let r: Result<i32, u64> = Result::Ok(1);
151 let v = r?;
152 v<|>;
153}
154
155//- /std.rs crate:std
156
157#[prelude_import] use ops::*;
158mod ops {
159 trait Try {
160 type Ok;
161 type Error;
162 }
163}
164
165#[prelude_import] use result::*;
166mod result {
167 enum Result<O, E> {
168 Ok(O),
169 Err(E)
170 }
171
172 impl<O, E> crate::ops::Try for Result<O, E> {
173 type Ok = O;
174 type Error = E;
175 }
176}
177
178"#,
179 );
180 assert_eq!("i32", type_at_pos(&db, pos));
181}
182
183#[test]
184fn infer_for_loop() {
185 let (db, pos) = TestDB::with_position(
186 r#"
187//- /main.rs crate:main deps:std
188
189use std::collections::Vec;
190
191fn test() {
192 let v = Vec::new();
193 v.push("foo");
194 for x in v {
195 x<|>;
196 }
197}
198
199//- /std.rs crate:std
200
201#[prelude_import] use iter::*;
202mod iter {
203 trait IntoIterator {
204 type Item;
205 }
206}
207
208mod collections {
209 struct Vec<T> {}
210 impl<T> Vec<T> {
211 fn new() -> Self { Vec {} }
212 fn push(&mut self, t: T) { }
213 }
214
215 impl<T> crate::iter::IntoIterator for Vec<T> {
216 type Item=T;
217 }
218}
219"#,
220 );
221 assert_eq!("&str", type_at_pos(&db, pos));
222}
223
224#[test]
225fn infer_ranges() {
226 let (db, pos) = TestDB::with_position(
227 r#"
228//- /main.rs crate:main deps:std
229fn test() {
230 let a = ..;
231 let b = 1..;
232 let c = ..2u32;
233 let d = 1..2usize;
234 let e = ..=10;
235 let f = 'a'..='z';
236
237 let t = (a, b, c, d, e, f);
238 t<|>;
239}
240
241//- /std.rs crate:std
242#[prelude_import] use prelude::*;
243mod prelude {}
244
245pub mod ops {
246 pub struct Range<Idx> {
247 pub start: Idx,
248 pub end: Idx,
249 }
250 pub struct RangeFrom<Idx> {
251 pub start: Idx,
252 }
253 struct RangeFull;
254 pub struct RangeInclusive<Idx> {
255 start: Idx,
256 end: Idx,
257 is_empty: u8,
258 }
259 pub struct RangeTo<Idx> {
260 pub end: Idx,
261 }
262 pub struct RangeToInclusive<Idx> {
263 pub end: Idx,
264 }
265}
266"#,
267 );
268 assert_eq!(
269 "(RangeFull, RangeFrom<i32>, RangeTo<u32>, Range<usize>, RangeToInclusive<i32>, RangeInclusive<char>)",
270 type_at_pos(&db, pos),
271 );
272}
273
274#[test]
275fn infer_while_let() {
276 let (db, pos) = TestDB::with_position(
277 r#"
278//- /main.rs
279enum Option<T> { Some(T), None }
280
281fn test() {
282 let foo: Option<f32> = None;
283 while let Option::Some(x) = foo {
284 <|>x
285 }
286}
287
288"#,
289 );
290 assert_eq!("f32", type_at_pos(&db, pos));
291}
292
293#[test]
294fn infer_basics() {
295 assert_snapshot!(
296 infer(r#"
297fn test(a: u32, b: isize, c: !, d: &str) {
298 a;
299 b;
300 c;
301 d;
302 1usize;
303 1isize;
304 "test";
305 1.0f32;
306}"#),
307 @r###"
308 [9; 10) 'a': u32
309 [17; 18) 'b': isize
310 [27; 28) 'c': !
311 [33; 34) 'd': &str
312 [42; 121) '{ ...f32; }': !
313 [48; 49) 'a': u32
314 [55; 56) 'b': isize
315 [62; 63) 'c': !
316 [69; 70) 'd': &str
317 [76; 82) '1usize': usize
318 [88; 94) '1isize': isize
319 [100; 106) '"test"': &str
320 [112; 118) '1.0f32': f32
321 "###
322 );
323}
324
325#[test]
326fn infer_let() {
327 assert_snapshot!(
328 infer(r#"
329fn test() {
330 let a = 1isize;
331 let b: usize = 1;
332 let c = b;
333 let d: u32;
334 let e;
335 let f: i32 = e;
336}
337"#),
338 @r###"
339 [11; 118) '{ ...= e; }': ()
340 [21; 22) 'a': isize
341 [25; 31) '1isize': isize
342 [41; 42) 'b': usize
343 [52; 53) '1': usize
344 [63; 64) 'c': usize
345 [67; 68) 'b': usize
346 [78; 79) 'd': u32
347 [94; 95) 'e': i32
348 [105; 106) 'f': i32
349 [114; 115) 'e': i32
350 "###
351 );
352}
353
354#[test]
355fn infer_paths() {
356 assert_snapshot!(
357 infer(r#"
358fn a() -> u32 { 1 }
359
360mod b {
361 fn c() -> u32 { 1 }
362}
363
364fn test() {
365 a();
366 b::c();
367}
368"#),
369 @r###"
370 [15; 20) '{ 1 }': u32
371 [17; 18) '1': u32
372 [48; 53) '{ 1 }': u32
373 [50; 51) '1': u32
374 [67; 91) '{ ...c(); }': ()
375 [73; 74) 'a': fn a() -> u32
376 [73; 76) 'a()': u32
377 [82; 86) 'b::c': fn c() -> u32
378 [82; 88) 'b::c()': u32
379 "###
380 );
381}
382
383#[test]
384fn infer_path_type() {
385 assert_snapshot!(
386 infer(r#"
387struct S;
388
389impl S {
390 fn foo() -> i32 { 1 }
391}
392
393fn test() {
394 S::foo();
395 <S>::foo();
396}
397"#),
398 @r###"
399 [41; 46) '{ 1 }': i32
400 [43; 44) '1': i32
401 [60; 93) '{ ...o(); }': ()
402 [66; 72) 'S::foo': fn foo() -> i32
403 [66; 74) 'S::foo()': i32
404 [80; 88) '<S>::foo': fn foo() -> i32
405 [80; 90) '<S>::foo()': i32
406 "###
407 );
408}
409
410#[test]
411fn infer_slice_method() {
412 assert_snapshot!(
413 infer(r#"
414#[lang = "slice"]
415impl<T> [T] {
416 fn foo(&self) -> T {
417 loop {}
418 }
419}
420
421#[lang = "slice_alloc"]
422impl<T> [T] {}
423
424fn test() {
425 <[_]>::foo(b"foo");
426}
427"#),
428 @r###"
429 [45; 49) 'self': &[T]
430 [56; 79) '{ ... }': T
431 [66; 73) 'loop {}': !
432 [71; 73) '{}': ()
433 [133; 160) '{ ...o"); }': ()
434 [139; 149) '<[_]>::foo': fn foo<u8>(&[T]) -> T
435 [139; 157) '<[_]>:..."foo")': u8
436 [150; 156) 'b"foo"': &[u8]
437 "###
438 );
439}
440
441#[test]
442fn infer_struct() {
443 assert_snapshot!(
444 infer(r#"
445struct A {
446 b: B,
447 c: C,
448}
449struct B;
450struct C(usize);
451
452fn test() {
453 let c = C(1);
454 B;
455 let a: A = A { b: B, c: C(1) };
456 a.b;
457 a.c;
458}
459"#),
460 @r###"
461 [72; 154) '{ ...a.c; }': ()
462 [82; 83) 'c': C
463 [86; 87) 'C': C(usize) -> C
464 [86; 90) 'C(1)': C
465 [88; 89) '1': usize
466 [96; 97) 'B': B
467 [107; 108) 'a': A
468 [114; 133) 'A { b:...C(1) }': A
469 [121; 122) 'B': B
470 [127; 128) 'C': C(usize) -> C
471 [127; 131) 'C(1)': C
472 [129; 130) '1': usize
473 [139; 140) 'a': A
474 [139; 142) 'a.b': B
475 [148; 149) 'a': A
476 [148; 151) 'a.c': C
477 "###
478 );
479}
480
481#[test]
482fn infer_enum() {
483 assert_snapshot!(
484 infer(r#"
485enum E {
486 V1 { field: u32 },
487 V2
488}
489fn test() {
490 E::V1 { field: 1 };
491 E::V2;
492}"#),
493 @r###"
494 [48; 82) '{ E:...:V2; }': ()
495 [52; 70) 'E::V1 ...d: 1 }': E
496 [67; 68) '1': u32
497 [74; 79) 'E::V2': E
498 "###
499 );
500}
501
502#[test]
503fn infer_refs() {
504 assert_snapshot!(
505 infer(r#"
506fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
507 a;
508 *a;
509 &a;
510 &mut a;
511 b;
512 *b;
513 &b;
514 c;
515 *c;
516 d;
517 *d;
518}
519"#),
520 @r###"
521 [9; 10) 'a': &u32
522 [18; 19) 'b': &mut u32
523 [31; 32) 'c': *const u32
524 [46; 47) 'd': *mut u32
525 [59; 150) '{ ... *d; }': ()
526 [65; 66) 'a': &u32
527 [72; 74) '*a': u32
528 [73; 74) 'a': &u32
529 [80; 82) '&a': &&u32
530 [81; 82) 'a': &u32
531 [88; 94) '&mut a': &mut &u32
532 [93; 94) 'a': &u32
533 [100; 101) 'b': &mut u32
534 [107; 109) '*b': u32
535 [108; 109) 'b': &mut u32
536 [115; 117) '&b': &&mut u32
537 [116; 117) 'b': &mut u32
538 [123; 124) 'c': *const u32
539 [130; 132) '*c': u32
540 [131; 132) 'c': *const u32
541 [138; 139) 'd': *mut u32
542 [145; 147) '*d': u32
543 [146; 147) 'd': *mut u32
544 "###
545 );
546}
547
548#[test]
549fn infer_literals() {
550 assert_snapshot!(
551 infer(r##"
552fn test() {
553 5i32;
554 5f32;
555 5f64;
556 "hello";
557 b"bytes";
558 'c';
559 b'b';
560 3.14;
561 5000;
562 false;
563 true;
564 r#"
565 //! doc
566 // non-doc
567 mod foo {}
568 "#;
569 br#"yolo"#;
570}
571"##),
572 @r###"
573 [11; 221) '{ ...o"#; }': ()
574 [17; 21) '5i32': i32
575 [27; 31) '5f32': f32
576 [37; 41) '5f64': f64
577 [47; 54) '"hello"': &str
578 [60; 68) 'b"bytes"': &[u8]
579 [74; 77) ''c'': char
580 [83; 87) 'b'b'': u8
581 [93; 97) '3.14': f64
582 [103; 107) '5000': i32
583 [113; 118) 'false': bool
584 [124; 128) 'true': bool
585 [134; 202) 'r#" ... "#': &str
586 [208; 218) 'br#"yolo"#': &[u8]
587 "###
588 );
589}
590
591#[test]
592fn infer_unary_op() {
593 assert_snapshot!(
594 infer(r#"
595enum SomeType {}
596
597fn test(x: SomeType) {
598 let b = false;
599 let c = !b;
600 let a = 100;
601 let d: i128 = -a;
602 let e = -100;
603 let f = !!!true;
604 let g = !42;
605 let h = !10u32;
606 let j = !a;
607 -3.14;
608 !3;
609 -x;
610 !x;
611 -"hello";
612 !"hello";
613}
614"#),
615 @r###"
616 [27; 28) 'x': SomeType
617 [40; 272) '{ ...lo"; }': ()
618 [50; 51) 'b': bool
619 [54; 59) 'false': bool
620 [69; 70) 'c': bool
621 [73; 75) '!b': bool
622 [74; 75) 'b': bool
623 [85; 86) 'a': i128
624 [89; 92) '100': i128
625 [102; 103) 'd': i128
626 [112; 114) '-a': i128
627 [113; 114) 'a': i128
628 [124; 125) 'e': i32
629 [128; 132) '-100': i32
630 [129; 132) '100': i32
631 [142; 143) 'f': bool
632 [146; 153) '!!!true': bool
633 [147; 153) '!!true': bool
634 [148; 153) '!true': bool
635 [149; 153) 'true': bool
636 [163; 164) 'g': i32
637 [167; 170) '!42': i32
638 [168; 170) '42': i32
639 [180; 181) 'h': u32
640 [184; 190) '!10u32': u32
641 [185; 190) '10u32': u32
642 [200; 201) 'j': i128
643 [204; 206) '!a': i128
644 [205; 206) 'a': i128
645 [212; 217) '-3.14': f64
646 [213; 217) '3.14': f64
647 [223; 225) '!3': i32
648 [224; 225) '3': i32
649 [231; 233) '-x': {unknown}
650 [232; 233) 'x': SomeType
651 [239; 241) '!x': {unknown}
652 [240; 241) 'x': SomeType
653 [247; 255) '-"hello"': {unknown}
654 [248; 255) '"hello"': &str
655 [261; 269) '!"hello"': {unknown}
656 [262; 269) '"hello"': &str
657 "###
658 );
659}
660
661#[test]
662fn infer_backwards() {
663 assert_snapshot!(
664 infer(r#"
665fn takes_u32(x: u32) {}
666
667struct S { i32_field: i32 }
668
669fn test() -> &mut &f64 {
670 let a = unknown_function();
671 takes_u32(a);
672 let b = unknown_function();
673 S { i32_field: b };
674 let c = unknown_function();
675 &mut &c
676}
677"#),
678 @r###"
679 [14; 15) 'x': u32
680 [22; 24) '{}': ()
681 [78; 231) '{ ...t &c }': &mut &f64
682 [88; 89) 'a': u32
683 [92; 108) 'unknow...nction': {unknown}
684 [92; 110) 'unknow...tion()': u32
685 [116; 125) 'takes_u32': fn takes_u32(u32) -> ()
686 [116; 128) 'takes_u32(a)': ()
687 [126; 127) 'a': u32
688 [138; 139) 'b': i32
689 [142; 158) 'unknow...nction': {unknown}
690 [142; 160) 'unknow...tion()': i32
691 [166; 184) 'S { i3...d: b }': S
692 [181; 182) 'b': i32
693 [194; 195) 'c': f64
694 [198; 214) 'unknow...nction': {unknown}
695 [198; 216) 'unknow...tion()': f64
696 [222; 229) '&mut &c': &mut &f64
697 [227; 229) '&c': &f64
698 [228; 229) 'c': f64
699 "###
700 );
701}
702
703#[test]
704fn infer_self() {
705 assert_snapshot!(
706 infer(r#"
707struct S;
708
709impl S {
710 fn test(&self) {
711 self;
712 }
713 fn test2(self: &Self) {
714 self;
715 }
716 fn test3() -> Self {
717 S {}
718 }
719 fn test4() -> Self {
720 Self {}
721 }
722}
723"#),
724 @r###"
725 [34; 38) 'self': &S
726 [40; 61) '{ ... }': ()
727 [50; 54) 'self': &S
728 [75; 79) 'self': &S
729 [88; 109) '{ ... }': ()
730 [98; 102) 'self': &S
731 [133; 153) '{ ... }': S
732 [143; 147) 'S {}': S
733 [177; 200) '{ ... }': S
734 [187; 194) 'Self {}': S
735 "###
736 );
737}
738
739#[test]
740fn infer_binary_op() {
741 assert_snapshot!(
742 infer(r#"
743fn f(x: bool) -> i32 {
744 0i32
745}
746
747fn test() -> bool {
748 let x = a && b;
749 let y = true || false;
750 let z = x == y;
751 let t = x != y;
752 let minus_forty: isize = -40isize;
753 let h = minus_forty <= CONST_2;
754 let c = f(z || y) + 5;
755 let d = b;
756 let g = minus_forty ^= i;
757 let ten: usize = 10;
758 let ten_is_eleven = ten == some_num;
759
760 ten < 3
761}
762"#),
763 @r###"
764 [6; 7) 'x': bool
765 [22; 34) '{ 0i32 }': i32
766 [28; 32) '0i32': i32
767 [54; 370) '{ ... < 3 }': bool
768 [64; 65) 'x': bool
769 [68; 69) 'a': bool
770 [68; 74) 'a && b': bool
771 [73; 74) 'b': bool
772 [84; 85) 'y': bool
773 [88; 92) 'true': bool
774 [88; 101) 'true || false': bool
775 [96; 101) 'false': bool
776 [111; 112) 'z': bool
777 [115; 116) 'x': bool
778 [115; 121) 'x == y': bool
779 [120; 121) 'y': bool
780 [131; 132) 't': bool
781 [135; 136) 'x': bool
782 [135; 141) 'x != y': bool
783 [140; 141) 'y': bool
784 [151; 162) 'minus_forty': isize
785 [172; 180) '-40isize': isize
786 [173; 180) '40isize': isize
787 [190; 191) 'h': bool
788 [194; 205) 'minus_forty': isize
789 [194; 216) 'minus_...ONST_2': bool
790 [209; 216) 'CONST_2': isize
791 [226; 227) 'c': i32
792 [230; 231) 'f': fn f(bool) -> i32
793 [230; 239) 'f(z || y)': i32
794 [230; 243) 'f(z || y) + 5': i32
795 [232; 233) 'z': bool
796 [232; 238) 'z || y': bool
797 [237; 238) 'y': bool
798 [242; 243) '5': i32
799 [253; 254) 'd': {unknown}
800 [257; 258) 'b': {unknown}
801 [268; 269) 'g': ()
802 [272; 283) 'minus_forty': isize
803 [272; 288) 'minus_...y ^= i': ()
804 [287; 288) 'i': isize
805 [298; 301) 'ten': usize
806 [311; 313) '10': usize
807 [323; 336) 'ten_is_eleven': bool
808 [339; 342) 'ten': usize
809 [339; 354) 'ten == some_num': bool
810 [346; 354) 'some_num': usize
811 [361; 364) 'ten': usize
812 [361; 368) 'ten < 3': bool
813 [367; 368) '3': usize
814 "###
815 );
816}
817
818#[test]
819fn infer_field_autoderef() {
820 assert_snapshot!(
821 infer(r#"
822struct A {
823 b: B,
824}
825struct B;
826
827fn test1(a: A) {
828 let a1 = a;
829 a1.b;
830 let a2 = &a;
831 a2.b;
832 let a3 = &mut a;
833 a3.b;
834 let a4 = &&&&&&&a;
835 a4.b;
836 let a5 = &mut &&mut &&mut a;
837 a5.b;
838}
839
840fn test2(a1: *const A, a2: *mut A) {
841 a1.b;
842 a2.b;
843}
844"#),
845 @r###"
846 [44; 45) 'a': A
847 [50; 213) '{ ...5.b; }': ()
848 [60; 62) 'a1': A
849 [65; 66) 'a': A
850 [72; 74) 'a1': A
851 [72; 76) 'a1.b': B
852 [86; 88) 'a2': &A
853 [91; 93) '&a': &A
854 [92; 93) 'a': A
855 [99; 101) 'a2': &A
856 [99; 103) 'a2.b': B
857 [113; 115) 'a3': &mut A
858 [118; 124) '&mut a': &mut A
859 [123; 124) 'a': A
860 [130; 132) 'a3': &mut A
861 [130; 134) 'a3.b': B
862 [144; 146) 'a4': &&&&&&&A
863 [149; 157) '&&&&&&&a': &&&&&&&A
864 [150; 157) '&&&&&&a': &&&&&&A
865 [151; 157) '&&&&&a': &&&&&A
866 [152; 157) '&&&&a': &&&&A
867 [153; 157) '&&&a': &&&A
868 [154; 157) '&&a': &&A
869 [155; 157) '&a': &A
870 [156; 157) 'a': A
871 [163; 165) 'a4': &&&&&&&A
872 [163; 167) 'a4.b': B
873 [177; 179) 'a5': &mut &&mut &&mut A
874 [182; 200) '&mut &...&mut a': &mut &&mut &&mut A
875 [187; 200) '&&mut &&mut a': &&mut &&mut A
876 [188; 200) '&mut &&mut a': &mut &&mut A
877 [193; 200) '&&mut a': &&mut A
878 [194; 200) '&mut a': &mut A
879 [199; 200) 'a': A
880 [206; 208) 'a5': &mut &&mut &&mut A
881 [206; 210) 'a5.b': B
882 [224; 226) 'a1': *const A
883 [238; 240) 'a2': *mut A
884 [250; 273) '{ ...2.b; }': ()
885 [256; 258) 'a1': *const A
886 [256; 260) 'a1.b': B
887 [266; 268) 'a2': *mut A
888 [266; 270) 'a2.b': B
889 "###
890 );
891}
892
893#[test]
894fn infer_argument_autoderef() {
895 assert_snapshot!(
896 infer(r#"
897#[lang = "deref"]
898pub trait Deref {
899 type Target;
900 fn deref(&self) -> &Self::Target;
901}
902
903struct A<T>(T);
904
905impl<T> A<T> {
906 fn foo(&self) -> &T {
907 &self.0
908 }
909}
910
911struct B<T>(T);
912
913impl<T> Deref for B<T> {
914 type Target = T;
915 fn deref(&self) -> &Self::Target {
916 &self.0
917 }
918}
919
920fn test() {
921 let t = A::foo(&&B(B(A(42))));
922}
923"#),
924 @r###"
925 [68; 72) 'self': &Self
926 [139; 143) 'self': &A<T>
927 [151; 174) '{ ... }': &T
928 [161; 168) '&self.0': &T
929 [162; 166) 'self': &A<T>
930 [162; 168) 'self.0': T
931 [255; 259) 'self': &B<T>
932 [278; 301) '{ ... }': &T
933 [288; 295) '&self.0': &T
934 [289; 293) 'self': &B<T>
935 [289; 295) 'self.0': T
936 [315; 353) '{ ...))); }': ()
937 [325; 326) 't': &i32
938 [329; 335) 'A::foo': fn foo<i32>(&A<T>) -> &T
939 [329; 350) 'A::foo...42))))': &i32
940 [336; 349) '&&B(B(A(42)))': &&B<B<A<i32>>>
941 [337; 349) '&B(B(A(42)))': &B<B<A<i32>>>
942 [338; 339) 'B': B<B<A<i32>>>(T) -> B<T>
943 [338; 349) 'B(B(A(42)))': B<B<A<i32>>>
944 [340; 341) 'B': B<A<i32>>(T) -> B<T>
945 [340; 348) 'B(A(42))': B<A<i32>>
946 [342; 343) 'A': A<i32>(T) -> A<T>
947 [342; 347) 'A(42)': A<i32>
948 [344; 346) '42': i32
949 "###
950 );
951}
952
953#[test]
954fn infer_method_argument_autoderef() {
955 assert_snapshot!(
956 infer(r#"
957#[lang = "deref"]
958pub trait Deref {
959 type Target;
960 fn deref(&self) -> &Self::Target;
961}
962
963struct A<T>(*mut T);
964
965impl<T> A<T> {
966 fn foo(&self, x: &A<T>) -> &T {
967 &*x.0
968 }
969}
970
971struct B<T>(T);
972
973impl<T> Deref for B<T> {
974 type Target = T;
975 fn deref(&self) -> &Self::Target {
976 &self.0
977 }
978}
979
980fn test(a: A<i32>) {
981 let t = A(0 as *mut _).foo(&&B(B(a)));
982}
983"#),
984 @r###"
985 [68; 72) 'self': &Self
986 [144; 148) 'self': &A<T>
987 [150; 151) 'x': &A<T>
988 [166; 187) '{ ... }': &T
989 [176; 181) '&*x.0': &T
990 [177; 181) '*x.0': T
991 [178; 179) 'x': &A<T>
992 [178; 181) 'x.0': *mut T
993 [268; 272) 'self': &B<T>
994 [291; 314) '{ ... }': &T
995 [301; 308) '&self.0': &T
996 [302; 306) 'self': &B<T>
997 [302; 308) 'self.0': T
998 [326; 327) 'a': A<i32>
999 [337; 383) '{ ...))); }': ()
1000 [347; 348) 't': &i32
1001 [351; 352) 'A': A<i32>(*mut T) -> A<T>
1002 [351; 365) 'A(0 as *mut _)': A<i32>
1003 [351; 380) 'A(0 as...B(a)))': &i32
1004 [353; 354) '0': i32
1005 [353; 364) '0 as *mut _': *mut i32
1006 [370; 379) '&&B(B(a))': &&B<B<A<i32>>>
1007 [371; 379) '&B(B(a))': &B<B<A<i32>>>
1008 [372; 373) 'B': B<B<A<i32>>>(T) -> B<T>
1009 [372; 379) 'B(B(a))': B<B<A<i32>>>
1010 [374; 375) 'B': B<A<i32>>(T) -> B<T>
1011 [374; 378) 'B(a)': B<A<i32>>
1012 [376; 377) 'a': A<i32>
1013 "###
1014 );
1015}
1016
1017#[test]
1018fn bug_484() {
1019 assert_snapshot!(
1020 infer(r#"
1021fn test() {
1022 let x = if true {};
1023}
1024"#),
1025 @r###"
1026 [11; 37) '{ l... {}; }': ()
1027 [20; 21) 'x': ()
1028 [24; 34) 'if true {}': ()
1029 [27; 31) 'true': bool
1030 [32; 34) '{}': ()
1031 "###
1032 );
1033}
1034
1035#[test]
1036fn infer_in_elseif() {
1037 assert_snapshot!(
1038 infer(r#"
1039struct Foo { field: i32 }
1040fn main(foo: Foo) {
1041 if true {
1042
1043 } else if false {
1044 foo.field
1045 }
1046}
1047"#),
1048 @r###"
1049 [35; 38) 'foo': Foo
1050 [45; 109) '{ ... } }': ()
1051 [51; 107) 'if tru... }': ()
1052 [54; 58) 'true': bool
1053 [59; 67) '{ }': ()
1054 [73; 107) 'if fal... }': ()
1055 [76; 81) 'false': bool
1056 [82; 107) '{ ... }': i32
1057 [92; 95) 'foo': Foo
1058 [92; 101) 'foo.field': i32
1059 "###
1060 )
1061}
1062
1063#[test]
1064fn infer_if_match_with_return() {
1065 assert_snapshot!(
1066 infer(r#"
1067fn foo() {
1068 let _x1 = if true {
1069 1
1070 } else {
1071 return;
1072 };
1073 let _x2 = if true {
1074 2
1075 } else {
1076 return
1077 };
1078 let _x3 = match true {
1079 true => 3,
1080 _ => {
1081 return;
1082 }
1083 };
1084 let _x4 = match true {
1085 true => 4,
1086 _ => return
1087 };
1088}"#),
1089 @r###"
1090 [10; 323) '{ ... }; }': ()
1091 [20; 23) '_x1': i32
1092 [26; 80) 'if tru... }': i32
1093 [29; 33) 'true': bool
1094 [34; 51) '{ ... }': i32
1095 [44; 45) '1': i32
1096 [57; 80) '{ ... }': !
1097 [67; 73) 'return': !
1098 [90; 93) '_x2': i32
1099 [96; 149) 'if tru... }': i32
1100 [99; 103) 'true': bool
1101 [104; 121) '{ ... }': i32
1102 [114; 115) '2': i32
1103 [127; 149) '{ ... }': !
1104 [137; 143) 'return': !
1105 [159; 162) '_x3': i32
1106 [165; 247) 'match ... }': i32
1107 [171; 175) 'true': bool
1108 [186; 190) 'true': bool
1109 [194; 195) '3': i32
1110 [205; 206) '_': bool
1111 [210; 241) '{ ... }': !
1112 [224; 230) 'return': !
1113 [257; 260) '_x4': i32
1114 [263; 320) 'match ... }': i32
1115 [269; 273) 'true': bool
1116 [284; 288) 'true': bool
1117 [292; 293) '4': i32
1118 [303; 304) '_': bool
1119 [308; 314) 'return': !
1120 "###
1121 )
1122}
1123
1124#[test]
1125fn infer_inherent_method() {
1126 assert_snapshot!(
1127 infer(r#"
1128struct A;
1129
1130impl A {
1131 fn foo(self, x: u32) -> i32 {}
1132}
1133
1134mod b {
1135 impl super::A {
1136 fn bar(&self, x: u64) -> i64 {}
1137 }
1138}
1139
1140fn test(a: A) {
1141 a.foo(1);
1142 (&a).bar(1);
1143 a.bar(1);
1144}
1145"#),
1146 @r###"
1147 [32; 36) 'self': A
1148 [38; 39) 'x': u32
1149 [53; 55) '{}': ()
1150 [103; 107) 'self': &A
1151 [109; 110) 'x': u64
1152 [124; 126) '{}': ()
1153 [144; 145) 'a': A
1154 [150; 198) '{ ...(1); }': ()
1155 [156; 157) 'a': A
1156 [156; 164) 'a.foo(1)': i32
1157 [162; 163) '1': u32
1158 [170; 181) '(&a).bar(1)': i64
1159 [171; 173) '&a': &A
1160 [172; 173) 'a': A
1161 [179; 180) '1': u64
1162 [187; 188) 'a': A
1163 [187; 195) 'a.bar(1)': i64
1164 [193; 194) '1': u64
1165 "###
1166 );
1167}
1168
1169#[test]
1170fn infer_inherent_method_str() {
1171 assert_snapshot!(
1172 infer(r#"
1173#[lang = "str"]
1174impl str {
1175 fn foo(&self) -> i32 {}
1176}
1177
1178fn test() {
1179 "foo".foo();
1180}
1181"#),
1182 @r###"
1183 [40; 44) 'self': &str
1184 [53; 55) '{}': ()
1185 [69; 89) '{ ...o(); }': ()
1186 [75; 80) '"foo"': &str
1187 [75; 86) '"foo".foo()': i32
1188 "###
1189 );
1190}
1191
1192#[test]
1193fn infer_tuple() {
1194 assert_snapshot!(
1195 infer(r#"
1196fn test(x: &str, y: isize) {
1197 let a: (u32, &str) = (1, "a");
1198 let b = (a, x);
1199 let c = (y, x);
1200 let d = (c, x);
1201 let e = (1, "e");
1202 let f = (e, "d");
1203}
1204"#),
1205 @r###"
1206 [9; 10) 'x': &str
1207 [18; 19) 'y': isize
1208 [28; 170) '{ ...d"); }': ()
1209 [38; 39) 'a': (u32, &str)
1210 [55; 63) '(1, "a")': (u32, &str)
1211 [56; 57) '1': u32
1212 [59; 62) '"a"': &str
1213 [73; 74) 'b': ((u32, &str), &str)
1214 [77; 83) '(a, x)': ((u32, &str), &str)
1215 [78; 79) 'a': (u32, &str)
1216 [81; 82) 'x': &str
1217 [93; 94) 'c': (isize, &str)
1218 [97; 103) '(y, x)': (isize, &str)
1219 [98; 99) 'y': isize
1220 [101; 102) 'x': &str
1221 [113; 114) 'd': ((isize, &str), &str)
1222 [117; 123) '(c, x)': ((isize, &str), &str)
1223 [118; 119) 'c': (isize, &str)
1224 [121; 122) 'x': &str
1225 [133; 134) 'e': (i32, &str)
1226 [137; 145) '(1, "e")': (i32, &str)
1227 [138; 139) '1': i32
1228 [141; 144) '"e"': &str
1229 [155; 156) 'f': ((i32, &str), &str)
1230 [159; 167) '(e, "d")': ((i32, &str), &str)
1231 [160; 161) 'e': (i32, &str)
1232 [163; 166) '"d"': &str
1233 "###
1234 );
1235}
1236
1237#[test]
1238fn infer_array() {
1239 assert_snapshot!(
1240 infer(r#"
1241fn test(x: &str, y: isize) {
1242 let a = [x];
1243 let b = [a, a];
1244 let c = [b, b];
1245
1246 let d = [y, 1, 2, 3];
1247 let d = [1, y, 2, 3];
1248 let e = [y];
1249 let f = [d, d];
1250 let g = [e, e];
1251
1252 let h = [1, 2];
1253 let i = ["a", "b"];
1254
1255 let b = [a, ["b"]];
1256 let x: [u8; 0] = [];
1257}
1258"#),
1259 @r###"
1260 [9; 10) 'x': &str
1261 [18; 19) 'y': isize
1262 [28; 293) '{ ... []; }': ()
1263 [38; 39) 'a': [&str;_]
1264 [42; 45) '[x]': [&str;_]
1265 [43; 44) 'x': &str
1266 [55; 56) 'b': [[&str;_];_]
1267 [59; 65) '[a, a]': [[&str;_];_]
1268 [60; 61) 'a': [&str;_]
1269 [63; 64) 'a': [&str;_]
1270 [75; 76) 'c': [[[&str;_];_];_]
1271 [79; 85) '[b, b]': [[[&str;_];_];_]
1272 [80; 81) 'b': [[&str;_];_]
1273 [83; 84) 'b': [[&str;_];_]
1274 [96; 97) 'd': [isize;_]
1275 [100; 112) '[y, 1, 2, 3]': [isize;_]
1276 [101; 102) 'y': isize
1277 [104; 105) '1': isize
1278 [107; 108) '2': isize
1279 [110; 111) '3': isize
1280 [122; 123) 'd': [isize;_]
1281 [126; 138) '[1, y, 2, 3]': [isize;_]
1282 [127; 128) '1': isize
1283 [130; 131) 'y': isize
1284 [133; 134) '2': isize
1285 [136; 137) '3': isize
1286 [148; 149) 'e': [isize;_]
1287 [152; 155) '[y]': [isize;_]
1288 [153; 154) 'y': isize
1289 [165; 166) 'f': [[isize;_];_]
1290 [169; 175) '[d, d]': [[isize;_];_]
1291 [170; 171) 'd': [isize;_]
1292 [173; 174) 'd': [isize;_]
1293 [185; 186) 'g': [[isize;_];_]
1294 [189; 195) '[e, e]': [[isize;_];_]
1295 [190; 191) 'e': [isize;_]
1296 [193; 194) 'e': [isize;_]
1297 [206; 207) 'h': [i32;_]
1298 [210; 216) '[1, 2]': [i32;_]
1299 [211; 212) '1': i32
1300 [214; 215) '2': i32
1301 [226; 227) 'i': [&str;_]
1302 [230; 240) '["a", "b"]': [&str;_]
1303 [231; 234) '"a"': &str
1304 [236; 239) '"b"': &str
1305 [251; 252) 'b': [[&str;_];_]
1306 [255; 265) '[a, ["b"]]': [[&str;_];_]
1307 [256; 257) 'a': [&str;_]
1308 [259; 264) '["b"]': [&str;_]
1309 [260; 263) '"b"': &str
1310 [275; 276) 'x': [u8;_]
1311 [288; 290) '[]': [u8;_]
1312 "###
1313 );
1314}
1315
1316#[test]
1317fn infer_pattern() {
1318 assert_snapshot!(
1319 infer(r#"
1320fn test(x: &i32) {
1321 let y = x;
1322 let &z = x;
1323 let a = z;
1324 let (c, d) = (1, "hello");
1325
1326 for (e, f) in some_iter {
1327 let g = e;
1328 }
1329
1330 if let [val] = opt {
1331 let h = val;
1332 }
1333
1334 let lambda = |a: u64, b, c: i32| { a + b; c };
1335
1336 let ref ref_to_x = x;
1337 let mut mut_x = x;
1338 let ref mut mut_ref_to_x = x;
1339 let k = mut_ref_to_x;
1340}
1341"#),
1342 @r###"
1343 [9; 10) 'x': &i32
1344 [18; 369) '{ ...o_x; }': ()
1345 [28; 29) 'y': &i32
1346 [32; 33) 'x': &i32
1347 [43; 45) '&z': &i32
1348 [44; 45) 'z': i32
1349 [48; 49) 'x': &i32
1350 [59; 60) 'a': i32
1351 [63; 64) 'z': i32
1352 [74; 80) '(c, d)': (i32, &str)
1353 [75; 76) 'c': i32
1354 [78; 79) 'd': &str
1355 [83; 95) '(1, "hello")': (i32, &str)
1356 [84; 85) '1': i32
1357 [87; 94) '"hello"': &str
1358 [102; 152) 'for (e... }': ()
1359 [106; 112) '(e, f)': ({unknown}, {unknown})
1360 [107; 108) 'e': {unknown}
1361 [110; 111) 'f': {unknown}
1362 [116; 125) 'some_iter': {unknown}
1363 [126; 152) '{ ... }': ()
1364 [140; 141) 'g': {unknown}
1365 [144; 145) 'e': {unknown}
1366 [158; 205) 'if let... }': ()
1367 [165; 170) '[val]': {unknown}
1368 [173; 176) 'opt': {unknown}
1369 [177; 205) '{ ... }': ()
1370 [191; 192) 'h': {unknown}
1371 [195; 198) 'val': {unknown}
1372 [215; 221) 'lambda': |u64, u64, i32| -> i32
1373 [224; 256) '|a: u6...b; c }': |u64, u64, i32| -> i32
1374 [225; 226) 'a': u64
1375 [233; 234) 'b': u64
1376 [236; 237) 'c': i32
1377 [244; 256) '{ a + b; c }': i32
1378 [246; 247) 'a': u64
1379 [246; 251) 'a + b': u64
1380 [250; 251) 'b': u64
1381 [253; 254) 'c': i32
1382 [267; 279) 'ref ref_to_x': &&i32
1383 [282; 283) 'x': &i32
1384 [293; 302) 'mut mut_x': &i32
1385 [305; 306) 'x': &i32
1386 [316; 336) 'ref mu...f_to_x': &mut &i32
1387 [339; 340) 'x': &i32
1388 [350; 351) 'k': &mut &i32
1389 [354; 366) 'mut_ref_to_x': &mut &i32
1390 "###
1391 );
1392}
1393
1394#[test]
1395fn infer_pattern_match_ergonomics() {
1396 assert_snapshot!(
1397 infer(r#"
1398struct A<T>(T);
1399
1400fn test() {
1401 let A(n) = &A(1);
1402 let A(n) = &mut A(1);
1403}
1404"#),
1405 @r###"
1406 [28; 79) '{ ...(1); }': ()
1407 [38; 42) 'A(n)': A<i32>
1408 [40; 41) 'n': &i32
1409 [45; 50) '&A(1)': &A<i32>
1410 [46; 47) 'A': A<i32>(T) -> A<T>
1411 [46; 50) 'A(1)': A<i32>
1412 [48; 49) '1': i32
1413 [60; 64) 'A(n)': A<i32>
1414 [62; 63) 'n': &mut i32
1415 [67; 76) '&mut A(1)': &mut A<i32>
1416 [72; 73) 'A': A<i32>(T) -> A<T>
1417 [72; 76) 'A(1)': A<i32>
1418 [74; 75) '1': i32
1419 "###
1420 );
1421}
1422
1423#[test]
1424fn infer_pattern_match_ergonomics_ref() {
1425 covers!(match_ergonomics_ref);
1426 assert_snapshot!(
1427 infer(r#"
1428fn test() {
1429 let v = &(1, &2);
1430 let (_, &w) = v;
1431}
1432"#),
1433 @r###"
1434 [11; 57) '{ ...= v; }': ()
1435 [21; 22) 'v': &(i32, &i32)
1436 [25; 33) '&(1, &2)': &(i32, &i32)
1437 [26; 33) '(1, &2)': (i32, &i32)
1438 [27; 28) '1': i32
1439 [30; 32) '&2': &i32
1440 [31; 32) '2': i32
1441 [43; 50) '(_, &w)': (i32, &i32)
1442 [44; 45) '_': i32
1443 [47; 49) '&w': &i32
1444 [48; 49) 'w': i32
1445 [53; 54) 'v': &(i32, &i32)
1446 "###
1447 );
1448}
1449
1450#[test]
1451fn infer_adt_pattern() {
1452 assert_snapshot!(
1453 infer(r#"
1454enum E {
1455 A { x: usize },
1456 B
1457}
1458
1459struct S(u32, E);
1460
1461fn test() {
1462 let e = E::A { x: 3 };
1463
1464 let S(y, z) = foo;
1465 let E::A { x: new_var } = e;
1466
1467 match e {
1468 E::A { x } => x,
1469 E::B if foo => 1,
1470 E::B => 10,
1471 };
1472
1473 let ref d @ E::A { .. } = e;
1474 d;
1475}
1476"#),
1477 @r###"
1478 [68; 289) '{ ... d; }': ()
1479 [78; 79) 'e': E
1480 [82; 95) 'E::A { x: 3 }': E
1481 [92; 93) '3': usize
1482 [106; 113) 'S(y, z)': S
1483 [108; 109) 'y': u32
1484 [111; 112) 'z': E
1485 [116; 119) 'foo': S
1486 [129; 148) 'E::A {..._var }': E
1487 [139; 146) 'new_var': usize
1488 [151; 152) 'e': E
1489 [159; 245) 'match ... }': usize
1490 [165; 166) 'e': E
1491 [177; 187) 'E::A { x }': E
1492 [184; 185) 'x': usize
1493 [191; 192) 'x': usize
1494 [202; 206) 'E::B': E
1495 [210; 213) 'foo': bool
1496 [217; 218) '1': usize
1497 [228; 232) 'E::B': E
1498 [236; 238) '10': usize
1499 [256; 275) 'ref d ...{ .. }': &E
1500 [264; 275) 'E::A { .. }': E
1501 [278; 279) 'e': E
1502 [285; 286) 'd': &E
1503 "###
1504 );
1505}
1506
1507#[test]
1508fn infer_struct_generics() {
1509 assert_snapshot!(
1510 infer(r#"
1511struct A<T> {
1512 x: T,
1513}
1514
1515fn test(a1: A<u32>, i: i32) {
1516 a1.x;
1517 let a2 = A { x: i };
1518 a2.x;
1519 let a3 = A::<i128> { x: 1 };
1520 a3.x;
1521}
1522"#),
1523 @r###"
1524 [36; 38) 'a1': A<u32>
1525 [48; 49) 'i': i32
1526 [56; 147) '{ ...3.x; }': ()
1527 [62; 64) 'a1': A<u32>
1528 [62; 66) 'a1.x': u32
1529 [76; 78) 'a2': A<i32>
1530 [81; 91) 'A { x: i }': A<i32>
1531 [88; 89) 'i': i32
1532 [97; 99) 'a2': A<i32>
1533 [97; 101) 'a2.x': i32
1534 [111; 113) 'a3': A<i128>
1535 [116; 134) 'A::<i1...x: 1 }': A<i128>
1536 [131; 132) '1': i128
1537 [140; 142) 'a3': A<i128>
1538 [140; 144) 'a3.x': i128
1539 "###
1540 );
1541}
1542
1543#[test]
1544fn infer_tuple_struct_generics() {
1545 assert_snapshot!(
1546 infer(r#"
1547struct A<T>(T);
1548enum Option<T> { Some(T), None }
1549use Option::*;
1550
1551fn test() {
1552 A(42);
1553 A(42u128);
1554 Some("x");
1555 Option::Some("x");
1556 None;
1557 let x: Option<i64> = None;
1558}
1559"#),
1560 @r###"
1561 [76; 184) '{ ...one; }': ()
1562 [82; 83) 'A': A<i32>(T) -> A<T>
1563 [82; 87) 'A(42)': A<i32>
1564 [84; 86) '42': i32
1565 [93; 94) 'A': A<u128>(T) -> A<T>
1566 [93; 102) 'A(42u128)': A<u128>
1567 [95; 101) '42u128': u128
1568 [108; 112) 'Some': Some<&str>(T) -> Option<T>
1569 [108; 117) 'Some("x")': Option<&str>
1570 [113; 116) '"x"': &str
1571 [123; 135) 'Option::Some': Some<&str>(T) -> Option<T>
1572 [123; 140) 'Option...e("x")': Option<&str>
1573 [136; 139) '"x"': &str
1574 [146; 150) 'None': Option<{unknown}>
1575 [160; 161) 'x': Option<i64>
1576 [177; 181) 'None': Option<i64>
1577 "###
1578 );
1579}
1580
1581#[test]
1582fn infer_generics_in_patterns() {
1583 assert_snapshot!(
1584 infer(r#"
1585struct A<T> {
1586 x: T,
1587}
1588
1589enum Option<T> {
1590 Some(T),
1591 None,
1592}
1593
1594fn test(a1: A<u32>, o: Option<u64>) {
1595 let A { x: x2 } = a1;
1596 let A::<i64> { x: x3 } = A { x: 1 };
1597 match o {
1598 Option::Some(t) => t,
1599 _ => 1,
1600 };
1601}
1602"#),
1603 @r###"
1604 [79; 81) 'a1': A<u32>
1605 [91; 92) 'o': Option<u64>
1606 [107; 244) '{ ... }; }': ()
1607 [117; 128) 'A { x: x2 }': A<u32>
1608 [124; 126) 'x2': u32
1609 [131; 133) 'a1': A<u32>
1610 [143; 161) 'A::<i6...: x3 }': A<i64>
1611 [157; 159) 'x3': i64
1612 [164; 174) 'A { x: 1 }': A<i64>
1613 [171; 172) '1': i64
1614 [180; 241) 'match ... }': u64
1615 [186; 187) 'o': Option<u64>
1616 [198; 213) 'Option::Some(t)': Option<u64>
1617 [211; 212) 't': u64
1618 [217; 218) 't': u64
1619 [228; 229) '_': Option<u64>
1620 [233; 234) '1': u64
1621 "###
1622 );
1623}
1624
1625#[test]
1626fn infer_function_generics() {
1627 assert_snapshot!(
1628 infer(r#"
1629fn id<T>(t: T) -> T { t }
1630
1631fn test() {
1632 id(1u32);
1633 id::<i128>(1);
1634 let x: u64 = id(1);
1635}
1636"#),
1637 @r###"
1638 [10; 11) 't': T
1639 [21; 26) '{ t }': T
1640 [23; 24) 't': T
1641 [38; 98) '{ ...(1); }': ()
1642 [44; 46) 'id': fn id<u32>(T) -> T
1643 [44; 52) 'id(1u32)': u32
1644 [47; 51) '1u32': u32
1645 [58; 68) 'id::<i128>': fn id<i128>(T) -> T
1646 [58; 71) 'id::<i128>(1)': i128
1647 [69; 70) '1': i128
1648 [81; 82) 'x': u64
1649 [90; 92) 'id': fn id<u64>(T) -> T
1650 [90; 95) 'id(1)': u64
1651 [93; 94) '1': u64
1652 "###
1653 );
1654}
1655
1656#[test]
1657fn infer_impl_generics() {
1658 assert_snapshot!(
1659 infer(r#"
1660struct A<T1, T2> {
1661 x: T1,
1662 y: T2,
1663}
1664impl<Y, X> A<X, Y> {
1665 fn x(self) -> X {
1666 self.x
1667 }
1668 fn y(self) -> Y {
1669 self.y
1670 }
1671 fn z<T>(self, t: T) -> (X, Y, T) {
1672 (self.x, self.y, t)
1673 }
1674}
1675
1676fn test() -> i128 {
1677 let a = A { x: 1u64, y: 1i64 };
1678 a.x();
1679 a.y();
1680 a.z(1i128);
1681 a.z::<u128>(1);
1682}
1683"#),
1684 @r###"
1685 [74; 78) 'self': A<X, Y>
1686 [85; 107) '{ ... }': X
1687 [95; 99) 'self': A<X, Y>
1688 [95; 101) 'self.x': X
1689 [117; 121) 'self': A<X, Y>
1690 [128; 150) '{ ... }': Y
1691 [138; 142) 'self': A<X, Y>
1692 [138; 144) 'self.y': Y
1693 [163; 167) 'self': A<X, Y>
1694 [169; 170) 't': T
1695 [188; 223) '{ ... }': (X, Y, T)
1696 [198; 217) '(self.....y, t)': (X, Y, T)
1697 [199; 203) 'self': A<X, Y>
1698 [199; 205) 'self.x': X
1699 [207; 211) 'self': A<X, Y>
1700 [207; 213) 'self.y': Y
1701 [215; 216) 't': T
1702 [245; 342) '{ ...(1); }': ()
1703 [255; 256) 'a': A<u64, i64>
1704 [259; 281) 'A { x:...1i64 }': A<u64, i64>
1705 [266; 270) '1u64': u64
1706 [275; 279) '1i64': i64
1707 [287; 288) 'a': A<u64, i64>
1708 [287; 292) 'a.x()': u64
1709 [298; 299) 'a': A<u64, i64>
1710 [298; 303) 'a.y()': i64
1711 [309; 310) 'a': A<u64, i64>
1712 [309; 319) 'a.z(1i128)': (u64, i64, i128)
1713 [313; 318) '1i128': i128
1714 [325; 326) 'a': A<u64, i64>
1715 [325; 339) 'a.z::<u128>(1)': (u64, i64, u128)
1716 [337; 338) '1': u128
1717 "###
1718 );
1719}
1720
1721#[test]
1722fn infer_impl_generics_with_autoderef() {
1723 assert_snapshot!(
1724 infer(r#"
1725enum Option<T> {
1726 Some(T),
1727 None,
1728}
1729impl<T> Option<T> {
1730 fn as_ref(&self) -> Option<&T> {}
1731}
1732fn test(o: Option<u32>) {
1733 (&o).as_ref();
1734 o.as_ref();
1735}
1736"#),
1737 @r###"
1738 [78; 82) 'self': &Option<T>
1739 [98; 100) '{}': ()
1740 [111; 112) 'o': Option<u32>
1741 [127; 165) '{ ...f(); }': ()
1742 [133; 146) '(&o).as_ref()': Option<&u32>
1743 [134; 136) '&o': &Option<u32>
1744 [135; 136) 'o': Option<u32>
1745 [152; 153) 'o': Option<u32>
1746 [152; 162) 'o.as_ref()': Option<&u32>
1747 "###
1748 );
1749}
1750
1751#[test]
1752fn infer_generic_chain() {
1753 assert_snapshot!(
1754 infer(r#"
1755struct A<T> {
1756 x: T,
1757}
1758impl<T2> A<T2> {
1759 fn x(self) -> T2 {
1760 self.x
1761 }
1762}
1763fn id<T>(t: T) -> T { t }
1764
1765fn test() -> i128 {
1766 let x = 1;
1767 let y = id(x);
1768 let a = A { x: id(y) };
1769 let z = id(a.x);
1770 let b = A { x: z };
1771 b.x()
1772}
1773"#),
1774 @r###"
1775 [53; 57) 'self': A<T2>
1776 [65; 87) '{ ... }': T2
1777 [75; 79) 'self': A<T2>
1778 [75; 81) 'self.x': T2
1779 [99; 100) 't': T
1780 [110; 115) '{ t }': T
1781 [112; 113) 't': T
1782 [135; 261) '{ ....x() }': i128
1783 [146; 147) 'x': i128
1784 [150; 151) '1': i128
1785 [162; 163) 'y': i128
1786 [166; 168) 'id': fn id<i128>(T) -> T
1787 [166; 171) 'id(x)': i128
1788 [169; 170) 'x': i128
1789 [182; 183) 'a': A<i128>
1790 [186; 200) 'A { x: id(y) }': A<i128>
1791 [193; 195) 'id': fn id<i128>(T) -> T
1792 [193; 198) 'id(y)': i128
1793 [196; 197) 'y': i128
1794 [211; 212) 'z': i128
1795 [215; 217) 'id': fn id<i128>(T) -> T
1796 [215; 222) 'id(a.x)': i128
1797 [218; 219) 'a': A<i128>
1798 [218; 221) 'a.x': i128
1799 [233; 234) 'b': A<i128>
1800 [237; 247) 'A { x: z }': A<i128>
1801 [244; 245) 'z': i128
1802 [254; 255) 'b': A<i128>
1803 [254; 259) 'b.x()': i128
1804 "###
1805 );
1806}
1807
1808#[test]
1809fn infer_associated_const() {
1810 assert_snapshot!(
1811 infer(r#"
1812struct Struct;
1813
1814impl Struct {
1815 const FOO: u32 = 1;
1816}
1817
1818enum Enum {}
1819
1820impl Enum {
1821 const BAR: u32 = 2;
1822}
1823
1824trait Trait {
1825 const ID: u32;
1826}
1827
1828struct TraitTest;
1829
1830impl Trait for TraitTest {
1831 const ID: u32 = 5;
1832}
1833
1834fn test() {
1835 let x = Struct::FOO;
1836 let y = Enum::BAR;
1837 let z = TraitTest::ID;
1838}
1839"#),
1840 @r###"
1841 [52; 53) '1': u32
1842 [105; 106) '2': u32
1843 [213; 214) '5': u32
1844 [229; 307) '{ ...:ID; }': ()
1845 [239; 240) 'x': u32
1846 [243; 254) 'Struct::FOO': u32
1847 [264; 265) 'y': u32
1848 [268; 277) 'Enum::BAR': u32
1849 [287; 288) 'z': u32
1850 [291; 304) 'TraitTest::ID': u32
1851 "###
1852 );
1853}
1854
1855#[test]
1856fn infer_associated_method_struct() {
1857 assert_snapshot!(
1858 infer(r#"
1859struct A { x: u32 }
1860
1861impl A {
1862 fn new() -> A {
1863 A { x: 0 }
1864 }
1865}
1866fn test() {
1867 let a = A::new();
1868 a.x;
1869}
1870"#),
1871 @r###"
1872 [49; 75) '{ ... }': A
1873 [59; 69) 'A { x: 0 }': A
1874 [66; 67) '0': u32
1875 [88; 122) '{ ...a.x; }': ()
1876 [98; 99) 'a': A
1877 [102; 108) 'A::new': fn new() -> A
1878 [102; 110) 'A::new()': A
1879 [116; 117) 'a': A
1880 [116; 119) 'a.x': u32
1881 "###
1882 );
1883}
1884
1885#[test]
1886fn infer_associated_method_enum() {
1887 assert_snapshot!(
1888 infer(r#"
1889enum A { B, C }
1890
1891impl A {
1892 pub fn b() -> A {
1893 A::B
1894 }
1895 pub fn c() -> A {
1896 A::C
1897 }
1898}
1899fn test() {
1900 let a = A::b();
1901 a;
1902 let c = A::c();
1903 c;
1904}
1905"#),
1906 @r###"
1907 [47; 67) '{ ... }': A
1908 [57; 61) 'A::B': A
1909 [88; 108) '{ ... }': A
1910 [98; 102) 'A::C': A
1911 [121; 178) '{ ... c; }': ()
1912 [131; 132) 'a': A
1913 [135; 139) 'A::b': fn b() -> A
1914 [135; 141) 'A::b()': A
1915 [147; 148) 'a': A
1916 [158; 159) 'c': A
1917 [162; 166) 'A::c': fn c() -> A
1918 [162; 168) 'A::c()': A
1919 [174; 175) 'c': A
1920 "###
1921 );
1922}
1923
1924#[test]
1925fn infer_associated_method_with_modules() {
1926 assert_snapshot!(
1927 infer(r#"
1928mod a {
1929 struct A;
1930 impl A { pub fn thing() -> A { A {} }}
1931}
1932
1933mod b {
1934 struct B;
1935 impl B { pub fn thing() -> u32 { 99 }}
1936
1937 mod c {
1938 struct C;
1939 impl C { pub fn thing() -> C { C {} }}
1940 }
1941}
1942use b::c;
1943
1944fn test() {
1945 let x = a::A::thing();
1946 let y = b::B::thing();
1947 let z = c::C::thing();
1948}
1949"#),
1950 @r###"
1951 [56; 64) '{ A {} }': A
1952 [58; 62) 'A {}': A
1953 [126; 132) '{ 99 }': u32
1954 [128; 130) '99': u32
1955 [202; 210) '{ C {} }': C
1956 [204; 208) 'C {}': C
1957 [241; 325) '{ ...g(); }': ()
1958 [251; 252) 'x': A
1959 [255; 266) 'a::A::thing': fn thing() -> A
1960 [255; 268) 'a::A::thing()': A
1961 [278; 279) 'y': u32
1962 [282; 293) 'b::B::thing': fn thing() -> u32
1963 [282; 295) 'b::B::thing()': u32
1964 [305; 306) 'z': C
1965 [309; 320) 'c::C::thing': fn thing() -> C
1966 [309; 322) 'c::C::thing()': C
1967 "###
1968 );
1969}
1970
1971#[test]
1972fn infer_associated_method_generics() {
1973 assert_snapshot!(
1974 infer(r#"
1975struct Gen<T> {
1976 val: T
1977}
1978
1979impl<T> Gen<T> {
1980 pub fn make(val: T) -> Gen<T> {
1981 Gen { val }
1982 }
1983}
1984
1985fn test() {
1986 let a = Gen::make(0u32);
1987}
1988"#),
1989 @r###"
1990 [64; 67) 'val': T
1991 [82; 109) '{ ... }': Gen<T>
1992 [92; 103) 'Gen { val }': Gen<T>
1993 [98; 101) 'val': T
1994 [123; 155) '{ ...32); }': ()
1995 [133; 134) 'a': Gen<u32>
1996 [137; 146) 'Gen::make': fn make<u32>(T) -> Gen<T>
1997 [137; 152) 'Gen::make(0u32)': Gen<u32>
1998 [147; 151) '0u32': u32
1999 "###
2000 );
2001}
2002
2003#[test]
2004fn infer_associated_method_generics_with_default_param() {
2005 assert_snapshot!(
2006 infer(r#"
2007struct Gen<T=u32> {
2008 val: T
2009}
2010
2011impl<T> Gen<T> {
2012 pub fn make() -> Gen<T> {
2013 loop { }
2014 }
2015}
2016
2017fn test() {
2018 let a = Gen::make();
2019}
2020"#),
2021 @r###"
2022 [80; 104) '{ ... }': Gen<T>
2023 [90; 98) 'loop { }': !
2024 [95; 98) '{ }': ()
2025 [118; 146) '{ ...e(); }': ()
2026 [128; 129) 'a': Gen<u32>
2027 [132; 141) 'Gen::make': fn make<u32>() -> Gen<T>
2028 [132; 143) 'Gen::make()': Gen<u32>
2029 "###
2030 );
2031}
2032
2033#[test]
2034fn infer_associated_method_generics_with_default_tuple_param() {
2035 let t = type_at(
2036 r#"
2037//- /main.rs
2038struct Gen<T=()> {
2039 val: T
2040}
2041
2042impl<T> Gen<T> {
2043 pub fn make() -> Gen<T> {
2044 loop { }
2045 }
2046}
2047
2048fn test() {
2049 let a = Gen::make();
2050 a.val<|>;
2051}
2052"#,
2053 );
2054 assert_eq!(t, "()");
2055}
2056
2057#[test]
2058fn infer_associated_method_generics_without_args() {
2059 assert_snapshot!(
2060 infer(r#"
2061struct Gen<T> {
2062 val: T
2063}
2064
2065impl<T> Gen<T> {
2066 pub fn make() -> Gen<T> {
2067 loop { }
2068 }
2069}
2070
2071fn test() {
2072 let a = Gen::<u32>::make();
2073}
2074"#),
2075 @r###"
2076 [76; 100) '{ ... }': Gen<T>
2077 [86; 94) 'loop { }': !
2078 [91; 94) '{ }': ()
2079 [114; 149) '{ ...e(); }': ()
2080 [124; 125) 'a': Gen<u32>
2081 [128; 144) 'Gen::<...::make': fn make<u32>() -> Gen<T>
2082 [128; 146) 'Gen::<...make()': Gen<u32>
2083 "###
2084 );
2085}
2086
2087#[test]
2088fn infer_associated_method_generics_2_type_params_without_args() {
2089 assert_snapshot!(
2090 infer(r#"
2091struct Gen<T, U> {
2092 val: T,
2093 val2: U,
2094}
2095
2096impl<T> Gen<u32, T> {
2097 pub fn make() -> Gen<u32,T> {
2098 loop { }
2099 }
2100}
2101
2102fn test() {
2103 let a = Gen::<u32, u64>::make();
2104}
2105"#),
2106 @r###"
2107 [102; 126) '{ ... }': Gen<u32, T>
2108 [112; 120) 'loop { }': !
2109 [117; 120) '{ }': ()
2110 [140; 180) '{ ...e(); }': ()
2111 [150; 151) 'a': Gen<u32, u64>
2112 [154; 175) 'Gen::<...::make': fn make<u64>() -> Gen<u32, T>
2113 [154; 177) 'Gen::<...make()': Gen<u32, u64>
2114 "###
2115 );
2116}
2117
2118#[test]
2119fn infer_type_alias() {
2120 assert_snapshot!(
2121 infer(r#"
2122struct A<X, Y> { x: X, y: Y }
2123type Foo = A<u32, i128>;
2124type Bar<T> = A<T, u128>;
2125type Baz<U, V> = A<V, U>;
2126fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
2127 x.x;
2128 x.y;
2129 y.x;
2130 y.y;
2131 z.x;
2132 z.y;
2133}
2134"#),
2135 @r###"
2136 [116; 117) 'x': A<u32, i128>
2137 [124; 125) 'y': A<&str, u128>
2138 [138; 139) 'z': A<u8, i8>
2139 [154; 211) '{ ...z.y; }': ()
2140 [160; 161) 'x': A<u32, i128>
2141 [160; 163) 'x.x': u32
2142 [169; 170) 'x': A<u32, i128>
2143 [169; 172) 'x.y': i128
2144 [178; 179) 'y': A<&str, u128>
2145 [178; 181) 'y.x': &str
2146 [187; 188) 'y': A<&str, u128>
2147 [187; 190) 'y.y': u128
2148 [196; 197) 'z': A<u8, i8>
2149 [196; 199) 'z.x': u8
2150 [205; 206) 'z': A<u8, i8>
2151 [205; 208) 'z.y': i8
2152 "###
2153 )
2154}
2155
2156#[test]
2157fn recursive_type_alias() {
2158 assert_snapshot!(
2159 infer(r#"
2160struct A<X> {}
2161type Foo = Foo;
2162type Bar = A<Bar>;
2163fn test(x: Foo) {}
2164"#),
2165 @r###"
2166 [59; 60) 'x': {unknown}
2167 [67; 69) '{}': ()
2168 "###
2169 )
2170}
2171
2172#[test]
2173fn no_panic_on_field_of_enum() {
2174 assert_snapshot!(
2175 infer(r#"
2176enum X {}
2177
2178fn test(x: X) {
2179 x.some_field;
2180}
2181"#),
2182 @r###"
2183 [20; 21) 'x': X
2184 [26; 47) '{ ...eld; }': ()
2185 [32; 33) 'x': X
2186 [32; 44) 'x.some_field': {unknown}
2187 "###
2188 );
2189}
2190
2191#[test]
2192fn bug_585() {
2193 assert_snapshot!(
2194 infer(r#"
2195fn test() {
2196 X {};
2197 match x {
2198 A::B {} => (),
2199 A::Y() => (),
2200 }
2201}
2202"#),
2203 @r###"
2204 [11; 89) '{ ... } }': ()
2205 [17; 21) 'X {}': {unknown}
2206 [27; 87) 'match ... }': ()
2207 [33; 34) 'x': {unknown}
2208 [45; 52) 'A::B {}': {unknown}
2209 [56; 58) '()': ()
2210 [68; 74) 'A::Y()': {unknown}
2211 [78; 80) '()': ()
2212 "###
2213 );
2214}
2215
2216#[test]
2217fn bug_651() {
2218 assert_snapshot!(
2219 infer(r#"
2220fn quux() {
2221 let y = 92;
2222 1 + y;
2223}
2224"#),
2225 @r###"
2226 [11; 41) '{ ...+ y; }': ()
2227 [21; 22) 'y': i32
2228 [25; 27) '92': i32
2229 [33; 34) '1': i32
2230 [33; 38) '1 + y': i32
2231 [37; 38) 'y': i32
2232 "###
2233 );
2234}
2235
2236#[test]
2237fn recursive_vars() {
2238 covers!(type_var_cycles_resolve_completely);
2239 covers!(type_var_cycles_resolve_as_possible);
2240 assert_snapshot!(
2241 infer(r#"
2242fn test() {
2243 let y = unknown;
2244 [y, &y];
2245}
2246"#),
2247 @r###"
2248 [11; 48) '{ ...&y]; }': ()
2249 [21; 22) 'y': &{unknown}
2250 [25; 32) 'unknown': &{unknown}
2251 [38; 45) '[y, &y]': [&&{unknown};_]
2252 [39; 40) 'y': &{unknown}
2253 [42; 44) '&y': &&{unknown}
2254 [43; 44) 'y': &{unknown}
2255 "###
2256 );
2257}
2258
2259#[test]
2260fn recursive_vars_2() {
2261 covers!(type_var_cycles_resolve_completely);
2262 covers!(type_var_cycles_resolve_as_possible);
2263 assert_snapshot!(
2264 infer(r#"
2265fn test() {
2266 let x = unknown;
2267 let y = unknown;
2268 [(x, y), (&y, &x)];
2269}
2270"#),
2271 @r###"
2272 [11; 80) '{ ...x)]; }': ()
2273 [21; 22) 'x': &&{unknown}
2274 [25; 32) 'unknown': &&{unknown}
2275 [42; 43) 'y': &&{unknown}
2276 [46; 53) 'unknown': &&{unknown}
2277 [59; 77) '[(x, y..., &x)]': [(&&&{unknown}, &&&{unknown});_]
2278 [60; 66) '(x, y)': (&&&{unknown}, &&&{unknown})
2279 [61; 62) 'x': &&{unknown}
2280 [64; 65) 'y': &&{unknown}
2281 [68; 76) '(&y, &x)': (&&&{unknown}, &&&{unknown})
2282 [69; 71) '&y': &&&{unknown}
2283 [70; 71) 'y': &&{unknown}
2284 [73; 75) '&x': &&&{unknown}
2285 [74; 75) 'x': &&{unknown}
2286 "###
2287 );
2288}
2289
2290#[test]
2291fn infer_type_param() {
2292 assert_snapshot!(
2293 infer(r#"
2294fn id<T>(x: T) -> T {
2295 x
2296}
2297
2298fn clone<T>(x: &T) -> T {
2299 *x
2300}
2301
2302fn test() {
2303 let y = 10u32;
2304 id(y);
2305 let x: bool = clone(z);
2306 id::<i128>(1);
2307}
2308"#),
2309 @r###"
2310 [10; 11) 'x': T
2311 [21; 30) '{ x }': T
2312 [27; 28) 'x': T
2313 [44; 45) 'x': &T
2314 [56; 66) '{ *x }': T
2315 [62; 64) '*x': T
2316 [63; 64) 'x': &T
2317 [78; 158) '{ ...(1); }': ()
2318 [88; 89) 'y': u32
2319 [92; 97) '10u32': u32
2320 [103; 105) 'id': fn id<u32>(T) -> T
2321 [103; 108) 'id(y)': u32
2322 [106; 107) 'y': u32
2323 [118; 119) 'x': bool
2324 [128; 133) 'clone': fn clone<bool>(&T) -> T
2325 [128; 136) 'clone(z)': bool
2326 [134; 135) 'z': &bool
2327 [142; 152) 'id::<i128>': fn id<i128>(T) -> T
2328 [142; 155) 'id::<i128>(1)': i128
2329 [153; 154) '1': i128
2330 "###
2331 );
2332}
2333
2334#[test]
2335fn infer_std_crash_1() {
2336 // caused stack overflow, taken from std
2337 assert_snapshot!(
2338 infer(r#"
2339enum Maybe<T> {
2340 Real(T),
2341 Fake,
2342}
2343
2344fn write() {
2345 match something_unknown {
2346 Maybe::Real(ref mut something) => (),
2347 }
2348}
2349"#),
2350 @r###"
2351 [54; 139) '{ ... } }': ()
2352 [60; 137) 'match ... }': ()
2353 [66; 83) 'someth...nknown': Maybe<{unknown}>
2354 [94; 124) 'Maybe:...thing)': Maybe<{unknown}>
2355 [106; 123) 'ref mu...ething': &mut {unknown}
2356 [128; 130) '()': ()
2357 "###
2358 );
2359}
2360
2361#[test]
2362fn infer_std_crash_2() {
2363 covers!(type_var_resolves_to_int_var);
2364 // caused "equating two type variables, ...", taken from std
2365 assert_snapshot!(
2366 infer(r#"
2367fn test_line_buffer() {
2368 &[0, b'\n', 1, b'\n'];
2369}
2370"#),
2371 @r###"
2372 [23; 53) '{ ...n']; }': ()
2373 [29; 50) '&[0, b...b'\n']': &[u8;_]
2374 [30; 50) '[0, b'...b'\n']': [u8;_]
2375 [31; 32) '0': u8
2376 [34; 39) 'b'\n'': u8
2377 [41; 42) '1': u8
2378 [44; 49) 'b'\n'': u8
2379 "###
2380 );
2381}
2382
2383#[test]
2384fn infer_std_crash_3() {
2385 // taken from rustc
2386 assert_snapshot!(
2387 infer(r#"
2388pub fn compute() {
2389 match nope!() {
2390 SizeSkeleton::Pointer { non_zero: true, tail } => {}
2391 }
2392}
2393"#),
2394 @r###"
2395 [18; 108) '{ ... } }': ()
2396 [24; 106) 'match ... }': ()
2397 [30; 37) 'nope!()': {unknown}
2398 [48; 94) 'SizeSk...tail }': {unknown}
2399 [82; 86) 'true': {unknown}
2400 [88; 92) 'tail': {unknown}
2401 [98; 100) '{}': ()
2402 "###
2403 );
2404}
2405
2406#[test]
2407fn infer_std_crash_4() {
2408 // taken from rustc
2409 assert_snapshot!(
2410 infer(r#"
2411pub fn primitive_type() {
2412 match *self {
2413 BorrowedRef { type_: Primitive(p), ..} => {},
2414 }
2415}
2416"#),
2417 @r###"
2418 [25; 106) '{ ... } }': ()
2419 [31; 104) 'match ... }': ()
2420 [37; 42) '*self': {unknown}
2421 [38; 42) 'self': {unknown}
2422 [53; 91) 'Borrow...), ..}': {unknown}
2423 [74; 86) 'Primitive(p)': {unknown}
2424 [84; 85) 'p': {unknown}
2425 [95; 97) '{}': ()
2426 "###
2427 );
2428}
2429
2430#[test]
2431fn infer_std_crash_5() {
2432 // taken from rustc
2433 assert_snapshot!(
2434 infer(r#"
2435fn extra_compiler_flags() {
2436 for content in doesnt_matter {
2437 let name = if doesnt_matter {
2438 first
2439 } else {
2440 &content
2441 };
2442
2443 let content = if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&name) {
2444 name
2445 } else {
2446 content
2447 };
2448 }
2449}
2450"#),
2451 @r###"
2452 [27; 323) '{ ... } }': ()
2453 [33; 321) 'for co... }': ()
2454 [37; 44) 'content': &{unknown}
2455 [48; 61) 'doesnt_matter': {unknown}
2456 [62; 321) '{ ... }': ()
2457 [76; 80) 'name': &&{unknown}
2458 [83; 167) 'if doe... }': &&{unknown}
2459 [86; 99) 'doesnt_matter': bool
2460 [100; 129) '{ ... }': &&{unknown}
2461 [114; 119) 'first': &&{unknown}
2462 [135; 167) '{ ... }': &&{unknown}
2463 [149; 157) '&content': &&{unknown}
2464 [150; 157) 'content': &{unknown}
2465 [182; 189) 'content': &{unknown}
2466 [192; 314) 'if ICE... }': &{unknown}
2467 [195; 232) 'ICE_RE..._VALUE': {unknown}
2468 [195; 248) 'ICE_RE...&name)': bool
2469 [242; 247) '&name': &&&{unknown}
2470 [243; 247) 'name': &&{unknown}
2471 [249; 277) '{ ... }': &&{unknown}
2472 [263; 267) 'name': &&{unknown}
2473 [283; 314) '{ ... }': &{unknown}
2474 [297; 304) 'content': &{unknown}
2475 "###
2476 );
2477}
2478
2479#[test]
2480fn infer_nested_generics_crash() {
2481 // another crash found typechecking rustc
2482 assert_snapshot!(
2483 infer(r#"
2484struct Canonical<V> {
2485 value: V,
2486}
2487struct QueryResponse<V> {
2488 value: V,
2489}
2490fn test<R>(query_response: Canonical<QueryResponse<R>>) {
2491 &query_response.value;
2492}
2493"#),
2494 @r###"
2495 [92; 106) 'query_response': Canonical<QueryResponse<R>>
2496 [137; 167) '{ ...lue; }': ()
2497 [143; 164) '&query....value': &QueryResponse<R>
2498 [144; 158) 'query_response': Canonical<QueryResponse<R>>
2499 [144; 164) 'query_....value': QueryResponse<R>
2500 "###
2501 );
2502}
2503
2504#[test]
2505fn bug_1030() {
2506 assert_snapshot!(infer(r#"
2507struct HashSet<T, H>;
2508struct FxHasher;
2509type FxHashSet<T> = HashSet<T, FxHasher>;
2510
2511impl<T, H> HashSet<T, H> {
2512 fn default() -> HashSet<T, H> {}
2513}
2514
2515pub fn main_loop() {
2516 FxHashSet::default();
2517}
2518"#),
2519 @r###"
2520 [144; 146) '{}': ()
2521 [169; 198) '{ ...t(); }': ()
2522 [175; 193) 'FxHash...efault': fn default<{unknown}, FxHasher>() -> HashSet<T, H>
2523 [175; 195) 'FxHash...ault()': HashSet<{unknown}, FxHasher>
2524 "###
2525 );
2526}
2527
2528#[test]
2529fn cross_crate_associated_method_call() {
2530 let (db, pos) = TestDB::with_position(
2531 r#"
2532//- /main.rs crate:main deps:other_crate
2533fn test() {
2534 let x = other_crate::foo::S::thing();
2535 x<|>;
2536}
2537
2538//- /lib.rs crate:other_crate
2539mod foo {
2540 struct S;
2541 impl S {
2542 fn thing() -> i128 {}
2543 }
2544}
2545"#,
2546 );
2547 assert_eq!("i128", type_at_pos(&db, pos));
2548}
2549
2550#[test]
2551fn infer_const() {
2552 assert_snapshot!(
2553 infer(r#"
2554struct Foo;
2555impl Foo { const ASSOC_CONST: u32 = 0; }
2556const GLOBAL_CONST: u32 = 101;
2557fn test() {
2558 const LOCAL_CONST: u32 = 99;
2559 let x = LOCAL_CONST;
2560 let z = GLOBAL_CONST;
2561 let id = Foo::ASSOC_CONST;
2562}
2563"#),
2564 @r###"
2565 [49; 50) '0': u32
2566 [80; 83) '101': u32
2567 [95; 213) '{ ...NST; }': ()
2568 [138; 139) 'x': {unknown}
2569 [142; 153) 'LOCAL_CONST': {unknown}
2570 [163; 164) 'z': u32
2571 [167; 179) 'GLOBAL_CONST': u32
2572 [189; 191) 'id': u32
2573 [194; 210) 'Foo::A..._CONST': u32
2574 "###
2575 );
2576}
2577
2578#[test]
2579fn infer_static() {
2580 assert_snapshot!(
2581 infer(r#"
2582static GLOBAL_STATIC: u32 = 101;
2583static mut GLOBAL_STATIC_MUT: u32 = 101;
2584fn test() {
2585 static LOCAL_STATIC: u32 = 99;
2586 static mut LOCAL_STATIC_MUT: u32 = 99;
2587 let x = LOCAL_STATIC;
2588 let y = LOCAL_STATIC_MUT;
2589 let z = GLOBAL_STATIC;
2590 let w = GLOBAL_STATIC_MUT;
2591}
2592"#),
2593 @r###"
2594 [29; 32) '101': u32
2595 [70; 73) '101': u32
2596 [85; 280) '{ ...MUT; }': ()
2597 [173; 174) 'x': {unknown}
2598 [177; 189) 'LOCAL_STATIC': {unknown}
2599 [199; 200) 'y': {unknown}
2600 [203; 219) 'LOCAL_...IC_MUT': {unknown}
2601 [229; 230) 'z': u32
2602 [233; 246) 'GLOBAL_STATIC': u32
2603 [256; 257) 'w': u32
2604 [260; 277) 'GLOBAL...IC_MUT': u32
2605 "###
2606 );
2607}
2608
2609#[test]
2610fn infer_trait_method_simple() {
2611 // the trait implementation is intentionally incomplete -- it shouldn't matter
2612 assert_snapshot!(
2613 infer(r#"
2614trait Trait1 {
2615 fn method(&self) -> u32;
2616}
2617struct S1;
2618impl Trait1 for S1 {}
2619trait Trait2 {
2620 fn method(&self) -> i128;
2621}
2622struct S2;
2623impl Trait2 for S2 {}
2624fn test() {
2625 S1.method(); // -> u32
2626 S2.method(); // -> i128
2627}
2628"#),
2629 @r###"
2630 [31; 35) 'self': &Self
2631 [110; 114) 'self': &Self
2632 [170; 228) '{ ...i128 }': ()
2633 [176; 178) 'S1': S1
2634 [176; 187) 'S1.method()': u32
2635 [203; 205) 'S2': S2
2636 [203; 214) 'S2.method()': i128
2637 "###
2638 );
2639}
2640
2641#[test]
2642fn infer_trait_method_scoped() {
2643 // the trait implementation is intentionally incomplete -- it shouldn't matter
2644 assert_snapshot!(
2645 infer(r#"
2646struct S;
2647mod foo {
2648 pub trait Trait1 {
2649 fn method(&self) -> u32;
2650 }
2651 impl Trait1 for super::S {}
2652}
2653mod bar {
2654 pub trait Trait2 {
2655 fn method(&self) -> i128;
2656 }
2657 impl Trait2 for super::S {}
2658}
2659
2660mod foo_test {
2661 use super::S;
2662 use super::foo::Trait1;
2663 fn test() {
2664 S.method(); // -> u32
2665 }
2666}
2667
2668mod bar_test {
2669 use super::S;
2670 use super::bar::Trait2;
2671 fn test() {
2672 S.method(); // -> i128
2673 }
2674}
2675"#),
2676 @r###"
2677 [63; 67) 'self': &Self
2678 [169; 173) 'self': &Self
2679 [300; 337) '{ ... }': ()
2680 [310; 311) 'S': S
2681 [310; 320) 'S.method()': u32
2682 [416; 454) '{ ... }': ()
2683 [426; 427) 'S': S
2684 [426; 436) 'S.method()': i128
2685 "###
2686 );
2687}
2688
2689#[test]
2690fn infer_trait_method_generic_1() {
2691 // the trait implementation is intentionally incomplete -- it shouldn't matter
2692 assert_snapshot!(
2693 infer(r#"
2694trait Trait<T> {
2695 fn method(&self) -> T;
2696}
2697struct S;
2698impl Trait<u32> for S {}
2699fn test() {
2700 S.method();
2701}
2702"#),
2703 @r###"
2704 [33; 37) 'self': &Self
2705 [92; 111) '{ ...d(); }': ()
2706 [98; 99) 'S': S
2707 [98; 108) 'S.method()': u32
2708 "###
2709 );
2710}
2711
2712#[test]
2713fn infer_trait_method_generic_more_params() {
2714 // the trait implementation is intentionally incomplete -- it shouldn't matter
2715 assert_snapshot!(
2716 infer(r#"
2717trait Trait<T1, T2, T3> {
2718 fn method1(&self) -> (T1, T2, T3);
2719 fn method2(&self) -> (T3, T2, T1);
2720}
2721struct S1;
2722impl Trait<u8, u16, u32> for S1 {}
2723struct S2;
2724impl<T> Trait<i8, i16, T> for S2 {}
2725fn test() {
2726 S1.method1(); // u8, u16, u32
2727 S1.method2(); // u32, u16, u8
2728 S2.method1(); // i8, i16, {unknown}
2729 S2.method2(); // {unknown}, i16, i8
2730}
2731"#),
2732 @r###"
2733 [43; 47) 'self': &Self
2734 [82; 86) 'self': &Self
2735 [210; 361) '{ ..., i8 }': ()
2736 [216; 218) 'S1': S1
2737 [216; 228) 'S1.method1()': (u8, u16, u32)
2738 [250; 252) 'S1': S1
2739 [250; 262) 'S1.method2()': (u32, u16, u8)
2740 [284; 286) 'S2': S2
2741 [284; 296) 'S2.method1()': (i8, i16, {unknown})
2742 [324; 326) 'S2': S2
2743 [324; 336) 'S2.method2()': ({unknown}, i16, i8)
2744 "###
2745 );
2746}
2747
2748#[test]
2749fn infer_trait_method_generic_2() {
2750 // the trait implementation is intentionally incomplete -- it shouldn't matter
2751 assert_snapshot!(
2752 infer(r#"
2753trait Trait<T> {
2754 fn method(&self) -> T;
2755}
2756struct S<T>(T);
2757impl<U> Trait<U> for S<U> {}
2758fn test() {
2759 S(1u32).method();
2760}
2761"#),
2762 @r###"
2763 [33; 37) 'self': &Self
2764 [102; 127) '{ ...d(); }': ()
2765 [108; 109) 'S': S<u32>(T) -> S<T>
2766 [108; 115) 'S(1u32)': S<u32>
2767 [108; 124) 'S(1u32...thod()': u32
2768 [110; 114) '1u32': u32
2769 "###
2770 );
2771}
2772
2773#[test]
2774fn infer_trait_assoc_method() {
2775 assert_snapshot!(
2776 infer(r#"
2777trait Default {
2778 fn default() -> Self;
2779}
2780struct S;
2781impl Default for S {}
2782fn test() {
2783 let s1: S = Default::default();
2784 let s2 = S::default();
2785 let s3 = <S as Default>::default();
2786}
2787"#),
2788 @r###"
2789 [87; 193) '{ ...t(); }': ()
2790 [97; 99) 's1': S
2791 [105; 121) 'Defaul...efault': fn default<S>() -> Self
2792 [105; 123) 'Defaul...ault()': S
2793 [133; 135) 's2': S
2794 [138; 148) 'S::default': fn default<S>() -> Self
2795 [138; 150) 'S::default()': S
2796 [160; 162) 's3': S
2797 [165; 188) '<S as ...efault': fn default<S>() -> Self
2798 [165; 190) '<S as ...ault()': S
2799 "###
2800 );
2801}
2802
2803#[test]
2804fn infer_trait_assoc_method_generics_1() {
2805 assert_snapshot!(
2806 infer(r#"
2807trait Trait<T> {
2808 fn make() -> T;
2809}
2810struct S;
2811impl Trait<u32> for S {}
2812struct G<T>;
2813impl<T> Trait<T> for G<T> {}
2814fn test() {
2815 let a = S::make();
2816 let b = G::<u64>::make();
2817 let c: f64 = G::make();
2818}
2819"#),
2820 @r###"
2821 [127; 211) '{ ...e(); }': ()
2822 [137; 138) 'a': u32
2823 [141; 148) 'S::make': fn make<S, u32>() -> T
2824 [141; 150) 'S::make()': u32
2825 [160; 161) 'b': u64
2826 [164; 178) 'G::<u64>::make': fn make<G<u64>, u64>() -> T
2827 [164; 180) 'G::<u6...make()': u64
2828 [190; 191) 'c': f64
2829 [199; 206) 'G::make': fn make<G<f64>, f64>() -> T
2830 [199; 208) 'G::make()': f64
2831 "###
2832 );
2833}
2834
2835#[test]
2836fn infer_trait_assoc_method_generics_2() {
2837 assert_snapshot!(
2838 infer(r#"
2839trait Trait<T> {
2840 fn make<U>() -> (T, U);
2841}
2842struct S;
2843impl Trait<u32> for S {}
2844struct G<T>;
2845impl<T> Trait<T> for G<T> {}
2846fn test() {
2847 let a = S::make::<i64>();
2848 let b: (_, i64) = S::make();
2849 let c = G::<u32>::make::<i64>();
2850 let d: (u32, _) = G::make::<i64>();
2851 let e: (u32, i64) = G::make();
2852}
2853"#),
2854 @r###"
2855 [135; 313) '{ ...e(); }': ()
2856 [145; 146) 'a': (u32, i64)
2857 [149; 163) 'S::make::<i64>': fn make<S, u32, i64>() -> (T, U)
2858 [149; 165) 'S::mak...i64>()': (u32, i64)
2859 [175; 176) 'b': (u32, i64)
2860 [189; 196) 'S::make': fn make<S, u32, i64>() -> (T, U)
2861 [189; 198) 'S::make()': (u32, i64)
2862 [208; 209) 'c': (u32, i64)
2863 [212; 233) 'G::<u3...:<i64>': fn make<G<u32>, u32, i64>() -> (T, U)
2864 [212; 235) 'G::<u3...i64>()': (u32, i64)
2865 [245; 246) 'd': (u32, i64)
2866 [259; 273) 'G::make::<i64>': fn make<G<u32>, u32, i64>() -> (T, U)
2867 [259; 275) 'G::mak...i64>()': (u32, i64)
2868 [285; 286) 'e': (u32, i64)
2869 [301; 308) 'G::make': fn make<G<u32>, u32, i64>() -> (T, U)
2870 [301; 310) 'G::make()': (u32, i64)
2871 "###
2872 );
2873}
2874
2875#[test]
2876fn infer_trait_assoc_method_generics_3() {
2877 assert_snapshot!(
2878 infer(r#"
2879trait Trait<T> {
2880 fn make() -> (Self, T);
2881}
2882struct S<T>;
2883impl Trait<i64> for S<i32> {}
2884fn test() {
2885 let a = S::make();
2886}
2887"#),
2888 @r###"
2889 [101; 127) '{ ...e(); }': ()
2890 [111; 112) 'a': (S<i32>, i64)
2891 [115; 122) 'S::make': fn make<S<i32>, i64>() -> (Self, T)
2892 [115; 124) 'S::make()': (S<i32>, i64)
2893 "###
2894 );
2895}
2896
2897#[test]
2898fn infer_trait_assoc_method_generics_4() {
2899 assert_snapshot!(
2900 infer(r#"
2901trait Trait<T> {
2902 fn make() -> (Self, T);
2903}
2904struct S<T>;
2905impl Trait<i64> for S<u64> {}
2906impl Trait<i32> for S<u32> {}
2907fn test() {
2908 let a: (S<u64>, _) = S::make();
2909 let b: (_, i32) = S::make();
2910}
2911"#),
2912 @r###"
2913 [131; 203) '{ ...e(); }': ()
2914 [141; 142) 'a': (S<u64>, i64)
2915 [158; 165) 'S::make': fn make<S<u64>, i64>() -> (Self, T)
2916 [158; 167) 'S::make()': (S<u64>, i64)
2917 [177; 178) 'b': (S<u32>, i32)
2918 [191; 198) 'S::make': fn make<S<u32>, i32>() -> (Self, T)
2919 [191; 200) 'S::make()': (S<u32>, i32)
2920 "###
2921 );
2922}
2923
2924#[test]
2925fn infer_trait_assoc_method_generics_5() {
2926 assert_snapshot!(
2927 infer(r#"
2928trait Trait<T> {
2929 fn make<U>() -> (Self, T, U);
2930}
2931struct S<T>;
2932impl Trait<i64> for S<u64> {}
2933fn test() {
2934 let a = <S as Trait<i64>>::make::<u8>();
2935 let b: (S<u64>, _, _) = Trait::<i64>::make::<u8>();
2936}
2937"#),
2938 @r###"
2939 [107; 211) '{ ...>(); }': ()
2940 [117; 118) 'a': (S<u64>, i64, u8)
2941 [121; 150) '<S as ...::<u8>': fn make<S<u64>, i64, u8>() -> (Self, T, U)
2942 [121; 152) '<S as ...<u8>()': (S<u64>, i64, u8)
2943 [162; 163) 'b': (S<u64>, i64, u8)
2944 [182; 206) 'Trait:...::<u8>': fn make<S<u64>, i64, u8>() -> (Self, T, U)
2945 [182; 208) 'Trait:...<u8>()': (S<u64>, i64, u8)
2946 "###
2947 );
2948}
2949
2950#[test]
2951fn infer_from_bound_1() {
2952 assert_snapshot!(
2953 infer(r#"
2954trait Trait<T> {}
2955struct S<T>(T);
2956impl<U> Trait<U> for S<U> {}
2957fn foo<T: Trait<u32>>(t: T) {}
2958fn test() {
2959 let s = S(unknown);
2960 foo(s);
2961}
2962"#),
2963 @r###"
2964 [86; 87) 't': T
2965 [92; 94) '{}': ()
2966 [105; 144) '{ ...(s); }': ()
2967 [115; 116) 's': S<u32>
2968 [119; 120) 'S': S<u32>(T) -> S<T>
2969 [119; 129) 'S(unknown)': S<u32>
2970 [121; 128) 'unknown': u32
2971 [135; 138) 'foo': fn foo<S<u32>>(T) -> ()
2972 [135; 141) 'foo(s)': ()
2973 [139; 140) 's': S<u32>
2974 "###
2975 );
2976}
2977
2978#[test]
2979fn infer_from_bound_2() {
2980 assert_snapshot!(
2981 infer(r#"
2982trait Trait<T> {}
2983struct S<T>(T);
2984impl<U> Trait<U> for S<U> {}
2985fn foo<U, T: Trait<U>>(t: T) -> U {}
2986fn test() {
2987 let s = S(unknown);
2988 let x: u32 = foo(s);
2989}
2990"#),
2991 @r###"
2992 [87; 88) 't': T
2993 [98; 100) '{}': ()
2994 [111; 163) '{ ...(s); }': ()
2995 [121; 122) 's': S<u32>
2996 [125; 126) 'S': S<u32>(T) -> S<T>
2997 [125; 135) 'S(unknown)': S<u32>
2998 [127; 134) 'unknown': u32
2999 [145; 146) 'x': u32
3000 [154; 157) 'foo': fn foo<u32, S<u32>>(T) -> U
3001 [154; 160) 'foo(s)': u32
3002 [158; 159) 's': S<u32>
3003 "###
3004 );
3005}
3006
3007#[test]
3008fn infer_call_trait_method_on_generic_param_1() {
3009 assert_snapshot!(
3010 infer(r#"
3011trait Trait {
3012 fn method(&self) -> u32;
3013}
3014fn test<T: Trait>(t: T) {
3015 t.method();
3016}
3017"#),
3018 @r###"
3019 [30; 34) 'self': &Self
3020 [64; 65) 't': T
3021 [70; 89) '{ ...d(); }': ()
3022 [76; 77) 't': T
3023 [76; 86) 't.method()': u32
3024 "###
3025 );
3026}
3027
3028#[test]
3029fn infer_call_trait_method_on_generic_param_2() {
3030 assert_snapshot!(
3031 infer(r#"
3032trait Trait<T> {
3033 fn method(&self) -> T;
3034}
3035fn test<U, T: Trait<U>>(t: T) {
3036 t.method();
3037}
3038"#),
3039 @r###"
3040 [33; 37) 'self': &Self
3041 [71; 72) 't': T
3042 [77; 96) '{ ...d(); }': ()
3043 [83; 84) 't': T
3044 [83; 93) 't.method()': [missing name]
3045 "###
3046 );
3047}
3048
3049#[test]
3050fn infer_with_multiple_trait_impls() {
3051 assert_snapshot!(
3052 infer(r#"
3053trait Into<T> {
3054 fn into(self) -> T;
3055}
3056struct S;
3057impl Into<u32> for S {}
3058impl Into<u64> for S {}
3059fn test() {
3060 let x: u32 = S.into();
3061 let y: u64 = S.into();
3062 let z = Into::<u64>::into(S);
3063}
3064"#),
3065 @r###"
3066 [29; 33) 'self': Self
3067 [111; 202) '{ ...(S); }': ()
3068 [121; 122) 'x': u32
3069 [130; 131) 'S': S
3070 [130; 138) 'S.into()': u32
3071 [148; 149) 'y': u64
3072 [157; 158) 'S': S
3073 [157; 165) 'S.into()': u64
3074 [175; 176) 'z': u64
3075 [179; 196) 'Into::...::into': fn into<S, u64>(Self) -> T
3076 [179; 199) 'Into::...nto(S)': u64
3077 [197; 198) 'S': S
3078 "###
3079 );
3080}
3081
3082#[test]
3083fn infer_project_associated_type() {
3084 // y, z, a don't yet work because of https://github.com/rust-lang/chalk/issues/234
3085 assert_snapshot!(
3086 infer(r#"
3087trait Iterable {
3088 type Item;
3089}
3090struct S;
3091impl Iterable for S { type Item = u32; }
3092fn test<T: Iterable>() {
3093 let x: <S as Iterable>::Item = 1;
3094 let y: <T as Iterable>::Item = no_matter;
3095 let z: T::Item = no_matter;
3096 let a: <T>::Item = no_matter;
3097}
3098"#),
3099 @r###"
3100 [108; 261) '{ ...ter; }': ()
3101 [118; 119) 'x': u32
3102 [145; 146) '1': u32
3103 [156; 157) 'y': {unknown}
3104 [183; 192) 'no_matter': {unknown}
3105 [202; 203) 'z': {unknown}
3106 [215; 224) 'no_matter': {unknown}
3107 [234; 235) 'a': {unknown}
3108 [249; 258) 'no_matter': {unknown}
3109 "###
3110 );
3111}
3112
3113#[test]
3114fn infer_return_associated_type() {
3115 assert_snapshot!(
3116 infer(r#"
3117trait Iterable {
3118 type Item;
3119}
3120struct S;
3121impl Iterable for S { type Item = u32; }
3122fn foo1<T: Iterable>(t: T) -> T::Item {}
3123fn foo2<T: Iterable>(t: T) -> <T as Iterable>::Item {}
3124fn foo3<T: Iterable>(t: T) -> <T>::Item {}
3125fn test() {
3126 let x = foo1(S);
3127 let y = foo2(S);
3128 let z = foo3(S);
3129}
3130"#),
3131 @r###"
3132 [106; 107) 't': T
3133 [123; 125) '{}': ()
3134 [147; 148) 't': T
3135 [178; 180) '{}': ()
3136 [202; 203) 't': T
3137 [221; 223) '{}': ()
3138 [234; 300) '{ ...(S); }': ()
3139 [244; 245) 'x': u32
3140 [248; 252) 'foo1': fn foo1<S>(T) -> <T as Iterable>::Item
3141 [248; 255) 'foo1(S)': u32
3142 [253; 254) 'S': S
3143 [265; 266) 'y': u32
3144 [269; 273) 'foo2': fn foo2<S>(T) -> <T as Iterable>::Item
3145 [269; 276) 'foo2(S)': u32
3146 [274; 275) 'S': S
3147 [286; 287) 'z': u32
3148 [290; 294) 'foo3': fn foo3<S>(T) -> <T as Iterable>::Item
3149 [290; 297) 'foo3(S)': u32
3150 [295; 296) 'S': S
3151 "###
3152 );
3153}
3154
3155#[test]
3156fn infer_associated_type_bound() {
3157 assert_snapshot!(
3158 infer(r#"
3159trait Iterable {
3160 type Item;
3161}
3162fn test<T: Iterable<Item=u32>>() {
3163 let y: T::Item = unknown;
3164}
3165"#),
3166 @r###"
3167 [67; 100) '{ ...own; }': ()
3168 [77; 78) 'y': {unknown}
3169 [90; 97) 'unknown': {unknown}
3170 "###
3171 );
3172}
3173
3174#[test]
3175fn infer_const_body() {
3176 assert_snapshot!(
3177 infer(r#"
3178const A: u32 = 1 + 1;
3179static B: u64 = { let x = 1; x };
3180"#),
3181 @r###"
3182 [16; 17) '1': u32
3183 [16; 21) '1 + 1': u32
3184 [20; 21) '1': u32
3185 [39; 55) '{ let ...1; x }': u64
3186 [45; 46) 'x': u64
3187 [49; 50) '1': u64
3188 [52; 53) 'x': u64
3189 "###
3190 );
3191}
3192
3193#[test]
3194fn tuple_struct_fields() {
3195 assert_snapshot!(
3196 infer(r#"
3197struct S(i32, u64);
3198fn test() -> u64 {
3199 let a = S(4, 6);
3200 let b = a.0;
3201 a.1
3202}
3203"#),
3204 @r###"
3205 [38; 87) '{ ... a.1 }': u64
3206 [48; 49) 'a': S
3207 [52; 53) 'S': S(i32, u64) -> S
3208 [52; 59) 'S(4, 6)': S
3209 [54; 55) '4': i32
3210 [57; 58) '6': u64
3211 [69; 70) 'b': i32
3212 [73; 74) 'a': S
3213 [73; 76) 'a.0': i32
3214 [82; 83) 'a': S
3215 [82; 85) 'a.1': u64
3216 "###
3217 );
3218}
3219
3220#[test]
3221fn tuple_struct_with_fn() {
3222 assert_snapshot!(
3223 infer(r#"
3224struct S(fn(u32) -> u64);
3225fn test() -> u64 {
3226 let a = S(|i| 2*i);
3227 let b = a.0(4);
3228 a.0(2)
3229}
3230"#),
3231 @r###"
3232 [44; 102) '{ ...0(2) }': u64
3233 [54; 55) 'a': S
3234 [58; 59) 'S': S(fn(u32) -> u64) -> S
3235 [58; 68) 'S(|i| 2*i)': S
3236 [60; 67) '|i| 2*i': |i32| -> i32
3237 [61; 62) 'i': i32
3238 [64; 65) '2': i32
3239 [64; 67) '2*i': i32
3240 [66; 67) 'i': i32
3241 [78; 79) 'b': u64
3242 [82; 83) 'a': S
3243 [82; 85) 'a.0': fn(u32) -> u64
3244 [82; 88) 'a.0(4)': u64
3245 [86; 87) '4': u32
3246 [94; 95) 'a': S
3247 [94; 97) 'a.0': fn(u32) -> u64
3248 [94; 100) 'a.0(2)': u64
3249 [98; 99) '2': u32
3250 "###
3251 );
3252}
3253
3254#[test]
3255fn indexing_arrays() {
3256 assert_snapshot!(
3257 infer("fn main() { &mut [9][2]; }"),
3258 @r###"
3259 [10; 26) '{ &mut...[2]; }': ()
3260 [12; 23) '&mut [9][2]': &mut {unknown}
3261 [17; 20) '[9]': [i32;_]
3262 [17; 23) '[9][2]': {unknown}
3263 [18; 19) '9': i32
3264 [21; 22) '2': i32
3265 "###
3266 )
3267}
3268
3269#[test]
3270fn infer_macros_expanded() {
3271 assert_snapshot!(
3272 infer(r#"
3273struct Foo(Vec<i32>);
3274
3275macro_rules! foo {
3276 ($($item:expr),*) => {
3277 {
3278 Foo(vec![$($item,)*])
3279 }
3280 };
3281}
3282
3283fn main() {
3284 let x = foo!(1,2);
3285}
3286"#),
3287 @r###"
3288 ![0; 17) '{Foo(v...,2,])}': Foo
3289 ![1; 4) 'Foo': Foo({unknown}) -> Foo
3290 ![1; 16) 'Foo(vec![1,2,])': Foo
3291 ![5; 15) 'vec![1,2,]': {unknown}
3292 [156; 182) '{ ...,2); }': ()
3293 [166; 167) 'x': Foo
3294 "###
3295 );
3296}
3297
3298#[test]
3299fn infer_legacy_textual_scoped_macros_expanded() {
3300 assert_snapshot!(
3301 infer(r#"
3302struct Foo(Vec<i32>);
3303
3304#[macro_use]
3305mod m {
3306 macro_rules! foo {
3307 ($($item:expr),*) => {
3308 {
3309 Foo(vec![$($item,)*])
3310 }
3311 };
3312 }
3313}
3314
3315fn main() {
3316 let x = foo!(1,2);
3317 let y = crate::foo!(1,2);
3318}
3319"#),
3320 @r###"
3321 ![0; 17) '{Foo(v...,2,])}': Foo
3322 ![1; 4) 'Foo': Foo({unknown}) -> Foo
3323 ![1; 16) 'Foo(vec![1,2,])': Foo
3324 ![5; 15) 'vec![1,2,]': {unknown}
3325 [195; 251) '{ ...,2); }': ()
3326 [205; 206) 'x': Foo
3327 [228; 229) 'y': {unknown}
3328 [232; 248) 'crate:...!(1,2)': {unknown}
3329 "###
3330 );
3331}
3332
3333#[test]
3334fn infer_path_qualified_macros_expanded() {
3335 assert_snapshot!(
3336 infer(r#"
3337#[macro_export]
3338macro_rules! foo {
3339 () => { 42i32 }
3340}
3341
3342mod m {
3343 pub use super::foo as bar;
3344}
3345
3346fn main() {
3347 let x = crate::foo!();
3348 let y = m::bar!();
3349}
3350"#),
3351 @r###"
3352 ![0; 5) '42i32': i32
3353 ![0; 5) '42i32': i32
3354 [111; 164) '{ ...!(); }': ()
3355 [121; 122) 'x': i32
3356 [148; 149) 'y': i32
3357 "###
3358 );
3359}
3360
3361#[test]
3362fn infer_type_value_macro_having_same_name() {
3363 assert_snapshot!(
3364 infer(r#"
3365#[macro_export]
3366macro_rules! foo {
3367 () => {
3368 mod foo {
3369 pub use super::foo;
3370 }
3371 };
3372 ($x:tt) => {
3373 $x
3374 };
3375}
3376
3377foo!();
3378
3379fn foo() {
3380 let foo = foo::foo!(42i32);
3381}
3382"#),
3383 @r###"
3384 ![0; 5) '42i32': i32
3385 [171; 206) '{ ...32); }': ()
3386 [181; 184) 'foo': i32
3387 "###
3388 );
3389}
3390
3391#[test]
3392fn processes_impls_generated_by_macros() {
3393 let t = type_at(
3394 r#"
3395//- /main.rs
3396macro_rules! m {
3397 ($ident:ident) => (impl Trait for $ident {})
3398}
3399trait Trait { fn foo(self) -> u128 {} }
3400struct S;
3401m!(S);
3402fn test() { S.foo()<|>; }
3403"#,
3404 );
3405 assert_eq!(t, "u128");
3406}
3407
3408#[test]
3409fn infer_macro_with_dollar_crate_is_correct_in_expr() {
3410 let (db, pos) = TestDB::with_position(
3411 r#"
3412//- /main.rs crate:main deps:foo
3413fn test() {
3414 let x = (foo::foo!(1), foo::foo!(2));
3415 x<|>;
3416}
3417
3418//- /lib.rs crate:foo
3419#[macro_export]
3420macro_rules! foo {
3421 (1) => { $crate::bar!() };
3422 (2) => { 1 + $crate::baz() };
3423}
3424
3425#[macro_export]
3426macro_rules! bar {
3427 () => { 42 }
3428}
3429
3430pub fn baz() -> usize { 31usize }
3431"#,
3432 );
3433 assert_eq!("(i32, usize)", type_at_pos(&db, pos));
3434}
3435
3436#[test]
3437fn method_resolution_unify_impl_self_type() {
3438 let t = type_at(
3439 r#"
3440//- /main.rs
3441struct S<T>;
3442impl S<u32> { fn foo(&self) -> u8 {} }
3443impl S<i32> { fn foo(&self) -> i8 {} }
3444fn test() { (S::<u32>.foo(), S::<i32>.foo())<|>; }
3445"#,
3446 );
3447 assert_eq!(t, "(u8, i8)");
3448}
3449
3450#[test]
3451fn method_resolution_trait_before_autoref() {
3452 let t = type_at(
3453 r#"
3454//- /main.rs
3455trait Trait { fn foo(self) -> u128; }
3456struct S;
3457impl S { fn foo(&self) -> i8 { 0 } }
3458impl Trait for S { fn foo(self) -> u128 { 0 } }
3459fn test() { S.foo()<|>; }
3460"#,
3461 );
3462 assert_eq!(t, "u128");
3463}
3464
3465#[test]
3466fn method_resolution_by_value_before_autoref() {
3467 let t = type_at(
3468 r#"
3469//- /main.rs
3470trait Clone { fn clone(&self) -> Self; }
3471struct S;
3472impl Clone for S {}
3473impl Clone for &S {}
3474fn test() { (S.clone(), (&S).clone(), (&&S).clone())<|>; }
3475"#,
3476 );
3477 assert_eq!(t, "(S, S, &S)");
3478}
3479
3480#[test]
3481fn method_resolution_trait_before_autoderef() {
3482 let t = type_at(
3483 r#"
3484//- /main.rs
3485trait Trait { fn foo(self) -> u128; }
3486struct S;
3487impl S { fn foo(self) -> i8 { 0 } }
3488impl Trait for &S { fn foo(self) -> u128 { 0 } }
3489fn test() { (&S).foo()<|>; }
3490"#,
3491 );
3492 assert_eq!(t, "u128");
3493}
3494
3495#[test]
3496fn method_resolution_impl_before_trait() {
3497 let t = type_at(
3498 r#"
3499//- /main.rs
3500trait Trait { fn foo(self) -> u128; }
3501struct S;
3502impl S { fn foo(self) -> i8 { 0 } }
3503impl Trait for S { fn foo(self) -> u128 { 0 } }
3504fn test() { S.foo()<|>; }
3505"#,
3506 );
3507 assert_eq!(t, "i8");
3508}
3509
3510#[test]
3511fn method_resolution_impl_ref_before_trait() {
3512 let t = type_at(
3513 r#"
3514//- /main.rs
3515trait Trait { fn foo(self) -> u128; }
3516struct S;
3517impl S { fn foo(&self) -> i8 { 0 } }
3518impl Trait for &S { fn foo(self) -> u128 { 0 } }
3519fn test() { S.foo()<|>; }
3520"#,
3521 );
3522 assert_eq!(t, "i8");
3523}
3524
3525#[test]
3526fn method_resolution_trait_autoderef() {
3527 let t = type_at(
3528 r#"
3529//- /main.rs
3530trait Trait { fn foo(self) -> u128; }
3531struct S;
3532impl Trait for S { fn foo(self) -> u128 { 0 } }
3533fn test() { (&S).foo()<|>; }
3534"#,
3535 );
3536 assert_eq!(t, "u128");
3537}
3538
3539#[test]
3540fn method_resolution_trait_from_prelude() {
3541 let (db, pos) = TestDB::with_position(
3542 r#"
3543//- /main.rs crate:main deps:other_crate
3544struct S;
3545impl Clone for S {}
3546
3547fn test() {
3548 S.clone()<|>;
3549}
3550
3551//- /lib.rs crate:other_crate
3552#[prelude_import] use foo::*;
3553
3554mod foo {
3555 trait Clone {
3556 fn clone(&self) -> Self;
3557 }
3558}
3559"#,
3560 );
3561 assert_eq!("S", type_at_pos(&db, pos));
3562}
3563
3564#[test]
3565fn method_resolution_where_clause_for_unknown_trait() {
3566 // The blanket impl shouldn't apply because we can't even resolve UnknownTrait
3567 let t = type_at(
3568 r#"
3569//- /main.rs
3570trait Trait { fn foo(self) -> u128; }
3571struct S;
3572impl<T> Trait for T where T: UnknownTrait {}
3573fn test() { (&S).foo()<|>; }
3574"#,
3575 );
3576 assert_eq!(t, "{unknown}");
3577}
3578
3579#[test]
3580fn method_resolution_where_clause_not_met() {
3581 // The blanket impl shouldn't apply because we can't prove S: Clone
3582 let t = type_at(
3583 r#"
3584//- /main.rs
3585trait Clone {}
3586trait Trait { fn foo(self) -> u128; }
3587struct S;
3588impl<T> Trait for T where T: Clone {}
3589fn test() { (&S).foo()<|>; }
3590"#,
3591 );
3592 // This is also to make sure that we don't resolve to the foo method just
3593 // because that's the only method named foo we can find, which would make
3594 // the below tests not work
3595 assert_eq!(t, "{unknown}");
3596}
3597
3598#[test]
3599fn method_resolution_where_clause_inline_not_met() {
3600 // The blanket impl shouldn't apply because we can't prove S: Clone
3601 let t = type_at(
3602 r#"
3603//- /main.rs
3604trait Clone {}
3605trait Trait { fn foo(self) -> u128; }
3606struct S;
3607impl<T: Clone> Trait for T {}
3608fn test() { (&S).foo()<|>; }
3609"#,
3610 );
3611 assert_eq!(t, "{unknown}");
3612}
3613
3614#[test]
3615fn method_resolution_where_clause_1() {
3616 let t = type_at(
3617 r#"
3618//- /main.rs
3619trait Clone {}
3620trait Trait { fn foo(self) -> u128; }
3621struct S;
3622impl Clone for S {}
3623impl<T> Trait for T where T: Clone {}
3624fn test() { S.foo()<|>; }
3625"#,
3626 );
3627 assert_eq!(t, "u128");
3628}
3629
3630#[test]
3631fn method_resolution_where_clause_2() {
3632 let t = type_at(
3633 r#"
3634//- /main.rs
3635trait Into<T> { fn into(self) -> T; }
3636trait From<T> { fn from(other: T) -> Self; }
3637struct S1;
3638struct S2;
3639impl From<S2> for S1 {}
3640impl<T, U> Into<U> for T where U: From<T> {}
3641fn test() { S2.into()<|>; }
3642"#,
3643 );
3644 assert_eq!(t, "{unknown}");
3645}
3646
3647#[test]
3648fn method_resolution_where_clause_inline() {
3649 let t = type_at(
3650 r#"
3651//- /main.rs
3652trait Into<T> { fn into(self) -> T; }
3653trait From<T> { fn from(other: T) -> Self; }
3654struct S1;
3655struct S2;
3656impl From<S2> for S1 {}
3657impl<T, U: From<T>> Into<U> for T {}
3658fn test() { S2.into()<|>; }
3659"#,
3660 );
3661 assert_eq!(t, "{unknown}");
3662}
3663
3664#[test]
3665fn method_resolution_encountering_fn_type() {
3666 type_at(
3667 r#"
3668//- /main.rs
3669fn foo() {}
3670trait FnOnce { fn call(self); }
3671fn test() { foo.call()<|>; }
3672"#,
3673 );
3674}
3675
3676#[test]
3677fn method_resolution_slow() {
3678 // this can get quite slow if we set the solver size limit too high
3679 let t = type_at(
3680 r#"
3681//- /main.rs
3682trait SendX {}
3683
3684struct S1; impl SendX for S1 {}
3685struct S2; impl SendX for S2 {}
3686struct U1;
3687
3688trait Trait { fn method(self); }
3689
3690struct X1<A, B> {}
3691impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
3692
3693struct S<B, C> {}
3694
3695trait FnX {}
3696
3697impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
3698
3699fn test() { (S {}).method()<|>; }
3700"#,
3701 );
3702 assert_eq!(t, "()");
3703}
3704
3705#[test]
3706fn shadowing_primitive() {
3707 let t = type_at(
3708 r#"
3709//- /main.rs
3710struct i32;
3711struct Foo;
3712
3713impl i32 { fn foo(&self) -> Foo { Foo } }
3714
3715fn main() {
3716 let x: i32 = i32;
3717 x.foo()<|>;
3718}"#,
3719 );
3720 assert_eq!(t, "Foo");
3721}
3722
3723#[test]
3724fn not_shadowing_primitive_by_module() {
3725 let t = type_at(
3726 r#"
3727//- /str.rs
3728fn foo() {}
3729
3730//- /main.rs
3731mod str;
3732fn foo() -> &'static str { "" }
3733
3734fn main() {
3735 foo()<|>;
3736}"#,
3737 );
3738 assert_eq!(t, "&str");
3739}
3740
3741#[test]
3742fn not_shadowing_module_by_primitive() {
3743 let t = type_at(
3744 r#"
3745//- /str.rs
3746fn foo() -> u32 {0}
3747
3748//- /main.rs
3749mod str;
3750fn foo() -> &'static str { "" }
3751
3752fn main() {
3753 str::foo()<|>;
3754}"#,
3755 );
3756 assert_eq!(t, "u32");
3757}
3758
3759#[test]
3760fn deref_trait() {
3761 let t = type_at(
3762 r#"
3763//- /main.rs
3764#[lang = "deref"]
3765trait Deref {
3766 type Target;
3767 fn deref(&self) -> &Self::Target;
3768}
3769
3770struct Arc<T>;
3771impl<T> Deref for Arc<T> {
3772 type Target = T;
3773}
3774
3775struct S;
3776impl S {
3777 fn foo(&self) -> u128 {}
3778}
3779
3780fn test(s: Arc<S>) {
3781 (*s, s.foo())<|>;
3782}
3783"#,
3784 );
3785 assert_eq!(t, "(S, u128)");
3786}
3787
3788#[test]
3789fn deref_trait_with_inference_var() {
3790 let t = type_at(
3791 r#"
3792//- /main.rs
3793#[lang = "deref"]
3794trait Deref {
3795 type Target;
3796 fn deref(&self) -> &Self::Target;
3797}
3798
3799struct Arc<T>;
3800fn new_arc<T>() -> Arc<T> {}
3801impl<T> Deref for Arc<T> {
3802 type Target = T;
3803}
3804
3805struct S;
3806fn foo(a: Arc<S>) {}
3807
3808fn test() {
3809 let a = new_arc();
3810 let b = (*a)<|>;
3811 foo(a);
3812}
3813"#,
3814 );
3815 assert_eq!(t, "S");
3816}
3817
3818#[test]
3819fn deref_trait_infinite_recursion() {
3820 let t = type_at(
3821 r#"
3822//- /main.rs
3823#[lang = "deref"]
3824trait Deref {
3825 type Target;
3826 fn deref(&self) -> &Self::Target;
3827}
3828
3829struct S;
3830
3831impl Deref for S {
3832 type Target = S;
3833}
3834
3835fn test(s: S) {
3836 s.foo()<|>;
3837}
3838"#,
3839 );
3840 assert_eq!(t, "{unknown}");
3841}
3842
3843#[test]
3844fn deref_trait_with_question_mark_size() {
3845 let t = type_at(
3846 r#"
3847//- /main.rs
3848#[lang = "deref"]
3849trait Deref {
3850 type Target;
3851 fn deref(&self) -> &Self::Target;
3852}
3853
3854struct Arc<T>;
3855impl<T> Deref for Arc<T> {
3856 type Target = T;
3857}
3858
3859struct S;
3860impl S {
3861 fn foo(&self) -> u128 {}
3862}
3863
3864fn test(s: Arc<S>) {
3865 (*s, s.foo())<|>;
3866}
3867"#,
3868 );
3869 assert_eq!(t, "(S, u128)");
3870}
3871
3872#[test]
3873fn obligation_from_function_clause() {
3874 let t = type_at(
3875 r#"
3876//- /main.rs
3877struct S;
3878
3879trait Trait<T> {}
3880impl Trait<u32> for S {}
3881
3882fn foo<T: Trait<U>, U>(t: T) -> U {}
3883
3884fn test(s: S) {
3885 foo(s)<|>;
3886}
3887"#,
3888 );
3889 assert_eq!(t, "u32");
3890}
3891
3892#[test]
3893fn obligation_from_method_clause() {
3894 let t = type_at(
3895 r#"
3896//- /main.rs
3897struct S;
3898
3899trait Trait<T> {}
3900impl Trait<isize> for S {}
3901
3902struct O;
3903impl O {
3904 fn foo<T: Trait<U>, U>(&self, t: T) -> U {}
3905}
3906
3907fn test() {
3908 O.foo(S)<|>;
3909}
3910"#,
3911 );
3912 assert_eq!(t, "isize");
3913}
3914
3915#[test]
3916fn obligation_from_self_method_clause() {
3917 let t = type_at(
3918 r#"
3919//- /main.rs
3920struct S;
3921
3922trait Trait<T> {}
3923impl Trait<i64> for S {}
3924
3925impl S {
3926 fn foo<U>(&self) -> U where Self: Trait<U> {}
3927}
3928
3929fn test() {
3930 S.foo()<|>;
3931}
3932"#,
3933 );
3934 assert_eq!(t, "i64");
3935}
3936
3937#[test]
3938fn obligation_from_impl_clause() {
3939 let t = type_at(
3940 r#"
3941//- /main.rs
3942struct S;
3943
3944trait Trait<T> {}
3945impl Trait<&str> for S {}
3946
3947struct O<T>;
3948impl<U, T: Trait<U>> O<T> {
3949 fn foo(&self) -> U {}
3950}
3951
3952fn test(o: O<S>) {
3953 o.foo()<|>;
3954}
3955"#,
3956 );
3957 assert_eq!(t, "&str");
3958}
3959
3960#[test]
3961fn generic_param_env_1() {
3962 let t = type_at(
3963 r#"
3964//- /main.rs
3965trait Clone {}
3966trait Trait { fn foo(self) -> u128; }
3967struct S;
3968impl Clone for S {}
3969impl<T> Trait for T where T: Clone {}
3970fn test<T: Clone>(t: T) { t.foo()<|>; }
3971"#,
3972 );
3973 assert_eq!(t, "u128");
3974}
3975
3976#[test]
3977fn generic_param_env_1_not_met() {
3978 let t = type_at(
3979 r#"
3980//- /main.rs
3981trait Clone {}
3982trait Trait { fn foo(self) -> u128; }
3983struct S;
3984impl Clone for S {}
3985impl<T> Trait for T where T: Clone {}
3986fn test<T>(t: T) { t.foo()<|>; }
3987"#,
3988 );
3989 assert_eq!(t, "{unknown}");
3990}
3991
3992#[test]
3993fn generic_param_env_2() {
3994 let t = type_at(
3995 r#"
3996//- /main.rs
3997trait Trait { fn foo(self) -> u128; }
3998struct S;
3999impl Trait for S {}
4000fn test<T: Trait>(t: T) { t.foo()<|>; }
4001"#,
4002 );
4003 assert_eq!(t, "u128");
4004}
4005
4006#[test]
4007fn generic_param_env_2_not_met() {
4008 let t = type_at(
4009 r#"
4010//- /main.rs
4011trait Trait { fn foo(self) -> u128; }
4012struct S;
4013impl Trait for S {}
4014fn test<T>(t: T) { t.foo()<|>; }
4015"#,
4016 );
4017 assert_eq!(t, "{unknown}");
4018}
4019
4020#[test]
4021fn generic_param_env_deref() {
4022 let t = type_at(
4023 r#"
4024//- /main.rs
4025#[lang = "deref"]
4026trait Deref {
4027 type Target;
4028}
4029trait Trait {}
4030impl<T> Deref for T where T: Trait {
4031 type Target = i128;
4032}
4033fn test<T: Trait>(t: T) { (*t)<|>; }
4034"#,
4035 );
4036 assert_eq!(t, "i128");
4037}
4038
4039#[test]
4040fn associated_type_placeholder() {
4041 let t = type_at(
4042 r#"
4043//- /main.rs
4044pub trait ApplyL {
4045 type Out;
4046}
4047
4048pub struct RefMutL<T>;
4049
4050impl<T> ApplyL for RefMutL<T> {
4051 type Out = <T as ApplyL>::Out;
4052}
4053
4054fn test<T: ApplyL>() {
4055 let y: <RefMutL<T> as ApplyL>::Out = no_matter;
4056 y<|>;
4057}
4058"#,
4059 );
4060 // inside the generic function, the associated type gets normalized to a placeholder `ApplL::Out<T>` [https://rust-lang.github.io/rustc-guide/traits/associated-types.html#placeholder-associated-types].
4061 // FIXME: fix type parameter names going missing when going through Chalk
4062 assert_eq!(t, "ApplyL::Out<[missing name]>");
4063}
4064
4065#[test]
4066fn associated_type_placeholder_2() {
4067 let t = type_at(
4068 r#"
4069//- /main.rs
4070pub trait ApplyL {
4071 type Out;
4072}
4073fn foo<T: ApplyL>(t: T) -> <T as ApplyL>::Out;
4074
4075fn test<T: ApplyL>(t: T) {
4076 let y = foo(t);
4077 y<|>;
4078}
4079"#,
4080 );
4081 // FIXME here Chalk doesn't normalize the type to a placeholder. I think we
4082 // need to add a rule like Normalize(<T as ApplyL>::Out -> ApplyL::Out<T>)
4083 // to the trait env ourselves here; probably Chalk can't do this by itself.
4084 // assert_eq!(t, "ApplyL::Out<[missing name]>");
4085 assert_eq!(t, "{unknown}");
4086}
4087
4088#[test]
4089fn impl_trait() {
4090 assert_snapshot!(
4091 infer(r#"
4092trait Trait<T> {
4093 fn foo(&self) -> T;
4094 fn foo2(&self) -> i64;
4095}
4096fn bar() -> impl Trait<u64> {}
4097
4098fn test(x: impl Trait<u64>, y: &impl Trait<u64>) {
4099 x;
4100 y;
4101 let z = bar();
4102 x.foo();
4103 y.foo();
4104 z.foo();
4105 x.foo2();
4106 y.foo2();
4107 z.foo2();
4108}
4109"#),
4110 @r###"
4111 [30; 34) 'self': &Self
4112 [55; 59) 'self': &Self
4113 [99; 101) '{}': ()
4114 [111; 112) 'x': impl Trait<u64>
4115 [131; 132) 'y': &impl Trait<u64>
4116 [152; 269) '{ ...2(); }': ()
4117 [158; 159) 'x': impl Trait<u64>
4118 [165; 166) 'y': &impl Trait<u64>
4119 [176; 177) 'z': impl Trait<u64>
4120 [180; 183) 'bar': fn bar() -> impl Trait<u64>
4121 [180; 185) 'bar()': impl Trait<u64>
4122 [191; 192) 'x': impl Trait<u64>
4123 [191; 198) 'x.foo()': u64
4124 [204; 205) 'y': &impl Trait<u64>
4125 [204; 211) 'y.foo()': u64
4126 [217; 218) 'z': impl Trait<u64>
4127 [217; 224) 'z.foo()': u64
4128 [230; 231) 'x': impl Trait<u64>
4129 [230; 238) 'x.foo2()': i64
4130 [244; 245) 'y': &impl Trait<u64>
4131 [244; 252) 'y.foo2()': i64
4132 [258; 259) 'z': impl Trait<u64>
4133 [258; 266) 'z.foo2()': i64
4134 "###
4135 );
4136}
4137
4138#[test]
4139fn dyn_trait() {
4140 assert_snapshot!(
4141 infer(r#"
4142trait Trait<T> {
4143 fn foo(&self) -> T;
4144 fn foo2(&self) -> i64;
4145}
4146fn bar() -> dyn Trait<u64> {}
4147
4148fn test(x: dyn Trait<u64>, y: &dyn Trait<u64>) {
4149 x;
4150 y;
4151 let z = bar();
4152 x.foo();
4153 y.foo();
4154 z.foo();
4155 x.foo2();
4156 y.foo2();
4157 z.foo2();
4158}
4159"#),
4160 @r###"
4161 [30; 34) 'self': &Self
4162 [55; 59) 'self': &Self
4163 [98; 100) '{}': ()
4164 [110; 111) 'x': dyn Trait<u64>
4165 [129; 130) 'y': &dyn Trait<u64>
4166 [149; 266) '{ ...2(); }': ()
4167 [155; 156) 'x': dyn Trait<u64>
4168 [162; 163) 'y': &dyn Trait<u64>
4169 [173; 174) 'z': dyn Trait<u64>
4170 [177; 180) 'bar': fn bar() -> dyn Trait<u64>
4171 [177; 182) 'bar()': dyn Trait<u64>
4172 [188; 189) 'x': dyn Trait<u64>
4173 [188; 195) 'x.foo()': u64
4174 [201; 202) 'y': &dyn Trait<u64>
4175 [201; 208) 'y.foo()': u64
4176 [214; 215) 'z': dyn Trait<u64>
4177 [214; 221) 'z.foo()': u64
4178 [227; 228) 'x': dyn Trait<u64>
4179 [227; 235) 'x.foo2()': i64
4180 [241; 242) 'y': &dyn Trait<u64>
4181 [241; 249) 'y.foo2()': i64
4182 [255; 256) 'z': dyn Trait<u64>
4183 [255; 263) 'z.foo2()': i64
4184 "###
4185 );
4186}
4187
4188#[test]
4189fn dyn_trait_bare() {
4190 assert_snapshot!(
4191 infer(r#"
4192trait Trait {
4193 fn foo(&self) -> u64;
4194}
4195fn bar() -> Trait {}
4196
4197fn test(x: Trait, y: &Trait) -> u64 {
4198 x;
4199 y;
4200 let z = bar();
4201 x.foo();
4202 y.foo();
4203 z.foo();
4204}
4205"#),
4206 @r###"
4207 [27; 31) 'self': &Self
4208 [61; 63) '{}': ()
4209 [73; 74) 'x': dyn Trait
4210 [83; 84) 'y': &dyn Trait
4211 [101; 176) '{ ...o(); }': ()
4212 [107; 108) 'x': dyn Trait
4213 [114; 115) 'y': &dyn Trait
4214 [125; 126) 'z': dyn Trait
4215 [129; 132) 'bar': fn bar() -> dyn Trait
4216 [129; 134) 'bar()': dyn Trait
4217 [140; 141) 'x': dyn Trait
4218 [140; 147) 'x.foo()': u64
4219 [153; 154) 'y': &dyn Trait
4220 [153; 160) 'y.foo()': u64
4221 [166; 167) 'z': dyn Trait
4222 [166; 173) 'z.foo()': u64
4223 "###
4224 );
4225}
4226
4227#[test]
4228fn weird_bounds() {
4229 assert_snapshot!(
4230 infer(r#"
4231trait Trait {}
4232fn test() {
4233 let a: impl Trait + 'lifetime = foo;
4234 let b: impl 'lifetime = foo;
4235 let b: impl (Trait) = foo;
4236 let b: impl ('lifetime) = foo;
4237 let d: impl ?Sized = foo;
4238 let e: impl Trait + ?Sized = foo;
4239}
4240"#),
4241 @r###"
4242 [26; 237) '{ ...foo; }': ()
4243 [36; 37) 'a': impl Trait + {error}
4244 [64; 67) 'foo': impl Trait + {error}
4245 [77; 78) 'b': impl {error}
4246 [97; 100) 'foo': impl {error}
4247 [110; 111) 'b': impl Trait
4248 [128; 131) 'foo': impl Trait
4249 [141; 142) 'b': impl {error}
4250 [163; 166) 'foo': impl {error}
4251 [176; 177) 'd': impl {error}
4252 [193; 196) 'foo': impl {error}
4253 [206; 207) 'e': impl Trait + {error}
4254 [231; 234) 'foo': impl Trait + {error}
4255 "###
4256 );
4257}
4258
4259#[test]
4260fn assoc_type_bindings() {
4261 assert_snapshot!(
4262 infer(r#"
4263trait Trait {
4264 type Type;
4265}
4266
4267fn get<T: Trait>(t: T) -> <T as Trait>::Type {}
4268fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
4269fn set<T: Trait<Type = u64>>(t: T) -> T {t}
4270
4271struct S<T>;
4272impl<T> Trait for S<T> { type Type = T; }
4273
4274fn test<T: Trait<Type = u32>>(x: T, y: impl Trait<Type = i64>) {
4275 get(x);
4276 get2(x);
4277 get(y);
4278 get2(y);
4279 get(set(S));
4280 get2(set(S));
4281 get2(S::<str>);
4282}
4283"#),
4284 @r###"
4285 [50; 51) 't': T
4286 [78; 80) '{}': ()
4287 [112; 113) 't': T
4288 [123; 125) '{}': ()
4289 [155; 156) 't': T
4290 [166; 169) '{t}': T
4291 [167; 168) 't': T
4292 [257; 258) 'x': T
4293 [263; 264) 'y': impl Trait<Type = i64>
4294 [290; 398) '{ ...r>); }': ()
4295 [296; 299) 'get': fn get<T>(T) -> <T as Trait>::Type
4296 [296; 302) 'get(x)': {unknown}
4297 [300; 301) 'x': T
4298 [308; 312) 'get2': fn get2<{unknown}, T>(T) -> U
4299 [308; 315) 'get2(x)': {unknown}
4300 [313; 314) 'x': T
4301 [321; 324) 'get': fn get<impl Trait<Type = i64>>(T) -> <T as Trait>::Type
4302 [321; 327) 'get(y)': {unknown}
4303 [325; 326) 'y': impl Trait<Type = i64>
4304 [333; 337) 'get2': fn get2<{unknown}, impl Trait<Type = i64>>(T) -> U
4305 [333; 340) 'get2(y)': {unknown}
4306 [338; 339) 'y': impl Trait<Type = i64>
4307 [346; 349) 'get': fn get<S<u64>>(T) -> <T as Trait>::Type
4308 [346; 357) 'get(set(S))': u64
4309 [350; 353) 'set': fn set<S<u64>>(T) -> T
4310 [350; 356) 'set(S)': S<u64>
4311 [354; 355) 'S': S<u64>
4312 [363; 367) 'get2': fn get2<u64, S<u64>>(T) -> U
4313 [363; 375) 'get2(set(S))': u64
4314 [368; 371) 'set': fn set<S<u64>>(T) -> T
4315 [368; 374) 'set(S)': S<u64>
4316 [372; 373) 'S': S<u64>
4317 [381; 385) 'get2': fn get2<str, S<str>>(T) -> U
4318 [381; 395) 'get2(S::<str>)': str
4319 [386; 394) 'S::<str>': S<str>
4320 "###
4321 );
4322}
4323
4324#[test]
4325fn impl_trait_assoc_binding_projection_bug() {
4326 let (db, pos) = TestDB::with_position(
4327 r#"
4328//- /main.rs crate:main deps:std
4329pub trait Language {
4330 type Kind;
4331}
4332pub enum RustLanguage {}
4333impl Language for RustLanguage {
4334 type Kind = SyntaxKind;
4335}
4336struct SyntaxNode<L> {}
4337fn foo() -> impl Iterator<Item = SyntaxNode<RustLanguage>> {}
4338
4339trait Clone {
4340 fn clone(&self) -> Self;
4341}
4342
4343fn api_walkthrough() {
4344 for node in foo() {
4345 node.clone()<|>;
4346 }
4347}
4348
4349//- /std.rs crate:std
4350#[prelude_import] use iter::*;
4351mod iter {
4352 trait IntoIterator {
4353 type Item;
4354 }
4355 trait Iterator {
4356 type Item;
4357 }
4358 impl<T: Iterator> IntoIterator for T {
4359 type Item = <T as Iterator>::Item;
4360 }
4361}
4362"#,
4363 );
4364 assert_eq!("{unknown}", type_at_pos(&db, pos));
4365}
4366
4367#[test]
4368fn projection_eq_within_chalk() {
4369 // std::env::set_var("CHALK_DEBUG", "1");
4370 assert_snapshot!(
4371 infer(r#"
4372trait Trait1 {
4373 type Type;
4374}
4375trait Trait2<T> {
4376 fn foo(self) -> T;
4377}
4378impl<T, U> Trait2<T> for U where U: Trait1<Type = T> {}
4379
4380fn test<T: Trait1<Type = u32>>(x: T) {
4381 x.foo();
4382}
4383"#),
4384 @r###"
4385 [62; 66) 'self': Self
4386 [164; 165) 'x': T
4387 [170; 186) '{ ...o(); }': ()
4388 [176; 177) 'x': T
4389 [176; 183) 'x.foo()': {unknown}
4390 "###
4391 );
4392}
4393
4394#[test]
4395fn where_clause_trait_in_scope_for_method_resolution() {
4396 let t = type_at(
4397 r#"
4398//- /main.rs
4399mod foo {
4400 trait Trait {
4401 fn foo(&self) -> u32 {}
4402 }
4403}
4404
4405fn test<T: foo::Trait>(x: T) {
4406 x.foo()<|>;
4407}
4408"#,
4409 );
4410 assert_eq!(t, "u32");
4411}
4412
4413#[test]
4414fn super_trait_method_resolution() {
4415 assert_snapshot!(
4416 infer(r#"
4417mod foo {
4418 trait SuperTrait {
4419 fn foo(&self) -> u32 {}
4420 }
4421}
4422trait Trait1: foo::SuperTrait {}
4423trait Trait2 where Self: foo::SuperTrait {}
4424
4425fn test<T: Trait1, U: Trait2>(x: T, y: U) {
4426 x.foo();
4427 y.foo();
4428}
4429"#),
4430 @r###"
4431 [50; 54) 'self': &Self
4432 [63; 65) '{}': ()
4433 [182; 183) 'x': T
4434 [188; 189) 'y': U
4435 [194; 223) '{ ...o(); }': ()
4436 [200; 201) 'x': T
4437 [200; 207) 'x.foo()': u32
4438 [213; 214) 'y': U
4439 [213; 220) 'y.foo()': u32
4440 "###
4441 );
4442}
4443
4444#[test]
4445fn super_trait_cycle() {
4446 // This just needs to not crash
4447 assert_snapshot!(
4448 infer(r#"
4449trait A: B {}
4450trait B: A {}
4451
4452fn test<T: A>(x: T) {
4453 x.foo();
4454}
4455"#),
4456 @r###"
4457 [44; 45) 'x': T
4458 [50; 66) '{ ...o(); }': ()
4459 [56; 57) 'x': T
4460 [56; 63) 'x.foo()': {unknown}
4461 "###
4462 );
4463}
4464
4465#[test]
4466fn super_trait_assoc_type_bounds() {
4467 assert_snapshot!(
4468 infer(r#"
4469trait SuperTrait { type Type; }
4470trait Trait where Self: SuperTrait {}
4471
4472fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
4473fn set<T: Trait<Type = u64>>(t: T) -> T {t}
4474
4475struct S<T>;
4476impl<T> SuperTrait for S<T> { type Type = T; }
4477impl<T> Trait for S<T> {}
4478
4479fn test() {
4480 get2(set(S));
4481}
4482"#),
4483 @r###"
4484 [103; 104) 't': T
4485 [114; 116) '{}': ()
4486 [146; 147) 't': T
4487 [157; 160) '{t}': T
4488 [158; 159) 't': T
4489 [259; 280) '{ ...S)); }': ()
4490 [265; 269) 'get2': fn get2<u64, S<u64>>(T) -> U
4491 [265; 277) 'get2(set(S))': u64
4492 [270; 273) 'set': fn set<S<u64>>(T) -> T
4493 [270; 276) 'set(S)': S<u64>
4494 [274; 275) 'S': S<u64>
4495 "###
4496 );
4497}
4498
4499#[test]
4500fn fn_trait() {
4501 assert_snapshot!(
4502 infer(r#"
4503trait FnOnce<Args> {
4504 type Output;
4505
4506 fn call_once(self, args: Args) -> <Self as FnOnce<Args>>::Output;
4507}
4508
4509fn test<F: FnOnce(u32, u64) -> u128>(f: F) {
4510 f.call_once((1, 2));
4511}
4512"#),
4513 @r###"
4514 [57; 61) 'self': Self
4515 [63; 67) 'args': Args
4516 [150; 151) 'f': F
4517 [156; 184) '{ ...2)); }': ()
4518 [162; 163) 'f': F
4519 [162; 181) 'f.call...1, 2))': {unknown}
4520 [174; 180) '(1, 2)': (u32, u64)
4521 [175; 176) '1': u32
4522 [178; 179) '2': u64
4523 "###
4524 );
4525}
4526
4527#[test]
4528fn closure_1() {
4529 assert_snapshot!(
4530 infer(r#"
4531#[lang = "fn_once"]
4532trait FnOnce<Args> {
4533 type Output;
4534}
4535
4536enum Option<T> { Some(T), None }
4537impl<T> Option<T> {
4538 fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {}
4539}
4540
4541fn test() {
4542 let x = Option::Some(1u32);
4543 x.map(|v| v + 1);
4544 x.map(|_v| 1u64);
4545 let y: Option<i64> = x.map(|_v| 1);
4546}
4547"#),
4548 @r###"
4549 [148; 152) 'self': Option<T>
4550 [154; 155) 'f': F
4551 [173; 175) '{}': ()
4552 [189; 308) '{ ... 1); }': ()
4553 [199; 200) 'x': Option<u32>
4554 [203; 215) 'Option::Some': Some<u32>(T) -> Option<T>
4555 [203; 221) 'Option...(1u32)': Option<u32>
4556 [216; 220) '1u32': u32
4557 [227; 228) 'x': Option<u32>
4558 [227; 243) 'x.map(...v + 1)': Option<u32>
4559 [233; 242) '|v| v + 1': |u32| -> u32
4560 [234; 235) 'v': u32
4561 [237; 238) 'v': u32
4562 [237; 242) 'v + 1': u32
4563 [241; 242) '1': u32
4564 [249; 250) 'x': Option<u32>
4565 [249; 265) 'x.map(... 1u64)': Option<u64>
4566 [255; 264) '|_v| 1u64': |u32| -> u64
4567 [256; 258) '_v': u32
4568 [260; 264) '1u64': u64
4569 [275; 276) 'y': Option<i64>
4570 [292; 293) 'x': Option<u32>
4571 [292; 305) 'x.map(|_v| 1)': Option<i64>
4572 [298; 304) '|_v| 1': |u32| -> i64
4573 [299; 301) '_v': u32
4574 [303; 304) '1': i64
4575 "###
4576 );
4577}
4578
4579#[test]
4580fn closure_2() {
4581 assert_snapshot!(
4582 infer(r#"
4583trait FnOnce<Args> {
4584 type Output;
4585}
4586
4587fn test<F: FnOnce(u32) -> u64>(f: F) {
4588 f(1);
4589 let g = |v| v + 1;
4590 g(1u64);
4591 let h = |v| 1u128 + v;
4592}
4593"#),
4594 @r###"
4595 [73; 74) 'f': F
4596 [79; 155) '{ ...+ v; }': ()
4597 [85; 86) 'f': F
4598 [85; 89) 'f(1)': {unknown}
4599 [87; 88) '1': i32
4600 [99; 100) 'g': |u64| -> i32
4601 [103; 112) '|v| v + 1': |u64| -> i32
4602 [104; 105) 'v': u64
4603 [107; 108) 'v': u64
4604 [107; 112) 'v + 1': i32
4605 [111; 112) '1': i32
4606 [118; 119) 'g': |u64| -> i32
4607 [118; 125) 'g(1u64)': i32
4608 [120; 124) '1u64': u64
4609 [135; 136) 'h': |u128| -> u128
4610 [139; 152) '|v| 1u128 + v': |u128| -> u128
4611 [140; 141) 'v': u128
4612 [143; 148) '1u128': u128
4613 [143; 152) '1u128 + v': u128
4614 [151; 152) 'v': u128
4615 "###
4616 );
4617}
4618
4619#[test]
4620fn closure_as_argument_inference_order() {
4621 assert_snapshot!(
4622 infer(r#"
4623#[lang = "fn_once"]
4624trait FnOnce<Args> {
4625 type Output;
4626}
4627
4628fn foo1<T, U, F: FnOnce(T) -> U>(x: T, f: F) -> U {}
4629fn foo2<T, U, F: FnOnce(T) -> U>(f: F, x: T) -> U {}
4630
4631struct S;
4632impl S {
4633 fn method(self) -> u64;
4634
4635 fn foo1<T, U, F: FnOnce(T) -> U>(self, x: T, f: F) -> U {}
4636 fn foo2<T, U, F: FnOnce(T) -> U>(self, f: F, x: T) -> U {}
4637}
4638
4639fn test() {
4640 let x1 = foo1(S, |s| s.method());
4641 let x2 = foo2(|s| s.method(), S);
4642 let x3 = S.foo1(S, |s| s.method());
4643 let x4 = S.foo2(|s| s.method(), S);
4644}
4645"#),
4646 @r###"
4647 [95; 96) 'x': T
4648 [101; 102) 'f': F
4649 [112; 114) '{}': ()
4650 [148; 149) 'f': F
4651 [154; 155) 'x': T
4652 [165; 167) '{}': ()
4653 [202; 206) 'self': S
4654 [254; 258) 'self': S
4655 [260; 261) 'x': T
4656 [266; 267) 'f': F
4657 [277; 279) '{}': ()
4658 [317; 321) 'self': S
4659 [323; 324) 'f': F
4660 [329; 330) 'x': T
4661 [340; 342) '{}': ()
4662 [356; 515) '{ ... S); }': ()
4663 [366; 368) 'x1': u64
4664 [371; 375) 'foo1': fn foo1<S, u64, |S| -> u64>(T, F) -> U
4665 [371; 394) 'foo1(S...hod())': u64
4666 [376; 377) 'S': S
4667 [379; 393) '|s| s.method()': |S| -> u64
4668 [380; 381) 's': S
4669 [383; 384) 's': S
4670 [383; 393) 's.method()': u64
4671 [404; 406) 'x2': u64
4672 [409; 413) 'foo2': fn foo2<S, u64, |S| -> u64>(F, T) -> U
4673 [409; 432) 'foo2(|...(), S)': u64
4674 [414; 428) '|s| s.method()': |S| -> u64
4675 [415; 416) 's': S
4676 [418; 419) 's': S
4677 [418; 428) 's.method()': u64
4678 [430; 431) 'S': S
4679 [442; 444) 'x3': u64
4680 [447; 448) 'S': S
4681 [447; 472) 'S.foo1...hod())': u64
4682 [454; 455) 'S': S
4683 [457; 471) '|s| s.method()': |S| -> u64
4684 [458; 459) 's': S
4685 [461; 462) 's': S
4686 [461; 471) 's.method()': u64
4687 [482; 484) 'x4': u64
4688 [487; 488) 'S': S
4689 [487; 512) 'S.foo2...(), S)': u64
4690 [494; 508) '|s| s.method()': |S| -> u64
4691 [495; 496) 's': S
4692 [498; 499) 's': S
4693 [498; 508) 's.method()': u64
4694 [510; 511) 'S': S
4695 "###
4696 );
4697}
4698
4699#[test]
4700fn unselected_projection_in_trait_env_1() {
4701 let t = type_at(
4702 r#"
4703//- /main.rs
4704trait Trait {
4705 type Item;
4706}
4707
4708trait Trait2 {
4709 fn foo(&self) -> u32;
4710}
4711
4712fn test<T: Trait>() where T::Item: Trait2 {
4713 let x: T::Item = no_matter;
4714 x.foo()<|>;
4715}
4716"#,
4717 );
4718 assert_eq!(t, "u32");
4719}
4720
4721#[test]
4722fn unselected_projection_in_trait_env_2() {
4723 let t = type_at(
4724 r#"
4725//- /main.rs
4726trait Trait<T> {
4727 type Item;
4728}
4729
4730trait Trait2 {
4731 fn foo(&self) -> u32;
4732}
4733
4734fn test<T, U>() where T::Item: Trait2, T: Trait<U::Item>, U: Trait<()> {
4735 let x: T::Item = no_matter;
4736 x.foo()<|>;
4737}
4738"#,
4739 );
4740 assert_eq!(t, "u32");
4741}
4742
4743#[test]
4744fn trait_impl_self_ty() {
4745 let t = type_at(
4746 r#"
4747//- /main.rs
4748trait Trait<T> {
4749 fn foo(&self);
4750}
4751
4752struct S;
4753
4754impl Trait<Self> for S {}
4755
4756fn test() {
4757 S.foo()<|>;
4758}
4759"#,
4760 );
4761 assert_eq!(t, "()");
4762}
4763
4764#[test]
4765fn trait_impl_self_ty_cycle() {
4766 let t = type_at(
4767 r#"
4768//- /main.rs
4769trait Trait {
4770 fn foo(&self);
4771}
4772
4773struct S<T>;
4774
4775impl Trait for S<Self> {}
4776
4777fn test() {
4778 S.foo()<|>;
4779}
4780"#,
4781 );
4782 assert_eq!(t, "{unknown}");
4783}
4784
4785#[test]
4786fn unselected_projection_in_trait_env_cycle_1() {
4787 let t = type_at(
4788 r#"
4789//- /main.rs
4790trait Trait {
4791 type Item;
4792}
4793
4794trait Trait2<T> {}
4795
4796fn test<T: Trait>() where T: Trait2<T::Item> {
4797 let x: T::Item = no_matter<|>;
4798}
4799"#,
4800 );
4801 // this is a legitimate cycle
4802 assert_eq!(t, "{unknown}");
4803}
4804
4805#[test]
4806fn unselected_projection_in_trait_env_cycle_2() {
4807 let t = type_at(
4808 r#"
4809//- /main.rs
4810trait Trait<T> {
4811 type Item;
4812}
4813
4814fn test<T, U>() where T: Trait<U::Item>, U: Trait<T::Item> {
4815 let x: T::Item = no_matter<|>;
4816}
4817"#,
4818 );
4819 // this is a legitimate cycle
4820 assert_eq!(t, "{unknown}");
4821}
4822
4823#[test]
4824fn bug_2467() {
4825 assert_snapshot!(
4826 infer(r#"
4827struct S<T>(T);
4828impl<T> S<T> {
4829 fn foo(self) -> T;
4830}
4831fn test() {
4832 // needs to nest multiple times for variable indices to get high enough
4833 let a = S::foo(S(1));
4834 let b = S::foo(S(a));
4835 let c = S::foo(S(b));
4836 let d: u32 = S::foo(S(c));
4837}
4838"#),
4839 @r###"
4840 [43; 47) 'self': S<T>
4841 [67; 255) '{ ...c)); }': ()
4842 [153; 154) 'a': u32
4843 [157; 163) 'S::foo': fn foo<u32>(S<T>) -> T
4844 [157; 169) 'S::foo(S(1))': u32
4845 [164; 165) 'S': S<u32>(T) -> S<T>
4846 [164; 168) 'S(1)': S<u32>
4847 [166; 167) '1': u32
4848 [179; 180) 'b': u32
4849 [183; 189) 'S::foo': fn foo<u32>(S<T>) -> T
4850 [183; 195) 'S::foo(S(a))': u32
4851 [190; 191) 'S': S<u32>(T) -> S<T>
4852 [190; 194) 'S(a)': S<u32>
4853 [192; 193) 'a': u32
4854 [205; 206) 'c': u32
4855 [209; 215) 'S::foo': fn foo<u32>(S<T>) -> T
4856 [209; 221) 'S::foo(S(b))': u32
4857 [216; 217) 'S': S<u32>(T) -> S<T>
4858 [216; 220) 'S(b)': S<u32>
4859 [218; 219) 'b': u32
4860 [231; 232) 'd': u32
4861 [240; 246) 'S::foo': fn foo<u32>(S<T>) -> T
4862 [240; 252) 'S::foo(S(c))': u32
4863 [247; 248) 'S': S<u32>(T) -> S<T>
4864 [247; 251) 'S(c)': S<u32>
4865 [249; 250) 'c': u32
4866 "###
4867 );
4868}
4869
4870fn type_at_pos(db: &TestDB, pos: FilePosition) -> String { 31fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
4871 let file = db.parse(pos.file_id).ok().unwrap(); 32 let file = db.parse(pos.file_id).ok().unwrap();
4872 let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap(); 33 let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap();
@@ -5095,60 +256,3 @@ fn no_such_field_diagnostics() {
5095 "### 256 "###
5096 ); 257 );
5097} 258}
5098
5099#[test]
5100fn infer_builtin_macros_line() {
5101 assert_snapshot!(
5102 infer(r#"
5103#[rustc_builtin_macro]
5104macro_rules! line {() => {}}
5105
5106fn main() {
5107 let x = line!();
5108}
5109"#),
5110 @r###"
5111 ![0; 1) '6': i32
5112 [64; 88) '{ ...!(); }': ()
5113 [74; 75) 'x': i32
5114 "###
5115 );
5116}
5117
5118#[test]
5119fn infer_builtin_macros_file() {
5120 assert_snapshot!(
5121 infer(r#"
5122#[rustc_builtin_macro]
5123macro_rules! file {() => {}}
5124
5125fn main() {
5126 let x = file!();
5127}
5128"#),
5129 @r###"
5130 ![0; 2) '""': &str
5131 [64; 88) '{ ...!(); }': ()
5132 [74; 75) 'x': &str
5133 "###
5134 );
5135}
5136
5137#[test]
5138fn infer_builtin_macros_column() {
5139 assert_snapshot!(
5140 infer(r#"
5141#[rustc_builtin_macro]
5142macro_rules! column {() => {}}
5143
5144fn main() {
5145 let x = column!();
5146}
5147"#),
5148 @r###"
5149 ![0; 2) '13': i32
5150 [66; 92) '{ ...!(); }': ()
5151 [76; 77) 'x': i32
5152 "###
5153 );
5154}
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs
new file mode 100644
index 000000000..0d9a35ce0
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/macros.rs
@@ -0,0 +1,268 @@
1use super::{infer, type_at, type_at_pos};
2use crate::test_db::TestDB;
3use insta::assert_snapshot;
4use ra_db::fixture::WithFixture;
5
6#[test]
7fn cfg_impl_block() {
8 let (db, pos) = TestDB::with_position(
9 r#"
10//- /main.rs crate:main deps:foo cfg:test
11use foo::S as T;
12struct S;
13
14#[cfg(test)]
15impl S {
16 fn foo1(&self) -> i32 { 0 }
17}
18
19#[cfg(not(test))]
20impl S {
21 fn foo2(&self) -> i32 { 0 }
22}
23
24fn test() {
25 let t = (S.foo1(), S.foo2(), T.foo3(), T.foo4());
26 t<|>;
27}
28
29//- /foo.rs crate:foo
30struct S;
31
32#[cfg(not(test))]
33impl S {
34 fn foo3(&self) -> i32 { 0 }
35}
36
37#[cfg(test)]
38impl S {
39 fn foo4(&self) -> i32 { 0 }
40}
41"#,
42 );
43 assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos));
44}
45
46#[test]
47fn infer_macros_expanded() {
48 assert_snapshot!(
49 infer(r#"
50struct Foo(Vec<i32>);
51
52macro_rules! foo {
53 ($($item:expr),*) => {
54 {
55 Foo(vec![$($item,)*])
56 }
57 };
58}
59
60fn main() {
61 let x = foo!(1,2);
62}
63"#),
64 @r###"
65 ![0; 17) '{Foo(v...,2,])}': Foo
66 ![1; 4) 'Foo': Foo({unknown}) -> Foo
67 ![1; 16) 'Foo(vec![1,2,])': Foo
68 ![5; 15) 'vec![1,2,]': {unknown}
69 [156; 182) '{ ...,2); }': ()
70 [166; 167) 'x': Foo
71 "###
72 );
73}
74
75#[test]
76fn infer_legacy_textual_scoped_macros_expanded() {
77 assert_snapshot!(
78 infer(r#"
79struct Foo(Vec<i32>);
80
81#[macro_use]
82mod m {
83 macro_rules! foo {
84 ($($item:expr),*) => {
85 {
86 Foo(vec![$($item,)*])
87 }
88 };
89 }
90}
91
92fn main() {
93 let x = foo!(1,2);
94 let y = crate::foo!(1,2);
95}
96"#),
97 @r###"
98 ![0; 17) '{Foo(v...,2,])}': Foo
99 ![1; 4) 'Foo': Foo({unknown}) -> Foo
100 ![1; 16) 'Foo(vec![1,2,])': Foo
101 ![5; 15) 'vec![1,2,]': {unknown}
102 [195; 251) '{ ...,2); }': ()
103 [205; 206) 'x': Foo
104 [228; 229) 'y': {unknown}
105 [232; 248) 'crate:...!(1,2)': {unknown}
106 "###
107 );
108}
109
110#[test]
111fn infer_path_qualified_macros_expanded() {
112 assert_snapshot!(
113 infer(r#"
114#[macro_export]
115macro_rules! foo {
116 () => { 42i32 }
117}
118
119mod m {
120 pub use super::foo as bar;
121}
122
123fn main() {
124 let x = crate::foo!();
125 let y = m::bar!();
126}
127"#),
128 @r###"
129 ![0; 5) '42i32': i32
130 ![0; 5) '42i32': i32
131 [111; 164) '{ ...!(); }': ()
132 [121; 122) 'x': i32
133 [148; 149) 'y': i32
134 "###
135 );
136}
137
138#[test]
139fn infer_type_value_macro_having_same_name() {
140 assert_snapshot!(
141 infer(r#"
142#[macro_export]
143macro_rules! foo {
144 () => {
145 mod foo {
146 pub use super::foo;
147 }
148 };
149 ($x:tt) => {
150 $x
151 };
152}
153
154foo!();
155
156fn foo() {
157 let foo = foo::foo!(42i32);
158}
159"#),
160 @r###"
161 ![0; 5) '42i32': i32
162 [171; 206) '{ ...32); }': ()
163 [181; 184) 'foo': i32
164 "###
165 );
166}
167
168#[test]
169fn processes_impls_generated_by_macros() {
170 let t = type_at(
171 r#"
172//- /main.rs
173macro_rules! m {
174 ($ident:ident) => (impl Trait for $ident {})
175}
176trait Trait { fn foo(self) -> u128 {} }
177struct S;
178m!(S);
179fn test() { S.foo()<|>; }
180"#,
181 );
182 assert_eq!(t, "u128");
183}
184
185#[test]
186fn infer_macro_with_dollar_crate_is_correct_in_expr() {
187 let (db, pos) = TestDB::with_position(
188 r#"
189//- /main.rs crate:main deps:foo
190fn test() {
191 let x = (foo::foo!(1), foo::foo!(2));
192 x<|>;
193}
194
195//- /lib.rs crate:foo
196#[macro_export]
197macro_rules! foo {
198 (1) => { $crate::bar!() };
199 (2) => { 1 + $crate::baz() };
200}
201
202#[macro_export]
203macro_rules! bar {
204 () => { 42 }
205}
206
207pub fn baz() -> usize { 31usize }
208"#,
209 );
210 assert_eq!("(i32, usize)", type_at_pos(&db, pos));
211}
212
213#[test]
214fn infer_builtin_macros_line() {
215 assert_snapshot!(
216 infer(r#"
217#[rustc_builtin_macro]
218macro_rules! line {() => {}}
219
220fn main() {
221 let x = line!();
222}
223"#),
224 @r###"
225 ![0; 1) '6': i32
226 [64; 88) '{ ...!(); }': ()
227 [74; 75) 'x': i32
228 "###
229 );
230}
231
232#[test]
233fn infer_builtin_macros_file() {
234 assert_snapshot!(
235 infer(r#"
236#[rustc_builtin_macro]
237macro_rules! file {() => {}}
238
239fn main() {
240 let x = file!();
241}
242"#),
243 @r###"
244 ![0; 2) '""': &str
245 [64; 88) '{ ...!(); }': ()
246 [74; 75) 'x': &str
247 "###
248 );
249}
250
251#[test]
252fn infer_builtin_macros_column() {
253 assert_snapshot!(
254 infer(r#"
255#[rustc_builtin_macro]
256macro_rules! column {() => {}}
257
258fn main() {
259 let x = column!();
260}
261"#),
262 @r###"
263 ![0; 2) '13': i32
264 [66; 92) '{ ...!(); }': ()
265 [76; 77) 'x': i32
266 "###
267 );
268}
diff --git a/crates/ra_hir_ty/src/tests/method_resolution.rs b/crates/ra_hir_ty/src/tests/method_resolution.rs
new file mode 100644
index 000000000..45164c9e9
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/method_resolution.rs
@@ -0,0 +1,1005 @@
1use super::{infer, type_at, type_at_pos};
2use crate::test_db::TestDB;
3use insta::assert_snapshot;
4use ra_db::fixture::WithFixture;
5
6#[test]
7fn infer_slice_method() {
8 assert_snapshot!(
9 infer(r#"
10#[lang = "slice"]
11impl<T> [T] {
12 fn foo(&self) -> T {
13 loop {}
14 }
15}
16
17#[lang = "slice_alloc"]
18impl<T> [T] {}
19
20fn test() {
21 <[_]>::foo(b"foo");
22}
23"#),
24 @r###"
25 [45; 49) 'self': &[T]
26 [56; 79) '{ ... }': T
27 [66; 73) 'loop {}': !
28 [71; 73) '{}': ()
29 [133; 160) '{ ...o"); }': ()
30 [139; 149) '<[_]>::foo': fn foo<u8>(&[T]) -> T
31 [139; 157) '<[_]>:..."foo")': u8
32 [150; 156) 'b"foo"': &[u8]
33 "###
34 );
35}
36
37#[test]
38fn infer_associated_method_struct() {
39 assert_snapshot!(
40 infer(r#"
41struct A { x: u32 }
42
43impl A {
44 fn new() -> A {
45 A { x: 0 }
46 }
47}
48fn test() {
49 let a = A::new();
50 a.x;
51}
52"#),
53 @r###"
54 [49; 75) '{ ... }': A
55 [59; 69) 'A { x: 0 }': A
56 [66; 67) '0': u32
57 [88; 122) '{ ...a.x; }': ()
58 [98; 99) 'a': A
59 [102; 108) 'A::new': fn new() -> A
60 [102; 110) 'A::new()': A
61 [116; 117) 'a': A
62 [116; 119) 'a.x': u32
63 "###
64 );
65}
66
67#[test]
68fn infer_associated_method_enum() {
69 assert_snapshot!(
70 infer(r#"
71enum A { B, C }
72
73impl A {
74 pub fn b() -> A {
75 A::B
76 }
77 pub fn c() -> A {
78 A::C
79 }
80}
81fn test() {
82 let a = A::b();
83 a;
84 let c = A::c();
85 c;
86}
87"#),
88 @r###"
89 [47; 67) '{ ... }': A
90 [57; 61) 'A::B': A
91 [88; 108) '{ ... }': A
92 [98; 102) 'A::C': A
93 [121; 178) '{ ... c; }': ()
94 [131; 132) 'a': A
95 [135; 139) 'A::b': fn b() -> A
96 [135; 141) 'A::b()': A
97 [147; 148) 'a': A
98 [158; 159) 'c': A
99 [162; 166) 'A::c': fn c() -> A
100 [162; 168) 'A::c()': A
101 [174; 175) 'c': A
102 "###
103 );
104}
105
106#[test]
107fn infer_associated_method_with_modules() {
108 assert_snapshot!(
109 infer(r#"
110mod a {
111 struct A;
112 impl A { pub fn thing() -> A { A {} }}
113}
114
115mod b {
116 struct B;
117 impl B { pub fn thing() -> u32 { 99 }}
118
119 mod c {
120 struct C;
121 impl C { pub fn thing() -> C { C {} }}
122 }
123}
124use b::c;
125
126fn test() {
127 let x = a::A::thing();
128 let y = b::B::thing();
129 let z = c::C::thing();
130}
131"#),
132 @r###"
133 [56; 64) '{ A {} }': A
134 [58; 62) 'A {}': A
135 [126; 132) '{ 99 }': u32
136 [128; 130) '99': u32
137 [202; 210) '{ C {} }': C
138 [204; 208) 'C {}': C
139 [241; 325) '{ ...g(); }': ()
140 [251; 252) 'x': A
141 [255; 266) 'a::A::thing': fn thing() -> A
142 [255; 268) 'a::A::thing()': A
143 [278; 279) 'y': u32
144 [282; 293) 'b::B::thing': fn thing() -> u32
145 [282; 295) 'b::B::thing()': u32
146 [305; 306) 'z': C
147 [309; 320) 'c::C::thing': fn thing() -> C
148 [309; 322) 'c::C::thing()': C
149 "###
150 );
151}
152
153#[test]
154fn infer_associated_method_generics() {
155 assert_snapshot!(
156 infer(r#"
157struct Gen<T> {
158 val: T
159}
160
161impl<T> Gen<T> {
162 pub fn make(val: T) -> Gen<T> {
163 Gen { val }
164 }
165}
166
167fn test() {
168 let a = Gen::make(0u32);
169}
170"#),
171 @r###"
172 [64; 67) 'val': T
173 [82; 109) '{ ... }': Gen<T>
174 [92; 103) 'Gen { val }': Gen<T>
175 [98; 101) 'val': T
176 [123; 155) '{ ...32); }': ()
177 [133; 134) 'a': Gen<u32>
178 [137; 146) 'Gen::make': fn make<u32>(T) -> Gen<T>
179 [137; 152) 'Gen::make(0u32)': Gen<u32>
180 [147; 151) '0u32': u32
181 "###
182 );
183}
184
185#[test]
186fn infer_associated_method_generics_with_default_param() {
187 assert_snapshot!(
188 infer(r#"
189struct Gen<T=u32> {
190 val: T
191}
192
193impl<T> Gen<T> {
194 pub fn make() -> Gen<T> {
195 loop { }
196 }
197}
198
199fn test() {
200 let a = Gen::make();
201}
202"#),
203 @r###"
204 [80; 104) '{ ... }': Gen<T>
205 [90; 98) 'loop { }': !
206 [95; 98) '{ }': ()
207 [118; 146) '{ ...e(); }': ()
208 [128; 129) 'a': Gen<u32>
209 [132; 141) 'Gen::make': fn make<u32>() -> Gen<T>
210 [132; 143) 'Gen::make()': Gen<u32>
211 "###
212 );
213}
214
215#[test]
216fn infer_associated_method_generics_with_default_tuple_param() {
217 let t = type_at(
218 r#"
219//- /main.rs
220struct Gen<T=()> {
221 val: T
222}
223
224impl<T> Gen<T> {
225 pub fn make() -> Gen<T> {
226 loop { }
227 }
228}
229
230fn test() {
231 let a = Gen::make();
232 a.val<|>;
233}
234"#,
235 );
236 assert_eq!(t, "()");
237}
238
239#[test]
240fn infer_associated_method_generics_without_args() {
241 assert_snapshot!(
242 infer(r#"
243struct Gen<T> {
244 val: T
245}
246
247impl<T> Gen<T> {
248 pub fn make() -> Gen<T> {
249 loop { }
250 }
251}
252
253fn test() {
254 let a = Gen::<u32>::make();
255}
256"#),
257 @r###"
258 [76; 100) '{ ... }': Gen<T>
259 [86; 94) 'loop { }': !
260 [91; 94) '{ }': ()
261 [114; 149) '{ ...e(); }': ()
262 [124; 125) 'a': Gen<u32>
263 [128; 144) 'Gen::<...::make': fn make<u32>() -> Gen<T>
264 [128; 146) 'Gen::<...make()': Gen<u32>
265 "###
266 );
267}
268
269#[test]
270fn infer_associated_method_generics_2_type_params_without_args() {
271 assert_snapshot!(
272 infer(r#"
273struct Gen<T, U> {
274 val: T,
275 val2: U,
276}
277
278impl<T> Gen<u32, T> {
279 pub fn make() -> Gen<u32,T> {
280 loop { }
281 }
282}
283
284fn test() {
285 let a = Gen::<u32, u64>::make();
286}
287"#),
288 @r###"
289 [102; 126) '{ ... }': Gen<u32, T>
290 [112; 120) 'loop { }': !
291 [117; 120) '{ }': ()
292 [140; 180) '{ ...e(); }': ()
293 [150; 151) 'a': Gen<u32, u64>
294 [154; 175) 'Gen::<...::make': fn make<u64>() -> Gen<u32, T>
295 [154; 177) 'Gen::<...make()': Gen<u32, u64>
296 "###
297 );
298}
299
300#[test]
301fn cross_crate_associated_method_call() {
302 let (db, pos) = TestDB::with_position(
303 r#"
304//- /main.rs crate:main deps:other_crate
305fn test() {
306 let x = other_crate::foo::S::thing();
307 x<|>;
308}
309
310//- /lib.rs crate:other_crate
311mod foo {
312 struct S;
313 impl S {
314 fn thing() -> i128 {}
315 }
316}
317"#,
318 );
319 assert_eq!("i128", type_at_pos(&db, pos));
320}
321
322#[test]
323fn infer_trait_method_simple() {
324 // the trait implementation is intentionally incomplete -- it shouldn't matter
325 assert_snapshot!(
326 infer(r#"
327trait Trait1 {
328 fn method(&self) -> u32;
329}
330struct S1;
331impl Trait1 for S1 {}
332trait Trait2 {
333 fn method(&self) -> i128;
334}
335struct S2;
336impl Trait2 for S2 {}
337fn test() {
338 S1.method(); // -> u32
339 S2.method(); // -> i128
340}
341"#),
342 @r###"
343 [31; 35) 'self': &Self
344 [110; 114) 'self': &Self
345 [170; 228) '{ ...i128 }': ()
346 [176; 178) 'S1': S1
347 [176; 187) 'S1.method()': u32
348 [203; 205) 'S2': S2
349 [203; 214) 'S2.method()': i128
350 "###
351 );
352}
353
354#[test]
355fn infer_trait_method_scoped() {
356 // the trait implementation is intentionally incomplete -- it shouldn't matter
357 assert_snapshot!(
358 infer(r#"
359struct S;
360mod foo {
361 pub trait Trait1 {
362 fn method(&self) -> u32;
363 }
364 impl Trait1 for super::S {}
365}
366mod bar {
367 pub trait Trait2 {
368 fn method(&self) -> i128;
369 }
370 impl Trait2 for super::S {}
371}
372
373mod foo_test {
374 use super::S;
375 use super::foo::Trait1;
376 fn test() {
377 S.method(); // -> u32
378 }
379}
380
381mod bar_test {
382 use super::S;
383 use super::bar::Trait2;
384 fn test() {
385 S.method(); // -> i128
386 }
387}
388"#),
389 @r###"
390 [63; 67) 'self': &Self
391 [169; 173) 'self': &Self
392 [300; 337) '{ ... }': ()
393 [310; 311) 'S': S
394 [310; 320) 'S.method()': u32
395 [416; 454) '{ ... }': ()
396 [426; 427) 'S': S
397 [426; 436) 'S.method()': i128
398 "###
399 );
400}
401
402#[test]
403fn infer_trait_method_generic_1() {
404 // the trait implementation is intentionally incomplete -- it shouldn't matter
405 assert_snapshot!(
406 infer(r#"
407trait Trait<T> {
408 fn method(&self) -> T;
409}
410struct S;
411impl Trait<u32> for S {}
412fn test() {
413 S.method();
414}
415"#),
416 @r###"
417 [33; 37) 'self': &Self
418 [92; 111) '{ ...d(); }': ()
419 [98; 99) 'S': S
420 [98; 108) 'S.method()': u32
421 "###
422 );
423}
424
425#[test]
426fn infer_trait_method_generic_more_params() {
427 // the trait implementation is intentionally incomplete -- it shouldn't matter
428 assert_snapshot!(
429 infer(r#"
430trait Trait<T1, T2, T3> {
431 fn method1(&self) -> (T1, T2, T3);
432 fn method2(&self) -> (T3, T2, T1);
433}
434struct S1;
435impl Trait<u8, u16, u32> for S1 {}
436struct S2;
437impl<T> Trait<i8, i16, T> for S2 {}
438fn test() {
439 S1.method1(); // u8, u16, u32
440 S1.method2(); // u32, u16, u8
441 S2.method1(); // i8, i16, {unknown}
442 S2.method2(); // {unknown}, i16, i8
443}
444"#),
445 @r###"
446 [43; 47) 'self': &Self
447 [82; 86) 'self': &Self
448 [210; 361) '{ ..., i8 }': ()
449 [216; 218) 'S1': S1
450 [216; 228) 'S1.method1()': (u8, u16, u32)
451 [250; 252) 'S1': S1
452 [250; 262) 'S1.method2()': (u32, u16, u8)
453 [284; 286) 'S2': S2
454 [284; 296) 'S2.method1()': (i8, i16, {unknown})
455 [324; 326) 'S2': S2
456 [324; 336) 'S2.method2()': ({unknown}, i16, i8)
457 "###
458 );
459}
460
461#[test]
462fn infer_trait_method_generic_2() {
463 // the trait implementation is intentionally incomplete -- it shouldn't matter
464 assert_snapshot!(
465 infer(r#"
466trait Trait<T> {
467 fn method(&self) -> T;
468}
469struct S<T>(T);
470impl<U> Trait<U> for S<U> {}
471fn test() {
472 S(1u32).method();
473}
474"#),
475 @r###"
476 [33; 37) 'self': &Self
477 [102; 127) '{ ...d(); }': ()
478 [108; 109) 'S': S<u32>(T) -> S<T>
479 [108; 115) 'S(1u32)': S<u32>
480 [108; 124) 'S(1u32...thod()': u32
481 [110; 114) '1u32': u32
482 "###
483 );
484}
485
486#[test]
487fn infer_trait_assoc_method() {
488 assert_snapshot!(
489 infer(r#"
490trait Default {
491 fn default() -> Self;
492}
493struct S;
494impl Default for S {}
495fn test() {
496 let s1: S = Default::default();
497 let s2 = S::default();
498 let s3 = <S as Default>::default();
499}
500"#),
501 @r###"
502 [87; 193) '{ ...t(); }': ()
503 [97; 99) 's1': S
504 [105; 121) 'Defaul...efault': fn default<S>() -> Self
505 [105; 123) 'Defaul...ault()': S
506 [133; 135) 's2': S
507 [138; 148) 'S::default': fn default<S>() -> Self
508 [138; 150) 'S::default()': S
509 [160; 162) 's3': S
510 [165; 188) '<S as ...efault': fn default<S>() -> Self
511 [165; 190) '<S as ...ault()': S
512 "###
513 );
514}
515
516#[test]
517fn infer_trait_assoc_method_generics_1() {
518 assert_snapshot!(
519 infer(r#"
520trait Trait<T> {
521 fn make() -> T;
522}
523struct S;
524impl Trait<u32> for S {}
525struct G<T>;
526impl<T> Trait<T> for G<T> {}
527fn test() {
528 let a = S::make();
529 let b = G::<u64>::make();
530 let c: f64 = G::make();
531}
532"#),
533 @r###"
534 [127; 211) '{ ...e(); }': ()
535 [137; 138) 'a': u32
536 [141; 148) 'S::make': fn make<S, u32>() -> T
537 [141; 150) 'S::make()': u32
538 [160; 161) 'b': u64
539 [164; 178) 'G::<u64>::make': fn make<G<u64>, u64>() -> T
540 [164; 180) 'G::<u6...make()': u64
541 [190; 191) 'c': f64
542 [199; 206) 'G::make': fn make<G<f64>, f64>() -> T
543 [199; 208) 'G::make()': f64
544 "###
545 );
546}
547
548#[test]
549fn infer_trait_assoc_method_generics_2() {
550 assert_snapshot!(
551 infer(r#"
552trait Trait<T> {
553 fn make<U>() -> (T, U);
554}
555struct S;
556impl Trait<u32> for S {}
557struct G<T>;
558impl<T> Trait<T> for G<T> {}
559fn test() {
560 let a = S::make::<i64>();
561 let b: (_, i64) = S::make();
562 let c = G::<u32>::make::<i64>();
563 let d: (u32, _) = G::make::<i64>();
564 let e: (u32, i64) = G::make();
565}
566"#),
567 @r###"
568 [135; 313) '{ ...e(); }': ()
569 [145; 146) 'a': (u32, i64)
570 [149; 163) 'S::make::<i64>': fn make<S, u32, i64>() -> (T, U)
571 [149; 165) 'S::mak...i64>()': (u32, i64)
572 [175; 176) 'b': (u32, i64)
573 [189; 196) 'S::make': fn make<S, u32, i64>() -> (T, U)
574 [189; 198) 'S::make()': (u32, i64)
575 [208; 209) 'c': (u32, i64)
576 [212; 233) 'G::<u3...:<i64>': fn make<G<u32>, u32, i64>() -> (T, U)
577 [212; 235) 'G::<u3...i64>()': (u32, i64)
578 [245; 246) 'd': (u32, i64)
579 [259; 273) 'G::make::<i64>': fn make<G<u32>, u32, i64>() -> (T, U)
580 [259; 275) 'G::mak...i64>()': (u32, i64)
581 [285; 286) 'e': (u32, i64)
582 [301; 308) 'G::make': fn make<G<u32>, u32, i64>() -> (T, U)
583 [301; 310) 'G::make()': (u32, i64)
584 "###
585 );
586}
587
588#[test]
589fn infer_trait_assoc_method_generics_3() {
590 assert_snapshot!(
591 infer(r#"
592trait Trait<T> {
593 fn make() -> (Self, T);
594}
595struct S<T>;
596impl Trait<i64> for S<i32> {}
597fn test() {
598 let a = S::make();
599}
600"#),
601 @r###"
602 [101; 127) '{ ...e(); }': ()
603 [111; 112) 'a': (S<i32>, i64)
604 [115; 122) 'S::make': fn make<S<i32>, i64>() -> (Self, T)
605 [115; 124) 'S::make()': (S<i32>, i64)
606 "###
607 );
608}
609
610#[test]
611fn infer_trait_assoc_method_generics_4() {
612 assert_snapshot!(
613 infer(r#"
614trait Trait<T> {
615 fn make() -> (Self, T);
616}
617struct S<T>;
618impl Trait<i64> for S<u64> {}
619impl Trait<i32> for S<u32> {}
620fn test() {
621 let a: (S<u64>, _) = S::make();
622 let b: (_, i32) = S::make();
623}
624"#),
625 @r###"
626 [131; 203) '{ ...e(); }': ()
627 [141; 142) 'a': (S<u64>, i64)
628 [158; 165) 'S::make': fn make<S<u64>, i64>() -> (Self, T)
629 [158; 167) 'S::make()': (S<u64>, i64)
630 [177; 178) 'b': (S<u32>, i32)
631 [191; 198) 'S::make': fn make<S<u32>, i32>() -> (Self, T)
632 [191; 200) 'S::make()': (S<u32>, i32)
633 "###
634 );
635}
636
637#[test]
638fn infer_trait_assoc_method_generics_5() {
639 assert_snapshot!(
640 infer(r#"
641trait Trait<T> {
642 fn make<U>() -> (Self, T, U);
643}
644struct S<T>;
645impl Trait<i64> for S<u64> {}
646fn test() {
647 let a = <S as Trait<i64>>::make::<u8>();
648 let b: (S<u64>, _, _) = Trait::<i64>::make::<u8>();
649}
650"#),
651 @r###"
652 [107; 211) '{ ...>(); }': ()
653 [117; 118) 'a': (S<u64>, i64, u8)
654 [121; 150) '<S as ...::<u8>': fn make<S<u64>, i64, u8>() -> (Self, T, U)
655 [121; 152) '<S as ...<u8>()': (S<u64>, i64, u8)
656 [162; 163) 'b': (S<u64>, i64, u8)
657 [182; 206) 'Trait:...::<u8>': fn make<S<u64>, i64, u8>() -> (Self, T, U)
658 [182; 208) 'Trait:...<u8>()': (S<u64>, i64, u8)
659 "###
660 );
661}
662
663#[test]
664fn infer_call_trait_method_on_generic_param_1() {
665 assert_snapshot!(
666 infer(r#"
667trait Trait {
668 fn method(&self) -> u32;
669}
670fn test<T: Trait>(t: T) {
671 t.method();
672}
673"#),
674 @r###"
675 [30; 34) 'self': &Self
676 [64; 65) 't': T
677 [70; 89) '{ ...d(); }': ()
678 [76; 77) 't': T
679 [76; 86) 't.method()': u32
680 "###
681 );
682}
683
684#[test]
685fn infer_call_trait_method_on_generic_param_2() {
686 assert_snapshot!(
687 infer(r#"
688trait Trait<T> {
689 fn method(&self) -> T;
690}
691fn test<U, T: Trait<U>>(t: T) {
692 t.method();
693}
694"#),
695 @r###"
696 [33; 37) 'self': &Self
697 [71; 72) 't': T
698 [77; 96) '{ ...d(); }': ()
699 [83; 84) 't': T
700 [83; 93) 't.method()': [missing name]
701 "###
702 );
703}
704
705#[test]
706fn infer_with_multiple_trait_impls() {
707 assert_snapshot!(
708 infer(r#"
709trait Into<T> {
710 fn into(self) -> T;
711}
712struct S;
713impl Into<u32> for S {}
714impl Into<u64> for S {}
715fn test() {
716 let x: u32 = S.into();
717 let y: u64 = S.into();
718 let z = Into::<u64>::into(S);
719}
720"#),
721 @r###"
722 [29; 33) 'self': Self
723 [111; 202) '{ ...(S); }': ()
724 [121; 122) 'x': u32
725 [130; 131) 'S': S
726 [130; 138) 'S.into()': u32
727 [148; 149) 'y': u64
728 [157; 158) 'S': S
729 [157; 165) 'S.into()': u64
730 [175; 176) 'z': u64
731 [179; 196) 'Into::...::into': fn into<S, u64>(Self) -> T
732 [179; 199) 'Into::...nto(S)': u64
733 [197; 198) 'S': S
734 "###
735 );
736}
737
738#[test]
739fn method_resolution_unify_impl_self_type() {
740 let t = type_at(
741 r#"
742//- /main.rs
743struct S<T>;
744impl S<u32> { fn foo(&self) -> u8 {} }
745impl S<i32> { fn foo(&self) -> i8 {} }
746fn test() { (S::<u32>.foo(), S::<i32>.foo())<|>; }
747"#,
748 );
749 assert_eq!(t, "(u8, i8)");
750}
751
752#[test]
753fn method_resolution_trait_before_autoref() {
754 let t = type_at(
755 r#"
756//- /main.rs
757trait Trait { fn foo(self) -> u128; }
758struct S;
759impl S { fn foo(&self) -> i8 { 0 } }
760impl Trait for S { fn foo(self) -> u128 { 0 } }
761fn test() { S.foo()<|>; }
762"#,
763 );
764 assert_eq!(t, "u128");
765}
766
767#[test]
768fn method_resolution_by_value_before_autoref() {
769 let t = type_at(
770 r#"
771//- /main.rs
772trait Clone { fn clone(&self) -> Self; }
773struct S;
774impl Clone for S {}
775impl Clone for &S {}
776fn test() { (S.clone(), (&S).clone(), (&&S).clone())<|>; }
777"#,
778 );
779 assert_eq!(t, "(S, S, &S)");
780}
781
782#[test]
783fn method_resolution_trait_before_autoderef() {
784 let t = type_at(
785 r#"
786//- /main.rs
787trait Trait { fn foo(self) -> u128; }
788struct S;
789impl S { fn foo(self) -> i8 { 0 } }
790impl Trait for &S { fn foo(self) -> u128 { 0 } }
791fn test() { (&S).foo()<|>; }
792"#,
793 );
794 assert_eq!(t, "u128");
795}
796
797#[test]
798fn method_resolution_impl_before_trait() {
799 let t = type_at(
800 r#"
801//- /main.rs
802trait Trait { fn foo(self) -> u128; }
803struct S;
804impl S { fn foo(self) -> i8 { 0 } }
805impl Trait for S { fn foo(self) -> u128 { 0 } }
806fn test() { S.foo()<|>; }
807"#,
808 );
809 assert_eq!(t, "i8");
810}
811
812#[test]
813fn method_resolution_impl_ref_before_trait() {
814 let t = type_at(
815 r#"
816//- /main.rs
817trait Trait { fn foo(self) -> u128; }
818struct S;
819impl S { fn foo(&self) -> i8 { 0 } }
820impl Trait for &S { fn foo(self) -> u128 { 0 } }
821fn test() { S.foo()<|>; }
822"#,
823 );
824 assert_eq!(t, "i8");
825}
826
827#[test]
828fn method_resolution_trait_autoderef() {
829 let t = type_at(
830 r#"
831//- /main.rs
832trait Trait { fn foo(self) -> u128; }
833struct S;
834impl Trait for S { fn foo(self) -> u128 { 0 } }
835fn test() { (&S).foo()<|>; }
836"#,
837 );
838 assert_eq!(t, "u128");
839}
840
841#[test]
842fn method_resolution_trait_from_prelude() {
843 let (db, pos) = TestDB::with_position(
844 r#"
845//- /main.rs crate:main deps:other_crate
846struct S;
847impl Clone for S {}
848
849fn test() {
850 S.clone()<|>;
851}
852
853//- /lib.rs crate:other_crate
854#[prelude_import] use foo::*;
855
856mod foo {
857 trait Clone {
858 fn clone(&self) -> Self;
859 }
860}
861"#,
862 );
863 assert_eq!("S", type_at_pos(&db, pos));
864}
865
866#[test]
867fn method_resolution_where_clause_for_unknown_trait() {
868 // The blanket impl shouldn't apply because we can't even resolve UnknownTrait
869 let t = type_at(
870 r#"
871//- /main.rs
872trait Trait { fn foo(self) -> u128; }
873struct S;
874impl<T> Trait for T where T: UnknownTrait {}
875fn test() { (&S).foo()<|>; }
876"#,
877 );
878 assert_eq!(t, "{unknown}");
879}
880
881#[test]
882fn method_resolution_where_clause_not_met() {
883 // The blanket impl shouldn't apply because we can't prove S: Clone
884 let t = type_at(
885 r#"
886//- /main.rs
887trait Clone {}
888trait Trait { fn foo(self) -> u128; }
889struct S;
890impl<T> Trait for T where T: Clone {}
891fn test() { (&S).foo()<|>; }
892"#,
893 );
894 // This is also to make sure that we don't resolve to the foo method just
895 // because that's the only method named foo we can find, which would make
896 // the below tests not work
897 assert_eq!(t, "{unknown}");
898}
899
900#[test]
901fn method_resolution_where_clause_inline_not_met() {
902 // The blanket impl shouldn't apply because we can't prove S: Clone
903 let t = type_at(
904 r#"
905//- /main.rs
906trait Clone {}
907trait Trait { fn foo(self) -> u128; }
908struct S;
909impl<T: Clone> Trait for T {}
910fn test() { (&S).foo()<|>; }
911"#,
912 );
913 assert_eq!(t, "{unknown}");
914}
915
916#[test]
917fn method_resolution_where_clause_1() {
918 let t = type_at(
919 r#"
920//- /main.rs
921trait Clone {}
922trait Trait { fn foo(self) -> u128; }
923struct S;
924impl Clone for S {}
925impl<T> Trait for T where T: Clone {}
926fn test() { S.foo()<|>; }
927"#,
928 );
929 assert_eq!(t, "u128");
930}
931
932#[test]
933fn method_resolution_where_clause_2() {
934 let t = type_at(
935 r#"
936//- /main.rs
937trait Into<T> { fn into(self) -> T; }
938trait From<T> { fn from(other: T) -> Self; }
939struct S1;
940struct S2;
941impl From<S2> for S1 {}
942impl<T, U> Into<U> for T where U: From<T> {}
943fn test() { S2.into()<|>; }
944"#,
945 );
946 assert_eq!(t, "{unknown}");
947}
948
949#[test]
950fn method_resolution_where_clause_inline() {
951 let t = type_at(
952 r#"
953//- /main.rs
954trait Into<T> { fn into(self) -> T; }
955trait From<T> { fn from(other: T) -> Self; }
956struct S1;
957struct S2;
958impl From<S2> for S1 {}
959impl<T, U: From<T>> Into<U> for T {}
960fn test() { S2.into()<|>; }
961"#,
962 );
963 assert_eq!(t, "{unknown}");
964}
965
966#[test]
967fn method_resolution_encountering_fn_type() {
968 type_at(
969 r#"
970//- /main.rs
971fn foo() {}
972trait FnOnce { fn call(self); }
973fn test() { foo.call()<|>; }
974"#,
975 );
976}
977
978#[test]
979fn method_resolution_slow() {
980 // this can get quite slow if we set the solver size limit too high
981 let t = type_at(
982 r#"
983//- /main.rs
984trait SendX {}
985
986struct S1; impl SendX for S1 {}
987struct S2; impl SendX for S2 {}
988struct U1;
989
990trait Trait { fn method(self); }
991
992struct X1<A, B> {}
993impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
994
995struct S<B, C> {}
996
997trait FnX {}
998
999impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
1000
1001fn test() { (S {}).method()<|>; }
1002"#,
1003 );
1004 assert_eq!(t, "()");
1005}
diff --git a/crates/ra_hir_ty/src/tests/patterns.rs b/crates/ra_hir_ty/src/tests/patterns.rs
new file mode 100644
index 000000000..cb3890b42
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/patterns.rs
@@ -0,0 +1,238 @@
1use super::infer;
2use insta::assert_snapshot;
3use test_utils::covers;
4
5#[test]
6fn infer_pattern() {
7 assert_snapshot!(
8 infer(r#"
9fn test(x: &i32) {
10 let y = x;
11 let &z = x;
12 let a = z;
13 let (c, d) = (1, "hello");
14
15 for (e, f) in some_iter {
16 let g = e;
17 }
18
19 if let [val] = opt {
20 let h = val;
21 }
22
23 let lambda = |a: u64, b, c: i32| { a + b; c };
24
25 let ref ref_to_x = x;
26 let mut mut_x = x;
27 let ref mut mut_ref_to_x = x;
28 let k = mut_ref_to_x;
29}
30"#),
31 @r###"
32 [9; 10) 'x': &i32
33 [18; 369) '{ ...o_x; }': ()
34 [28; 29) 'y': &i32
35 [32; 33) 'x': &i32
36 [43; 45) '&z': &i32
37 [44; 45) 'z': i32
38 [48; 49) 'x': &i32
39 [59; 60) 'a': i32
40 [63; 64) 'z': i32
41 [74; 80) '(c, d)': (i32, &str)
42 [75; 76) 'c': i32
43 [78; 79) 'd': &str
44 [83; 95) '(1, "hello")': (i32, &str)
45 [84; 85) '1': i32
46 [87; 94) '"hello"': &str
47 [102; 152) 'for (e... }': ()
48 [106; 112) '(e, f)': ({unknown}, {unknown})
49 [107; 108) 'e': {unknown}
50 [110; 111) 'f': {unknown}
51 [116; 125) 'some_iter': {unknown}
52 [126; 152) '{ ... }': ()
53 [140; 141) 'g': {unknown}
54 [144; 145) 'e': {unknown}
55 [158; 205) 'if let... }': ()
56 [165; 170) '[val]': {unknown}
57 [173; 176) 'opt': {unknown}
58 [177; 205) '{ ... }': ()
59 [191; 192) 'h': {unknown}
60 [195; 198) 'val': {unknown}
61 [215; 221) 'lambda': |u64, u64, i32| -> i32
62 [224; 256) '|a: u6...b; c }': |u64, u64, i32| -> i32
63 [225; 226) 'a': u64
64 [233; 234) 'b': u64
65 [236; 237) 'c': i32
66 [244; 256) '{ a + b; c }': i32
67 [246; 247) 'a': u64
68 [246; 251) 'a + b': u64
69 [250; 251) 'b': u64
70 [253; 254) 'c': i32
71 [267; 279) 'ref ref_to_x': &&i32
72 [282; 283) 'x': &i32
73 [293; 302) 'mut mut_x': &i32
74 [305; 306) 'x': &i32
75 [316; 336) 'ref mu...f_to_x': &mut &i32
76 [339; 340) 'x': &i32
77 [350; 351) 'k': &mut &i32
78 [354; 366) 'mut_ref_to_x': &mut &i32
79 "###
80 );
81}
82
83#[test]
84fn infer_pattern_match_ergonomics() {
85 assert_snapshot!(
86 infer(r#"
87struct A<T>(T);
88
89fn test() {
90 let A(n) = &A(1);
91 let A(n) = &mut A(1);
92}
93"#),
94 @r###"
95 [28; 79) '{ ...(1); }': ()
96 [38; 42) 'A(n)': A<i32>
97 [40; 41) 'n': &i32
98 [45; 50) '&A(1)': &A<i32>
99 [46; 47) 'A': A<i32>(T) -> A<T>
100 [46; 50) 'A(1)': A<i32>
101 [48; 49) '1': i32
102 [60; 64) 'A(n)': A<i32>
103 [62; 63) 'n': &mut i32
104 [67; 76) '&mut A(1)': &mut A<i32>
105 [72; 73) 'A': A<i32>(T) -> A<T>
106 [72; 76) 'A(1)': A<i32>
107 [74; 75) '1': i32
108 "###
109 );
110}
111
112#[test]
113fn infer_pattern_match_ergonomics_ref() {
114 covers!(match_ergonomics_ref);
115 assert_snapshot!(
116 infer(r#"
117fn test() {
118 let v = &(1, &2);
119 let (_, &w) = v;
120}
121"#),
122 @r###"
123 [11; 57) '{ ...= v; }': ()
124 [21; 22) 'v': &(i32, &i32)
125 [25; 33) '&(1, &2)': &(i32, &i32)
126 [26; 33) '(1, &2)': (i32, &i32)
127 [27; 28) '1': i32
128 [30; 32) '&2': &i32
129 [31; 32) '2': i32
130 [43; 50) '(_, &w)': (i32, &i32)
131 [44; 45) '_': i32
132 [47; 49) '&w': &i32
133 [48; 49) 'w': i32
134 [53; 54) 'v': &(i32, &i32)
135 "###
136 );
137}
138
139#[test]
140fn infer_adt_pattern() {
141 assert_snapshot!(
142 infer(r#"
143enum E {
144 A { x: usize },
145 B
146}
147
148struct S(u32, E);
149
150fn test() {
151 let e = E::A { x: 3 };
152
153 let S(y, z) = foo;
154 let E::A { x: new_var } = e;
155
156 match e {
157 E::A { x } => x,
158 E::B if foo => 1,
159 E::B => 10,
160 };
161
162 let ref d @ E::A { .. } = e;
163 d;
164}
165"#),
166 @r###"
167 [68; 289) '{ ... d; }': ()
168 [78; 79) 'e': E
169 [82; 95) 'E::A { x: 3 }': E
170 [92; 93) '3': usize
171 [106; 113) 'S(y, z)': S
172 [108; 109) 'y': u32
173 [111; 112) 'z': E
174 [116; 119) 'foo': S
175 [129; 148) 'E::A {..._var }': E
176 [139; 146) 'new_var': usize
177 [151; 152) 'e': E
178 [159; 245) 'match ... }': usize
179 [165; 166) 'e': E
180 [177; 187) 'E::A { x }': E
181 [184; 185) 'x': usize
182 [191; 192) 'x': usize
183 [202; 206) 'E::B': E
184 [210; 213) 'foo': bool
185 [217; 218) '1': usize
186 [228; 232) 'E::B': E
187 [236; 238) '10': usize
188 [256; 275) 'ref d ...{ .. }': &E
189 [264; 275) 'E::A { .. }': E
190 [278; 279) 'e': E
191 [285; 286) 'd': &E
192 "###
193 );
194}
195
196#[test]
197fn infer_generics_in_patterns() {
198 assert_snapshot!(
199 infer(r#"
200struct A<T> {
201 x: T,
202}
203
204enum Option<T> {
205 Some(T),
206 None,
207}
208
209fn test(a1: A<u32>, o: Option<u64>) {
210 let A { x: x2 } = a1;
211 let A::<i64> { x: x3 } = A { x: 1 };
212 match o {
213 Option::Some(t) => t,
214 _ => 1,
215 };
216}
217"#),
218 @r###"
219 [79; 81) 'a1': A<u32>
220 [91; 92) 'o': Option<u64>
221 [107; 244) '{ ... }; }': ()
222 [117; 128) 'A { x: x2 }': A<u32>
223 [124; 126) 'x2': u32
224 [131; 133) 'a1': A<u32>
225 [143; 161) 'A::<i6...: x3 }': A<i64>
226 [157; 159) 'x3': i64
227 [164; 174) 'A { x: 1 }': A<i64>
228 [171; 172) '1': i64
229 [180; 241) 'match ... }': u64
230 [186; 187) 'o': Option<u64>
231 [198; 213) 'Option::Some(t)': Option<u64>
232 [211; 212) 't': u64
233 [217; 218) 't': u64
234 [228; 229) '_': Option<u64>
235 [233; 234) '1': u64
236 "###
237 );
238}
diff --git a/crates/ra_hir_ty/src/tests/regression.rs b/crates/ra_hir_ty/src/tests/regression.rs
new file mode 100644
index 000000000..09d684ac2
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/regression.rs
@@ -0,0 +1,333 @@
1use super::infer;
2use insta::assert_snapshot;
3use test_utils::covers;
4
5#[test]
6fn bug_484() {
7 assert_snapshot!(
8 infer(r#"
9fn test() {
10 let x = if true {};
11}
12"#),
13 @r###"
14 [11; 37) '{ l... {}; }': ()
15 [20; 21) 'x': ()
16 [24; 34) 'if true {}': ()
17 [27; 31) 'true': bool
18 [32; 34) '{}': ()
19 "###
20 );
21}
22
23#[test]
24fn no_panic_on_field_of_enum() {
25 assert_snapshot!(
26 infer(r#"
27enum X {}
28
29fn test(x: X) {
30 x.some_field;
31}
32"#),
33 @r###"
34 [20; 21) 'x': X
35 [26; 47) '{ ...eld; }': ()
36 [32; 33) 'x': X
37 [32; 44) 'x.some_field': {unknown}
38 "###
39 );
40}
41
42#[test]
43fn bug_585() {
44 assert_snapshot!(
45 infer(r#"
46fn test() {
47 X {};
48 match x {
49 A::B {} => (),
50 A::Y() => (),
51 }
52}
53"#),
54 @r###"
55 [11; 89) '{ ... } }': ()
56 [17; 21) 'X {}': {unknown}
57 [27; 87) 'match ... }': ()
58 [33; 34) 'x': {unknown}
59 [45; 52) 'A::B {}': {unknown}
60 [56; 58) '()': ()
61 [68; 74) 'A::Y()': {unknown}
62 [78; 80) '()': ()
63 "###
64 );
65}
66
67#[test]
68fn bug_651() {
69 assert_snapshot!(
70 infer(r#"
71fn quux() {
72 let y = 92;
73 1 + y;
74}
75"#),
76 @r###"
77 [11; 41) '{ ...+ y; }': ()
78 [21; 22) 'y': i32
79 [25; 27) '92': i32
80 [33; 34) '1': i32
81 [33; 38) '1 + y': i32
82 [37; 38) 'y': i32
83 "###
84 );
85}
86
87#[test]
88fn recursive_vars() {
89 covers!(type_var_cycles_resolve_completely);
90 covers!(type_var_cycles_resolve_as_possible);
91 assert_snapshot!(
92 infer(r#"
93fn test() {
94 let y = unknown;
95 [y, &y];
96}
97"#),
98 @r###"
99 [11; 48) '{ ...&y]; }': ()
100 [21; 22) 'y': &{unknown}
101 [25; 32) 'unknown': &{unknown}
102 [38; 45) '[y, &y]': [&&{unknown};_]
103 [39; 40) 'y': &{unknown}
104 [42; 44) '&y': &&{unknown}
105 [43; 44) 'y': &{unknown}
106 "###
107 );
108}
109
110#[test]
111fn recursive_vars_2() {
112 covers!(type_var_cycles_resolve_completely);
113 covers!(type_var_cycles_resolve_as_possible);
114 assert_snapshot!(
115 infer(r#"
116fn test() {
117 let x = unknown;
118 let y = unknown;
119 [(x, y), (&y, &x)];
120}
121"#),
122 @r###"
123 [11; 80) '{ ...x)]; }': ()
124 [21; 22) 'x': &&{unknown}
125 [25; 32) 'unknown': &&{unknown}
126 [42; 43) 'y': &&{unknown}
127 [46; 53) 'unknown': &&{unknown}
128 [59; 77) '[(x, y..., &x)]': [(&&&{unknown}, &&&{unknown});_]
129 [60; 66) '(x, y)': (&&&{unknown}, &&&{unknown})
130 [61; 62) 'x': &&{unknown}
131 [64; 65) 'y': &&{unknown}
132 [68; 76) '(&y, &x)': (&&&{unknown}, &&&{unknown})
133 [69; 71) '&y': &&&{unknown}
134 [70; 71) 'y': &&{unknown}
135 [73; 75) '&x': &&&{unknown}
136 [74; 75) 'x': &&{unknown}
137 "###
138 );
139}
140
141#[test]
142fn infer_std_crash_1() {
143 // caused stack overflow, taken from std
144 assert_snapshot!(
145 infer(r#"
146enum Maybe<T> {
147 Real(T),
148 Fake,
149}
150
151fn write() {
152 match something_unknown {
153 Maybe::Real(ref mut something) => (),
154 }
155}
156"#),
157 @r###"
158 [54; 139) '{ ... } }': ()
159 [60; 137) 'match ... }': ()
160 [66; 83) 'someth...nknown': Maybe<{unknown}>
161 [94; 124) 'Maybe:...thing)': Maybe<{unknown}>
162 [106; 123) 'ref mu...ething': &mut {unknown}
163 [128; 130) '()': ()
164 "###
165 );
166}
167
168#[test]
169fn infer_std_crash_2() {
170 covers!(type_var_resolves_to_int_var);
171 // caused "equating two type variables, ...", taken from std
172 assert_snapshot!(
173 infer(r#"
174fn test_line_buffer() {
175 &[0, b'\n', 1, b'\n'];
176}
177"#),
178 @r###"
179 [23; 53) '{ ...n']; }': ()
180 [29; 50) '&[0, b...b'\n']': &[u8;_]
181 [30; 50) '[0, b'...b'\n']': [u8;_]
182 [31; 32) '0': u8
183 [34; 39) 'b'\n'': u8
184 [41; 42) '1': u8
185 [44; 49) 'b'\n'': u8
186 "###
187 );
188}
189
190#[test]
191fn infer_std_crash_3() {
192 // taken from rustc
193 assert_snapshot!(
194 infer(r#"
195pub fn compute() {
196 match nope!() {
197 SizeSkeleton::Pointer { non_zero: true, tail } => {}
198 }
199}
200"#),
201 @r###"
202 [18; 108) '{ ... } }': ()
203 [24; 106) 'match ... }': ()
204 [30; 37) 'nope!()': {unknown}
205 [48; 94) 'SizeSk...tail }': {unknown}
206 [82; 86) 'true': {unknown}
207 [88; 92) 'tail': {unknown}
208 [98; 100) '{}': ()
209 "###
210 );
211}
212
213#[test]
214fn infer_std_crash_4() {
215 // taken from rustc
216 assert_snapshot!(
217 infer(r#"
218pub fn primitive_type() {
219 match *self {
220 BorrowedRef { type_: Primitive(p), ..} => {},
221 }
222}
223"#),
224 @r###"
225 [25; 106) '{ ... } }': ()
226 [31; 104) 'match ... }': ()
227 [37; 42) '*self': {unknown}
228 [38; 42) 'self': {unknown}
229 [53; 91) 'Borrow...), ..}': {unknown}
230 [74; 86) 'Primitive(p)': {unknown}
231 [84; 85) 'p': {unknown}
232 [95; 97) '{}': ()
233 "###
234 );
235}
236
237#[test]
238fn infer_std_crash_5() {
239 // taken from rustc
240 assert_snapshot!(
241 infer(r#"
242fn extra_compiler_flags() {
243 for content in doesnt_matter {
244 let name = if doesnt_matter {
245 first
246 } else {
247 &content
248 };
249
250 let content = if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&name) {
251 name
252 } else {
253 content
254 };
255 }
256}
257"#),
258 @r###"
259 [27; 323) '{ ... } }': ()
260 [33; 321) 'for co... }': ()
261 [37; 44) 'content': &{unknown}
262 [48; 61) 'doesnt_matter': {unknown}
263 [62; 321) '{ ... }': ()
264 [76; 80) 'name': &&{unknown}
265 [83; 167) 'if doe... }': &&{unknown}
266 [86; 99) 'doesnt_matter': bool
267 [100; 129) '{ ... }': &&{unknown}
268 [114; 119) 'first': &&{unknown}
269 [135; 167) '{ ... }': &&{unknown}
270 [149; 157) '&content': &&{unknown}
271 [150; 157) 'content': &{unknown}
272 [182; 189) 'content': &{unknown}
273 [192; 314) 'if ICE... }': &{unknown}
274 [195; 232) 'ICE_RE..._VALUE': {unknown}
275 [195; 248) 'ICE_RE...&name)': bool
276 [242; 247) '&name': &&&{unknown}
277 [243; 247) 'name': &&{unknown}
278 [249; 277) '{ ... }': &&{unknown}
279 [263; 267) 'name': &&{unknown}
280 [283; 314) '{ ... }': &{unknown}
281 [297; 304) 'content': &{unknown}
282 "###
283 );
284}
285
286#[test]
287fn infer_nested_generics_crash() {
288 // another crash found typechecking rustc
289 assert_snapshot!(
290 infer(r#"
291struct Canonical<V> {
292 value: V,
293}
294struct QueryResponse<V> {
295 value: V,
296}
297fn test<R>(query_response: Canonical<QueryResponse<R>>) {
298 &query_response.value;
299}
300"#),
301 @r###"
302 [92; 106) 'query_response': Canonical<QueryResponse<R>>
303 [137; 167) '{ ...lue; }': ()
304 [143; 164) '&query....value': &QueryResponse<R>
305 [144; 158) 'query_response': Canonical<QueryResponse<R>>
306 [144; 164) 'query_....value': QueryResponse<R>
307 "###
308 );
309}
310
311#[test]
312fn bug_1030() {
313 assert_snapshot!(infer(r#"
314struct HashSet<T, H>;
315struct FxHasher;
316type FxHashSet<T> = HashSet<T, FxHasher>;
317
318impl<T, H> HashSet<T, H> {
319 fn default() -> HashSet<T, H> {}
320}
321
322pub fn main_loop() {
323 FxHashSet::default();
324}
325"#),
326 @r###"
327 [144; 146) '{}': ()
328 [169; 198) '{ ...t(); }': ()
329 [175; 193) 'FxHash...efault': fn default<{unknown}, FxHasher>() -> HashSet<T, H>
330 [175; 195) 'FxHash...ault()': HashSet<{unknown}, FxHasher>
331 "###
332 );
333}
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
new file mode 100644
index 000000000..18976c9ae
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -0,0 +1,1608 @@
1use super::{infer, type_at, type_at_pos};
2use crate::test_db::TestDB;
3use insta::assert_snapshot;
4use ra_db::fixture::WithFixture;
5
6#[test]
7fn infer_box() {
8 let (db, pos) = TestDB::with_position(
9 r#"
10//- /main.rs crate:main deps:std
11
12fn test() {
13 let x = box 1;
14 let t = (x, box x, box &1, box [1]);
15 t<|>;
16}
17
18//- /std.rs crate:std
19#[prelude_import] use prelude::*;
20mod prelude {}
21
22mod boxed {
23 pub struct Box<T: ?Sized> {
24 inner: *mut T,
25 }
26}
27
28"#,
29 );
30 assert_eq!("(Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32;_]>)", type_at_pos(&db, pos));
31}
32
33#[test]
34fn infer_adt_self() {
35 let (db, pos) = TestDB::with_position(
36 r#"
37//- /main.rs
38enum Nat { Succ(Self), Demo(Nat), Zero }
39
40fn test() {
41 let foo: Nat = Nat::Zero;
42 if let Nat::Succ(x) = foo {
43 x<|>
44 }
45}
46
47"#,
48 );
49 assert_eq!("Nat", type_at_pos(&db, pos));
50}
51
52#[test]
53fn infer_ranges() {
54 let (db, pos) = TestDB::with_position(
55 r#"
56//- /main.rs crate:main deps:std
57fn test() {
58 let a = ..;
59 let b = 1..;
60 let c = ..2u32;
61 let d = 1..2usize;
62 let e = ..=10;
63 let f = 'a'..='z';
64
65 let t = (a, b, c, d, e, f);
66 t<|>;
67}
68
69//- /std.rs crate:std
70#[prelude_import] use prelude::*;
71mod prelude {}
72
73pub mod ops {
74 pub struct Range<Idx> {
75 pub start: Idx,
76 pub end: Idx,
77 }
78 pub struct RangeFrom<Idx> {
79 pub start: Idx,
80 }
81 struct RangeFull;
82 pub struct RangeInclusive<Idx> {
83 start: Idx,
84 end: Idx,
85 is_empty: u8,
86 }
87 pub struct RangeTo<Idx> {
88 pub end: Idx,
89 }
90 pub struct RangeToInclusive<Idx> {
91 pub end: Idx,
92 }
93}
94"#,
95 );
96 assert_eq!(
97 "(RangeFull, RangeFrom<i32>, RangeTo<u32>, Range<usize>, RangeToInclusive<i32>, RangeInclusive<char>)",
98 type_at_pos(&db, pos),
99 );
100}
101
102#[test]
103fn infer_while_let() {
104 let (db, pos) = TestDB::with_position(
105 r#"
106//- /main.rs
107enum Option<T> { Some(T), None }
108
109fn test() {
110 let foo: Option<f32> = None;
111 while let Option::Some(x) = foo {
112 <|>x
113 }
114}
115
116"#,
117 );
118 assert_eq!("f32", type_at_pos(&db, pos));
119}
120
121#[test]
122fn infer_basics() {
123 assert_snapshot!(
124 infer(r#"
125fn test(a: u32, b: isize, c: !, d: &str) {
126 a;
127 b;
128 c;
129 d;
130 1usize;
131 1isize;
132 "test";
133 1.0f32;
134}"#),
135 @r###"
136 [9; 10) 'a': u32
137 [17; 18) 'b': isize
138 [27; 28) 'c': !
139 [33; 34) 'd': &str
140 [42; 121) '{ ...f32; }': !
141 [48; 49) 'a': u32
142 [55; 56) 'b': isize
143 [62; 63) 'c': !
144 [69; 70) 'd': &str
145 [76; 82) '1usize': usize
146 [88; 94) '1isize': isize
147 [100; 106) '"test"': &str
148 [112; 118) '1.0f32': f32
149 "###
150 );
151}
152
153#[test]
154fn infer_let() {
155 assert_snapshot!(
156 infer(r#"
157fn test() {
158 let a = 1isize;
159 let b: usize = 1;
160 let c = b;
161 let d: u32;
162 let e;
163 let f: i32 = e;
164}
165"#),
166 @r###"
167 [11; 118) '{ ...= e; }': ()
168 [21; 22) 'a': isize
169 [25; 31) '1isize': isize
170 [41; 42) 'b': usize
171 [52; 53) '1': usize
172 [63; 64) 'c': usize
173 [67; 68) 'b': usize
174 [78; 79) 'd': u32
175 [94; 95) 'e': i32
176 [105; 106) 'f': i32
177 [114; 115) 'e': i32
178 "###
179 );
180}
181
182#[test]
183fn infer_paths() {
184 assert_snapshot!(
185 infer(r#"
186fn a() -> u32 { 1 }
187
188mod b {
189 fn c() -> u32 { 1 }
190}
191
192fn test() {
193 a();
194 b::c();
195}
196"#),
197 @r###"
198 [15; 20) '{ 1 }': u32
199 [17; 18) '1': u32
200 [48; 53) '{ 1 }': u32
201 [50; 51) '1': u32
202 [67; 91) '{ ...c(); }': ()
203 [73; 74) 'a': fn a() -> u32
204 [73; 76) 'a()': u32
205 [82; 86) 'b::c': fn c() -> u32
206 [82; 88) 'b::c()': u32
207 "###
208 );
209}
210
211#[test]
212fn infer_path_type() {
213 assert_snapshot!(
214 infer(r#"
215struct S;
216
217impl S {
218 fn foo() -> i32 { 1 }
219}
220
221fn test() {
222 S::foo();
223 <S>::foo();
224}
225"#),
226 @r###"
227 [41; 46) '{ 1 }': i32
228 [43; 44) '1': i32
229 [60; 93) '{ ...o(); }': ()
230 [66; 72) 'S::foo': fn foo() -> i32
231 [66; 74) 'S::foo()': i32
232 [80; 88) '<S>::foo': fn foo() -> i32
233 [80; 90) '<S>::foo()': i32
234 "###
235 );
236}
237
238#[test]
239fn infer_struct() {
240 assert_snapshot!(
241 infer(r#"
242struct A {
243 b: B,
244 c: C,
245}
246struct B;
247struct C(usize);
248
249fn test() {
250 let c = C(1);
251 B;
252 let a: A = A { b: B, c: C(1) };
253 a.b;
254 a.c;
255}
256"#),
257 @r###"
258 [72; 154) '{ ...a.c; }': ()
259 [82; 83) 'c': C
260 [86; 87) 'C': C(usize) -> C
261 [86; 90) 'C(1)': C
262 [88; 89) '1': usize
263 [96; 97) 'B': B
264 [107; 108) 'a': A
265 [114; 133) 'A { b:...C(1) }': A
266 [121; 122) 'B': B
267 [127; 128) 'C': C(usize) -> C
268 [127; 131) 'C(1)': C
269 [129; 130) '1': usize
270 [139; 140) 'a': A
271 [139; 142) 'a.b': B
272 [148; 149) 'a': A
273 [148; 151) 'a.c': C
274 "###
275 );
276}
277
278#[test]
279fn infer_enum() {
280 assert_snapshot!(
281 infer(r#"
282enum E {
283 V1 { field: u32 },
284 V2
285}
286fn test() {
287 E::V1 { field: 1 };
288 E::V2;
289}"#),
290 @r###"
291 [48; 82) '{ E:...:V2; }': ()
292 [52; 70) 'E::V1 ...d: 1 }': E
293 [67; 68) '1': u32
294 [74; 79) 'E::V2': E
295 "###
296 );
297}
298
299#[test]
300fn infer_refs() {
301 assert_snapshot!(
302 infer(r#"
303fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
304 a;
305 *a;
306 &a;
307 &mut a;
308 b;
309 *b;
310 &b;
311 c;
312 *c;
313 d;
314 *d;
315}
316"#),
317 @r###"
318 [9; 10) 'a': &u32
319 [18; 19) 'b': &mut u32
320 [31; 32) 'c': *const u32
321 [46; 47) 'd': *mut u32
322 [59; 150) '{ ... *d; }': ()
323 [65; 66) 'a': &u32
324 [72; 74) '*a': u32
325 [73; 74) 'a': &u32
326 [80; 82) '&a': &&u32
327 [81; 82) 'a': &u32
328 [88; 94) '&mut a': &mut &u32
329 [93; 94) 'a': &u32
330 [100; 101) 'b': &mut u32
331 [107; 109) '*b': u32
332 [108; 109) 'b': &mut u32
333 [115; 117) '&b': &&mut u32
334 [116; 117) 'b': &mut u32
335 [123; 124) 'c': *const u32
336 [130; 132) '*c': u32
337 [131; 132) 'c': *const u32
338 [138; 139) 'd': *mut u32
339 [145; 147) '*d': u32
340 [146; 147) 'd': *mut u32
341 "###
342 );
343}
344
345#[test]
346fn infer_literals() {
347 assert_snapshot!(
348 infer(r##"
349fn test() {
350 5i32;
351 5f32;
352 5f64;
353 "hello";
354 b"bytes";
355 'c';
356 b'b';
357 3.14;
358 5000;
359 false;
360 true;
361 r#"
362 //! doc
363 // non-doc
364 mod foo {}
365 "#;
366 br#"yolo"#;
367}
368"##),
369 @r###"
370 [11; 221) '{ ...o"#; }': ()
371 [17; 21) '5i32': i32
372 [27; 31) '5f32': f32
373 [37; 41) '5f64': f64
374 [47; 54) '"hello"': &str
375 [60; 68) 'b"bytes"': &[u8]
376 [74; 77) ''c'': char
377 [83; 87) 'b'b'': u8
378 [93; 97) '3.14': f64
379 [103; 107) '5000': i32
380 [113; 118) 'false': bool
381 [124; 128) 'true': bool
382 [134; 202) 'r#" ... "#': &str
383 [208; 218) 'br#"yolo"#': &[u8]
384 "###
385 );
386}
387
388#[test]
389fn infer_unary_op() {
390 assert_snapshot!(
391 infer(r#"
392enum SomeType {}
393
394fn test(x: SomeType) {
395 let b = false;
396 let c = !b;
397 let a = 100;
398 let d: i128 = -a;
399 let e = -100;
400 let f = !!!true;
401 let g = !42;
402 let h = !10u32;
403 let j = !a;
404 -3.14;
405 !3;
406 -x;
407 !x;
408 -"hello";
409 !"hello";
410}
411"#),
412 @r###"
413 [27; 28) 'x': SomeType
414 [40; 272) '{ ...lo"; }': ()
415 [50; 51) 'b': bool
416 [54; 59) 'false': bool
417 [69; 70) 'c': bool
418 [73; 75) '!b': bool
419 [74; 75) 'b': bool
420 [85; 86) 'a': i128
421 [89; 92) '100': i128
422 [102; 103) 'd': i128
423 [112; 114) '-a': i128
424 [113; 114) 'a': i128
425 [124; 125) 'e': i32
426 [128; 132) '-100': i32
427 [129; 132) '100': i32
428 [142; 143) 'f': bool
429 [146; 153) '!!!true': bool
430 [147; 153) '!!true': bool
431 [148; 153) '!true': bool
432 [149; 153) 'true': bool
433 [163; 164) 'g': i32
434 [167; 170) '!42': i32
435 [168; 170) '42': i32
436 [180; 181) 'h': u32
437 [184; 190) '!10u32': u32
438 [185; 190) '10u32': u32
439 [200; 201) 'j': i128
440 [204; 206) '!a': i128
441 [205; 206) 'a': i128
442 [212; 217) '-3.14': f64
443 [213; 217) '3.14': f64
444 [223; 225) '!3': i32
445 [224; 225) '3': i32
446 [231; 233) '-x': {unknown}
447 [232; 233) 'x': SomeType
448 [239; 241) '!x': {unknown}
449 [240; 241) 'x': SomeType
450 [247; 255) '-"hello"': {unknown}
451 [248; 255) '"hello"': &str
452 [261; 269) '!"hello"': {unknown}
453 [262; 269) '"hello"': &str
454 "###
455 );
456}
457
458#[test]
459fn infer_backwards() {
460 assert_snapshot!(
461 infer(r#"
462fn takes_u32(x: u32) {}
463
464struct S { i32_field: i32 }
465
466fn test() -> &mut &f64 {
467 let a = unknown_function();
468 takes_u32(a);
469 let b = unknown_function();
470 S { i32_field: b };
471 let c = unknown_function();
472 &mut &c
473}
474"#),
475 @r###"
476 [14; 15) 'x': u32
477 [22; 24) '{}': ()
478 [78; 231) '{ ...t &c }': &mut &f64
479 [88; 89) 'a': u32
480 [92; 108) 'unknow...nction': {unknown}
481 [92; 110) 'unknow...tion()': u32
482 [116; 125) 'takes_u32': fn takes_u32(u32) -> ()
483 [116; 128) 'takes_u32(a)': ()
484 [126; 127) 'a': u32
485 [138; 139) 'b': i32
486 [142; 158) 'unknow...nction': {unknown}
487 [142; 160) 'unknow...tion()': i32
488 [166; 184) 'S { i3...d: b }': S
489 [181; 182) 'b': i32
490 [194; 195) 'c': f64
491 [198; 214) 'unknow...nction': {unknown}
492 [198; 216) 'unknow...tion()': f64
493 [222; 229) '&mut &c': &mut &f64
494 [227; 229) '&c': &f64
495 [228; 229) 'c': f64
496 "###
497 );
498}
499
500#[test]
501fn infer_self() {
502 assert_snapshot!(
503 infer(r#"
504struct S;
505
506impl S {
507 fn test(&self) {
508 self;
509 }
510 fn test2(self: &Self) {
511 self;
512 }
513 fn test3() -> Self {
514 S {}
515 }
516 fn test4() -> Self {
517 Self {}
518 }
519}
520"#),
521 @r###"
522 [34; 38) 'self': &S
523 [40; 61) '{ ... }': ()
524 [50; 54) 'self': &S
525 [75; 79) 'self': &S
526 [88; 109) '{ ... }': ()
527 [98; 102) 'self': &S
528 [133; 153) '{ ... }': S
529 [143; 147) 'S {}': S
530 [177; 200) '{ ... }': S
531 [187; 194) 'Self {}': S
532 "###
533 );
534}
535
536#[test]
537fn infer_binary_op() {
538 assert_snapshot!(
539 infer(r#"
540fn f(x: bool) -> i32 {
541 0i32
542}
543
544fn test() -> bool {
545 let x = a && b;
546 let y = true || false;
547 let z = x == y;
548 let t = x != y;
549 let minus_forty: isize = -40isize;
550 let h = minus_forty <= CONST_2;
551 let c = f(z || y) + 5;
552 let d = b;
553 let g = minus_forty ^= i;
554 let ten: usize = 10;
555 let ten_is_eleven = ten == some_num;
556
557 ten < 3
558}
559"#),
560 @r###"
561 [6; 7) 'x': bool
562 [22; 34) '{ 0i32 }': i32
563 [28; 32) '0i32': i32
564 [54; 370) '{ ... < 3 }': bool
565 [64; 65) 'x': bool
566 [68; 69) 'a': bool
567 [68; 74) 'a && b': bool
568 [73; 74) 'b': bool
569 [84; 85) 'y': bool
570 [88; 92) 'true': bool
571 [88; 101) 'true || false': bool
572 [96; 101) 'false': bool
573 [111; 112) 'z': bool
574 [115; 116) 'x': bool
575 [115; 121) 'x == y': bool
576 [120; 121) 'y': bool
577 [131; 132) 't': bool
578 [135; 136) 'x': bool
579 [135; 141) 'x != y': bool
580 [140; 141) 'y': bool
581 [151; 162) 'minus_forty': isize
582 [172; 180) '-40isize': isize
583 [173; 180) '40isize': isize
584 [190; 191) 'h': bool
585 [194; 205) 'minus_forty': isize
586 [194; 216) 'minus_...ONST_2': bool
587 [209; 216) 'CONST_2': isize
588 [226; 227) 'c': i32
589 [230; 231) 'f': fn f(bool) -> i32
590 [230; 239) 'f(z || y)': i32
591 [230; 243) 'f(z || y) + 5': i32
592 [232; 233) 'z': bool
593 [232; 238) 'z || y': bool
594 [237; 238) 'y': bool
595 [242; 243) '5': i32
596 [253; 254) 'd': {unknown}
597 [257; 258) 'b': {unknown}
598 [268; 269) 'g': ()
599 [272; 283) 'minus_forty': isize
600 [272; 288) 'minus_...y ^= i': ()
601 [287; 288) 'i': isize
602 [298; 301) 'ten': usize
603 [311; 313) '10': usize
604 [323; 336) 'ten_is_eleven': bool
605 [339; 342) 'ten': usize
606 [339; 354) 'ten == some_num': bool
607 [346; 354) 'some_num': usize
608 [361; 364) 'ten': usize
609 [361; 368) 'ten < 3': bool
610 [367; 368) '3': usize
611 "###
612 );
613}
614
615#[test]
616fn infer_field_autoderef() {
617 assert_snapshot!(
618 infer(r#"
619struct A {
620 b: B,
621}
622struct B;
623
624fn test1(a: A) {
625 let a1 = a;
626 a1.b;
627 let a2 = &a;
628 a2.b;
629 let a3 = &mut a;
630 a3.b;
631 let a4 = &&&&&&&a;
632 a4.b;
633 let a5 = &mut &&mut &&mut a;
634 a5.b;
635}
636
637fn test2(a1: *const A, a2: *mut A) {
638 a1.b;
639 a2.b;
640}
641"#),
642 @r###"
643 [44; 45) 'a': A
644 [50; 213) '{ ...5.b; }': ()
645 [60; 62) 'a1': A
646 [65; 66) 'a': A
647 [72; 74) 'a1': A
648 [72; 76) 'a1.b': B
649 [86; 88) 'a2': &A
650 [91; 93) '&a': &A
651 [92; 93) 'a': A
652 [99; 101) 'a2': &A
653 [99; 103) 'a2.b': B
654 [113; 115) 'a3': &mut A
655 [118; 124) '&mut a': &mut A
656 [123; 124) 'a': A
657 [130; 132) 'a3': &mut A
658 [130; 134) 'a3.b': B
659 [144; 146) 'a4': &&&&&&&A
660 [149; 157) '&&&&&&&a': &&&&&&&A
661 [150; 157) '&&&&&&a': &&&&&&A
662 [151; 157) '&&&&&a': &&&&&A
663 [152; 157) '&&&&a': &&&&A
664 [153; 157) '&&&a': &&&A
665 [154; 157) '&&a': &&A
666 [155; 157) '&a': &A
667 [156; 157) 'a': A
668 [163; 165) 'a4': &&&&&&&A
669 [163; 167) 'a4.b': B
670 [177; 179) 'a5': &mut &&mut &&mut A
671 [182; 200) '&mut &...&mut a': &mut &&mut &&mut A
672 [187; 200) '&&mut &&mut a': &&mut &&mut A
673 [188; 200) '&mut &&mut a': &mut &&mut A
674 [193; 200) '&&mut a': &&mut A
675 [194; 200) '&mut a': &mut A
676 [199; 200) 'a': A
677 [206; 208) 'a5': &mut &&mut &&mut A
678 [206; 210) 'a5.b': B
679 [224; 226) 'a1': *const A
680 [238; 240) 'a2': *mut A
681 [250; 273) '{ ...2.b; }': ()
682 [256; 258) 'a1': *const A
683 [256; 260) 'a1.b': B
684 [266; 268) 'a2': *mut A
685 [266; 270) 'a2.b': B
686 "###
687 );
688}
689
690#[test]
691fn infer_argument_autoderef() {
692 assert_snapshot!(
693 infer(r#"
694#[lang = "deref"]
695pub trait Deref {
696 type Target;
697 fn deref(&self) -> &Self::Target;
698}
699
700struct A<T>(T);
701
702impl<T> A<T> {
703 fn foo(&self) -> &T {
704 &self.0
705 }
706}
707
708struct B<T>(T);
709
710impl<T> Deref for B<T> {
711 type Target = T;
712 fn deref(&self) -> &Self::Target {
713 &self.0
714 }
715}
716
717fn test() {
718 let t = A::foo(&&B(B(A(42))));
719}
720"#),
721 @r###"
722 [68; 72) 'self': &Self
723 [139; 143) 'self': &A<T>
724 [151; 174) '{ ... }': &T
725 [161; 168) '&self.0': &T
726 [162; 166) 'self': &A<T>
727 [162; 168) 'self.0': T
728 [255; 259) 'self': &B<T>
729 [278; 301) '{ ... }': &T
730 [288; 295) '&self.0': &T
731 [289; 293) 'self': &B<T>
732 [289; 295) 'self.0': T
733 [315; 353) '{ ...))); }': ()
734 [325; 326) 't': &i32
735 [329; 335) 'A::foo': fn foo<i32>(&A<T>) -> &T
736 [329; 350) 'A::foo...42))))': &i32
737 [336; 349) '&&B(B(A(42)))': &&B<B<A<i32>>>
738 [337; 349) '&B(B(A(42)))': &B<B<A<i32>>>
739 [338; 339) 'B': B<B<A<i32>>>(T) -> B<T>
740 [338; 349) 'B(B(A(42)))': B<B<A<i32>>>
741 [340; 341) 'B': B<A<i32>>(T) -> B<T>
742 [340; 348) 'B(A(42))': B<A<i32>>
743 [342; 343) 'A': A<i32>(T) -> A<T>
744 [342; 347) 'A(42)': A<i32>
745 [344; 346) '42': i32
746 "###
747 );
748}
749
750#[test]
751fn infer_method_argument_autoderef() {
752 assert_snapshot!(
753 infer(r#"
754#[lang = "deref"]
755pub trait Deref {
756 type Target;
757 fn deref(&self) -> &Self::Target;
758}
759
760struct A<T>(*mut T);
761
762impl<T> A<T> {
763 fn foo(&self, x: &A<T>) -> &T {
764 &*x.0
765 }
766}
767
768struct B<T>(T);
769
770impl<T> Deref for B<T> {
771 type Target = T;
772 fn deref(&self) -> &Self::Target {
773 &self.0
774 }
775}
776
777fn test(a: A<i32>) {
778 let t = A(0 as *mut _).foo(&&B(B(a)));
779}
780"#),
781 @r###"
782 [68; 72) 'self': &Self
783 [144; 148) 'self': &A<T>
784 [150; 151) 'x': &A<T>
785 [166; 187) '{ ... }': &T
786 [176; 181) '&*x.0': &T
787 [177; 181) '*x.0': T
788 [178; 179) 'x': &A<T>
789 [178; 181) 'x.0': *mut T
790 [268; 272) 'self': &B<T>
791 [291; 314) '{ ... }': &T
792 [301; 308) '&self.0': &T
793 [302; 306) 'self': &B<T>
794 [302; 308) 'self.0': T
795 [326; 327) 'a': A<i32>
796 [337; 383) '{ ...))); }': ()
797 [347; 348) 't': &i32
798 [351; 352) 'A': A<i32>(*mut T) -> A<T>
799 [351; 365) 'A(0 as *mut _)': A<i32>
800 [351; 380) 'A(0 as...B(a)))': &i32
801 [353; 354) '0': i32
802 [353; 364) '0 as *mut _': *mut i32
803 [370; 379) '&&B(B(a))': &&B<B<A<i32>>>
804 [371; 379) '&B(B(a))': &B<B<A<i32>>>
805 [372; 373) 'B': B<B<A<i32>>>(T) -> B<T>
806 [372; 379) 'B(B(a))': B<B<A<i32>>>
807 [374; 375) 'B': B<A<i32>>(T) -> B<T>
808 [374; 378) 'B(a)': B<A<i32>>
809 [376; 377) 'a': A<i32>
810 "###
811 );
812}
813
814#[test]
815fn infer_in_elseif() {
816 assert_snapshot!(
817 infer(r#"
818struct Foo { field: i32 }
819fn main(foo: Foo) {
820 if true {
821
822 } else if false {
823 foo.field
824 }
825}
826"#),
827 @r###"
828 [35; 38) 'foo': Foo
829 [45; 109) '{ ... } }': ()
830 [51; 107) 'if tru... }': ()
831 [54; 58) 'true': bool
832 [59; 67) '{ }': ()
833 [73; 107) 'if fal... }': ()
834 [76; 81) 'false': bool
835 [82; 107) '{ ... }': i32
836 [92; 95) 'foo': Foo
837 [92; 101) 'foo.field': i32
838 "###
839 )
840}
841
842#[test]
843fn infer_if_match_with_return() {
844 assert_snapshot!(
845 infer(r#"
846fn foo() {
847 let _x1 = if true {
848 1
849 } else {
850 return;
851 };
852 let _x2 = if true {
853 2
854 } else {
855 return
856 };
857 let _x3 = match true {
858 true => 3,
859 _ => {
860 return;
861 }
862 };
863 let _x4 = match true {
864 true => 4,
865 _ => return
866 };
867}"#),
868 @r###"
869 [10; 323) '{ ... }; }': ()
870 [20; 23) '_x1': i32
871 [26; 80) 'if tru... }': i32
872 [29; 33) 'true': bool
873 [34; 51) '{ ... }': i32
874 [44; 45) '1': i32
875 [57; 80) '{ ... }': !
876 [67; 73) 'return': !
877 [90; 93) '_x2': i32
878 [96; 149) 'if tru... }': i32
879 [99; 103) 'true': bool
880 [104; 121) '{ ... }': i32
881 [114; 115) '2': i32
882 [127; 149) '{ ... }': !
883 [137; 143) 'return': !
884 [159; 162) '_x3': i32
885 [165; 247) 'match ... }': i32
886 [171; 175) 'true': bool
887 [186; 190) 'true': bool
888 [194; 195) '3': i32
889 [205; 206) '_': bool
890 [210; 241) '{ ... }': !
891 [224; 230) 'return': !
892 [257; 260) '_x4': i32
893 [263; 320) 'match ... }': i32
894 [269; 273) 'true': bool
895 [284; 288) 'true': bool
896 [292; 293) '4': i32
897 [303; 304) '_': bool
898 [308; 314) 'return': !
899 "###
900 )
901}
902
903#[test]
904fn infer_inherent_method() {
905 assert_snapshot!(
906 infer(r#"
907struct A;
908
909impl A {
910 fn foo(self, x: u32) -> i32 {}
911}
912
913mod b {
914 impl super::A {
915 fn bar(&self, x: u64) -> i64 {}
916 }
917}
918
919fn test(a: A) {
920 a.foo(1);
921 (&a).bar(1);
922 a.bar(1);
923}
924"#),
925 @r###"
926 [32; 36) 'self': A
927 [38; 39) 'x': u32
928 [53; 55) '{}': ()
929 [103; 107) 'self': &A
930 [109; 110) 'x': u64
931 [124; 126) '{}': ()
932 [144; 145) 'a': A
933 [150; 198) '{ ...(1); }': ()
934 [156; 157) 'a': A
935 [156; 164) 'a.foo(1)': i32
936 [162; 163) '1': u32
937 [170; 181) '(&a).bar(1)': i64
938 [171; 173) '&a': &A
939 [172; 173) 'a': A
940 [179; 180) '1': u64
941 [187; 188) 'a': A
942 [187; 195) 'a.bar(1)': i64
943 [193; 194) '1': u64
944 "###
945 );
946}
947
948#[test]
949fn infer_inherent_method_str() {
950 assert_snapshot!(
951 infer(r#"
952#[lang = "str"]
953impl str {
954 fn foo(&self) -> i32 {}
955}
956
957fn test() {
958 "foo".foo();
959}
960"#),
961 @r###"
962 [40; 44) 'self': &str
963 [53; 55) '{}': ()
964 [69; 89) '{ ...o(); }': ()
965 [75; 80) '"foo"': &str
966 [75; 86) '"foo".foo()': i32
967 "###
968 );
969}
970
971#[test]
972fn infer_tuple() {
973 assert_snapshot!(
974 infer(r#"
975fn test(x: &str, y: isize) {
976 let a: (u32, &str) = (1, "a");
977 let b = (a, x);
978 let c = (y, x);
979 let d = (c, x);
980 let e = (1, "e");
981 let f = (e, "d");
982}
983"#),
984 @r###"
985 [9; 10) 'x': &str
986 [18; 19) 'y': isize
987 [28; 170) '{ ...d"); }': ()
988 [38; 39) 'a': (u32, &str)
989 [55; 63) '(1, "a")': (u32, &str)
990 [56; 57) '1': u32
991 [59; 62) '"a"': &str
992 [73; 74) 'b': ((u32, &str), &str)
993 [77; 83) '(a, x)': ((u32, &str), &str)
994 [78; 79) 'a': (u32, &str)
995 [81; 82) 'x': &str
996 [93; 94) 'c': (isize, &str)
997 [97; 103) '(y, x)': (isize, &str)
998 [98; 99) 'y': isize
999 [101; 102) 'x': &str
1000 [113; 114) 'd': ((isize, &str), &str)
1001 [117; 123) '(c, x)': ((isize, &str), &str)
1002 [118; 119) 'c': (isize, &str)
1003 [121; 122) 'x': &str
1004 [133; 134) 'e': (i32, &str)
1005 [137; 145) '(1, "e")': (i32, &str)
1006 [138; 139) '1': i32
1007 [141; 144) '"e"': &str
1008 [155; 156) 'f': ((i32, &str), &str)
1009 [159; 167) '(e, "d")': ((i32, &str), &str)
1010 [160; 161) 'e': (i32, &str)
1011 [163; 166) '"d"': &str
1012 "###
1013 );
1014}
1015
1016#[test]
1017fn infer_array() {
1018 assert_snapshot!(
1019 infer(r#"
1020fn test(x: &str, y: isize) {
1021 let a = [x];
1022 let b = [a, a];
1023 let c = [b, b];
1024
1025 let d = [y, 1, 2, 3];
1026 let d = [1, y, 2, 3];
1027 let e = [y];
1028 let f = [d, d];
1029 let g = [e, e];
1030
1031 let h = [1, 2];
1032 let i = ["a", "b"];
1033
1034 let b = [a, ["b"]];
1035 let x: [u8; 0] = [];
1036}
1037"#),
1038 @r###"
1039 [9; 10) 'x': &str
1040 [18; 19) 'y': isize
1041 [28; 293) '{ ... []; }': ()
1042 [38; 39) 'a': [&str;_]
1043 [42; 45) '[x]': [&str;_]
1044 [43; 44) 'x': &str
1045 [55; 56) 'b': [[&str;_];_]
1046 [59; 65) '[a, a]': [[&str;_];_]
1047 [60; 61) 'a': [&str;_]
1048 [63; 64) 'a': [&str;_]
1049 [75; 76) 'c': [[[&str;_];_];_]
1050 [79; 85) '[b, b]': [[[&str;_];_];_]
1051 [80; 81) 'b': [[&str;_];_]
1052 [83; 84) 'b': [[&str;_];_]
1053 [96; 97) 'd': [isize;_]
1054 [100; 112) '[y, 1, 2, 3]': [isize;_]
1055 [101; 102) 'y': isize
1056 [104; 105) '1': isize
1057 [107; 108) '2': isize
1058 [110; 111) '3': isize
1059 [122; 123) 'd': [isize;_]
1060 [126; 138) '[1, y, 2, 3]': [isize;_]
1061 [127; 128) '1': isize
1062 [130; 131) 'y': isize
1063 [133; 134) '2': isize
1064 [136; 137) '3': isize
1065 [148; 149) 'e': [isize;_]
1066 [152; 155) '[y]': [isize;_]
1067 [153; 154) 'y': isize
1068 [165; 166) 'f': [[isize;_];_]
1069 [169; 175) '[d, d]': [[isize;_];_]
1070 [170; 171) 'd': [isize;_]
1071 [173; 174) 'd': [isize;_]
1072 [185; 186) 'g': [[isize;_];_]
1073 [189; 195) '[e, e]': [[isize;_];_]
1074 [190; 191) 'e': [isize;_]
1075 [193; 194) 'e': [isize;_]
1076 [206; 207) 'h': [i32;_]
1077 [210; 216) '[1, 2]': [i32;_]
1078 [211; 212) '1': i32
1079 [214; 215) '2': i32
1080 [226; 227) 'i': [&str;_]
1081 [230; 240) '["a", "b"]': [&str;_]
1082 [231; 234) '"a"': &str
1083 [236; 239) '"b"': &str
1084 [251; 252) 'b': [[&str;_];_]
1085 [255; 265) '[a, ["b"]]': [[&str;_];_]
1086 [256; 257) 'a': [&str;_]
1087 [259; 264) '["b"]': [&str;_]
1088 [260; 263) '"b"': &str
1089 [275; 276) 'x': [u8;_]
1090 [288; 290) '[]': [u8;_]
1091 "###
1092 );
1093}
1094
1095#[test]
1096fn infer_struct_generics() {
1097 assert_snapshot!(
1098 infer(r#"
1099struct A<T> {
1100 x: T,
1101}
1102
1103fn test(a1: A<u32>, i: i32) {
1104 a1.x;
1105 let a2 = A { x: i };
1106 a2.x;
1107 let a3 = A::<i128> { x: 1 };
1108 a3.x;
1109}
1110"#),
1111 @r###"
1112 [36; 38) 'a1': A<u32>
1113 [48; 49) 'i': i32
1114 [56; 147) '{ ...3.x; }': ()
1115 [62; 64) 'a1': A<u32>
1116 [62; 66) 'a1.x': u32
1117 [76; 78) 'a2': A<i32>
1118 [81; 91) 'A { x: i }': A<i32>
1119 [88; 89) 'i': i32
1120 [97; 99) 'a2': A<i32>
1121 [97; 101) 'a2.x': i32
1122 [111; 113) 'a3': A<i128>
1123 [116; 134) 'A::<i1...x: 1 }': A<i128>
1124 [131; 132) '1': i128
1125 [140; 142) 'a3': A<i128>
1126 [140; 144) 'a3.x': i128
1127 "###
1128 );
1129}
1130
1131#[test]
1132fn infer_tuple_struct_generics() {
1133 assert_snapshot!(
1134 infer(r#"
1135struct A<T>(T);
1136enum Option<T> { Some(T), None }
1137use Option::*;
1138
1139fn test() {
1140 A(42);
1141 A(42u128);
1142 Some("x");
1143 Option::Some("x");
1144 None;
1145 let x: Option<i64> = None;
1146}
1147"#),
1148 @r###"
1149 [76; 184) '{ ...one; }': ()
1150 [82; 83) 'A': A<i32>(T) -> A<T>
1151 [82; 87) 'A(42)': A<i32>
1152 [84; 86) '42': i32
1153 [93; 94) 'A': A<u128>(T) -> A<T>
1154 [93; 102) 'A(42u128)': A<u128>
1155 [95; 101) '42u128': u128
1156 [108; 112) 'Some': Some<&str>(T) -> Option<T>
1157 [108; 117) 'Some("x")': Option<&str>
1158 [113; 116) '"x"': &str
1159 [123; 135) 'Option::Some': Some<&str>(T) -> Option<T>
1160 [123; 140) 'Option...e("x")': Option<&str>
1161 [136; 139) '"x"': &str
1162 [146; 150) 'None': Option<{unknown}>
1163 [160; 161) 'x': Option<i64>
1164 [177; 181) 'None': Option<i64>
1165 "###
1166 );
1167}
1168
1169#[test]
1170fn infer_function_generics() {
1171 assert_snapshot!(
1172 infer(r#"
1173fn id<T>(t: T) -> T { t }
1174
1175fn test() {
1176 id(1u32);
1177 id::<i128>(1);
1178 let x: u64 = id(1);
1179}
1180"#),
1181 @r###"
1182 [10; 11) 't': T
1183 [21; 26) '{ t }': T
1184 [23; 24) 't': T
1185 [38; 98) '{ ...(1); }': ()
1186 [44; 46) 'id': fn id<u32>(T) -> T
1187 [44; 52) 'id(1u32)': u32
1188 [47; 51) '1u32': u32
1189 [58; 68) 'id::<i128>': fn id<i128>(T) -> T
1190 [58; 71) 'id::<i128>(1)': i128
1191 [69; 70) '1': i128
1192 [81; 82) 'x': u64
1193 [90; 92) 'id': fn id<u64>(T) -> T
1194 [90; 95) 'id(1)': u64
1195 [93; 94) '1': u64
1196 "###
1197 );
1198}
1199
1200#[test]
1201fn infer_impl_generics() {
1202 assert_snapshot!(
1203 infer(r#"
1204struct A<T1, T2> {
1205 x: T1,
1206 y: T2,
1207}
1208impl<Y, X> A<X, Y> {
1209 fn x(self) -> X {
1210 self.x
1211 }
1212 fn y(self) -> Y {
1213 self.y
1214 }
1215 fn z<T>(self, t: T) -> (X, Y, T) {
1216 (self.x, self.y, t)
1217 }
1218}
1219
1220fn test() -> i128 {
1221 let a = A { x: 1u64, y: 1i64 };
1222 a.x();
1223 a.y();
1224 a.z(1i128);
1225 a.z::<u128>(1);
1226}
1227"#),
1228 @r###"
1229 [74; 78) 'self': A<X, Y>
1230 [85; 107) '{ ... }': X
1231 [95; 99) 'self': A<X, Y>
1232 [95; 101) 'self.x': X
1233 [117; 121) 'self': A<X, Y>
1234 [128; 150) '{ ... }': Y
1235 [138; 142) 'self': A<X, Y>
1236 [138; 144) 'self.y': Y
1237 [163; 167) 'self': A<X, Y>
1238 [169; 170) 't': T
1239 [188; 223) '{ ... }': (X, Y, T)
1240 [198; 217) '(self.....y, t)': (X, Y, T)
1241 [199; 203) 'self': A<X, Y>
1242 [199; 205) 'self.x': X
1243 [207; 211) 'self': A<X, Y>
1244 [207; 213) 'self.y': Y
1245 [215; 216) 't': T
1246 [245; 342) '{ ...(1); }': ()
1247 [255; 256) 'a': A<u64, i64>
1248 [259; 281) 'A { x:...1i64 }': A<u64, i64>
1249 [266; 270) '1u64': u64
1250 [275; 279) '1i64': i64
1251 [287; 288) 'a': A<u64, i64>
1252 [287; 292) 'a.x()': u64
1253 [298; 299) 'a': A<u64, i64>
1254 [298; 303) 'a.y()': i64
1255 [309; 310) 'a': A<u64, i64>
1256 [309; 319) 'a.z(1i128)': (u64, i64, i128)
1257 [313; 318) '1i128': i128
1258 [325; 326) 'a': A<u64, i64>
1259 [325; 339) 'a.z::<u128>(1)': (u64, i64, u128)
1260 [337; 338) '1': u128
1261 "###
1262 );
1263}
1264
1265#[test]
1266fn infer_impl_generics_with_autoderef() {
1267 assert_snapshot!(
1268 infer(r#"
1269enum Option<T> {
1270 Some(T),
1271 None,
1272}
1273impl<T> Option<T> {
1274 fn as_ref(&self) -> Option<&T> {}
1275}
1276fn test(o: Option<u32>) {
1277 (&o).as_ref();
1278 o.as_ref();
1279}
1280"#),
1281 @r###"
1282 [78; 82) 'self': &Option<T>
1283 [98; 100) '{}': ()
1284 [111; 112) 'o': Option<u32>
1285 [127; 165) '{ ...f(); }': ()
1286 [133; 146) '(&o).as_ref()': Option<&u32>
1287 [134; 136) '&o': &Option<u32>
1288 [135; 136) 'o': Option<u32>
1289 [152; 153) 'o': Option<u32>
1290 [152; 162) 'o.as_ref()': Option<&u32>
1291 "###
1292 );
1293}
1294
1295#[test]
1296fn infer_generic_chain() {
1297 assert_snapshot!(
1298 infer(r#"
1299struct A<T> {
1300 x: T,
1301}
1302impl<T2> A<T2> {
1303 fn x(self) -> T2 {
1304 self.x
1305 }
1306}
1307fn id<T>(t: T) -> T { t }
1308
1309fn test() -> i128 {
1310 let x = 1;
1311 let y = id(x);
1312 let a = A { x: id(y) };
1313 let z = id(a.x);
1314 let b = A { x: z };
1315 b.x()
1316}
1317"#),
1318 @r###"
1319 [53; 57) 'self': A<T2>
1320 [65; 87) '{ ... }': T2
1321 [75; 79) 'self': A<T2>
1322 [75; 81) 'self.x': T2
1323 [99; 100) 't': T
1324 [110; 115) '{ t }': T
1325 [112; 113) 't': T
1326 [135; 261) '{ ....x() }': i128
1327 [146; 147) 'x': i128
1328 [150; 151) '1': i128
1329 [162; 163) 'y': i128
1330 [166; 168) 'id': fn id<i128>(T) -> T
1331 [166; 171) 'id(x)': i128
1332 [169; 170) 'x': i128
1333 [182; 183) 'a': A<i128>
1334 [186; 200) 'A { x: id(y) }': A<i128>
1335 [193; 195) 'id': fn id<i128>(T) -> T
1336 [193; 198) 'id(y)': i128
1337 [196; 197) 'y': i128
1338 [211; 212) 'z': i128
1339 [215; 217) 'id': fn id<i128>(T) -> T
1340 [215; 222) 'id(a.x)': i128
1341 [218; 219) 'a': A<i128>
1342 [218; 221) 'a.x': i128
1343 [233; 234) 'b': A<i128>
1344 [237; 247) 'A { x: z }': A<i128>
1345 [244; 245) 'z': i128
1346 [254; 255) 'b': A<i128>
1347 [254; 259) 'b.x()': i128
1348 "###
1349 );
1350}
1351
1352#[test]
1353fn infer_associated_const() {
1354 assert_snapshot!(
1355 infer(r#"
1356struct Struct;
1357
1358impl Struct {
1359 const FOO: u32 = 1;
1360}
1361
1362enum Enum {}
1363
1364impl Enum {
1365 const BAR: u32 = 2;
1366}
1367
1368trait Trait {
1369 const ID: u32;
1370}
1371
1372struct TraitTest;
1373
1374impl Trait for TraitTest {
1375 const ID: u32 = 5;
1376}
1377
1378fn test() {
1379 let x = Struct::FOO;
1380 let y = Enum::BAR;
1381 let z = TraitTest::ID;
1382}
1383"#),
1384 @r###"
1385 [52; 53) '1': u32
1386 [105; 106) '2': u32
1387 [213; 214) '5': u32
1388 [229; 307) '{ ...:ID; }': ()
1389 [239; 240) 'x': u32
1390 [243; 254) 'Struct::FOO': u32
1391 [264; 265) 'y': u32
1392 [268; 277) 'Enum::BAR': u32
1393 [287; 288) 'z': u32
1394 [291; 304) 'TraitTest::ID': u32
1395 "###
1396 );
1397}
1398
1399#[test]
1400fn infer_type_alias() {
1401 assert_snapshot!(
1402 infer(r#"
1403struct A<X, Y> { x: X, y: Y }
1404type Foo = A<u32, i128>;
1405type Bar<T> = A<T, u128>;
1406type Baz<U, V> = A<V, U>;
1407fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
1408 x.x;
1409 x.y;
1410 y.x;
1411 y.y;
1412 z.x;
1413 z.y;
1414}
1415"#),
1416 @r###"
1417 [116; 117) 'x': A<u32, i128>
1418 [124; 125) 'y': A<&str, u128>
1419 [138; 139) 'z': A<u8, i8>
1420 [154; 211) '{ ...z.y; }': ()
1421 [160; 161) 'x': A<u32, i128>
1422 [160; 163) 'x.x': u32
1423 [169; 170) 'x': A<u32, i128>
1424 [169; 172) 'x.y': i128
1425 [178; 179) 'y': A<&str, u128>
1426 [178; 181) 'y.x': &str
1427 [187; 188) 'y': A<&str, u128>
1428 [187; 190) 'y.y': u128
1429 [196; 197) 'z': A<u8, i8>
1430 [196; 199) 'z.x': u8
1431 [205; 206) 'z': A<u8, i8>
1432 [205; 208) 'z.y': i8
1433 "###
1434 )
1435}
1436
1437#[test]
1438fn recursive_type_alias() {
1439 assert_snapshot!(
1440 infer(r#"
1441struct A<X> {}
1442type Foo = Foo;
1443type Bar = A<Bar>;
1444fn test(x: Foo) {}
1445"#),
1446 @r###"
1447 [59; 60) 'x': {unknown}
1448 [67; 69) '{}': ()
1449 "###
1450 )
1451}
1452
1453#[test]
1454fn infer_type_param() {
1455 assert_snapshot!(
1456 infer(r#"
1457fn id<T>(x: T) -> T {
1458 x
1459}
1460
1461fn clone<T>(x: &T) -> T {
1462 *x
1463}
1464
1465fn test() {
1466 let y = 10u32;
1467 id(y);
1468 let x: bool = clone(z);
1469 id::<i128>(1);
1470}
1471"#),
1472 @r###"
1473 [10; 11) 'x': T
1474 [21; 30) '{ x }': T
1475 [27; 28) 'x': T
1476 [44; 45) 'x': &T
1477 [56; 66) '{ *x }': T
1478 [62; 64) '*x': T
1479 [63; 64) 'x': &T
1480 [78; 158) '{ ...(1); }': ()
1481 [88; 89) 'y': u32
1482 [92; 97) '10u32': u32
1483 [103; 105) 'id': fn id<u32>(T) -> T
1484 [103; 108) 'id(y)': u32
1485 [106; 107) 'y': u32
1486 [118; 119) 'x': bool
1487 [128; 133) 'clone': fn clone<bool>(&T) -> T
1488 [128; 136) 'clone(z)': bool
1489 [134; 135) 'z': &bool
1490 [142; 152) 'id::<i128>': fn id<i128>(T) -> T
1491 [142; 155) 'id::<i128>(1)': i128
1492 [153; 154) '1': i128
1493 "###
1494 );
1495}
1496
1497#[test]
1498fn infer_const() {
1499 assert_snapshot!(
1500 infer(r#"
1501struct Foo;
1502impl Foo { const ASSOC_CONST: u32 = 0; }
1503const GLOBAL_CONST: u32 = 101;
1504fn test() {
1505 const LOCAL_CONST: u32 = 99;
1506 let x = LOCAL_CONST;
1507 let z = GLOBAL_CONST;
1508 let id = Foo::ASSOC_CONST;
1509}
1510"#),
1511 @r###"
1512 [49; 50) '0': u32
1513 [80; 83) '101': u32
1514 [95; 213) '{ ...NST; }': ()
1515 [138; 139) 'x': {unknown}
1516 [142; 153) 'LOCAL_CONST': {unknown}
1517 [163; 164) 'z': u32
1518 [167; 179) 'GLOBAL_CONST': u32
1519 [189; 191) 'id': u32
1520 [194; 210) 'Foo::A..._CONST': u32
1521 "###
1522 );
1523}
1524
1525#[test]
1526fn infer_static() {
1527 assert_snapshot!(
1528 infer(r#"
1529static GLOBAL_STATIC: u32 = 101;
1530static mut GLOBAL_STATIC_MUT: u32 = 101;
1531fn test() {
1532 static LOCAL_STATIC: u32 = 99;
1533 static mut LOCAL_STATIC_MUT: u32 = 99;
1534 let x = LOCAL_STATIC;
1535 let y = LOCAL_STATIC_MUT;
1536 let z = GLOBAL_STATIC;
1537 let w = GLOBAL_STATIC_MUT;
1538}
1539"#),
1540 @r###"
1541 [29; 32) '101': u32
1542 [70; 73) '101': u32
1543 [85; 280) '{ ...MUT; }': ()
1544 [173; 174) 'x': {unknown}
1545 [177; 189) 'LOCAL_STATIC': {unknown}
1546 [199; 200) 'y': {unknown}
1547 [203; 219) 'LOCAL_...IC_MUT': {unknown}
1548 [229; 230) 'z': u32
1549 [233; 246) 'GLOBAL_STATIC': u32
1550 [256; 257) 'w': u32
1551 [260; 277) 'GLOBAL...IC_MUT': u32
1552 "###
1553 );
1554}
1555
1556#[test]
1557fn shadowing_primitive() {
1558 let t = type_at(
1559 r#"
1560//- /main.rs
1561struct i32;
1562struct Foo;
1563
1564impl i32 { fn foo(&self) -> Foo { Foo } }
1565
1566fn main() {
1567 let x: i32 = i32;
1568 x.foo()<|>;
1569}"#,
1570 );
1571 assert_eq!(t, "Foo");
1572}
1573
1574#[test]
1575fn not_shadowing_primitive_by_module() {
1576 let t = type_at(
1577 r#"
1578//- /str.rs
1579fn foo() {}
1580
1581//- /main.rs
1582mod str;
1583fn foo() -> &'static str { "" }
1584
1585fn main() {
1586 foo()<|>;
1587}"#,
1588 );
1589 assert_eq!(t, "&str");
1590}
1591
1592#[test]
1593fn not_shadowing_module_by_primitive() {
1594 let t = type_at(
1595 r#"
1596//- /str.rs
1597fn foo() -> u32 {0}
1598
1599//- /main.rs
1600mod str;
1601fn foo() -> &'static str { "" }
1602
1603fn main() {
1604 str::foo()<|>;
1605}"#,
1606 );
1607 assert_eq!(t, "u32");
1608}
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs
new file mode 100644
index 000000000..93c5f9a15
--- /dev/null
+++ b/crates/ra_hir_ty/src/tests/traits.rs
@@ -0,0 +1,1424 @@
1use super::{infer, type_at, type_at_pos};
2use crate::test_db::TestDB;
3use insta::assert_snapshot;
4use ra_db::fixture::WithFixture;
5
6#[test]
7fn infer_await() {
8 let (db, pos) = TestDB::with_position(
9 r#"
10//- /main.rs crate:main deps:std
11
12struct IntFuture;
13
14impl Future for IntFuture {
15 type Output = u64;
16}
17
18fn test() {
19 let r = IntFuture;
20 let v = r.await;
21 v<|>;
22}
23
24//- /std.rs crate:std
25#[prelude_import] use future::*;
26mod future {
27 trait Future {
28 type Output;
29 }
30}
31
32"#,
33 );
34 assert_eq!("u64", type_at_pos(&db, pos));
35}
36
37#[test]
38fn infer_try() {
39 let (db, pos) = TestDB::with_position(
40 r#"
41//- /main.rs crate:main deps:std
42
43fn test() {
44 let r: Result<i32, u64> = Result::Ok(1);
45 let v = r?;
46 v<|>;
47}
48
49//- /std.rs crate:std
50
51#[prelude_import] use ops::*;
52mod ops {
53 trait Try {
54 type Ok;
55 type Error;
56 }
57}
58
59#[prelude_import] use result::*;
60mod result {
61 enum Result<O, E> {
62 Ok(O),
63 Err(E)
64 }
65
66 impl<O, E> crate::ops::Try for Result<O, E> {
67 type Ok = O;
68 type Error = E;
69 }
70}
71
72"#,
73 );
74 assert_eq!("i32", type_at_pos(&db, pos));
75}
76
77#[test]
78fn infer_for_loop() {
79 let (db, pos) = TestDB::with_position(
80 r#"
81//- /main.rs crate:main deps:std
82
83use std::collections::Vec;
84
85fn test() {
86 let v = Vec::new();
87 v.push("foo");
88 for x in v {
89 x<|>;
90 }
91}
92
93//- /std.rs crate:std
94
95#[prelude_import] use iter::*;
96mod iter {
97 trait IntoIterator {
98 type Item;
99 }
100}
101
102mod collections {
103 struct Vec<T> {}
104 impl<T> Vec<T> {
105 fn new() -> Self { Vec {} }
106 fn push(&mut self, t: T) { }
107 }
108
109 impl<T> crate::iter::IntoIterator for Vec<T> {
110 type Item=T;
111 }
112}
113"#,
114 );
115 assert_eq!("&str", type_at_pos(&db, pos));
116}
117
118#[test]
119fn infer_from_bound_1() {
120 assert_snapshot!(
121 infer(r#"
122trait Trait<T> {}
123struct S<T>(T);
124impl<U> Trait<U> for S<U> {}
125fn foo<T: Trait<u32>>(t: T) {}
126fn test() {
127 let s = S(unknown);
128 foo(s);
129}
130"#),
131 @r###"
132 [86; 87) 't': T
133 [92; 94) '{}': ()
134 [105; 144) '{ ...(s); }': ()
135 [115; 116) 's': S<u32>
136 [119; 120) 'S': S<u32>(T) -> S<T>
137 [119; 129) 'S(unknown)': S<u32>
138 [121; 128) 'unknown': u32
139 [135; 138) 'foo': fn foo<S<u32>>(T) -> ()
140 [135; 141) 'foo(s)': ()
141 [139; 140) 's': S<u32>
142 "###
143 );
144}
145
146#[test]
147fn infer_from_bound_2() {
148 assert_snapshot!(
149 infer(r#"
150trait Trait<T> {}
151struct S<T>(T);
152impl<U> Trait<U> for S<U> {}
153fn foo<U, T: Trait<U>>(t: T) -> U {}
154fn test() {
155 let s = S(unknown);
156 let x: u32 = foo(s);
157}
158"#),
159 @r###"
160 [87; 88) 't': T
161 [98; 100) '{}': ()
162 [111; 163) '{ ...(s); }': ()
163 [121; 122) 's': S<u32>
164 [125; 126) 'S': S<u32>(T) -> S<T>
165 [125; 135) 'S(unknown)': S<u32>
166 [127; 134) 'unknown': u32
167 [145; 146) 'x': u32
168 [154; 157) 'foo': fn foo<u32, S<u32>>(T) -> U
169 [154; 160) 'foo(s)': u32
170 [158; 159) 's': S<u32>
171 "###
172 );
173}
174
175#[test]
176fn infer_project_associated_type() {
177 // y, z, a don't yet work because of https://github.com/rust-lang/chalk/issues/234
178 assert_snapshot!(
179 infer(r#"
180trait Iterable {
181 type Item;
182}
183struct S;
184impl Iterable for S { type Item = u32; }
185fn test<T: Iterable>() {
186 let x: <S as Iterable>::Item = 1;
187 let y: <T as Iterable>::Item = no_matter;
188 let z: T::Item = no_matter;
189 let a: <T>::Item = no_matter;
190}
191"#),
192 @r###"
193 [108; 261) '{ ...ter; }': ()
194 [118; 119) 'x': u32
195 [145; 146) '1': u32
196 [156; 157) 'y': {unknown}
197 [183; 192) 'no_matter': {unknown}
198 [202; 203) 'z': {unknown}
199 [215; 224) 'no_matter': {unknown}
200 [234; 235) 'a': {unknown}
201 [249; 258) 'no_matter': {unknown}
202 "###
203 );
204}
205
206#[test]
207fn infer_return_associated_type() {
208 assert_snapshot!(
209 infer(r#"
210trait Iterable {
211 type Item;
212}
213struct S;
214impl Iterable for S { type Item = u32; }
215fn foo1<T: Iterable>(t: T) -> T::Item {}
216fn foo2<T: Iterable>(t: T) -> <T as Iterable>::Item {}
217fn foo3<T: Iterable>(t: T) -> <T>::Item {}
218fn test() {
219 let x = foo1(S);
220 let y = foo2(S);
221 let z = foo3(S);
222}
223"#),
224 @r###"
225 [106; 107) 't': T
226 [123; 125) '{}': ()
227 [147; 148) 't': T
228 [178; 180) '{}': ()
229 [202; 203) 't': T
230 [221; 223) '{}': ()
231 [234; 300) '{ ...(S); }': ()
232 [244; 245) 'x': u32
233 [248; 252) 'foo1': fn foo1<S>(T) -> <T as Iterable>::Item
234 [248; 255) 'foo1(S)': u32
235 [253; 254) 'S': S
236 [265; 266) 'y': u32
237 [269; 273) 'foo2': fn foo2<S>(T) -> <T as Iterable>::Item
238 [269; 276) 'foo2(S)': u32
239 [274; 275) 'S': S
240 [286; 287) 'z': u32
241 [290; 294) 'foo3': fn foo3<S>(T) -> <T as Iterable>::Item
242 [290; 297) 'foo3(S)': u32
243 [295; 296) 'S': S
244 "###
245 );
246}
247
248#[test]
249fn infer_associated_type_bound() {
250 assert_snapshot!(
251 infer(r#"
252trait Iterable {
253 type Item;
254}
255fn test<T: Iterable<Item=u32>>() {
256 let y: T::Item = unknown;
257}
258"#),
259 @r###"
260 [67; 100) '{ ...own; }': ()
261 [77; 78) 'y': {unknown}
262 [90; 97) 'unknown': {unknown}
263 "###
264 );
265}
266
267#[test]
268fn infer_const_body() {
269 assert_snapshot!(
270 infer(r#"
271const A: u32 = 1 + 1;
272static B: u64 = { let x = 1; x };
273"#),
274 @r###"
275 [16; 17) '1': u32
276 [16; 21) '1 + 1': u32
277 [20; 21) '1': u32
278 [39; 55) '{ let ...1; x }': u64
279 [45; 46) 'x': u64
280 [49; 50) '1': u64
281 [52; 53) 'x': u64
282 "###
283 );
284}
285
286#[test]
287fn tuple_struct_fields() {
288 assert_snapshot!(
289 infer(r#"
290struct S(i32, u64);
291fn test() -> u64 {
292 let a = S(4, 6);
293 let b = a.0;
294 a.1
295}
296"#),
297 @r###"
298 [38; 87) '{ ... a.1 }': u64
299 [48; 49) 'a': S
300 [52; 53) 'S': S(i32, u64) -> S
301 [52; 59) 'S(4, 6)': S
302 [54; 55) '4': i32
303 [57; 58) '6': u64
304 [69; 70) 'b': i32
305 [73; 74) 'a': S
306 [73; 76) 'a.0': i32
307 [82; 83) 'a': S
308 [82; 85) 'a.1': u64
309 "###
310 );
311}
312
313#[test]
314fn tuple_struct_with_fn() {
315 assert_snapshot!(
316 infer(r#"
317struct S(fn(u32) -> u64);
318fn test() -> u64 {
319 let a = S(|i| 2*i);
320 let b = a.0(4);
321 a.0(2)
322}
323"#),
324 @r###"
325 [44; 102) '{ ...0(2) }': u64
326 [54; 55) 'a': S
327 [58; 59) 'S': S(fn(u32) -> u64) -> S
328 [58; 68) 'S(|i| 2*i)': S
329 [60; 67) '|i| 2*i': |i32| -> i32
330 [61; 62) 'i': i32
331 [64; 65) '2': i32
332 [64; 67) '2*i': i32
333 [66; 67) 'i': i32
334 [78; 79) 'b': u64
335 [82; 83) 'a': S
336 [82; 85) 'a.0': fn(u32) -> u64
337 [82; 88) 'a.0(4)': u64
338 [86; 87) '4': u32
339 [94; 95) 'a': S
340 [94; 97) 'a.0': fn(u32) -> u64
341 [94; 100) 'a.0(2)': u64
342 [98; 99) '2': u32
343 "###
344 );
345}
346
347#[test]
348fn indexing_arrays() {
349 assert_snapshot!(
350 infer("fn main() { &mut [9][2]; }"),
351 @r###"
352 [10; 26) '{ &mut...[2]; }': ()
353 [12; 23) '&mut [9][2]': &mut {unknown}
354 [17; 20) '[9]': [i32;_]
355 [17; 23) '[9][2]': {unknown}
356 [18; 19) '9': i32
357 [21; 22) '2': i32
358 "###
359 )
360}
361
362#[test]
363fn deref_trait() {
364 let t = type_at(
365 r#"
366//- /main.rs
367#[lang = "deref"]
368trait Deref {
369 type Target;
370 fn deref(&self) -> &Self::Target;
371}
372
373struct Arc<T>;
374impl<T> Deref for Arc<T> {
375 type Target = T;
376}
377
378struct S;
379impl S {
380 fn foo(&self) -> u128 {}
381}
382
383fn test(s: Arc<S>) {
384 (*s, s.foo())<|>;
385}
386"#,
387 );
388 assert_eq!(t, "(S, u128)");
389}
390
391#[test]
392fn deref_trait_with_inference_var() {
393 let t = type_at(
394 r#"
395//- /main.rs
396#[lang = "deref"]
397trait Deref {
398 type Target;
399 fn deref(&self) -> &Self::Target;
400}
401
402struct Arc<T>;
403fn new_arc<T>() -> Arc<T> {}
404impl<T> Deref for Arc<T> {
405 type Target = T;
406}
407
408struct S;
409fn foo(a: Arc<S>) {}
410
411fn test() {
412 let a = new_arc();
413 let b = (*a)<|>;
414 foo(a);
415}
416"#,
417 );
418 assert_eq!(t, "S");
419}
420
421#[test]
422fn deref_trait_infinite_recursion() {
423 let t = type_at(
424 r#"
425//- /main.rs
426#[lang = "deref"]
427trait Deref {
428 type Target;
429 fn deref(&self) -> &Self::Target;
430}
431
432struct S;
433
434impl Deref for S {
435 type Target = S;
436}
437
438fn test(s: S) {
439 s.foo()<|>;
440}
441"#,
442 );
443 assert_eq!(t, "{unknown}");
444}
445
446#[test]
447fn deref_trait_with_question_mark_size() {
448 let t = type_at(
449 r#"
450//- /main.rs
451#[lang = "deref"]
452trait Deref {
453 type Target;
454 fn deref(&self) -> &Self::Target;
455}
456
457struct Arc<T>;
458impl<T> Deref for Arc<T> {
459 type Target = T;
460}
461
462struct S;
463impl S {
464 fn foo(&self) -> u128 {}
465}
466
467fn test(s: Arc<S>) {
468 (*s, s.foo())<|>;
469}
470"#,
471 );
472 assert_eq!(t, "(S, u128)");
473}
474
475#[test]
476fn obligation_from_function_clause() {
477 let t = type_at(
478 r#"
479//- /main.rs
480struct S;
481
482trait Trait<T> {}
483impl Trait<u32> for S {}
484
485fn foo<T: Trait<U>, U>(t: T) -> U {}
486
487fn test(s: S) {
488 foo(s)<|>;
489}
490"#,
491 );
492 assert_eq!(t, "u32");
493}
494
495#[test]
496fn obligation_from_method_clause() {
497 let t = type_at(
498 r#"
499//- /main.rs
500struct S;
501
502trait Trait<T> {}
503impl Trait<isize> for S {}
504
505struct O;
506impl O {
507 fn foo<T: Trait<U>, U>(&self, t: T) -> U {}
508}
509
510fn test() {
511 O.foo(S)<|>;
512}
513"#,
514 );
515 assert_eq!(t, "isize");
516}
517
518#[test]
519fn obligation_from_self_method_clause() {
520 let t = type_at(
521 r#"
522//- /main.rs
523struct S;
524
525trait Trait<T> {}
526impl Trait<i64> for S {}
527
528impl S {
529 fn foo<U>(&self) -> U where Self: Trait<U> {}
530}
531
532fn test() {
533 S.foo()<|>;
534}
535"#,
536 );
537 assert_eq!(t, "i64");
538}
539
540#[test]
541fn obligation_from_impl_clause() {
542 let t = type_at(
543 r#"
544//- /main.rs
545struct S;
546
547trait Trait<T> {}
548impl Trait<&str> for S {}
549
550struct O<T>;
551impl<U, T: Trait<U>> O<T> {
552 fn foo(&self) -> U {}
553}
554
555fn test(o: O<S>) {
556 o.foo()<|>;
557}
558"#,
559 );
560 assert_eq!(t, "&str");
561}
562
563#[test]
564fn generic_param_env_1() {
565 let t = type_at(
566 r#"
567//- /main.rs
568trait Clone {}
569trait Trait { fn foo(self) -> u128; }
570struct S;
571impl Clone for S {}
572impl<T> Trait for T where T: Clone {}
573fn test<T: Clone>(t: T) { t.foo()<|>; }
574"#,
575 );
576 assert_eq!(t, "u128");
577}
578
579#[test]
580fn generic_param_env_1_not_met() {
581 let t = type_at(
582 r#"
583//- /main.rs
584trait Clone {}
585trait Trait { fn foo(self) -> u128; }
586struct S;
587impl Clone for S {}
588impl<T> Trait for T where T: Clone {}
589fn test<T>(t: T) { t.foo()<|>; }
590"#,
591 );
592 assert_eq!(t, "{unknown}");
593}
594
595#[test]
596fn generic_param_env_2() {
597 let t = type_at(
598 r#"
599//- /main.rs
600trait Trait { fn foo(self) -> u128; }
601struct S;
602impl Trait for S {}
603fn test<T: Trait>(t: T) { t.foo()<|>; }
604"#,
605 );
606 assert_eq!(t, "u128");
607}
608
609#[test]
610fn generic_param_env_2_not_met() {
611 let t = type_at(
612 r#"
613//- /main.rs
614trait Trait { fn foo(self) -> u128; }
615struct S;
616impl Trait for S {}
617fn test<T>(t: T) { t.foo()<|>; }
618"#,
619 );
620 assert_eq!(t, "{unknown}");
621}
622
623#[test]
624fn generic_param_env_deref() {
625 let t = type_at(
626 r#"
627//- /main.rs
628#[lang = "deref"]
629trait Deref {
630 type Target;
631}
632trait Trait {}
633impl<T> Deref for T where T: Trait {
634 type Target = i128;
635}
636fn test<T: Trait>(t: T) { (*t)<|>; }
637"#,
638 );
639 assert_eq!(t, "i128");
640}
641
642#[test]
643fn associated_type_placeholder() {
644 let t = type_at(
645 r#"
646//- /main.rs
647pub trait ApplyL {
648 type Out;
649}
650
651pub struct RefMutL<T>;
652
653impl<T> ApplyL for RefMutL<T> {
654 type Out = <T as ApplyL>::Out;
655}
656
657fn test<T: ApplyL>() {
658 let y: <RefMutL<T> as ApplyL>::Out = no_matter;
659 y<|>;
660}
661"#,
662 );
663 // inside the generic function, the associated type gets normalized to a placeholder `ApplL::Out<T>` [https://rust-lang.github.io/rustc-guide/traits/associated-types.html#placeholder-associated-types].
664 // FIXME: fix type parameter names going missing when going through Chalk
665 assert_eq!(t, "ApplyL::Out<[missing name]>");
666}
667
668#[test]
669fn associated_type_placeholder_2() {
670 let t = type_at(
671 r#"
672//- /main.rs
673pub trait ApplyL {
674 type Out;
675}
676fn foo<T: ApplyL>(t: T) -> <T as ApplyL>::Out;
677
678fn test<T: ApplyL>(t: T) {
679 let y = foo(t);
680 y<|>;
681}
682"#,
683 );
684 // FIXME here Chalk doesn't normalize the type to a placeholder. I think we
685 // need to add a rule like Normalize(<T as ApplyL>::Out -> ApplyL::Out<T>)
686 // to the trait env ourselves here; probably Chalk can't do this by itself.
687 // assert_eq!(t, "ApplyL::Out<[missing name]>");
688 assert_eq!(t, "{unknown}");
689}
690
691#[test]
692fn impl_trait() {
693 assert_snapshot!(
694 infer(r#"
695trait Trait<T> {
696 fn foo(&self) -> T;
697 fn foo2(&self) -> i64;
698}
699fn bar() -> impl Trait<u64> {}
700
701fn test(x: impl Trait<u64>, y: &impl Trait<u64>) {
702 x;
703 y;
704 let z = bar();
705 x.foo();
706 y.foo();
707 z.foo();
708 x.foo2();
709 y.foo2();
710 z.foo2();
711}
712"#),
713 @r###"
714 [30; 34) 'self': &Self
715 [55; 59) 'self': &Self
716 [99; 101) '{}': ()
717 [111; 112) 'x': impl Trait<u64>
718 [131; 132) 'y': &impl Trait<u64>
719 [152; 269) '{ ...2(); }': ()
720 [158; 159) 'x': impl Trait<u64>
721 [165; 166) 'y': &impl Trait<u64>
722 [176; 177) 'z': impl Trait<u64>
723 [180; 183) 'bar': fn bar() -> impl Trait<u64>
724 [180; 185) 'bar()': impl Trait<u64>
725 [191; 192) 'x': impl Trait<u64>
726 [191; 198) 'x.foo()': u64
727 [204; 205) 'y': &impl Trait<u64>
728 [204; 211) 'y.foo()': u64
729 [217; 218) 'z': impl Trait<u64>
730 [217; 224) 'z.foo()': u64
731 [230; 231) 'x': impl Trait<u64>
732 [230; 238) 'x.foo2()': i64
733 [244; 245) 'y': &impl Trait<u64>
734 [244; 252) 'y.foo2()': i64
735 [258; 259) 'z': impl Trait<u64>
736 [258; 266) 'z.foo2()': i64
737 "###
738 );
739}
740
741#[test]
742fn dyn_trait() {
743 assert_snapshot!(
744 infer(r#"
745trait Trait<T> {
746 fn foo(&self) -> T;
747 fn foo2(&self) -> i64;
748}
749fn bar() -> dyn Trait<u64> {}
750
751fn test(x: dyn Trait<u64>, y: &dyn Trait<u64>) {
752 x;
753 y;
754 let z = bar();
755 x.foo();
756 y.foo();
757 z.foo();
758 x.foo2();
759 y.foo2();
760 z.foo2();
761}
762"#),
763 @r###"
764 [30; 34) 'self': &Self
765 [55; 59) 'self': &Self
766 [98; 100) '{}': ()
767 [110; 111) 'x': dyn Trait<u64>
768 [129; 130) 'y': &dyn Trait<u64>
769 [149; 266) '{ ...2(); }': ()
770 [155; 156) 'x': dyn Trait<u64>
771 [162; 163) 'y': &dyn Trait<u64>
772 [173; 174) 'z': dyn Trait<u64>
773 [177; 180) 'bar': fn bar() -> dyn Trait<u64>
774 [177; 182) 'bar()': dyn Trait<u64>
775 [188; 189) 'x': dyn Trait<u64>
776 [188; 195) 'x.foo()': u64
777 [201; 202) 'y': &dyn Trait<u64>
778 [201; 208) 'y.foo()': u64
779 [214; 215) 'z': dyn Trait<u64>
780 [214; 221) 'z.foo()': u64
781 [227; 228) 'x': dyn Trait<u64>
782 [227; 235) 'x.foo2()': i64
783 [241; 242) 'y': &dyn Trait<u64>
784 [241; 249) 'y.foo2()': i64
785 [255; 256) 'z': dyn Trait<u64>
786 [255; 263) 'z.foo2()': i64
787 "###
788 );
789}
790
791#[test]
792fn dyn_trait_bare() {
793 assert_snapshot!(
794 infer(r#"
795trait Trait {
796 fn foo(&self) -> u64;
797}
798fn bar() -> Trait {}
799
800fn test(x: Trait, y: &Trait) -> u64 {
801 x;
802 y;
803 let z = bar();
804 x.foo();
805 y.foo();
806 z.foo();
807}
808"#),
809 @r###"
810 [27; 31) 'self': &Self
811 [61; 63) '{}': ()
812 [73; 74) 'x': dyn Trait
813 [83; 84) 'y': &dyn Trait
814 [101; 176) '{ ...o(); }': ()
815 [107; 108) 'x': dyn Trait
816 [114; 115) 'y': &dyn Trait
817 [125; 126) 'z': dyn Trait
818 [129; 132) 'bar': fn bar() -> dyn Trait
819 [129; 134) 'bar()': dyn Trait
820 [140; 141) 'x': dyn Trait
821 [140; 147) 'x.foo()': u64
822 [153; 154) 'y': &dyn Trait
823 [153; 160) 'y.foo()': u64
824 [166; 167) 'z': dyn Trait
825 [166; 173) 'z.foo()': u64
826 "###
827 );
828}
829
830#[test]
831fn weird_bounds() {
832 assert_snapshot!(
833 infer(r#"
834trait Trait {}
835fn test() {
836 let a: impl Trait + 'lifetime = foo;
837 let b: impl 'lifetime = foo;
838 let b: impl (Trait) = foo;
839 let b: impl ('lifetime) = foo;
840 let d: impl ?Sized = foo;
841 let e: impl Trait + ?Sized = foo;
842}
843"#),
844 @r###"
845 [26; 237) '{ ...foo; }': ()
846 [36; 37) 'a': impl Trait + {error}
847 [64; 67) 'foo': impl Trait + {error}
848 [77; 78) 'b': impl {error}
849 [97; 100) 'foo': impl {error}
850 [110; 111) 'b': impl Trait
851 [128; 131) 'foo': impl Trait
852 [141; 142) 'b': impl {error}
853 [163; 166) 'foo': impl {error}
854 [176; 177) 'd': impl {error}
855 [193; 196) 'foo': impl {error}
856 [206; 207) 'e': impl Trait + {error}
857 [231; 234) 'foo': impl Trait + {error}
858 "###
859 );
860}
861
862#[test]
863fn assoc_type_bindings() {
864 assert_snapshot!(
865 infer(r#"
866trait Trait {
867 type Type;
868}
869
870fn get<T: Trait>(t: T) -> <T as Trait>::Type {}
871fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
872fn set<T: Trait<Type = u64>>(t: T) -> T {t}
873
874struct S<T>;
875impl<T> Trait for S<T> { type Type = T; }
876
877fn test<T: Trait<Type = u32>>(x: T, y: impl Trait<Type = i64>) {
878 get(x);
879 get2(x);
880 get(y);
881 get2(y);
882 get(set(S));
883 get2(set(S));
884 get2(S::<str>);
885}
886"#),
887 @r###"
888 [50; 51) 't': T
889 [78; 80) '{}': ()
890 [112; 113) 't': T
891 [123; 125) '{}': ()
892 [155; 156) 't': T
893 [166; 169) '{t}': T
894 [167; 168) 't': T
895 [257; 258) 'x': T
896 [263; 264) 'y': impl Trait<Type = i64>
897 [290; 398) '{ ...r>); }': ()
898 [296; 299) 'get': fn get<T>(T) -> <T as Trait>::Type
899 [296; 302) 'get(x)': {unknown}
900 [300; 301) 'x': T
901 [308; 312) 'get2': fn get2<{unknown}, T>(T) -> U
902 [308; 315) 'get2(x)': {unknown}
903 [313; 314) 'x': T
904 [321; 324) 'get': fn get<impl Trait<Type = i64>>(T) -> <T as Trait>::Type
905 [321; 327) 'get(y)': {unknown}
906 [325; 326) 'y': impl Trait<Type = i64>
907 [333; 337) 'get2': fn get2<{unknown}, impl Trait<Type = i64>>(T) -> U
908 [333; 340) 'get2(y)': {unknown}
909 [338; 339) 'y': impl Trait<Type = i64>
910 [346; 349) 'get': fn get<S<u64>>(T) -> <T as Trait>::Type
911 [346; 357) 'get(set(S))': u64
912 [350; 353) 'set': fn set<S<u64>>(T) -> T
913 [350; 356) 'set(S)': S<u64>
914 [354; 355) 'S': S<u64>
915 [363; 367) 'get2': fn get2<u64, S<u64>>(T) -> U
916 [363; 375) 'get2(set(S))': u64
917 [368; 371) 'set': fn set<S<u64>>(T) -> T
918 [368; 374) 'set(S)': S<u64>
919 [372; 373) 'S': S<u64>
920 [381; 385) 'get2': fn get2<str, S<str>>(T) -> U
921 [381; 395) 'get2(S::<str>)': str
922 [386; 394) 'S::<str>': S<str>
923 "###
924 );
925}
926
927#[test]
928fn impl_trait_assoc_binding_projection_bug() {
929 let (db, pos) = TestDB::with_position(
930 r#"
931//- /main.rs crate:main deps:std
932pub trait Language {
933 type Kind;
934}
935pub enum RustLanguage {}
936impl Language for RustLanguage {
937 type Kind = SyntaxKind;
938}
939struct SyntaxNode<L> {}
940fn foo() -> impl Iterator<Item = SyntaxNode<RustLanguage>> {}
941
942trait Clone {
943 fn clone(&self) -> Self;
944}
945
946fn api_walkthrough() {
947 for node in foo() {
948 node.clone()<|>;
949 }
950}
951
952//- /std.rs crate:std
953#[prelude_import] use iter::*;
954mod iter {
955 trait IntoIterator {
956 type Item;
957 }
958 trait Iterator {
959 type Item;
960 }
961 impl<T: Iterator> IntoIterator for T {
962 type Item = <T as Iterator>::Item;
963 }
964}
965"#,
966 );
967 assert_eq!("{unknown}", type_at_pos(&db, pos));
968}
969
970#[test]
971fn projection_eq_within_chalk() {
972 // std::env::set_var("CHALK_DEBUG", "1");
973 assert_snapshot!(
974 infer(r#"
975trait Trait1 {
976 type Type;
977}
978trait Trait2<T> {
979 fn foo(self) -> T;
980}
981impl<T, U> Trait2<T> for U where U: Trait1<Type = T> {}
982
983fn test<T: Trait1<Type = u32>>(x: T) {
984 x.foo();
985}
986"#),
987 @r###"
988 [62; 66) 'self': Self
989 [164; 165) 'x': T
990 [170; 186) '{ ...o(); }': ()
991 [176; 177) 'x': T
992 [176; 183) 'x.foo()': {unknown}
993 "###
994 );
995}
996
997#[test]
998fn where_clause_trait_in_scope_for_method_resolution() {
999 let t = type_at(
1000 r#"
1001//- /main.rs
1002mod foo {
1003 trait Trait {
1004 fn foo(&self) -> u32 {}
1005 }
1006}
1007
1008fn test<T: foo::Trait>(x: T) {
1009 x.foo()<|>;
1010}
1011"#,
1012 );
1013 assert_eq!(t, "u32");
1014}
1015
1016#[test]
1017fn super_trait_method_resolution() {
1018 assert_snapshot!(
1019 infer(r#"
1020mod foo {
1021 trait SuperTrait {
1022 fn foo(&self) -> u32 {}
1023 }
1024}
1025trait Trait1: foo::SuperTrait {}
1026trait Trait2 where Self: foo::SuperTrait {}
1027
1028fn test<T: Trait1, U: Trait2>(x: T, y: U) {
1029 x.foo();
1030 y.foo();
1031}
1032"#),
1033 @r###"
1034 [50; 54) 'self': &Self
1035 [63; 65) '{}': ()
1036 [182; 183) 'x': T
1037 [188; 189) 'y': U
1038 [194; 223) '{ ...o(); }': ()
1039 [200; 201) 'x': T
1040 [200; 207) 'x.foo()': u32
1041 [213; 214) 'y': U
1042 [213; 220) 'y.foo()': u32
1043 "###
1044 );
1045}
1046
1047#[test]
1048fn super_trait_cycle() {
1049 // This just needs to not crash
1050 assert_snapshot!(
1051 infer(r#"
1052trait A: B {}
1053trait B: A {}
1054
1055fn test<T: A>(x: T) {
1056 x.foo();
1057}
1058"#),
1059 @r###"
1060 [44; 45) 'x': T
1061 [50; 66) '{ ...o(); }': ()
1062 [56; 57) 'x': T
1063 [56; 63) 'x.foo()': {unknown}
1064 "###
1065 );
1066}
1067
1068#[test]
1069fn super_trait_assoc_type_bounds() {
1070 assert_snapshot!(
1071 infer(r#"
1072trait SuperTrait { type Type; }
1073trait Trait where Self: SuperTrait {}
1074
1075fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
1076fn set<T: Trait<Type = u64>>(t: T) -> T {t}
1077
1078struct S<T>;
1079impl<T> SuperTrait for S<T> { type Type = T; }
1080impl<T> Trait for S<T> {}
1081
1082fn test() {
1083 get2(set(S));
1084}
1085"#),
1086 @r###"
1087 [103; 104) 't': T
1088 [114; 116) '{}': ()
1089 [146; 147) 't': T
1090 [157; 160) '{t}': T
1091 [158; 159) 't': T
1092 [259; 280) '{ ...S)); }': ()
1093 [265; 269) 'get2': fn get2<u64, S<u64>>(T) -> U
1094 [265; 277) 'get2(set(S))': u64
1095 [270; 273) 'set': fn set<S<u64>>(T) -> T
1096 [270; 276) 'set(S)': S<u64>
1097 [274; 275) 'S': S<u64>
1098 "###
1099 );
1100}
1101
1102#[test]
1103fn fn_trait() {
1104 assert_snapshot!(
1105 infer(r#"
1106trait FnOnce<Args> {
1107 type Output;
1108
1109 fn call_once(self, args: Args) -> <Self as FnOnce<Args>>::Output;
1110}
1111
1112fn test<F: FnOnce(u32, u64) -> u128>(f: F) {
1113 f.call_once((1, 2));
1114}
1115"#),
1116 @r###"
1117 [57; 61) 'self': Self
1118 [63; 67) 'args': Args
1119 [150; 151) 'f': F
1120 [156; 184) '{ ...2)); }': ()
1121 [162; 163) 'f': F
1122 [162; 181) 'f.call...1, 2))': {unknown}
1123 [174; 180) '(1, 2)': (u32, u64)
1124 [175; 176) '1': u32
1125 [178; 179) '2': u64
1126 "###
1127 );
1128}
1129
1130#[test]
1131fn closure_1() {
1132 assert_snapshot!(
1133 infer(r#"
1134#[lang = "fn_once"]
1135trait FnOnce<Args> {
1136 type Output;
1137}
1138
1139enum Option<T> { Some(T), None }
1140impl<T> Option<T> {
1141 fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {}
1142}
1143
1144fn test() {
1145 let x = Option::Some(1u32);
1146 x.map(|v| v + 1);
1147 x.map(|_v| 1u64);
1148 let y: Option<i64> = x.map(|_v| 1);
1149}
1150"#),
1151 @r###"
1152 [148; 152) 'self': Option<T>
1153 [154; 155) 'f': F
1154 [173; 175) '{}': ()
1155 [189; 308) '{ ... 1); }': ()
1156 [199; 200) 'x': Option<u32>
1157 [203; 215) 'Option::Some': Some<u32>(T) -> Option<T>
1158 [203; 221) 'Option...(1u32)': Option<u32>
1159 [216; 220) '1u32': u32
1160 [227; 228) 'x': Option<u32>
1161 [227; 243) 'x.map(...v + 1)': Option<u32>
1162 [233; 242) '|v| v + 1': |u32| -> u32
1163 [234; 235) 'v': u32
1164 [237; 238) 'v': u32
1165 [237; 242) 'v + 1': u32
1166 [241; 242) '1': u32
1167 [249; 250) 'x': Option<u32>
1168 [249; 265) 'x.map(... 1u64)': Option<u64>
1169 [255; 264) '|_v| 1u64': |u32| -> u64
1170 [256; 258) '_v': u32
1171 [260; 264) '1u64': u64
1172 [275; 276) 'y': Option<i64>
1173 [292; 293) 'x': Option<u32>
1174 [292; 305) 'x.map(|_v| 1)': Option<i64>
1175 [298; 304) '|_v| 1': |u32| -> i64
1176 [299; 301) '_v': u32
1177 [303; 304) '1': i64
1178 "###
1179 );
1180}
1181
1182#[test]
1183fn closure_2() {
1184 assert_snapshot!(
1185 infer(r#"
1186trait FnOnce<Args> {
1187 type Output;
1188}
1189
1190fn test<F: FnOnce(u32) -> u64>(f: F) {
1191 f(1);
1192 let g = |v| v + 1;
1193 g(1u64);
1194 let h = |v| 1u128 + v;
1195}
1196"#),
1197 @r###"
1198 [73; 74) 'f': F
1199 [79; 155) '{ ...+ v; }': ()
1200 [85; 86) 'f': F
1201 [85; 89) 'f(1)': {unknown}
1202 [87; 88) '1': i32
1203 [99; 100) 'g': |u64| -> i32
1204 [103; 112) '|v| v + 1': |u64| -> i32
1205 [104; 105) 'v': u64
1206 [107; 108) 'v': u64
1207 [107; 112) 'v + 1': i32
1208 [111; 112) '1': i32
1209 [118; 119) 'g': |u64| -> i32
1210 [118; 125) 'g(1u64)': i32
1211 [120; 124) '1u64': u64
1212 [135; 136) 'h': |u128| -> u128
1213 [139; 152) '|v| 1u128 + v': |u128| -> u128
1214 [140; 141) 'v': u128
1215 [143; 148) '1u128': u128
1216 [143; 152) '1u128 + v': u128
1217 [151; 152) 'v': u128
1218 "###
1219 );
1220}
1221
1222#[test]
1223fn closure_as_argument_inference_order() {
1224 assert_snapshot!(
1225 infer(r#"
1226#[lang = "fn_once"]
1227trait FnOnce<Args> {
1228 type Output;
1229}
1230
1231fn foo1<T, U, F: FnOnce(T) -> U>(x: T, f: F) -> U {}
1232fn foo2<T, U, F: FnOnce(T) -> U>(f: F, x: T) -> U {}
1233
1234struct S;
1235impl S {
1236 fn method(self) -> u64;
1237
1238 fn foo1<T, U, F: FnOnce(T) -> U>(self, x: T, f: F) -> U {}
1239 fn foo2<T, U, F: FnOnce(T) -> U>(self, f: F, x: T) -> U {}
1240}
1241
1242fn test() {
1243 let x1 = foo1(S, |s| s.method());
1244 let x2 = foo2(|s| s.method(), S);
1245 let x3 = S.foo1(S, |s| s.method());
1246 let x4 = S.foo2(|s| s.method(), S);
1247}
1248"#),
1249 @r###"
1250 [95; 96) 'x': T
1251 [101; 102) 'f': F
1252 [112; 114) '{}': ()
1253 [148; 149) 'f': F
1254 [154; 155) 'x': T
1255 [165; 167) '{}': ()
1256 [202; 206) 'self': S
1257 [254; 258) 'self': S
1258 [260; 261) 'x': T
1259 [266; 267) 'f': F
1260 [277; 279) '{}': ()
1261 [317; 321) 'self': S
1262 [323; 324) 'f': F
1263 [329; 330) 'x': T
1264 [340; 342) '{}': ()
1265 [356; 515) '{ ... S); }': ()
1266 [366; 368) 'x1': u64
1267 [371; 375) 'foo1': fn foo1<S, u64, |S| -> u64>(T, F) -> U
1268 [371; 394) 'foo1(S...hod())': u64
1269 [376; 377) 'S': S
1270 [379; 393) '|s| s.method()': |S| -> u64
1271 [380; 381) 's': S
1272 [383; 384) 's': S
1273 [383; 393) 's.method()': u64
1274 [404; 406) 'x2': u64
1275 [409; 413) 'foo2': fn foo2<S, u64, |S| -> u64>(F, T) -> U
1276 [409; 432) 'foo2(|...(), S)': u64
1277 [414; 428) '|s| s.method()': |S| -> u64
1278 [415; 416) 's': S
1279 [418; 419) 's': S
1280 [418; 428) 's.method()': u64
1281 [430; 431) 'S': S
1282 [442; 444) 'x3': u64
1283 [447; 448) 'S': S
1284 [447; 472) 'S.foo1...hod())': u64
1285 [454; 455) 'S': S
1286 [457; 471) '|s| s.method()': |S| -> u64
1287 [458; 459) 's': S
1288 [461; 462) 's': S
1289 [461; 471) 's.method()': u64
1290 [482; 484) 'x4': u64
1291 [487; 488) 'S': S
1292 [487; 512) 'S.foo2...(), S)': u64
1293 [494; 508) '|s| s.method()': |S| -> u64
1294 [495; 496) 's': S
1295 [498; 499) 's': S
1296 [498; 508) 's.method()': u64
1297 [510; 511) 'S': S
1298 "###
1299 );
1300}
1301
1302#[test]
1303fn unselected_projection_in_trait_env_1() {
1304 let t = type_at(
1305 r#"
1306//- /main.rs
1307trait Trait {
1308 type Item;
1309}
1310
1311trait Trait2 {
1312 fn foo(&self) -> u32;
1313}
1314
1315fn test<T: Trait>() where T::Item: Trait2 {
1316 let x: T::Item = no_matter;
1317 x.foo()<|>;
1318}
1319"#,
1320 );
1321 assert_eq!(t, "u32");
1322}
1323
1324#[test]
1325fn unselected_projection_in_trait_env_2() {
1326 let t = type_at(
1327 r#"
1328//- /main.rs
1329trait Trait<T> {
1330 type Item;
1331}
1332
1333trait Trait2 {
1334 fn foo(&self) -> u32;
1335}
1336
1337fn test<T, U>() where T::Item: Trait2, T: Trait<U::Item>, U: Trait<()> {
1338 let x: T::Item = no_matter;
1339 x.foo()<|>;
1340}
1341"#,
1342 );
1343 assert_eq!(t, "u32");
1344}
1345
1346#[test]
1347fn trait_impl_self_ty() {
1348 let t = type_at(
1349 r#"
1350//- /main.rs
1351trait Trait<T> {
1352 fn foo(&self);
1353}
1354
1355struct S;
1356
1357impl Trait<Self> for S {}
1358
1359fn test() {
1360 S.foo()<|>;
1361}
1362"#,
1363 );
1364 assert_eq!(t, "()");
1365}
1366
1367#[test]
1368fn trait_impl_self_ty_cycle() {
1369 let t = type_at(
1370 r#"
1371//- /main.rs
1372trait Trait {
1373 fn foo(&self);
1374}
1375
1376struct S<T>;
1377
1378impl Trait for S<Self> {}
1379
1380fn test() {
1381 S.foo()<|>;
1382}
1383"#,
1384 );
1385 assert_eq!(t, "{unknown}");
1386}
1387
1388#[test]
1389fn unselected_projection_in_trait_env_cycle_1() {
1390 let t = type_at(
1391 r#"
1392//- /main.rs
1393trait Trait {
1394 type Item;
1395}
1396
1397trait Trait2<T> {}
1398
1399fn test<T: Trait>() where T: Trait2<T::Item> {
1400 let x: T::Item = no_matter<|>;
1401}
1402"#,
1403 );
1404 // this is a legitimate cycle
1405 assert_eq!(t, "{unknown}");
1406}
1407
1408#[test]
1409fn unselected_projection_in_trait_env_cycle_2() {
1410 let t = type_at(
1411 r#"
1412//- /main.rs
1413trait Trait<T> {
1414 type Item;
1415}
1416
1417fn test<T, U>() where T: Trait<U::Item>, U: Trait<T::Item> {
1418 let x: T::Item = no_matter<|>;
1419}
1420"#,
1421 );
1422 // this is a legitimate cycle
1423 assert_eq!(t, "{unknown}");
1424}