]> git.lizzy.rs Git - rust.git/blob - crates/hir-ty/src/tests/simple.rs
add box expection hint
[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...     }': ()
1000             75..80 'false': bool
1001             81..106 '{     ...     }': ()
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 '{}': i32
1098             106..110 'self': &A
1099             112..113 'x': u64
1100             127..129 '{}': i64
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 '{}': i32
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); }': i128
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 '{}': Option<&T>
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         mod m {
1592             pub enum Enum {
1593                 Foo(u8),
1594             }
1595             pub type Alias = Enum;
1596         }
1597         fn f() {
1598             let e = m::Alias::Foo(0);
1599             let m::Alias::Foo(x) = &e;
1600         }
1601         "#,
1602         expect![[r#"
1603             115..116 'x': A<u32, i128>
1604             123..124 'y': A<&str, u128>
1605             137..138 'z': A<u8, i8>
1606             153..210 '{     ...z.y; }': ()
1607             159..160 'x': A<u32, i128>
1608             159..162 'x.x': u32
1609             168..169 'x': A<u32, i128>
1610             168..171 'x.y': i128
1611             177..178 'y': A<&str, u128>
1612             177..180 'y.x': &str
1613             186..187 'y': A<&str, u128>
1614             186..189 'y.y': u128
1615             195..196 'z': A<u8, i8>
1616             195..198 'z.x': u8
1617             204..205 'z': A<u8, i8>
1618             204..207 'z.y': i8
1619             298..362 '{     ... &e; }': ()
1620             308..309 'e': Enum
1621             312..325 'm::Alias::Foo': Foo(u8) -> Enum
1622             312..328 'm::Ali...Foo(0)': Enum
1623             326..327 '0': u8
1624             338..354 'm::Ali...Foo(x)': Enum
1625             352..353 'x': &u8
1626             357..359 '&e': &Enum
1627             358..359 'e': Enum
1628         "#]],
1629     )
1630 }
1631
1632 #[test]
1633 fn recursive_type_alias() {
1634     check_infer(
1635         r#"
1636         struct A<X> {}
1637         type Foo = Foo;
1638         type Bar = A<Bar>;
1639         fn test(x: Foo) {}
1640         "#,
1641         expect![[r#"
1642             58..59 'x': {unknown}
1643             66..68 '{}': ()
1644         "#]],
1645     )
1646 }
1647
1648 #[test]
1649 fn infer_type_param() {
1650     check_infer(
1651         r#"
1652         fn id<T>(x: T) -> T {
1653             x
1654         }
1655
1656         fn clone<T>(x: &T) -> T {
1657             *x
1658         }
1659
1660         fn test() {
1661             let y = 10u32;
1662             id(y);
1663             let x: bool = clone(z);
1664             id::<i128>(1);
1665         }
1666         "#,
1667         expect![[r#"
1668             9..10 'x': T
1669             20..29 '{     x }': T
1670             26..27 'x': T
1671             43..44 'x': &T
1672             55..65 '{     *x }': T
1673             61..63 '*x': T
1674             62..63 'x': &T
1675             77..157 '{     ...(1); }': ()
1676             87..88 'y': u32
1677             91..96 '10u32': u32
1678             102..104 'id': fn id<u32>(u32) -> u32
1679             102..107 'id(y)': u32
1680             105..106 'y': u32
1681             117..118 'x': bool
1682             127..132 'clone': fn clone<bool>(&bool) -> bool
1683             127..135 'clone(z)': bool
1684             133..134 'z': &bool
1685             141..151 'id::<i128>': fn id<i128>(i128) -> i128
1686             141..154 'id::<i128>(1)': i128
1687             152..153 '1': i128
1688         "#]],
1689     );
1690 }
1691
1692 #[test]
1693 fn infer_const() {
1694     check_infer(
1695         r#"
1696         struct Foo;
1697         impl Foo { const ASSOC_CONST: u32 = 0; }
1698         const GLOBAL_CONST: u32 = 101;
1699         fn test() {
1700             const LOCAL_CONST: u32 = 99;
1701             let x = LOCAL_CONST;
1702             let z = GLOBAL_CONST;
1703             let id = Foo::ASSOC_CONST;
1704         }
1705         "#,
1706         expect![[r#"
1707             48..49 '0': u32
1708             79..82 '101': u32
1709             94..212 '{     ...NST; }': ()
1710             137..138 'x': u32
1711             141..152 'LOCAL_CONST': u32
1712             162..163 'z': u32
1713             166..178 'GLOBAL_CONST': u32
1714             188..190 'id': u32
1715             193..209 'Foo::A..._CONST': u32
1716             125..127 '99': u32
1717         "#]],
1718     );
1719 }
1720
1721 #[test]
1722 fn infer_static() {
1723     check_infer(
1724         r#"
1725         static GLOBAL_STATIC: u32 = 101;
1726         static mut GLOBAL_STATIC_MUT: u32 = 101;
1727         fn test() {
1728             static LOCAL_STATIC: u32 = 99;
1729             static mut LOCAL_STATIC_MUT: u32 = 99;
1730             let x = LOCAL_STATIC;
1731             let y = LOCAL_STATIC_MUT;
1732             let z = GLOBAL_STATIC;
1733             let w = GLOBAL_STATIC_MUT;
1734         }
1735         "#,
1736         expect![[r#"
1737             28..31 '101': u32
1738             69..72 '101': u32
1739             84..279 '{     ...MUT; }': ()
1740             172..173 'x': u32
1741             176..188 'LOCAL_STATIC': u32
1742             198..199 'y': u32
1743             202..218 'LOCAL_...IC_MUT': u32
1744             228..229 'z': u32
1745             232..245 'GLOBAL_STATIC': u32
1746             255..256 'w': u32
1747             259..276 'GLOBAL...IC_MUT': u32
1748             117..119 '99': u32
1749             160..162 '99': u32
1750         "#]],
1751     );
1752 }
1753
1754 #[test]
1755 fn shadowing_primitive() {
1756     check_types(
1757         r#"
1758 struct i32;
1759 struct Foo;
1760
1761 impl i32 { fn foo(&self) -> Foo { Foo } }
1762
1763 fn main() {
1764     let x: i32 = i32;
1765     x.foo();
1766   //^^^^^^^ Foo
1767 }"#,
1768     );
1769 }
1770
1771 #[test]
1772 fn const_eval_array_repeat_expr() {
1773     check_types(
1774         r#"
1775 fn main() {
1776     const X: usize = 6 - 1;
1777     let t = [(); X + 2];
1778       //^ [(); 7]
1779 }"#,
1780     );
1781 }
1782
1783 #[test]
1784 fn shadowing_primitive_with_inner_items() {
1785     check_types(
1786         r#"
1787 struct i32;
1788 struct Foo;
1789
1790 impl i32 { fn foo(&self) -> Foo { Foo } }
1791
1792 fn main() {
1793     fn inner() {}
1794     let x: i32 = i32;
1795     x.foo();
1796   //^^^^^^^ Foo
1797 }"#,
1798     );
1799 }
1800
1801 #[test]
1802 fn not_shadowing_primitive_by_module() {
1803     check_types(
1804         r#"
1805 //- /str.rs
1806 fn foo() {}
1807
1808 //- /main.rs
1809 mod str;
1810 fn foo() -> &'static str { "" }
1811
1812 fn main() {
1813     foo();
1814   //^^^^^ &str
1815 }"#,
1816     );
1817 }
1818
1819 #[test]
1820 fn not_shadowing_module_by_primitive() {
1821     check_types(
1822         r#"
1823 //- /str.rs
1824 fn foo() -> u32 {0}
1825
1826 //- /main.rs
1827 mod str;
1828 fn foo() -> &'static str { "" }
1829
1830 fn main() {
1831     str::foo();
1832   //^^^^^^^^^^ u32
1833 }"#,
1834     );
1835 }
1836
1837 // This test is actually testing the shadowing behavior within hir_def. It
1838 // lives here because the testing infrastructure in hir_def isn't currently
1839 // capable of asserting the necessary conditions.
1840 #[test]
1841 fn should_be_shadowing_imports() {
1842     check_types(
1843         r#"
1844 mod a {
1845     pub fn foo() -> i8 {0}
1846     pub struct foo { a: i8 }
1847 }
1848 mod b { pub fn foo () -> u8 {0} }
1849 mod c { pub struct foo { a: u8 } }
1850 mod d {
1851     pub use super::a::*;
1852     pub use super::c::foo;
1853     pub use super::b::foo;
1854 }
1855
1856 fn main() {
1857     d::foo();
1858   //^^^^^^^^ u8
1859     d::foo{a:0};
1860   //^^^^^^^^^^^ foo
1861 }"#,
1862     );
1863 }
1864
1865 #[test]
1866 fn closure_return() {
1867     check_infer(
1868         r#"
1869         fn foo() -> u32 {
1870             let x = || -> usize { return 1; };
1871         }
1872         "#,
1873         expect![[r#"
1874             16..58 '{     ...; }; }': u32
1875             26..27 'x': || -> usize
1876             30..55 '|| -> ...n 1; }': || -> usize
1877             42..55 '{ return 1; }': usize
1878             44..52 'return 1': !
1879             51..52 '1': usize
1880         "#]],
1881     );
1882 }
1883
1884 #[test]
1885 fn closure_return_unit() {
1886     check_infer(
1887         r#"
1888         fn foo() -> u32 {
1889             let x = || { return; };
1890         }
1891         "#,
1892         expect![[r#"
1893             16..47 '{     ...; }; }': u32
1894             26..27 'x': || -> ()
1895             30..44 '|| { return; }': || -> ()
1896             33..44 '{ return; }': ()
1897             35..41 'return': !
1898         "#]],
1899     );
1900 }
1901
1902 #[test]
1903 fn closure_return_inferred() {
1904     check_infer(
1905         r#"
1906         fn foo() -> u32 {
1907             let x = || { "test" };
1908         }
1909         "#,
1910         expect![[r#"
1911             16..46 '{     ..." }; }': u32
1912             26..27 'x': || -> &str
1913             30..43 '|| { "test" }': || -> &str
1914             33..43 '{ "test" }': &str
1915             35..41 '"test"': &str
1916         "#]],
1917     );
1918 }
1919
1920 #[test]
1921 fn fn_pointer_return() {
1922     check_infer(
1923         r#"
1924         struct Vtable {
1925             method: fn(),
1926         }
1927
1928         fn main() {
1929             let vtable = Vtable { method: || {} };
1930             let m = vtable.method;
1931         }
1932         "#,
1933         expect![[r#"
1934             47..120 '{     ...hod; }': ()
1935             57..63 'vtable': Vtable
1936             66..90 'Vtable...| {} }': Vtable
1937             83..88 '|| {}': || -> ()
1938             86..88 '{}': ()
1939             100..101 'm': fn()
1940             104..110 'vtable': Vtable
1941             104..117 'vtable.method': fn()
1942         "#]],
1943     );
1944 }
1945
1946 #[test]
1947 fn block_modifiers_smoke_test() {
1948     check_infer(
1949         r#"
1950 //- minicore: future
1951 async fn main() {
1952     let x = unsafe { 92 };
1953     let y = async { async { () }.await };
1954     let z = try { () };
1955     let w = const { 92 };
1956     let t = 'a: { 92 };
1957 }
1958         "#,
1959         expect![[r#"
1960             16..162 '{     ...2 }; }': ()
1961             26..27 'x': i32
1962             30..43 'unsafe { 92 }': i32
1963             30..43 'unsafe { 92 }': i32
1964             39..41 '92': i32
1965             53..54 'y': impl Future<Output = ()>
1966             57..85 'async ...wait }': ()
1967             57..85 'async ...wait }': impl Future<Output = ()>
1968             65..77 'async { () }': ()
1969             65..77 'async { () }': impl Future<Output = ()>
1970             65..83 'async ....await': ()
1971             73..75 '()': ()
1972             95..96 'z': {unknown}
1973             99..109 'try { () }': ()
1974             99..109 'try { () }': {unknown}
1975             105..107 '()': ()
1976             119..120 'w': i32
1977             123..135 'const { 92 }': i32
1978             123..135 'const { 92 }': i32
1979             131..133 '92': i32
1980             145..146 't': i32
1981             149..159 ''a: { 92 }': i32
1982             155..157 '92': i32
1983         "#]],
1984     )
1985 }
1986 #[test]
1987 fn async_block_early_return() {
1988     check_infer(
1989         r#"
1990 //- minicore: future, result, fn
1991 fn test<I, E, F: FnMut() -> Fut, Fut: core::future::Future<Output = Result<I, E>>>(f: F) {}
1992
1993 fn main() {
1994     async {
1995         return Err(());
1996         Ok(())
1997     };
1998     test(|| async {
1999         return Err(());
2000         Ok(())
2001     });
2002 }
2003         "#,
2004         expect![[r#"
2005             83..84 'f': F
2006             89..91 '{}': ()
2007             103..231 '{     ... }); }': ()
2008             109..161 'async ...     }': Result<(), ()>
2009             109..161 'async ...     }': impl Future<Output = Result<(), ()>>
2010             125..139 'return Err(())': !
2011             132..135 'Err': Err<(), ()>(()) -> Result<(), ()>
2012             132..139 'Err(())': Result<(), ()>
2013             136..138 '()': ()
2014             149..151 'Ok': Ok<(), ()>(()) -> Result<(), ()>
2015             149..155 'Ok(())': Result<(), ()>
2016             152..154 '()': ()
2017             167..171 'test': fn test<(), (), || -> impl Future<Output = Result<(), ()>>, impl Future<Output = Result<(), ()>>>(|| -> impl Future<Output = Result<(), ()>>)
2018             167..228 'test(|...    })': ()
2019             172..227 '|| asy...     }': || -> impl Future<Output = Result<(), ()>>
2020             175..227 'async ...     }': Result<(), ()>
2021             175..227 'async ...     }': impl Future<Output = Result<(), ()>>
2022             191..205 'return Err(())': !
2023             198..201 'Err': Err<(), ()>(()) -> Result<(), ()>
2024             198..205 'Err(())': Result<(), ()>
2025             202..204 '()': ()
2026             215..217 'Ok': Ok<(), ()>(()) -> Result<(), ()>
2027             215..221 'Ok(())': Result<(), ()>
2028             218..220 '()': ()
2029         "#]],
2030     )
2031 }
2032
2033 #[test]
2034 fn infer_generic_from_later_assignment() {
2035     check_infer(
2036         r#"
2037         enum Option<T> { Some(T), None }
2038         use Option::*;
2039
2040         fn test() {
2041             let mut end = None;
2042             loop {
2043                 end = Some(true);
2044             }
2045         }
2046         "#,
2047         expect![[r#"
2048             59..129 '{     ...   } }': ()
2049             69..76 'mut end': Option<bool>
2050             79..83 'None': Option<bool>
2051             89..127 'loop {...     }': !
2052             94..127 '{     ...     }': ()
2053             104..107 'end': Option<bool>
2054             104..120 'end = ...(true)': ()
2055             110..114 'Some': Some<bool>(bool) -> Option<bool>
2056             110..120 'Some(true)': Option<bool>
2057             115..119 'true': bool
2058         "#]],
2059     );
2060 }
2061
2062 #[test]
2063 fn infer_loop_break_with_val() {
2064     check_infer(
2065         r#"
2066         enum Option<T> { Some(T), None }
2067         use Option::*;
2068
2069         fn test() {
2070             let x = loop {
2071                 if false {
2072                     break None;
2073                 }
2074
2075                 break Some(true);
2076             };
2077         }
2078         "#,
2079         expect![[r#"
2080             59..168 '{     ...  }; }': ()
2081             69..70 'x': Option<bool>
2082             73..165 'loop {...     }': Option<bool>
2083             78..165 '{     ...     }': ()
2084             88..132 'if fal...     }': ()
2085             91..96 'false': bool
2086             97..132 '{     ...     }': ()
2087             111..121 'break None': !
2088             117..121 'None': Option<bool>
2089             142..158 'break ...(true)': !
2090             148..152 'Some': Some<bool>(bool) -> Option<bool>
2091             148..158 'Some(true)': Option<bool>
2092             153..157 'true': bool
2093         "#]],
2094     );
2095 }
2096
2097 #[test]
2098 fn infer_loop_break_without_val() {
2099     check_infer(
2100         r#"
2101         enum Option<T> { Some(T), None }
2102         use Option::*;
2103
2104         fn test() {
2105             let x = loop {
2106                 if false {
2107                     break;
2108                 }
2109             };
2110         }
2111         "#,
2112         expect![[r#"
2113             59..136 '{     ...  }; }': ()
2114             69..70 'x': ()
2115             73..133 'loop {...     }': ()
2116             78..133 '{     ...     }': ()
2117             88..127 'if fal...     }': ()
2118             91..96 'false': bool
2119             97..127 '{     ...     }': ()
2120             111..116 'break': !
2121         "#]],
2122     );
2123 }
2124
2125 #[test]
2126 fn infer_labelled_break_with_val() {
2127     check_infer(
2128         r#"
2129         fn foo() {
2130             let _x = || 'outer: loop {
2131                 let inner = 'inner: loop {
2132                     let i = Default::default();
2133                     if (break 'outer i) {
2134                         loop { break 'inner 5i8; };
2135                     } else if true {
2136                         break 'inner 6;
2137                     }
2138                     break 7;
2139                 };
2140                 break inner < 8;
2141             };
2142         }
2143         "#,
2144         expect![[r#"
2145             9..335 '{     ...  }; }': ()
2146             19..21 '_x': || -> bool
2147             24..332 '|| 'ou...     }': || -> bool
2148             27..332 ''outer...     }': bool
2149             40..332 '{     ...     }': ()
2150             54..59 'inner': i8
2151             62..300 ''inner...     }': i8
2152             75..300 '{     ...     }': ()
2153             93..94 'i': bool
2154             97..113 'Defaul...efault': {unknown}
2155             97..115 'Defaul...ault()': bool
2156             129..269 'if (br...     }': ()
2157             133..147 'break 'outer i': !
2158             146..147 'i': bool
2159             149..208 '{     ...     }': ()
2160             167..193 'loop {...5i8; }': !
2161             172..193 '{ brea...5i8; }': ()
2162             174..190 'break ...er 5i8': !
2163             187..190 '5i8': i8
2164             214..269 'if tru...     }': ()
2165             217..221 'true': bool
2166             222..269 '{     ...     }': ()
2167             240..254 'break 'inner 6': !
2168             253..254 '6': i8
2169             282..289 'break 7': !
2170             288..289 '7': i8
2171             310..325 'break inner < 8': !
2172             316..321 'inner': i8
2173             316..325 'inner < 8': bool
2174             324..325 '8': i8
2175         "#]],
2176     );
2177 }
2178
2179 #[test]
2180 fn infer_labelled_block_break_with_val() {
2181     check_infer(
2182         r#"
2183 fn default<T>() -> T { loop {} }
2184 fn foo() {
2185     let _x = 'outer: {
2186         let inner = 'inner: {
2187             let i = default();
2188             if (break 'outer i) {
2189                 break 'inner 5i8;
2190             } else if true {
2191                 break 'inner 6;
2192             }
2193             break 'inner 'innermost: { 0 };
2194             42
2195         };
2196         break 'outer inner < 8;
2197     };
2198 }
2199 "#,
2200         expect![[r#"
2201             21..32 '{ loop {} }': T
2202             23..30 'loop {}': !
2203             28..30 '{}': ()
2204             42..381 '{     ...  }; }': ()
2205             52..54 '_x': bool
2206             57..378 ''outer...     }': bool
2207             79..84 'inner': i8
2208             87..339 ''inner...     }': i8
2209             113..114 'i': bool
2210             117..124 'default': fn default<bool>() -> bool
2211             117..126 'default()': bool
2212             140..270 'if (br...     }': ()
2213             144..158 'break 'outer i': !
2214             157..158 'i': bool
2215             160..209 '{     ...     }': ()
2216             178..194 'break ...er 5i8': !
2217             191..194 '5i8': i8
2218             215..270 'if tru...     }': ()
2219             218..222 'true': bool
2220             223..270 '{     ...     }': ()
2221             241..255 'break 'inner 6': !
2222             254..255 '6': i8
2223             283..313 'break ... { 0 }': !
2224             296..313 ''inner... { 0 }': i8
2225             310..311 '0': i8
2226             327..329 '42': i8
2227             349..371 'break ...er < 8': !
2228             362..367 'inner': i8
2229             362..371 'inner < 8': bool
2230             370..371 '8': i8
2231         "#]],
2232     );
2233 }
2234
2235 #[test]
2236 fn generic_default() {
2237     check_infer(
2238         r#"
2239         struct Thing<T = ()> { t: T }
2240         enum OtherThing<T = ()> {
2241             One { t: T },
2242             Two(T),
2243         }
2244
2245         fn test(t1: Thing, t2: OtherThing, t3: Thing<i32>, t4: OtherThing<i32>) {
2246             t1.t;
2247             t3.t;
2248             match t2 {
2249                 OtherThing::One { t } => { t; },
2250                 OtherThing::Two(t) => { t; },
2251             }
2252             match t4 {
2253                 OtherThing::One { t } => { t; },
2254                 OtherThing::Two(t) => { t; },
2255             }
2256         }
2257         "#,
2258         expect![[r#"
2259             97..99 't1': Thing<()>
2260             108..110 't2': OtherThing<()>
2261             124..126 't3': Thing<i32>
2262             140..142 't4': OtherThing<i32>
2263             161..384 '{     ...   } }': ()
2264             167..169 't1': Thing<()>
2265             167..171 't1.t': ()
2266             177..179 't3': Thing<i32>
2267             177..181 't3.t': i32
2268             187..282 'match ...     }': ()
2269             193..195 't2': OtherThing<()>
2270             206..227 'OtherT... { t }': OtherThing<()>
2271             224..225 't': ()
2272             231..237 '{ t; }': ()
2273             233..234 't': ()
2274             247..265 'OtherT...Two(t)': OtherThing<()>
2275             263..264 't': ()
2276             269..275 '{ t; }': ()
2277             271..272 't': ()
2278             287..382 'match ...     }': ()
2279             293..295 't4': OtherThing<i32>
2280             306..327 'OtherT... { t }': OtherThing<i32>
2281             324..325 't': i32
2282             331..337 '{ t; }': ()
2283             333..334 't': i32
2284             347..365 'OtherT...Two(t)': OtherThing<i32>
2285             363..364 't': i32
2286             369..375 '{ t; }': ()
2287             371..372 't': i32
2288         "#]],
2289     );
2290 }
2291
2292 #[test]
2293 fn generic_default_in_struct_literal() {
2294     check_infer(
2295         r#"
2296         struct Thing<T = ()> { t: T }
2297         enum OtherThing<T = ()> {
2298             One { t: T },
2299             Two(T),
2300         }
2301
2302         fn test() {
2303             let x = Thing { t: loop {} };
2304             let y = Thing { t: () };
2305             let z = Thing { t: 1i32 };
2306             if let Thing { t } = z {
2307                 t;
2308             }
2309
2310             let a = OtherThing::One { t: 1i32 };
2311             let b = OtherThing::Two(1i32);
2312         }
2313         "#,
2314         expect![[r#"
2315             99..319 '{     ...32); }': ()
2316             109..110 'x': Thing<!>
2317             113..133 'Thing ...p {} }': Thing<!>
2318             124..131 'loop {}': !
2319             129..131 '{}': ()
2320             143..144 'y': Thing<()>
2321             147..162 'Thing { t: () }': Thing<()>
2322             158..160 '()': ()
2323             172..173 'z': Thing<i32>
2324             176..193 'Thing ...1i32 }': Thing<i32>
2325             187..191 '1i32': i32
2326             199..240 'if let...     }': ()
2327             202..221 'let Th... } = z': bool
2328             206..217 'Thing { t }': Thing<i32>
2329             214..215 't': i32
2330             220..221 'z': Thing<i32>
2331             222..240 '{     ...     }': ()
2332             232..233 't': i32
2333             250..251 'a': OtherThing<i32>
2334             254..281 'OtherT...1i32 }': OtherThing<i32>
2335             275..279 '1i32': i32
2336             291..292 'b': OtherThing<i32>
2337             295..310 'OtherThing::Two': Two<i32>(i32) -> OtherThing<i32>
2338             295..316 'OtherT...(1i32)': OtherThing<i32>
2339             311..315 '1i32': i32
2340         "#]],
2341     );
2342 }
2343
2344 #[test]
2345 fn generic_default_depending_on_other_type_arg() {
2346     // FIXME: the {unknown} is a bug
2347     check_infer(
2348         r#"
2349         struct Thing<T = u128, F = fn() -> T> { t: T }
2350
2351         fn test(t1: Thing<u32>, t2: Thing) {
2352             t1;
2353             t2;
2354             Thing::<_> { t: 1u32 };
2355         }
2356         "#,
2357         expect![[r#"
2358             56..58 't1': Thing<u32, fn() -> u32>
2359             72..74 't2': Thing<u128, fn() -> u128>
2360             83..130 '{     ...2 }; }': ()
2361             89..91 't1': Thing<u32, fn() -> u32>
2362             97..99 't2': Thing<u128, fn() -> u128>
2363             105..127 'Thing:...1u32 }': Thing<u32, fn() -> {unknown}>
2364             121..125 '1u32': u32
2365         "#]],
2366     );
2367 }
2368
2369 #[test]
2370 fn generic_default_depending_on_other_type_arg_forward() {
2371     // the {unknown} here is intentional, as defaults are not allowed to
2372     // refer to type parameters coming later
2373     check_infer(
2374         r#"
2375         struct Thing<F = fn() -> T, T = u128> { t: T }
2376
2377         fn test(t1: Thing) {
2378             t1;
2379         }
2380         "#,
2381         expect![[r#"
2382             56..58 't1': Thing<fn() -> {unknown}, u128>
2383             67..78 '{     t1; }': ()
2384             73..75 't1': Thing<fn() -> {unknown}, u128>
2385         "#]],
2386     );
2387 }
2388
2389 #[test]
2390 fn infer_operator_overload() {
2391     check_types(
2392         r#"
2393 //- minicore: add
2394 struct V2([f32; 2]);
2395
2396 impl core::ops::Add<V2> for V2 {
2397     type Output = V2;
2398 }
2399
2400 fn test() {
2401     let va = V2([0.0, 1.0]);
2402     let vb = V2([0.0, 1.0]);
2403
2404     let r = va + vb;
2405     //      ^^^^^^^ V2
2406 }
2407
2408         "#,
2409     );
2410 }
2411
2412 #[test]
2413 fn infer_const_params() {
2414     check_infer(
2415         r#"
2416         fn foo<const FOO: usize>() {
2417             let bar = FOO;
2418         }
2419         "#,
2420         expect![[r#"
2421             27..49 '{     ...FOO; }': ()
2422             37..40 'bar': usize
2423             43..46 'FOO': usize
2424         "#]],
2425     );
2426 }
2427
2428 #[test]
2429 fn infer_inner_type() {
2430     check_infer(
2431         r#"
2432         fn foo() {
2433             struct S { field: u32 }
2434             let s = S { field: 0 };
2435             let f = s.field;
2436         }
2437     "#,
2438         expect![[r#"
2439             9..89 '{     ...eld; }': ()
2440             47..48 's': S
2441             51..65 'S { field: 0 }': S
2442             62..63 '0': u32
2443             75..76 'f': u32
2444             79..80 's': S
2445             79..86 's.field': u32
2446         "#]],
2447     );
2448 }
2449
2450 #[test]
2451 fn infer_nested_inner_type() {
2452     check_infer(
2453         r#"
2454         fn foo() {
2455             {
2456                 let s = S { field: 0 };
2457                 let f = s.field;
2458             }
2459             struct S { field: u32 }
2460         }
2461     "#,
2462         expect![[r#"
2463             9..109 '{     ...32 } }': ()
2464             15..79 '{     ...     }': ()
2465             29..30 's': S
2466             33..47 'S { field: 0 }': S
2467             44..45 '0': u32
2468             61..62 'f': u32
2469             65..66 's': S
2470             65..72 's.field': u32
2471         "#]],
2472     );
2473 }
2474
2475 #[test]
2476 fn inner_use_enum_rename() {
2477     check_infer(
2478         r#"
2479         enum Request {
2480             Info
2481         }
2482
2483         fn f() {
2484             use Request as R;
2485
2486             let r = R::Info;
2487             match r {
2488                 R::Info => {}
2489             }
2490         }
2491     "#,
2492         expect![[r#"
2493             34..123 '{     ...   } }': ()
2494             67..68 'r': Request
2495             71..78 'R::Info': Request
2496             84..121 'match ...     }': ()
2497             90..91 'r': Request
2498             102..109 'R::Info': Request
2499             113..115 '{}': ()
2500         "#]],
2501     )
2502 }
2503
2504 #[test]
2505 fn box_into_vec() {
2506     check_infer(
2507         r#"
2508 #[lang = "sized"]
2509 pub trait Sized {}
2510
2511 #[lang = "unsize"]
2512 pub trait Unsize<T: ?Sized> {}
2513
2514 #[lang = "coerce_unsized"]
2515 pub trait CoerceUnsized<T> {}
2516
2517 pub unsafe trait Allocator {}
2518
2519 pub struct Global;
2520 unsafe impl Allocator for Global {}
2521
2522 #[lang = "owned_box"]
2523 #[fundamental]
2524 pub struct Box<T: ?Sized, A: Allocator = Global>;
2525
2526 impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}
2527
2528 pub struct Vec<T, A: Allocator = Global> {}
2529
2530 #[lang = "slice"]
2531 impl<T> [T] {}
2532
2533 #[lang = "slice_alloc"]
2534 impl<T> [T] {
2535     pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
2536         unimplemented!()
2537     }
2538 }
2539
2540 fn test() {
2541     let vec = <[_]>::into_vec(box [1i32]);
2542     let v: Vec<Box<dyn B>> = <[_]> :: into_vec(box [box Astruct]);
2543 }
2544
2545 trait B{}
2546 struct Astruct;
2547 impl B for Astruct {}
2548 "#,
2549         expect![[r#"
2550             569..573 'self': Box<[T], A>
2551             602..634 '{     ...     }': Vec<T, A>
2552             612..628 'unimpl...ted!()': Vec<T, A>
2553             648..761 '{     ...t]); }': ()
2554             658..661 'vec': Vec<i32, Global>
2555             664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>
2556             664..691 '<[_]>:...1i32])': Vec<i32, Global>
2557             680..690 'box [1i32]': Box<[i32; 1], Global>
2558             684..690 '[1i32]': [i32; 1]
2559             685..689 '1i32': i32
2560             701..702 'v': Vec<Box<dyn B, Global>, Global>
2561             722..739 '<[_]> ...to_vec': fn into_vec<Box<dyn B, Global>, Global>(Box<[Box<dyn B, Global>], Global>) -> Vec<Box<dyn B, Global>, Global>
2562             722..758 '<[_]> ...ruct])': Vec<Box<dyn B, Global>, Global>
2563             740..757 'box [b...truct]': Box<[Box<dyn B, Global>; 1], Global>
2564             744..757 '[box Astruct]': [Box<dyn B, Global>; 1]
2565             745..756 'box Astruct': Box<Astruct, Global>
2566             749..756 'Astruct': Astruct
2567         "#]],
2568     )
2569 }
2570
2571 #[test]
2572 fn cfgd_out_assoc_items() {
2573     check_types(
2574         r#"
2575 struct S;
2576
2577 impl S {
2578     #[cfg(FALSE)]
2579     const C: S = S;
2580 }
2581
2582 fn f() {
2583     S::C;
2584   //^^^^ {unknown}
2585 }
2586     "#,
2587     )
2588 }
2589
2590 #[test]
2591 fn infer_missing_type() {
2592     check_types(
2593         r#"
2594 struct S;
2595
2596 fn f() {
2597     let s: = S;
2598       //^ S
2599 }
2600     "#,
2601     );
2602 }
2603
2604 #[test]
2605 fn infer_type_alias_variant() {
2606     check_infer(
2607         r#"
2608 type Qux = Foo;
2609 enum Foo {
2610     Bar(i32),
2611     Baz { baz: f32 }
2612 }
2613
2614 fn f() {
2615     match Foo::Bar(3) {
2616         Qux::Bar(bar) => (),
2617         Qux::Baz { baz } => (),
2618     }
2619 }
2620     "#,
2621         expect![[r#"
2622             72..166 '{     ...   } }': ()
2623             78..164 'match ...     }': ()
2624             84..92 'Foo::Bar': Bar(i32) -> Foo
2625             84..95 'Foo::Bar(3)': Foo
2626             93..94 '3': i32
2627             106..119 'Qux::Bar(bar)': Foo
2628             115..118 'bar': i32
2629             123..125 '()': ()
2630             135..151 'Qux::B... baz }': Foo
2631             146..149 'baz': f32
2632             155..157 '()': ()
2633         "#]],
2634     )
2635 }
2636
2637 #[test]
2638 fn infer_boxed_self_receiver() {
2639     check_infer(
2640         r#"
2641 //- minicore: deref
2642 use core::ops::Deref;
2643
2644 struct Box<T>(T);
2645
2646 impl<T> Deref for Box<T> {
2647     type Target = T;
2648     fn deref(&self) -> &Self::Target;
2649 }
2650
2651 struct Foo<T>(T);
2652
2653 impl<T> Foo<T> {
2654     fn get_inner<'a>(self: &'a Box<Self>) -> &'a T {}
2655
2656     fn get_self<'a>(self: &'a Box<Self>) -> &'a Self {}
2657
2658     fn into_inner(self: Box<Self>) -> Self {}
2659 }
2660
2661 fn main() {
2662     let boxed = Box(Foo(0_i32));
2663
2664     let bad1 = boxed.get_inner();
2665     let good1 = Foo::get_inner(&boxed);
2666
2667     let bad2 = boxed.get_self();
2668     let good2 = Foo::get_self(&boxed);
2669
2670     let inner = boxed.into_inner();
2671 }
2672         "#,
2673         expect![[r#"
2674             104..108 'self': &Box<T>
2675             188..192 'self': &Box<Foo<T>>
2676             218..220 '{}': &T
2677             242..246 'self': &Box<Foo<T>>
2678             275..277 '{}': &Foo<T>
2679             297..301 'self': Box<Foo<T>>
2680             322..324 '{}': Foo<T>
2681             338..559 '{     ...r(); }': ()
2682             348..353 'boxed': Box<Foo<i32>>
2683             356..359 'Box': Box<Foo<i32>>(Foo<i32>) -> Box<Foo<i32>>
2684             356..371 'Box(Foo(0_i32))': Box<Foo<i32>>
2685             360..363 'Foo': Foo<i32>(i32) -> Foo<i32>
2686             360..370 'Foo(0_i32)': Foo<i32>
2687             364..369 '0_i32': i32
2688             382..386 'bad1': &i32
2689             389..394 'boxed': Box<Foo<i32>>
2690             389..406 'boxed....nner()': &i32
2691             416..421 'good1': &i32
2692             424..438 'Foo::get_inner': fn get_inner<i32>(&Box<Foo<i32>>) -> &i32
2693             424..446 'Foo::g...boxed)': &i32
2694             439..445 '&boxed': &Box<Foo<i32>>
2695             440..445 'boxed': Box<Foo<i32>>
2696             457..461 'bad2': &Foo<i32>
2697             464..469 'boxed': Box<Foo<i32>>
2698             464..480 'boxed....self()': &Foo<i32>
2699             490..495 'good2': &Foo<i32>
2700             498..511 'Foo::get_self': fn get_self<i32>(&Box<Foo<i32>>) -> &Foo<i32>
2701             498..519 'Foo::g...boxed)': &Foo<i32>
2702             512..518 '&boxed': &Box<Foo<i32>>
2703             513..518 'boxed': Box<Foo<i32>>
2704             530..535 'inner': Foo<i32>
2705             538..543 'boxed': Box<Foo<i32>>
2706             538..556 'boxed....nner()': Foo<i32>
2707         "#]],
2708     );
2709 }
2710
2711 #[test]
2712 fn prelude_2015() {
2713     check_types(
2714         r#"
2715 //- /main.rs edition:2015 crate:main deps:core
2716 fn f() {
2717     Rust;
2718   //^^^^ Rust
2719 }
2720
2721 //- /core.rs crate:core
2722 pub mod prelude {
2723     pub mod rust_2015 {
2724         pub struct Rust;
2725     }
2726 }
2727     "#,
2728     );
2729 }
2730
2731 #[test]
2732 fn legacy_const_generics() {
2733     check_no_mismatches(
2734         r#"
2735 #[rustc_legacy_const_generics(1, 3)]
2736 fn mixed<const N1: &'static str, const N2: bool>(
2737     a: u8,
2738     b: i8,
2739 ) {}
2740
2741 fn f() {
2742     mixed(0, "", -1, true);
2743     mixed::<"", true>(0, -1);
2744 }
2745     "#,
2746     );
2747 }
2748
2749 #[test]
2750 fn nested_tuple_index() {
2751     check_no_mismatches(
2752         r#"
2753 fn main() {
2754     let fld: i32 = ((0,),).0.0;
2755 }
2756 "#,
2757     );
2758 }