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