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