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