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