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