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