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