]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests/simple.rs
internal: switch some tests to minicore
[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 //- minicore: deref
955 use core::ops::Deref;
956 struct A<T>(*mut T);
957
958 impl<T> A<T> {
959     fn foo(&self, x: &A<T>) -> &T {
960         &*x.0
961     }
962 }
963
964 struct B<T>(T);
965
966 impl<T> Deref for B<T> {
967     type Target = T;
968     fn deref(&self) -> &Self::Target {
969         &self.0
970     }
971 }
972
973 fn test(a: A<i32>) {
974     let t = A(0 as *mut _).foo(&&B(B(a)));
975 }
976 "#,
977         expect![[r#"
978             71..75 'self': &A<T>
979             77..78 'x': &A<T>
980             93..114 '{     ...     }': &T
981             103..108 '&*x.0': &T
982             104..108 '*x.0': T
983             105..106 'x': &A<T>
984             105..108 'x.0': *mut T
985             195..199 'self': &B<T>
986             218..241 '{     ...     }': &T
987             228..235 '&self.0': &T
988             229..233 'self': &B<T>
989             229..235 'self.0': T
990             253..254 'a': A<i32>
991             264..310 '{     ...))); }': ()
992             274..275 't': &i32
993             278..279 'A': A<i32>(*mut i32) -> A<i32>
994             278..292 'A(0 as *mut _)': A<i32>
995             278..307 'A(0 as...B(a)))': &i32
996             280..281 '0': i32
997             280..291 '0 as *mut _': *mut i32
998             297..306 '&&B(B(a))': &&B<B<A<i32>>>
999             298..306 '&B(B(a))': &B<B<A<i32>>>
1000             299..300 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>>
1001             299..306 'B(B(a))': B<B<A<i32>>>
1002             301..302 'B': B<A<i32>>(A<i32>) -> B<A<i32>>
1003             301..305 'B(a)': B<A<i32>>
1004             303..304 'a': A<i32>
1005         "#]],
1006     );
1007 }
1008
1009 #[test]
1010 fn infer_in_elseif() {
1011     check_infer(
1012         r#"
1013 struct Foo { field: i32 }
1014 fn main(foo: Foo) {
1015     if true {
1016
1017     } else if false {
1018         foo.field
1019     }
1020 }
1021 "#,
1022         expect![[r#"
1023             34..37 'foo': Foo
1024             44..108 '{     ...   } }': ()
1025             50..106 'if tru...     }': ()
1026             53..57 'true': bool
1027             58..66 '{      }': ()
1028             72..106 'if fal...     }': i32
1029             75..80 'false': bool
1030             81..106 '{     ...     }': i32
1031             91..94 'foo': Foo
1032             91..100 'foo.field': i32
1033         "#]],
1034     )
1035 }
1036
1037 #[test]
1038 fn infer_if_match_with_return() {
1039     check_infer(
1040         r#"
1041 fn foo() {
1042     let _x1 = if true {
1043         1
1044     } else {
1045         return;
1046     };
1047     let _x2 = if true {
1048         2
1049     } else {
1050         return
1051     };
1052     let _x3 = match true {
1053         true => 3,
1054         _ => {
1055             return;
1056         }
1057     };
1058     let _x4 = match true {
1059         true => 4,
1060         _ => return
1061     };
1062 }
1063 "#,
1064         expect![[r#"
1065             9..322 '{     ...  }; }': ()
1066             19..22 '_x1': i32
1067             25..79 'if tru...     }': i32
1068             28..32 'true': bool
1069             33..50 '{     ...     }': i32
1070             43..44 '1': i32
1071             56..79 '{     ...     }': i32
1072             66..72 'return': !
1073             89..92 '_x2': i32
1074             95..148 'if tru...     }': i32
1075             98..102 'true': bool
1076             103..120 '{     ...     }': i32
1077             113..114 '2': i32
1078             126..148 '{     ...     }': !
1079             136..142 'return': !
1080             158..161 '_x3': i32
1081             164..246 'match ...     }': i32
1082             170..174 'true': bool
1083             185..189 'true': bool
1084             185..189 'true': bool
1085             193..194 '3': i32
1086             204..205 '_': bool
1087             209..240 '{     ...     }': i32
1088             223..229 'return': !
1089             256..259 '_x4': i32
1090             262..319 'match ...     }': i32
1091             268..272 'true': bool
1092             283..287 'true': bool
1093             283..287 'true': bool
1094             291..292 '4': i32
1095             302..303 '_': bool
1096             307..313 'return': !
1097         "#]],
1098     )
1099 }
1100
1101 #[test]
1102 fn infer_inherent_method() {
1103     check_infer(
1104         r#"
1105         struct A;
1106
1107         impl A {
1108             fn foo(self, x: u32) -> i32 {}
1109         }
1110
1111         mod b {
1112             impl super::A {
1113                 pub fn bar(&self, x: u64) -> i64 {}
1114             }
1115         }
1116
1117         fn test(a: A) {
1118             a.foo(1);
1119             (&a).bar(1);
1120             a.bar(1);
1121         }
1122         "#,
1123         expect![[r#"
1124             31..35 'self': A
1125             37..38 'x': u32
1126             52..54 '{}': ()
1127             106..110 'self': &A
1128             112..113 'x': u64
1129             127..129 '{}': ()
1130             147..148 'a': A
1131             153..201 '{     ...(1); }': ()
1132             159..160 'a': A
1133             159..167 'a.foo(1)': i32
1134             165..166 '1': u32
1135             173..184 '(&a).bar(1)': i64
1136             174..176 '&a': &A
1137             175..176 'a': A
1138             182..183 '1': u64
1139             190..191 'a': A
1140             190..198 'a.bar(1)': i64
1141             196..197 '1': u64
1142         "#]],
1143     );
1144 }
1145
1146 #[test]
1147 fn infer_inherent_method_str() {
1148     check_infer(
1149         r#"
1150         #[lang = "str"]
1151         impl str {
1152             fn foo(&self) -> i32 {}
1153         }
1154
1155         fn test() {
1156             "foo".foo();
1157         }
1158         "#,
1159         expect![[r#"
1160             39..43 'self': &str
1161             52..54 '{}': ()
1162             68..88 '{     ...o(); }': ()
1163             74..79 '"foo"': &str
1164             74..85 '"foo".foo()': i32
1165         "#]],
1166     );
1167 }
1168
1169 #[test]
1170 fn infer_tuple() {
1171     check_infer(
1172         r#"
1173         fn test(x: &str, y: isize) {
1174             let a: (u32, &str) = (1, "a");
1175             let b = (a, x);
1176             let c = (y, x);
1177             let d = (c, x);
1178             let e = (1, "e");
1179             let f = (e, "d");
1180         }
1181         "#,
1182         expect![[r#"
1183             8..9 'x': &str
1184             17..18 'y': isize
1185             27..169 '{     ...d"); }': ()
1186             37..38 'a': (u32, &str)
1187             54..62 '(1, "a")': (u32, &str)
1188             55..56 '1': u32
1189             58..61 '"a"': &str
1190             72..73 'b': ((u32, &str), &str)
1191             76..82 '(a, x)': ((u32, &str), &str)
1192             77..78 'a': (u32, &str)
1193             80..81 'x': &str
1194             92..93 'c': (isize, &str)
1195             96..102 '(y, x)': (isize, &str)
1196             97..98 'y': isize
1197             100..101 'x': &str
1198             112..113 'd': ((isize, &str), &str)
1199             116..122 '(c, x)': ((isize, &str), &str)
1200             117..118 'c': (isize, &str)
1201             120..121 'x': &str
1202             132..133 'e': (i32, &str)
1203             136..144 '(1, "e")': (i32, &str)
1204             137..138 '1': i32
1205             140..143 '"e"': &str
1206             154..155 'f': ((i32, &str), &str)
1207             158..166 '(e, "d")': ((i32, &str), &str)
1208             159..160 'e': (i32, &str)
1209             162..165 '"d"': &str
1210         "#]],
1211     );
1212 }
1213
1214 #[test]
1215 fn infer_array() {
1216     check_infer(
1217         r#"
1218         fn test(x: &str, y: isize) {
1219             let a = [x];
1220             let b = [a, a];
1221             let c = [b, b];
1222
1223             let d = [y, 1, 2, 3];
1224             let d = [1, y, 2, 3];
1225             let e = [y];
1226             let f = [d, d];
1227             let g = [e, e];
1228
1229             let h = [1, 2];
1230             let i = ["a", "b"];
1231
1232             let b = [a, ["b"]];
1233             let x: [u8; 0] = [];
1234             // FIXME: requires const evaluation/taking type from rhs somehow
1235             let y: [u8; 2+2] = [1,2,3,4];
1236         }
1237         "#,
1238         expect![[r#"
1239             8..9 'x': &str
1240             17..18 'y': isize
1241             27..395 '{     ...,4]; }': ()
1242             37..38 'a': [&str; 1]
1243             41..44 '[x]': [&str; 1]
1244             42..43 'x': &str
1245             54..55 'b': [[&str; 1]; 2]
1246             58..64 '[a, a]': [[&str; 1]; 2]
1247             59..60 'a': [&str; 1]
1248             62..63 'a': [&str; 1]
1249             74..75 'c': [[[&str; 1]; 2]; 2]
1250             78..84 '[b, b]': [[[&str; 1]; 2]; 2]
1251             79..80 'b': [[&str; 1]; 2]
1252             82..83 'b': [[&str; 1]; 2]
1253             95..96 'd': [isize; 4]
1254             99..111 '[y, 1, 2, 3]': [isize; 4]
1255             100..101 'y': isize
1256             103..104 '1': isize
1257             106..107 '2': isize
1258             109..110 '3': isize
1259             121..122 'd': [isize; 4]
1260             125..137 '[1, y, 2, 3]': [isize; 4]
1261             126..127 '1': isize
1262             129..130 'y': isize
1263             132..133 '2': isize
1264             135..136 '3': isize
1265             147..148 'e': [isize; 1]
1266             151..154 '[y]': [isize; 1]
1267             152..153 'y': isize
1268             164..165 'f': [[isize; 4]; 2]
1269             168..174 '[d, d]': [[isize; 4]; 2]
1270             169..170 'd': [isize; 4]
1271             172..173 'd': [isize; 4]
1272             184..185 'g': [[isize; 1]; 2]
1273             188..194 '[e, e]': [[isize; 1]; 2]
1274             189..190 'e': [isize; 1]
1275             192..193 'e': [isize; 1]
1276             205..206 'h': [i32; 2]
1277             209..215 '[1, 2]': [i32; 2]
1278             210..211 '1': i32
1279             213..214 '2': i32
1280             225..226 'i': [&str; 2]
1281             229..239 '["a", "b"]': [&str; 2]
1282             230..233 '"a"': &str
1283             235..238 '"b"': &str
1284             250..251 'b': [[&str; 1]; 2]
1285             254..264 '[a, ["b"]]': [[&str; 1]; 2]
1286             255..256 'a': [&str; 1]
1287             258..263 '["b"]': [&str; 1]
1288             259..262 '"b"': &str
1289             274..275 'x': [u8; 0]
1290             287..289 '[]': [u8; 0]
1291             368..369 'y': [u8; _]
1292             383..392 '[1,2,3,4]': [u8; 4]
1293             384..385 '1': u8
1294             386..387 '2': u8
1295             388..389 '3': u8
1296             390..391 '4': u8
1297         "#]],
1298     );
1299 }
1300
1301 #[test]
1302 fn infer_struct_generics() {
1303     check_infer(
1304         r#"
1305         struct A<T> {
1306             x: T,
1307         }
1308
1309         fn test(a1: A<u32>, i: i32) {
1310             a1.x;
1311             let a2 = A { x: i };
1312             a2.x;
1313             let a3 = A::<i128> { x: 1 };
1314             a3.x;
1315         }
1316         "#,
1317         expect![[r#"
1318             35..37 'a1': A<u32>
1319             47..48 'i': i32
1320             55..146 '{     ...3.x; }': ()
1321             61..63 'a1': A<u32>
1322             61..65 'a1.x': u32
1323             75..77 'a2': A<i32>
1324             80..90 'A { x: i }': A<i32>
1325             87..88 'i': i32
1326             96..98 'a2': A<i32>
1327             96..100 'a2.x': i32
1328             110..112 'a3': A<i128>
1329             115..133 'A::<i1...x: 1 }': A<i128>
1330             130..131 '1': i128
1331             139..141 'a3': A<i128>
1332             139..143 'a3.x': i128
1333         "#]],
1334     );
1335 }
1336
1337 #[test]
1338 fn infer_tuple_struct_generics() {
1339     check_infer(
1340         r#"
1341         struct A<T>(T);
1342         enum Option<T> { Some(T), None }
1343         use Option::*;
1344
1345         fn test() {
1346             A(42);
1347             A(42u128);
1348             Some("x");
1349             Option::Some("x");
1350             None;
1351             let x: Option<i64> = None;
1352         }
1353         "#,
1354         expect![[r#"
1355             75..183 '{     ...one; }': ()
1356             81..82 'A': A<i32>(i32) -> A<i32>
1357             81..86 'A(42)': A<i32>
1358             83..85 '42': i32
1359             92..93 'A': A<u128>(u128) -> A<u128>
1360             92..101 'A(42u128)': A<u128>
1361             94..100 '42u128': u128
1362             107..111 'Some': Some<&str>(&str) -> Option<&str>
1363             107..116 'Some("x")': Option<&str>
1364             112..115 '"x"': &str
1365             122..134 'Option::Some': Some<&str>(&str) -> Option<&str>
1366             122..139 'Option...e("x")': Option<&str>
1367             135..138 '"x"': &str
1368             145..149 'None': Option<{unknown}>
1369             159..160 'x': Option<i64>
1370             176..180 'None': Option<i64>
1371         "#]],
1372     );
1373 }
1374
1375 #[test]
1376 fn infer_function_generics() {
1377     check_infer(
1378         r#"
1379         fn id<T>(t: T) -> T { t }
1380
1381         fn test() {
1382             id(1u32);
1383             id::<i128>(1);
1384             let x: u64 = id(1);
1385         }
1386         "#,
1387         expect![[r#"
1388             9..10 't': T
1389             20..25 '{ t }': T
1390             22..23 't': T
1391             37..97 '{     ...(1); }': ()
1392             43..45 'id': fn id<u32>(u32) -> u32
1393             43..51 'id(1u32)': u32
1394             46..50 '1u32': u32
1395             57..67 'id::<i128>': fn id<i128>(i128) -> i128
1396             57..70 'id::<i128>(1)': i128
1397             68..69 '1': i128
1398             80..81 'x': u64
1399             89..91 'id': fn id<u64>(u64) -> u64
1400             89..94 'id(1)': u64
1401             92..93 '1': u64
1402         "#]],
1403     );
1404 }
1405
1406 #[test]
1407 fn infer_impl_generics_basic() {
1408     check_infer(
1409         r#"
1410         struct A<T1, T2> {
1411             x: T1,
1412             y: T2,
1413         }
1414         impl<Y, X> A<X, Y> {
1415             fn x(self) -> X {
1416                 self.x
1417             }
1418             fn y(self) -> Y {
1419                 self.y
1420             }
1421             fn z<T>(self, t: T) -> (X, Y, T) {
1422                 (self.x, self.y, t)
1423             }
1424         }
1425
1426         fn test() -> i128 {
1427             let a = A { x: 1u64, y: 1i64 };
1428             a.x();
1429             a.y();
1430             a.z(1i128);
1431             a.z::<u128>(1);
1432         }
1433         "#,
1434         expect![[r#"
1435             73..77 'self': A<X, Y>
1436             84..106 '{     ...     }': X
1437             94..98 'self': A<X, Y>
1438             94..100 'self.x': X
1439             116..120 'self': A<X, Y>
1440             127..149 '{     ...     }': Y
1441             137..141 'self': A<X, Y>
1442             137..143 'self.y': Y
1443             162..166 'self': A<X, Y>
1444             168..169 't': T
1445             187..222 '{     ...     }': (X, Y, T)
1446             197..216 '(self.....y, t)': (X, Y, T)
1447             198..202 'self': A<X, Y>
1448             198..204 'self.x': X
1449             206..210 'self': A<X, Y>
1450             206..212 'self.y': Y
1451             214..215 't': T
1452             244..341 '{     ...(1); }': ()
1453             254..255 'a': A<u64, i64>
1454             258..280 'A { x:...1i64 }': A<u64, i64>
1455             265..269 '1u64': u64
1456             274..278 '1i64': i64
1457             286..287 'a': A<u64, i64>
1458             286..291 'a.x()': u64
1459             297..298 'a': A<u64, i64>
1460             297..302 'a.y()': i64
1461             308..309 'a': A<u64, i64>
1462             308..318 'a.z(1i128)': (u64, i64, i128)
1463             312..317 '1i128': i128
1464             324..325 'a': A<u64, i64>
1465             324..338 'a.z::<u128>(1)': (u64, i64, u128)
1466             336..337 '1': u128
1467         "#]],
1468     );
1469 }
1470
1471 #[test]
1472 fn infer_impl_generics_with_autoderef() {
1473     check_infer(
1474         r#"
1475         enum Option<T> {
1476             Some(T),
1477             None,
1478         }
1479         impl<T> Option<T> {
1480             fn as_ref(&self) -> Option<&T> {}
1481         }
1482         fn test(o: Option<u32>) {
1483             (&o).as_ref();
1484             o.as_ref();
1485         }
1486         "#,
1487         expect![[r#"
1488             77..81 'self': &Option<T>
1489             97..99 '{}': ()
1490             110..111 'o': Option<u32>
1491             126..164 '{     ...f(); }': ()
1492             132..145 '(&o).as_ref()': Option<&u32>
1493             133..135 '&o': &Option<u32>
1494             134..135 'o': Option<u32>
1495             151..152 'o': Option<u32>
1496             151..161 'o.as_ref()': Option<&u32>
1497         "#]],
1498     );
1499 }
1500
1501 #[test]
1502 fn infer_generic_chain() {
1503     check_infer(
1504         r#"
1505         struct A<T> {
1506             x: T,
1507         }
1508         impl<T2> A<T2> {
1509             fn x(self) -> T2 {
1510                 self.x
1511             }
1512         }
1513         fn id<T>(t: T) -> T { t }
1514
1515         fn test() -> i128 {
1516             let x = 1;
1517             let y = id(x);
1518             let a = A { x: id(y) };
1519             let z = id(a.x);
1520             let b = A { x: z };
1521             b.x()
1522         }
1523         "#,
1524         expect![[r#"
1525             52..56 'self': A<T2>
1526             64..86 '{     ...     }': T2
1527             74..78 'self': A<T2>
1528             74..80 'self.x': T2
1529             98..99 't': T
1530             109..114 '{ t }': T
1531             111..112 't': T
1532             134..254 '{     ....x() }': i128
1533             144..145 'x': i128
1534             148..149 '1': i128
1535             159..160 'y': i128
1536             163..165 'id': fn id<i128>(i128) -> i128
1537             163..168 'id(x)': i128
1538             166..167 'x': i128
1539             178..179 'a': A<i128>
1540             182..196 'A { x: id(y) }': A<i128>
1541             189..191 'id': fn id<i128>(i128) -> i128
1542             189..194 'id(y)': i128
1543             192..193 'y': i128
1544             206..207 'z': i128
1545             210..212 'id': fn id<i128>(i128) -> i128
1546             210..217 'id(a.x)': i128
1547             213..214 'a': A<i128>
1548             213..216 'a.x': i128
1549             227..228 'b': A<i128>
1550             231..241 'A { x: z }': A<i128>
1551             238..239 'z': i128
1552             247..248 'b': A<i128>
1553             247..252 'b.x()': i128
1554         "#]],
1555     );
1556 }
1557
1558 #[test]
1559 fn infer_associated_const() {
1560     check_infer(
1561         r#"
1562         struct Struct;
1563
1564         impl Struct {
1565             const FOO: u32 = 1;
1566         }
1567
1568         enum Enum {}
1569
1570         impl Enum {
1571             const BAR: u32 = 2;
1572         }
1573
1574         trait Trait {
1575             const ID: u32;
1576         }
1577
1578         struct TraitTest;
1579
1580         impl Trait for TraitTest {
1581             const ID: u32 = 5;
1582         }
1583
1584         fn test() {
1585             let x = Struct::FOO;
1586             let y = Enum::BAR;
1587             let z = TraitTest::ID;
1588         }
1589         "#,
1590         expect![[r#"
1591             51..52 '1': u32
1592             104..105 '2': u32
1593             212..213 '5': u32
1594             228..306 '{     ...:ID; }': ()
1595             238..239 'x': u32
1596             242..253 'Struct::FOO': u32
1597             263..264 'y': u32
1598             267..276 'Enum::BAR': u32
1599             286..287 'z': u32
1600             290..303 'TraitTest::ID': u32
1601         "#]],
1602     );
1603 }
1604
1605 #[test]
1606 fn infer_type_alias() {
1607     check_infer(
1608         r#"
1609         struct A<X, Y> { x: X, y: Y }
1610         type Foo = A<u32, i128>;
1611         type Bar<T> = A<T, u128>;
1612         type Baz<U, V> = A<V, U>;
1613         fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
1614             x.x;
1615             x.y;
1616             y.x;
1617             y.y;
1618             z.x;
1619             z.y;
1620         }
1621         "#,
1622         expect![[r#"
1623             115..116 'x': A<u32, i128>
1624             123..124 'y': A<&str, u128>
1625             137..138 'z': A<u8, i8>
1626             153..210 '{     ...z.y; }': ()
1627             159..160 'x': A<u32, i128>
1628             159..162 'x.x': u32
1629             168..169 'x': A<u32, i128>
1630             168..171 'x.y': i128
1631             177..178 'y': A<&str, u128>
1632             177..180 'y.x': &str
1633             186..187 'y': A<&str, u128>
1634             186..189 'y.y': u128
1635             195..196 'z': A<u8, i8>
1636             195..198 'z.x': u8
1637             204..205 'z': A<u8, i8>
1638             204..207 'z.y': i8
1639         "#]],
1640     )
1641 }
1642
1643 #[test]
1644 fn recursive_type_alias() {
1645     check_infer(
1646         r#"
1647         struct A<X> {}
1648         type Foo = Foo;
1649         type Bar = A<Bar>;
1650         fn test(x: Foo) {}
1651         "#,
1652         expect![[r#"
1653             58..59 'x': {unknown}
1654             66..68 '{}': ()
1655         "#]],
1656     )
1657 }
1658
1659 #[test]
1660 fn infer_type_param() {
1661     check_infer(
1662         r#"
1663         fn id<T>(x: T) -> T {
1664             x
1665         }
1666
1667         fn clone<T>(x: &T) -> T {
1668             *x
1669         }
1670
1671         fn test() {
1672             let y = 10u32;
1673             id(y);
1674             let x: bool = clone(z);
1675             id::<i128>(1);
1676         }
1677         "#,
1678         expect![[r#"
1679             9..10 'x': T
1680             20..29 '{     x }': T
1681             26..27 'x': T
1682             43..44 'x': &T
1683             55..65 '{     *x }': T
1684             61..63 '*x': T
1685             62..63 'x': &T
1686             77..157 '{     ...(1); }': ()
1687             87..88 'y': u32
1688             91..96 '10u32': u32
1689             102..104 'id': fn id<u32>(u32) -> u32
1690             102..107 'id(y)': u32
1691             105..106 'y': u32
1692             117..118 'x': bool
1693             127..132 'clone': fn clone<bool>(&bool) -> bool
1694             127..135 'clone(z)': bool
1695             133..134 'z': &bool
1696             141..151 'id::<i128>': fn id<i128>(i128) -> i128
1697             141..154 'id::<i128>(1)': i128
1698             152..153 '1': i128
1699         "#]],
1700     );
1701 }
1702
1703 #[test]
1704 fn infer_const() {
1705     check_infer(
1706         r#"
1707         struct Foo;
1708         impl Foo { const ASSOC_CONST: u32 = 0; }
1709         const GLOBAL_CONST: u32 = 101;
1710         fn test() {
1711             const LOCAL_CONST: u32 = 99;
1712             let x = LOCAL_CONST;
1713             let z = GLOBAL_CONST;
1714             let id = Foo::ASSOC_CONST;
1715         }
1716         "#,
1717         expect![[r#"
1718             48..49 '0': u32
1719             79..82 '101': u32
1720             94..212 '{     ...NST; }': ()
1721             137..138 'x': u32
1722             141..152 'LOCAL_CONST': u32
1723             162..163 'z': u32
1724             166..178 'GLOBAL_CONST': u32
1725             188..190 'id': u32
1726             193..209 'Foo::A..._CONST': u32
1727             125..127 '99': u32
1728         "#]],
1729     );
1730 }
1731
1732 #[test]
1733 fn infer_static() {
1734     check_infer(
1735         r#"
1736         static GLOBAL_STATIC: u32 = 101;
1737         static mut GLOBAL_STATIC_MUT: u32 = 101;
1738         fn test() {
1739             static LOCAL_STATIC: u32 = 99;
1740             static mut LOCAL_STATIC_MUT: u32 = 99;
1741             let x = LOCAL_STATIC;
1742             let y = LOCAL_STATIC_MUT;
1743             let z = GLOBAL_STATIC;
1744             let w = GLOBAL_STATIC_MUT;
1745         }
1746         "#,
1747         expect![[r#"
1748             28..31 '101': u32
1749             69..72 '101': u32
1750             84..279 '{     ...MUT; }': ()
1751             172..173 'x': u32
1752             176..188 'LOCAL_STATIC': u32
1753             198..199 'y': u32
1754             202..218 'LOCAL_...IC_MUT': u32
1755             228..229 'z': u32
1756             232..245 'GLOBAL_STATIC': u32
1757             255..256 'w': u32
1758             259..276 'GLOBAL...IC_MUT': u32
1759             117..119 '99': u32
1760             160..162 '99': u32
1761         "#]],
1762     );
1763 }
1764
1765 #[test]
1766 fn shadowing_primitive() {
1767     check_types(
1768         r#"
1769 struct i32;
1770 struct Foo;
1771
1772 impl i32 { fn foo(&self) -> Foo { Foo } }
1773
1774 fn main() {
1775     let x: i32 = i32;
1776     x.foo();
1777         //^ Foo
1778 }"#,
1779     );
1780 }
1781
1782 #[test]
1783 fn shadowing_primitive_with_inner_items() {
1784     check_types(
1785         r#"
1786 struct i32;
1787 struct Foo;
1788
1789 impl i32 { fn foo(&self) -> Foo { Foo } }
1790
1791 fn main() {
1792     fn inner() {}
1793     let x: i32 = i32;
1794     x.foo();
1795         //^ Foo
1796 }"#,
1797     );
1798 }
1799
1800 #[test]
1801 fn not_shadowing_primitive_by_module() {
1802     check_types(
1803         r#"
1804 //- /str.rs
1805 fn foo() {}
1806
1807 //- /main.rs
1808 mod str;
1809 fn foo() -> &'static str { "" }
1810
1811 fn main() {
1812     foo();
1813       //^ &str
1814 }"#,
1815     );
1816 }
1817
1818 #[test]
1819 fn not_shadowing_module_by_primitive() {
1820     check_types(
1821         r#"
1822 //- /str.rs
1823 fn foo() -> u32 {0}
1824
1825 //- /main.rs
1826 mod str;
1827 fn foo() -> &'static str { "" }
1828
1829 fn main() {
1830     str::foo();
1831            //^ u32
1832 }"#,
1833     );
1834 }
1835
1836 // This test is actually testing the shadowing behavior within hir_def. It
1837 // lives here because the testing infrastructure in hir_def isn't currently
1838 // capable of asserting the necessary conditions.
1839 #[test]
1840 fn should_be_shadowing_imports() {
1841     check_types(
1842         r#"
1843 mod a {
1844     pub fn foo() -> i8 {0}
1845     pub struct foo { a: i8 }
1846 }
1847 mod b { pub fn foo () -> u8 {0} }
1848 mod c { pub struct foo { a: u8 } }
1849 mod d {
1850     pub use super::a::*;
1851     pub use super::c::foo;
1852     pub use super::b::foo;
1853 }
1854
1855 fn main() {
1856     d::foo();
1857          //^ u8
1858     d::foo{a:0};
1859            //^ u8
1860 }"#,
1861     );
1862 }
1863
1864 #[test]
1865 fn closure_return() {
1866     check_infer(
1867         r#"
1868         fn foo() -> u32 {
1869             let x = || -> usize { return 1; };
1870         }
1871         "#,
1872         expect![[r#"
1873             16..58 '{     ...; }; }': ()
1874             26..27 'x': || -> usize
1875             30..55 '|| -> ...n 1; }': || -> usize
1876             42..55 '{ return 1; }': usize
1877             44..52 'return 1': !
1878             51..52 '1': usize
1879         "#]],
1880     );
1881 }
1882
1883 #[test]
1884 fn closure_return_unit() {
1885     check_infer(
1886         r#"
1887         fn foo() -> u32 {
1888             let x = || { return; };
1889         }
1890         "#,
1891         expect![[r#"
1892             16..47 '{     ...; }; }': ()
1893             26..27 'x': || -> ()
1894             30..44 '|| { return; }': || -> ()
1895             33..44 '{ return; }': ()
1896             35..41 'return': !
1897         "#]],
1898     );
1899 }
1900
1901 #[test]
1902 fn closure_return_inferred() {
1903     check_infer(
1904         r#"
1905         fn foo() -> u32 {
1906             let x = || { "test" };
1907         }
1908         "#,
1909         expect![[r#"
1910             16..46 '{     ..." }; }': ()
1911             26..27 'x': || -> &str
1912             30..43 '|| { "test" }': || -> &str
1913             33..43 '{ "test" }': &str
1914             35..41 '"test"': &str
1915         "#]],
1916     );
1917 }
1918
1919 #[test]
1920 fn fn_pointer_return() {
1921     check_infer(
1922         r#"
1923         struct Vtable {
1924             method: fn(),
1925         }
1926
1927         fn main() {
1928             let vtable = Vtable { method: || {} };
1929             let m = vtable.method;
1930         }
1931         "#,
1932         expect![[r#"
1933             47..120 '{     ...hod; }': ()
1934             57..63 'vtable': Vtable
1935             66..90 'Vtable...| {} }': Vtable
1936             83..88 '|| {}': || -> ()
1937             86..88 '{}': ()
1938             100..101 'm': fn()
1939             104..110 'vtable': Vtable
1940             104..117 'vtable.method': fn()
1941         "#]],
1942     );
1943 }
1944
1945 #[test]
1946 fn effects_smoke_test() {
1947     check_infer(
1948         r#"
1949         async fn main() {
1950             let x = unsafe { 92 };
1951             let y = async { async { () }.await };
1952             let z = try { () };
1953             let w = const { 92 };
1954             let t = 'a: { 92 };
1955         }
1956
1957         #[prelude_import] use future::*;
1958
1959         mod future {
1960             #[lang = "future_trait"]
1961             pub trait Future { type Output; }
1962         }
1963         "#,
1964         expect![[r#"
1965             16..162 '{     ...2 }; }': ()
1966             26..27 'x': i32
1967             30..43 'unsafe { 92 }': i32
1968             37..43 '{ 92 }': i32
1969             39..41 '92': i32
1970             53..54 'y': impl Future<Output = ()>
1971             57..85 'async ...wait }': impl Future<Output = ()>
1972             63..85 '{ asyn...wait }': ()
1973             65..77 'async { () }': impl Future<Output = ()>
1974             65..83 'async ....await': ()
1975             71..77 '{ () }': ()
1976             73..75 '()': ()
1977             95..96 'z': {unknown}
1978             99..109 'try { () }': {unknown}
1979             103..109 '{ () }': ()
1980             105..107 '()': ()
1981             119..120 'w': i32
1982             123..135 'const { 92 }': i32
1983             129..135 '{ 92 }': i32
1984             131..133 '92': i32
1985             145..146 't': i32
1986             153..159 '{ 92 }': i32
1987             155..157 '92': i32
1988         "#]],
1989     )
1990 }
1991
1992 #[test]
1993 fn infer_generic_from_later_assignment() {
1994     check_infer(
1995         r#"
1996         enum Option<T> { Some(T), None }
1997         use Option::*;
1998
1999         fn test() {
2000             let mut end = None;
2001             loop {
2002                 end = Some(true);
2003             }
2004         }
2005         "#,
2006         expect![[r#"
2007             59..129 '{     ...   } }': ()
2008             69..76 'mut end': Option<bool>
2009             79..83 'None': Option<bool>
2010             89..127 'loop {...     }': !
2011             94..127 '{     ...     }': ()
2012             104..107 'end': Option<bool>
2013             104..120 'end = ...(true)': ()
2014             110..114 'Some': Some<bool>(bool) -> Option<bool>
2015             110..120 'Some(true)': Option<bool>
2016             115..119 'true': bool
2017         "#]],
2018     );
2019 }
2020
2021 #[test]
2022 fn infer_loop_break_with_val() {
2023     check_infer(
2024         r#"
2025         enum Option<T> { Some(T), None }
2026         use Option::*;
2027
2028         fn test() {
2029             let x = loop {
2030                 if false {
2031                     break None;
2032                 }
2033
2034                 break Some(true);
2035             };
2036         }
2037         "#,
2038         expect![[r#"
2039             59..168 '{     ...  }; }': ()
2040             69..70 'x': Option<bool>
2041             73..165 'loop {...     }': Option<bool>
2042             78..165 '{     ...     }': ()
2043             88..132 'if fal...     }': ()
2044             91..96 'false': bool
2045             97..132 '{     ...     }': ()
2046             111..121 'break None': !
2047             117..121 'None': Option<bool>
2048             142..158 'break ...(true)': !
2049             148..152 'Some': Some<bool>(bool) -> Option<bool>
2050             148..158 'Some(true)': Option<bool>
2051             153..157 'true': bool
2052         "#]],
2053     );
2054 }
2055
2056 #[test]
2057 fn infer_loop_break_without_val() {
2058     check_infer(
2059         r#"
2060         enum Option<T> { Some(T), None }
2061         use Option::*;
2062
2063         fn test() {
2064             let x = loop {
2065                 if false {
2066                     break;
2067                 }
2068             };
2069         }
2070         "#,
2071         expect![[r#"
2072             59..136 '{     ...  }; }': ()
2073             69..70 'x': ()
2074             73..133 'loop {...     }': ()
2075             78..133 '{     ...     }': ()
2076             88..127 'if fal...     }': ()
2077             91..96 'false': bool
2078             97..127 '{     ...     }': ()
2079             111..116 'break': !
2080         "#]],
2081     );
2082 }
2083
2084 #[test]
2085 fn infer_labelled_break_with_val() {
2086     check_infer(
2087         r#"
2088         fn foo() {
2089             let _x = || 'outer: loop {
2090                 let inner = 'inner: loop {
2091                     let i = Default::default();
2092                     if (break 'outer i) {
2093                         loop { break 'inner 5i8; };
2094                     } else if true {
2095                         break 'inner 6;
2096                     }
2097                     break 7;
2098                 };
2099                 break inner < 8;
2100             };
2101         }
2102         "#,
2103         expect![[r#"
2104             9..335 '{     ...  }; }': ()
2105             19..21 '_x': || -> bool
2106             24..332 '|| 'ou...     }': || -> bool
2107             27..332 ''outer...     }': bool
2108             40..332 '{     ...     }': ()
2109             54..59 'inner': i8
2110             62..300 ''inner...     }': i8
2111             75..300 '{     ...     }': ()
2112             93..94 'i': bool
2113             97..113 'Defaul...efault': {unknown}
2114             97..115 'Defaul...ault()': bool
2115             129..269 'if (br...     }': ()
2116             133..147 'break 'outer i': !
2117             146..147 'i': bool
2118             149..208 '{     ...     }': ()
2119             167..193 'loop {...5i8; }': !
2120             172..193 '{ brea...5i8; }': ()
2121             174..190 'break ...er 5i8': !
2122             187..190 '5i8': i8
2123             214..269 'if tru...     }': ()
2124             217..221 'true': bool
2125             222..269 '{     ...     }': ()
2126             240..254 'break 'inner 6': !
2127             253..254 '6': i8
2128             282..289 'break 7': !
2129             288..289 '7': i8
2130             310..325 'break inner < 8': !
2131             316..321 'inner': i8
2132             316..325 'inner < 8': bool
2133             324..325 '8': i8
2134         "#]],
2135     );
2136 }
2137
2138 #[test]
2139 fn infer_labelled_block_break_with_val() {
2140     check_infer(
2141         r#"
2142         fn default<T>() -> T { loop {} }
2143         fn foo() {
2144             let _x = 'outer: {
2145                 let inner = 'inner: {
2146                     let i = default();
2147                     if (break 'outer i) {
2148                         break 'inner 5i8;
2149                     } else if true {
2150                         break 'inner 6;
2151                     }
2152                     break 'inner 'innermost: { 0 };
2153                     42
2154                 };
2155                 break 'outer inner < 8;
2156             };
2157         }
2158         "#,
2159         expect![[r#"
2160             21..32 '{ loop {} }': T
2161             23..30 'loop {}': !
2162             28..30 '{}': ()
2163             42..381 '{     ...  }; }': ()
2164             52..54 '_x': bool
2165             65..378 '{     ...     }': bool
2166             79..84 'inner': i8
2167             95..339 '{     ...     }': i8
2168             113..114 'i': bool
2169             117..124 'default': fn default<bool>() -> bool
2170             117..126 'default()': bool
2171             140..270 'if (br...     }': ()
2172             144..158 'break 'outer i': !
2173             157..158 'i': bool
2174             160..209 '{     ...     }': ()
2175             178..194 'break ...er 5i8': !
2176             191..194 '5i8': i8
2177             215..270 'if tru...     }': ()
2178             218..222 'true': bool
2179             223..270 '{     ...     }': ()
2180             241..255 'break 'inner 6': !
2181             254..255 '6': i8
2182             283..313 'break ... { 0 }': !
2183             308..313 '{ 0 }': i8
2184             310..311 '0': i8
2185             327..329 '42': i8
2186             349..371 'break ...er < 8': !
2187             362..367 'inner': i8
2188             362..371 'inner < 8': bool
2189             370..371 '8': i8
2190         "#]],
2191     );
2192 }
2193
2194 #[test]
2195 fn generic_default() {
2196     check_infer(
2197         r#"
2198         struct Thing<T = ()> { t: T }
2199         enum OtherThing<T = ()> {
2200             One { t: T },
2201             Two(T),
2202         }
2203
2204         fn test(t1: Thing, t2: OtherThing, t3: Thing<i32>, t4: OtherThing<i32>) {
2205             t1.t;
2206             t3.t;
2207             match t2 {
2208                 OtherThing::One { t } => { t; },
2209                 OtherThing::Two(t) => { t; },
2210             }
2211             match t4 {
2212                 OtherThing::One { t } => { t; },
2213                 OtherThing::Two(t) => { t; },
2214             }
2215         }
2216         "#,
2217         expect![[r#"
2218             97..99 't1': Thing<()>
2219             108..110 't2': OtherThing<()>
2220             124..126 't3': Thing<i32>
2221             140..142 't4': OtherThing<i32>
2222             161..384 '{     ...   } }': ()
2223             167..169 't1': Thing<()>
2224             167..171 't1.t': ()
2225             177..179 't3': Thing<i32>
2226             177..181 't3.t': i32
2227             187..282 'match ...     }': ()
2228             193..195 't2': OtherThing<()>
2229             206..227 'OtherT... { t }': OtherThing<()>
2230             224..225 't': ()
2231             231..237 '{ t; }': ()
2232             233..234 't': ()
2233             247..265 'OtherT...Two(t)': OtherThing<()>
2234             263..264 't': ()
2235             269..275 '{ t; }': ()
2236             271..272 't': ()
2237             287..382 'match ...     }': ()
2238             293..295 't4': OtherThing<i32>
2239             306..327 'OtherT... { t }': OtherThing<i32>
2240             324..325 't': i32
2241             331..337 '{ t; }': ()
2242             333..334 't': i32
2243             347..365 'OtherT...Two(t)': OtherThing<i32>
2244             363..364 't': i32
2245             369..375 '{ t; }': ()
2246             371..372 't': i32
2247         "#]],
2248     );
2249 }
2250
2251 #[test]
2252 fn generic_default_in_struct_literal() {
2253     check_infer(
2254         r#"
2255         struct Thing<T = ()> { t: T }
2256         enum OtherThing<T = ()> {
2257             One { t: T },
2258             Two(T),
2259         }
2260
2261         fn test() {
2262             let x = Thing { t: loop {} };
2263             let y = Thing { t: () };
2264             let z = Thing { t: 1i32 };
2265             if let Thing { t } = z {
2266                 t;
2267             }
2268
2269             let a = OtherThing::One { t: 1i32 };
2270             let b = OtherThing::Two(1i32);
2271         }
2272         "#,
2273         expect![[r#"
2274             99..319 '{     ...32); }': ()
2275             109..110 'x': Thing<!>
2276             113..133 'Thing ...p {} }': Thing<!>
2277             124..131 'loop {}': !
2278             129..131 '{}': ()
2279             143..144 'y': Thing<()>
2280             147..162 'Thing { t: () }': Thing<()>
2281             158..160 '()': ()
2282             172..173 'z': Thing<i32>
2283             176..193 'Thing ...1i32 }': Thing<i32>
2284             187..191 '1i32': i32
2285             199..240 'if let...     }': ()
2286             206..217 'Thing { t }': Thing<i32>
2287             214..215 't': i32
2288             220..221 'z': Thing<i32>
2289             222..240 '{     ...     }': ()
2290             232..233 't': i32
2291             250..251 'a': OtherThing<i32>
2292             254..281 'OtherT...1i32 }': OtherThing<i32>
2293             275..279 '1i32': i32
2294             291..292 'b': OtherThing<i32>
2295             295..310 'OtherThing::Two': Two<i32>(i32) -> OtherThing<i32>
2296             295..316 'OtherT...(1i32)': OtherThing<i32>
2297             311..315 '1i32': i32
2298         "#]],
2299     );
2300 }
2301
2302 #[test]
2303 fn generic_default_depending_on_other_type_arg() {
2304     // FIXME: the {unknown} is a bug
2305     check_infer(
2306         r#"
2307         struct Thing<T = u128, F = fn() -> T> { t: T }
2308
2309         fn test(t1: Thing<u32>, t2: Thing) {
2310             t1;
2311             t2;
2312             Thing::<_> { t: 1u32 };
2313         }
2314         "#,
2315         expect![[r#"
2316             56..58 't1': Thing<u32, fn() -> u32>
2317             72..74 't2': Thing<u128, fn() -> u128>
2318             83..130 '{     ...2 }; }': ()
2319             89..91 't1': Thing<u32, fn() -> u32>
2320             97..99 't2': Thing<u128, fn() -> u128>
2321             105..127 'Thing:...1u32 }': Thing<u32, fn() -> {unknown}>
2322             121..125 '1u32': u32
2323         "#]],
2324     );
2325 }
2326
2327 #[test]
2328 fn generic_default_depending_on_other_type_arg_forward() {
2329     // the {unknown} here is intentional, as defaults are not allowed to
2330     // refer to type parameters coming later
2331     check_infer(
2332         r#"
2333         struct Thing<F = fn() -> T, T = u128> { t: T }
2334
2335         fn test(t1: Thing) {
2336             t1;
2337         }
2338         "#,
2339         expect![[r#"
2340             56..58 't1': Thing<fn() -> {unknown}, u128>
2341             67..78 '{     t1; }': ()
2342             73..75 't1': Thing<fn() -> {unknown}, u128>
2343         "#]],
2344     );
2345 }
2346
2347 #[test]
2348 fn infer_operator_overload() {
2349     cov_mark::check!(infer_expr_inner_binary_operator_overload);
2350
2351     check_infer(
2352         r#"
2353         struct V2([f32; 2]);
2354
2355         #[lang = "add"]
2356         pub trait Add<Rhs = Self> {
2357             /// The resulting type after applying the `+` operator.
2358             type Output;
2359
2360             /// Performs the `+` operation.
2361             #[must_use]
2362             fn add(self, rhs: Rhs) -> Self::Output;
2363         }
2364
2365         impl Add<V2> for V2 {
2366             type Output = V2;
2367
2368             fn add(self, rhs: V2) -> V2 {
2369                 let x = self.0[0] + rhs.0[0];
2370                 let y = self.0[1] + rhs.0[1];
2371                 V2([x, y])
2372             }
2373         }
2374
2375         fn test() {
2376             let va = V2([0.0, 1.0]);
2377             let vb = V2([0.0, 1.0]);
2378
2379             let r = va + vb;
2380         }
2381
2382         "#,
2383         expect![[r#"
2384             207..211 'self': Self
2385             213..216 'rhs': Rhs
2386             299..303 'self': V2
2387             305..308 'rhs': V2
2388             320..422 '{     ...     }': V2
2389             334..335 'x': f32
2390             338..342 'self': V2
2391             338..344 'self.0': [f32; 2]
2392             338..347 'self.0[0]': {unknown}
2393             338..358 'self.0...s.0[0]': f32
2394             345..346 '0': i32
2395             350..353 'rhs': V2
2396             350..355 'rhs.0': [f32; 2]
2397             350..358 'rhs.0[0]': {unknown}
2398             356..357 '0': i32
2399             372..373 'y': f32
2400             376..380 'self': V2
2401             376..382 'self.0': [f32; 2]
2402             376..385 'self.0[1]': {unknown}
2403             376..396 'self.0...s.0[1]': f32
2404             383..384 '1': i32
2405             388..391 'rhs': V2
2406             388..393 'rhs.0': [f32; 2]
2407             388..396 'rhs.0[1]': {unknown}
2408             394..395 '1': i32
2409             406..408 'V2': V2([f32; 2]) -> V2
2410             406..416 'V2([x, y])': V2
2411             409..415 '[x, y]': [f32; 2]
2412             410..411 'x': f32
2413             413..414 'y': f32
2414             436..519 '{     ... vb; }': ()
2415             446..448 'va': V2
2416             451..453 'V2': V2([f32; 2]) -> V2
2417             451..465 'V2([0.0, 1.0])': V2
2418             454..464 '[0.0, 1.0]': [f32; 2]
2419             455..458 '0.0': f32
2420             460..463 '1.0': f32
2421             475..477 'vb': V2
2422             480..482 'V2': V2([f32; 2]) -> V2
2423             480..494 'V2([0.0, 1.0])': V2
2424             483..493 '[0.0, 1.0]': [f32; 2]
2425             484..487 '0.0': f32
2426             489..492 '1.0': f32
2427             505..506 'r': V2
2428             509..511 'va': V2
2429             509..516 'va + vb': V2
2430             514..516 'vb': V2
2431         "#]],
2432     );
2433 }
2434
2435 #[test]
2436 fn infer_const_params() {
2437     check_infer(
2438         r#"
2439         fn foo<const FOO: usize>() {
2440             let bar = FOO;
2441         }
2442         "#,
2443         expect![[r#"
2444             27..49 '{     ...FOO; }': ()
2445             37..40 'bar': usize
2446             43..46 'FOO': usize
2447         "#]],
2448     );
2449 }
2450
2451 #[test]
2452 fn infer_inner_type() {
2453     check_infer(
2454         r#"
2455         fn foo() {
2456             struct S { field: u32 }
2457             let s = S { field: 0 };
2458             let f = s.field;
2459         }
2460     "#,
2461         expect![[r#"
2462             9..89 '{     ...eld; }': ()
2463             47..48 's': S
2464             51..65 'S { field: 0 }': S
2465             62..63 '0': u32
2466             75..76 'f': u32
2467             79..80 's': S
2468             79..86 's.field': u32
2469         "#]],
2470     );
2471 }
2472
2473 #[test]
2474 fn infer_nested_inner_type() {
2475     check_infer(
2476         r#"
2477         fn foo() {
2478             {
2479                 let s = S { field: 0 };
2480                 let f = s.field;
2481             }
2482             struct S { field: u32 }
2483         }
2484     "#,
2485         expect![[r#"
2486             9..109 '{     ...32 } }': ()
2487             15..79 '{     ...     }': ()
2488             29..30 's': S
2489             33..47 'S { field: 0 }': S
2490             44..45 '0': u32
2491             61..62 'f': u32
2492             65..66 's': S
2493             65..72 's.field': u32
2494         "#]],
2495     );
2496 }
2497
2498 #[test]
2499 fn inner_use_enum_rename() {
2500     check_infer(
2501         r#"
2502         enum Request {
2503             Info
2504         }
2505
2506         fn f() {
2507             use Request as R;
2508
2509             let r = R::Info;
2510             match r {
2511                 R::Info => {}
2512             }
2513         }
2514     "#,
2515         expect![[r#"
2516             34..123 '{     ...   } }': ()
2517             67..68 'r': Request
2518             71..78 'R::Info': Request
2519             84..121 'match ...     }': ()
2520             90..91 'r': Request
2521             102..109 'R::Info': Request
2522             113..115 '{}': ()
2523         "#]],
2524     )
2525 }
2526
2527 #[test]
2528 fn box_into_vec() {
2529     check_infer(
2530         r#"
2531 #[lang = "sized"]
2532 pub trait Sized {}
2533
2534 #[lang = "unsize"]
2535 pub trait Unsize<T: ?Sized> {}
2536
2537 #[lang = "coerce_unsized"]
2538 pub trait CoerceUnsized<T> {}
2539
2540 pub unsafe trait Allocator {}
2541
2542 pub struct Global;
2543 unsafe impl Allocator for Global {}
2544
2545 #[lang = "owned_box"]
2546 #[fundamental]
2547 pub struct Box<T: ?Sized, A: Allocator = Global>;
2548
2549 impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}
2550
2551 pub struct Vec<T, A: Allocator = Global> {}
2552
2553 #[lang = "slice"]
2554 impl<T> [T] {}
2555
2556 #[lang = "slice_alloc"]
2557 impl<T> [T] {
2558     pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
2559         unimplemented!()
2560     }
2561 }
2562
2563 fn test() {
2564     let vec = <[_]>::into_vec(box [1i32]);
2565 }
2566 "#,
2567         expect![[r#"
2568             569..573 'self': Box<[T], A>
2569             602..634 '{     ...     }': Vec<T, A>
2570             612..628 'unimpl...ted!()': Vec<T, A>
2571             648..694 '{     ...2]); }': ()
2572             658..661 'vec': Vec<i32, Global>
2573             664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>
2574             664..691 '<[_]>:...1i32])': Vec<i32, Global>
2575             680..690 'box [1i32]': Box<[i32; 1], Global>
2576             684..690 '[1i32]': [i32; 1]
2577             685..689 '1i32': i32
2578         "#]],
2579     )
2580 }
2581
2582 #[test]
2583 fn cfgd_out_assoc_items() {
2584     check_types(
2585         r#"
2586 struct S;
2587
2588 impl S {
2589     #[cfg(FALSE)]
2590     const C: S = S;
2591 }
2592
2593 fn f() {
2594     S::C;
2595   //^^^^ {unknown}
2596 }
2597     "#,
2598     )
2599 }
2600
2601 #[test]
2602 fn infer_type_alias_variant() {
2603     check_infer(
2604         r#"
2605 type Qux = Foo;
2606 enum Foo {
2607     Bar(i32),
2608     Baz { baz: f32 }
2609 }
2610
2611 fn f() {
2612     match Foo::Bar(3) {
2613         Qux::Bar(bar) => (),
2614         Qux::Baz { baz } => (),
2615     }
2616 }
2617     "#,
2618         expect![[r#"
2619             72..166 '{     ...   } }': ()
2620             78..164 'match ...     }': ()
2621             84..92 'Foo::Bar': Bar(i32) -> Foo
2622             84..95 'Foo::Bar(3)': Foo
2623             93..94 '3': i32
2624             106..119 'Qux::Bar(bar)': Foo
2625             115..118 'bar': i32
2626             123..125 '()': ()
2627             135..151 'Qux::B... baz }': Foo
2628             146..149 'baz': f32
2629             155..157 '()': ()
2630         "#]],
2631     )
2632 }
2633
2634 #[test]
2635 fn infer_boxed_self_receiver() {
2636     check_infer(
2637         r#"
2638 #[lang = "deref"]
2639 pub trait Deref {
2640     type Target;
2641     fn deref(&self) -> &Self::Target;
2642 }
2643
2644 struct Box<T>(T);
2645
2646 impl<T> Deref for Box<T> {
2647     type Target = T;
2648     fn deref(&self) -> &Self::Target;
2649 }
2650
2651 struct Foo<T>(T);
2652
2653 impl<T> Foo<T> {
2654     fn get_inner<'a>(self: &'a Box<Self>) -> &'a T {}
2655
2656     fn get_self<'a>(self: &'a Box<Self>) -> &'a Self {}
2657
2658     fn into_inner(self: Box<Self>) -> Self {}
2659 }
2660
2661 fn main() {
2662     let boxed = Box(Foo(0_i32));
2663
2664     let bad1 = boxed.get_inner();
2665     let good1 = Foo::get_inner(&boxed);
2666
2667     let bad2 = boxed.get_self();
2668     let good2 = Foo::get_self(&boxed);
2669
2670     let inner = boxed.into_inner();
2671 }
2672         "#,
2673         expect![[r#"
2674             67..71 'self': &Self
2675             175..179 'self': &Box<T>
2676             259..263 'self': &Box<Foo<T>>
2677             289..291 '{}': ()
2678             313..317 'self': &Box<Foo<T>>
2679             346..348 '{}': ()
2680             368..372 'self': Box<Foo<T>>
2681             393..395 '{}': ()
2682             409..630 '{     ...r(); }': ()
2683             419..424 'boxed': Box<Foo<i32>>
2684             427..430 'Box': Box<Foo<i32>>(Foo<i32>) -> Box<Foo<i32>>
2685             427..442 'Box(Foo(0_i32))': Box<Foo<i32>>
2686             431..434 'Foo': Foo<i32>(i32) -> Foo<i32>
2687             431..441 'Foo(0_i32)': Foo<i32>
2688             435..440 '0_i32': i32
2689             453..457 'bad1': &i32
2690             460..465 'boxed': Box<Foo<i32>>
2691             460..477 'boxed....nner()': &i32
2692             487..492 'good1': &i32
2693             495..509 'Foo::get_inner': fn get_inner<i32>(&Box<Foo<i32>>) -> &i32
2694             495..517 'Foo::g...boxed)': &i32
2695             510..516 '&boxed': &Box<Foo<i32>>
2696             511..516 'boxed': Box<Foo<i32>>
2697             528..532 'bad2': &Foo<i32>
2698             535..540 'boxed': Box<Foo<i32>>
2699             535..551 'boxed....self()': &Foo<i32>
2700             561..566 'good2': &Foo<i32>
2701             569..582 'Foo::get_self': fn get_self<i32>(&Box<Foo<i32>>) -> &Foo<i32>
2702             569..590 'Foo::g...boxed)': &Foo<i32>
2703             583..589 '&boxed': &Box<Foo<i32>>
2704             584..589 'boxed': Box<Foo<i32>>
2705             601..606 'inner': Foo<i32>
2706             609..614 'boxed': Box<Foo<i32>>
2707             609..627 'boxed....nner()': Foo<i32>
2708         "#]],
2709     );
2710 }
2711
2712 #[test]
2713 fn prelude_2015() {
2714     check_types(
2715         r#"
2716 //- /main.rs edition:2015 crate:main deps:core
2717 fn f() {
2718     Rust;
2719      //^ Rust
2720 }
2721
2722 //- /core.rs crate:core
2723 pub mod prelude {
2724     pub mod rust_2015 {
2725         pub struct Rust;
2726     }
2727 }
2728     "#,
2729     );
2730 }