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