]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests/traits.rs
Add const generics
[rust.git] / crates / hir_ty / src / tests / traits.rs
1 use cov_mark::check;
2 use expect_test::expect;
3
4 use super::{check, check_infer, check_infer_with_mismatches, check_types};
5
6 #[test]
7 fn infer_await() {
8     check_types(
9         r#"
10 //- minicore: future
11 struct IntFuture;
12
13 impl core::future::Future for IntFuture {
14     type Output = u64;
15 }
16
17 fn test() {
18     let r = IntFuture;
19     let v = r.await;
20     v;
21 } //^ u64
22 "#,
23     );
24 }
25
26 #[test]
27 fn infer_async() {
28     check_types(
29         r#"
30 //- minicore: future
31 async fn foo() -> u64 { 128 }
32
33 fn test() {
34     let r = foo();
35     let v = r.await;
36     v;
37 } //^ u64
38 "#,
39     );
40 }
41
42 #[test]
43 fn infer_desugar_async() {
44     check_types(
45         r#"
46 //- minicore: future, sized
47 async fn foo() -> u64 { 128 }
48
49 fn test() {
50     let r = foo();
51     r;
52 } //^ impl Future<Output = u64>
53 "#,
54     );
55 }
56
57 #[test]
58 fn infer_async_block() {
59     check_types(
60         r#"
61 //- minicore: future, option
62 async fn test() {
63     let a = async { 42 };
64     a;
65 //  ^ impl Future<Output = i32>
66     let x = a.await;
67     x;
68 //  ^ i32
69     let b = async {}.await;
70     b;
71 //  ^ ()
72     let c = async {
73         let y = None;
74         y
75     //  ^ Option<u64>
76     };
77     let _: Option<u64> = c.await;
78     c;
79 //  ^ impl Future<Output = Option<u64>>
80 }
81 "#,
82     );
83 }
84
85 #[test]
86 fn infer_try() {
87     check_types(
88         r#"
89 //- /main.rs crate:main deps:core
90 fn test() {
91     let r: Result<i32, u64> = Result::Ok(1);
92     let v = r?;
93     v;
94 } //^ i32
95
96 //- /core.rs crate:core
97 pub mod ops {
98     pub trait Try {
99         type Ok;
100         type Error;
101     }
102 }
103
104 pub mod result {
105     pub enum Result<O, E> {
106         Ok(O),
107         Err(E)
108     }
109
110     impl<O, E> crate::ops::Try for Result<O, E> {
111         type Ok = O;
112         type Error = E;
113     }
114 }
115
116 pub mod prelude {
117     pub mod rust_2018 {
118         pub use crate::{result::*, ops::*};
119     }
120 }
121 "#,
122     );
123 }
124
125 #[test]
126 fn infer_try_trait_v2() {
127     check_types(
128         r#"
129 //- /main.rs crate:main deps:core
130 fn test() {
131     let r: Result<i32, u64> = Result::Ok(1);
132     let v = r?;
133     v;
134 } //^ i32
135
136 //- /core.rs crate:core
137 mod ops {
138     mod try_trait {
139         pub trait Try: FromResidual {
140             type Output;
141             type Residual;
142         }
143         pub trait FromResidual<R = <Self as Try>::Residual> {}
144     }
145
146     pub use self::try_trait::FromResidual;
147     pub use self::try_trait::Try;
148 }
149
150 mod convert {
151     pub trait From<T> {}
152     impl<T> From<T> for T {}
153 }
154
155 pub mod result {
156     use crate::convert::From;
157     use crate::ops::{Try, FromResidual};
158
159     pub enum Infallible {}
160     pub enum Result<O, E> {
161         Ok(O),
162         Err(E)
163     }
164
165     impl<O, E> Try for Result<O, E> {
166         type Output = O;
167         type Error = Result<Infallible, E>;
168     }
169
170     impl<T, E, F: From<E>> FromResidual<Result<Infallible, E>> for Result<T, F> {}
171 }
172
173 pub mod prelude {
174     pub mod rust_2018 {
175         pub use crate::result::*;
176     }
177 }
178 "#,
179     );
180 }
181
182 #[test]
183 fn infer_for_loop() {
184     check_types(
185         r#"
186 //- /main.rs crate:main deps:core,alloc
187 #![no_std]
188 use alloc::collections::Vec;
189
190 fn test() {
191     let v = Vec::new();
192     v.push("foo");
193     for x in v {
194         x;
195     } //^ &str
196 }
197
198 //- /core.rs crate:core
199 pub mod iter {
200     pub trait IntoIterator {
201         type Item;
202     }
203 }
204 pub mod prelude {
205     pub mod rust_2018 {
206         pub use crate::iter::*;
207     }
208 }
209
210 //- /alloc.rs crate:alloc deps:core
211 #![no_std]
212 pub mod collections {
213     pub struct Vec<T> {}
214     impl<T> Vec<T> {
215         pub fn new() -> Self { Vec {} }
216         pub fn push(&mut self, t: T) { }
217     }
218
219     impl<T> IntoIterator for Vec<T> {
220         type Item=T;
221     }
222 }
223 "#,
224     );
225 }
226
227 #[test]
228 fn infer_ops_neg() {
229     check_types(
230         r#"
231 //- /main.rs crate:main deps:std
232 struct Bar;
233 struct Foo;
234
235 impl std::ops::Neg for Bar {
236     type Output = Foo;
237 }
238
239 fn test() {
240     let a = Bar;
241     let b = -a;
242     b;
243 } //^ Foo
244
245 //- /std.rs crate:std
246 #[prelude_import] use ops::*;
247 mod ops {
248     #[lang = "neg"]
249     pub trait Neg {
250         type Output;
251     }
252 }
253 "#,
254     );
255 }
256
257 #[test]
258 fn infer_ops_not() {
259     check_types(
260         r#"
261 //- /main.rs crate:main deps:std
262 struct Bar;
263 struct Foo;
264
265 impl std::ops::Not for Bar {
266     type Output = Foo;
267 }
268
269 fn test() {
270     let a = Bar;
271     let b = !a;
272     b;
273 } //^ Foo
274
275 //- /std.rs crate:std
276 #[prelude_import] use ops::*;
277 mod ops {
278     #[lang = "not"]
279     pub trait Not {
280         type Output;
281     }
282 }
283 "#,
284     );
285 }
286
287 #[test]
288 fn infer_from_bound_1() {
289     check_types(
290         r#"
291 trait Trait<T> {}
292 struct S<T>(T);
293 impl<U> Trait<U> for S<U> {}
294 fn foo<T: Trait<u32>>(t: T) {}
295 fn test() {
296     let s = S(unknown);
297            // ^^^^^^^ u32
298     foo(s);
299 }"#,
300     );
301 }
302
303 #[test]
304 fn infer_from_bound_2() {
305     check_types(
306         r#"
307 trait Trait<T> {}
308 struct S<T>(T);
309 impl<U> Trait<U> for S<U> {}
310 fn foo<U, T: Trait<U>>(t: T) -> U { loop {} }
311 fn test() {
312     let s = S(unknown);
313            // ^^^^^^^ u32
314     let x: u32 = foo(s);
315 }"#,
316     );
317 }
318
319 #[test]
320 fn trait_default_method_self_bound_implements_trait() {
321     cov_mark::check!(trait_self_implements_self);
322     check(
323         r#"
324 trait Trait {
325     fn foo(&self) -> i64;
326     fn bar(&self) -> () {
327         self.foo();
328      // ^^^^^^^^^^ type: i64
329     }
330 }"#,
331     );
332 }
333
334 #[test]
335 fn trait_default_method_self_bound_implements_super_trait() {
336     check(
337         r#"
338 trait SuperTrait {
339     fn foo(&self) -> i64;
340 }
341 trait Trait: SuperTrait {
342     fn bar(&self) -> () {
343         self.foo();
344      // ^^^^^^^^^^ type: i64
345     }
346 }"#,
347     );
348 }
349
350 #[test]
351 fn infer_project_associated_type() {
352     check_types(
353         r#"
354 trait Iterable {
355     type Item;
356 }
357 struct S;
358 impl Iterable for S { type Item = u32; }
359 fn test<T: Iterable>() {
360     let x: <S as Iterable>::Item = 1;
361                                 // ^ u32
362     let y: <T as Iterable>::Item = u;
363                                 // ^ Iterable::Item<T>
364     let z: T::Item = u;
365                   // ^ Iterable::Item<T>
366     let a: <T>::Item = u;
367                     // ^ Iterable::Item<T>
368 }"#,
369     );
370 }
371
372 #[test]
373 fn infer_return_associated_type() {
374     check_types(
375         r#"
376 trait Iterable {
377     type Item;
378 }
379 struct S;
380 impl Iterable for S { type Item = u32; }
381 fn foo1<T: Iterable>(t: T) -> T::Item { loop {} }
382 fn foo2<T: Iterable>(t: T) -> <T as Iterable>::Item { loop {} }
383 fn foo3<T: Iterable>(t: T) -> <T>::Item { loop {} }
384 fn test() {
385     foo1(S);
386  // ^^^^^^^ u32
387     foo2(S);
388  // ^^^^^^^ u32
389     foo3(S);
390  // ^^^^^^^ u32
391 }"#,
392     );
393 }
394
395 #[test]
396 fn associated_type_shorthand_from_method_bound() {
397     check_types(
398         r#"
399 trait Iterable {
400     type Item;
401 }
402 struct S<T>;
403 impl<T> S<T> {
404     fn foo(self) -> T::Item where T: Iterable { loop {} }
405 }
406 fn test<T: Iterable>() {
407     let s: S<T>;
408     s.foo();
409  // ^^^^^^^ Iterable::Item<T>
410 }"#,
411     );
412 }
413
414 #[test]
415 fn infer_associated_type_bound() {
416     check_types(
417         r#"
418 trait Iterable {
419     type Item;
420 }
421 fn test<T: Iterable<Item=u32>>() {
422     let y: T::Item = unknown;
423                   // ^^^^^^^ u32
424 }"#,
425     );
426 }
427
428 #[test]
429 fn infer_const_body() {
430     // FIXME make check_types work with other bodies
431     check_infer(
432         r#"
433 const A: u32 = 1 + 1;
434 static B: u64 = { let x = 1; x };
435 "#,
436         expect![[r#"
437             15..16 '1': u32
438             15..20 '1 + 1': u32
439             19..20 '1': u32
440             38..54 '{ let ...1; x }': u64
441             44..45 'x': u64
442             48..49 '1': u64
443             51..52 'x': u64
444         "#]],
445     );
446 }
447
448 #[test]
449 fn tuple_struct_fields() {
450     check_infer(
451         r#"
452 struct S(i32, u64);
453 fn test() -> u64 {
454     let a = S(4, 6);
455     let b = a.0;
456     a.1
457 }"#,
458         expect![[r#"
459             37..86 '{     ... a.1 }': u64
460             47..48 'a': S
461             51..52 'S': S(i32, u64) -> S
462             51..58 'S(4, 6)': S
463             53..54 '4': i32
464             56..57 '6': u64
465             68..69 'b': i32
466             72..73 'a': S
467             72..75 'a.0': i32
468             81..82 'a': S
469             81..84 'a.1': u64
470         "#]],
471     );
472 }
473
474 #[test]
475 fn tuple_struct_with_fn() {
476     check_infer(
477         r#"
478 struct S(fn(u32) -> u64);
479 fn test() -> u64 {
480     let a = S(|i| 2*i);
481     let b = a.0(4);
482     a.0(2)
483 }"#,
484         expect![[r#"
485             43..101 '{     ...0(2) }': u64
486             53..54 'a': S
487             57..58 'S': S(fn(u32) -> u64) -> S
488             57..67 'S(|i| 2*i)': S
489             59..66 '|i| 2*i': |u32| -> u64
490             60..61 'i': u32
491             63..64 '2': u32
492             63..66 '2*i': u32
493             65..66 'i': u32
494             77..78 'b': u64
495             81..82 'a': S
496             81..84 'a.0': fn(u32) -> u64
497             81..87 'a.0(4)': u64
498             85..86 '4': u32
499             93..94 'a': S
500             93..96 'a.0': fn(u32) -> u64
501             93..99 'a.0(2)': u64
502             97..98 '2': u32
503         "#]],
504     );
505 }
506
507 #[test]
508 fn indexing_arrays() {
509     check_infer(
510         "fn main() { &mut [9][2]; }",
511         expect![[r#"
512             10..26 '{ &mut...[2]; }': ()
513             12..23 '&mut [9][2]': &mut {unknown}
514             17..20 '[9]': [i32; 1]
515             17..23 '[9][2]': {unknown}
516             18..19 '9': i32
517             21..22 '2': i32
518         "#]],
519     )
520 }
521
522 #[test]
523 fn infer_ops_index() {
524     check_types(
525         r#"
526 //- minicore: index
527 struct Bar;
528 struct Foo;
529
530 impl core::ops::Index<u32> for Bar {
531     type Output = Foo;
532 }
533
534 fn test() {
535     let a = Bar;
536     let b = a[1u32];
537     b;
538 } //^ Foo
539 "#,
540     );
541 }
542
543 #[test]
544 fn infer_ops_index_field() {
545     check_types(
546         r#"
547 //- minicore: index
548 struct Bar;
549 struct Foo {
550     field: u32;
551 }
552
553 impl core::ops::Index<u32> for Bar {
554     type Output = Foo;
555 }
556
557 fn test() {
558     let a = Bar;
559     let b = a[1u32].field;
560     b;
561 } //^ u32
562 "#,
563     );
564 }
565
566 #[test]
567 fn infer_ops_index_field_autoderef() {
568     check_types(
569         r#"
570 //- minicore: index
571 struct Bar;
572 struct Foo {
573     field: u32;
574 }
575
576 impl core::ops::Index<u32> for Bar {
577     type Output = Foo;
578 }
579
580 fn test() {
581     let a = Bar;
582     let b = (&a[1u32]).field;
583     b;
584 } //^ u32
585 "#,
586     );
587 }
588
589 #[test]
590 fn infer_ops_index_int() {
591     check_types(
592         r#"
593 //- minicore: index
594 struct Bar;
595 struct Foo;
596
597 impl core::ops::Index<u32> for Bar {
598     type Output = Foo;
599 }
600
601 struct Range;
602 impl core::ops::Index<Range> for Bar {
603     type Output = Bar;
604 }
605
606 fn test() {
607     let a = Bar;
608     let b = a[1];
609     b;
610   //^ Foo
611 }
612 "#,
613     );
614 }
615
616 #[test]
617 fn infer_ops_index_autoderef() {
618     check_types(
619         r#"
620 //- minicore: index, slice
621 fn test() {
622     let a = &[1u32, 2, 3];
623     let b = a[1];
624     b;
625 } //^ u32
626 "#,
627     );
628 }
629
630 #[test]
631 fn deref_trait() {
632     check_types(
633         r#"
634 //- minicore: deref
635 struct Arc<T: ?Sized>;
636 impl<T: ?Sized> core::ops::Deref for Arc<T> {
637     type Target = T;
638 }
639
640 struct S;
641 impl S {
642     fn foo(&self) -> u128 { 0 }
643 }
644
645 fn test(s: Arc<S>) {
646     (*s, s.foo());
647 } //^^^^^^^^^^^^^ (S, u128)
648 "#,
649     );
650 }
651
652 #[test]
653 fn deref_trait_with_inference_var() {
654     check_types(
655         r#"
656 //- minicore: deref
657 struct Arc<T: ?Sized>;
658 fn new_arc<T: ?Sized>() -> Arc<T> { Arc }
659 impl<T: ?Sized> core::ops::Deref for Arc<T> {
660     type Target = T;
661 }
662
663 struct S;
664 fn foo(a: Arc<S>) {}
665
666 fn test() {
667     let a = new_arc();
668     let b = *a;
669           //^^ S
670     foo(a);
671 }
672 "#,
673     );
674 }
675
676 #[test]
677 fn deref_trait_infinite_recursion() {
678     check_types(
679         r#"
680 //- minicore: deref
681 struct S;
682
683 impl core::ops::Deref for S {
684     type Target = S;
685 }
686
687 fn test(s: S) {
688     s.foo();
689 } //^^^^^^^ {unknown}
690 "#,
691     );
692 }
693
694 #[test]
695 fn deref_trait_with_question_mark_size() {
696     check_types(
697         r#"
698 //- minicore: deref
699 struct Arc<T: ?Sized>;
700 impl<T: ?Sized> core::ops::Deref for Arc<T> {
701     type Target = T;
702 }
703
704 struct S;
705 impl S {
706     fn foo(&self) -> u128 { 0 }
707 }
708
709 fn test(s: Arc<S>) {
710     (*s, s.foo());
711 } //^^^^^^^^^^^^^ (S, u128)
712 "#,
713     );
714 }
715
716 #[test]
717 fn deref_trait_with_implicit_sized_requirement_on_inference_var() {
718     check_types(
719         r#"
720 //- minicore: deref
721 struct Foo<T>;
722 impl<T> core::ops::Deref for Foo<T> {
723     type Target = ();
724 }
725 fn test() {
726     let foo = Foo;
727     *foo;
728   //^^^^ ()
729     let _: Foo<u8> = foo;
730 }
731 "#,
732     )
733 }
734
735 #[test]
736 fn obligation_from_function_clause() {
737     check_types(
738         r#"
739 struct S;
740
741 trait Trait<T> {}
742 impl Trait<u32> for S {}
743
744 fn foo<T: Trait<U>, U>(t: T) -> U { loop {} }
745
746 fn test(s: S) {
747     foo(s);
748 } //^^^^^^ u32
749 "#,
750     );
751 }
752
753 #[test]
754 fn obligation_from_method_clause() {
755     check_types(
756         r#"
757 //- /main.rs
758 struct S;
759
760 trait Trait<T> {}
761 impl Trait<isize> for S {}
762
763 struct O;
764 impl O {
765     fn foo<T: Trait<U>, U>(&self, t: T) -> U { loop {} }
766 }
767
768 fn test() {
769     O.foo(S);
770 } //^^^^^^^^ isize
771 "#,
772     );
773 }
774
775 #[test]
776 fn obligation_from_self_method_clause() {
777     check_types(
778         r#"
779 struct S;
780
781 trait Trait<T> {}
782 impl Trait<i64> for S {}
783
784 impl S {
785     fn foo<U>(&self) -> U where Self: Trait<U> { loop {} }
786 }
787
788 fn test() {
789     S.foo();
790 } //^^^^^^^ i64
791 "#,
792     );
793 }
794
795 #[test]
796 fn obligation_from_impl_clause() {
797     check_types(
798         r#"
799 struct S;
800
801 trait Trait<T> {}
802 impl Trait<&str> for S {}
803
804 struct O<T>;
805 impl<U, T: Trait<U>> O<T> {
806     fn foo(&self) -> U { loop {} }
807 }
808
809 fn test(o: O<S>) {
810     o.foo();
811 } //^^^^^^^ &str
812 "#,
813     );
814 }
815
816 #[test]
817 fn generic_param_env_1() {
818     check_types(
819         r#"
820 trait Clone {}
821 trait Trait { fn foo(self) -> u128; }
822 struct S;
823 impl Clone for S {}
824 impl<T> Trait for T where T: Clone {}
825 fn test<T: Clone>(t: T) { t.foo(); }
826                         //^^^^^^^ u128
827 "#,
828     );
829 }
830
831 #[test]
832 fn generic_param_env_1_not_met() {
833     check_types(
834         r#"
835 //- /main.rs
836 trait Clone {}
837 trait Trait { fn foo(self) -> u128; }
838 struct S;
839 impl Clone for S {}
840 impl<T> Trait for T where T: Clone {}
841 fn test<T>(t: T) { t.foo(); }
842                  //^^^^^^^ {unknown}
843 "#,
844     );
845 }
846
847 #[test]
848 fn generic_param_env_2() {
849     check_types(
850         r#"
851 trait Trait { fn foo(self) -> u128; }
852 struct S;
853 impl Trait for S {}
854 fn test<T: Trait>(t: T) { t.foo(); }
855                         //^^^^^^^ u128
856 "#,
857     );
858 }
859
860 #[test]
861 fn generic_param_env_2_not_met() {
862     check_types(
863         r#"
864 trait Trait { fn foo(self) -> u128; }
865 struct S;
866 impl Trait for S {}
867 fn test<T>(t: T) { t.foo(); }
868                  //^^^^^^^ {unknown}
869 "#,
870     );
871 }
872
873 #[test]
874 fn generic_param_env_deref() {
875     check_types(
876         r#"
877 //- minicore: deref
878 trait Trait {}
879 impl<T> core::ops::Deref for T where T: Trait {
880     type Target = i128;
881 }
882 fn test<T: Trait>(t: T) { *t; }
883                         //^^ i128
884 "#,
885     );
886 }
887
888 #[test]
889 fn associated_type_placeholder() {
890     // inside the generic function, the associated type gets normalized to a placeholder `ApplL::Out<T>` [https://rust-lang.github.io/rustc-guide/traits/associated-types.html#placeholder-associated-types].
891     check_types(
892         r#"
893 pub trait ApplyL {
894     type Out;
895 }
896
897 pub struct RefMutL<T>;
898
899 impl<T> ApplyL for RefMutL<T> {
900     type Out = <T as ApplyL>::Out;
901 }
902
903 fn test<T: ApplyL>() {
904     let y: <RefMutL<T> as ApplyL>::Out = no_matter;
905     y;
906 } //^ ApplyL::Out<T>
907 "#,
908     );
909 }
910
911 #[test]
912 fn associated_type_placeholder_2() {
913     check_types(
914         r#"
915 pub trait ApplyL {
916     type Out;
917 }
918 fn foo<T: ApplyL>(t: T) -> <T as ApplyL>::Out;
919
920 fn test<T: ApplyL>(t: T) {
921     let y = foo(t);
922     y;
923 } //^ ApplyL::Out<T>
924 "#,
925     );
926 }
927
928 #[test]
929 fn argument_impl_trait() {
930     check_infer_with_mismatches(
931         r#"
932 //- minicore: sized
933 trait Trait<T> {
934     fn foo(&self) -> T;
935     fn foo2(&self) -> i64;
936 }
937 fn bar(x: impl Trait<u16>) {}
938 struct S<T>(T);
939 impl<T> Trait<T> for S<T> {}
940
941 fn test(x: impl Trait<u64>, y: &impl Trait<u32>) {
942     x;
943     y;
944     let z = S(1);
945     bar(z);
946     x.foo();
947     y.foo();
948     z.foo();
949     x.foo2();
950     y.foo2();
951     z.foo2();
952 }"#,
953         expect![[r#"
954             29..33 'self': &Self
955             54..58 'self': &Self
956             77..78 'x': impl Trait<u16>
957             97..99 '{}': ()
958             154..155 'x': impl Trait<u64>
959             174..175 'y': &impl Trait<u32>
960             195..323 '{     ...2(); }': ()
961             201..202 'x': impl Trait<u64>
962             208..209 'y': &impl Trait<u32>
963             219..220 'z': S<u16>
964             223..224 'S': S<u16>(u16) -> S<u16>
965             223..227 'S(1)': S<u16>
966             225..226 '1': u16
967             233..236 'bar': fn bar(S<u16>)
968             233..239 'bar(z)': ()
969             237..238 'z': S<u16>
970             245..246 'x': impl Trait<u64>
971             245..252 'x.foo()': u64
972             258..259 'y': &impl Trait<u32>
973             258..265 'y.foo()': u32
974             271..272 'z': S<u16>
975             271..278 'z.foo()': u16
976             284..285 'x': impl Trait<u64>
977             284..292 'x.foo2()': i64
978             298..299 'y': &impl Trait<u32>
979             298..306 'y.foo2()': i64
980             312..313 'z': S<u16>
981             312..320 'z.foo2()': i64
982         "#]],
983     );
984 }
985
986 #[test]
987 fn argument_impl_trait_type_args_1() {
988     check_infer_with_mismatches(
989         r#"
990 //- minicore: sized
991 trait Trait {}
992 trait Foo {
993     // this function has an implicit Self param, an explicit type param,
994     // and an implicit impl Trait param!
995     fn bar<T>(x: impl Trait) -> T { loop {} }
996 }
997 fn foo<T>(x: impl Trait) -> T { loop {} }
998 struct S;
999 impl Trait for S {}
1000 struct F;
1001 impl Foo for F {}
1002
1003 fn test() {
1004     Foo::bar(S);
1005     <F as Foo>::bar(S);
1006     F::bar(S);
1007     Foo::bar::<u32>(S);
1008     <F as Foo>::bar::<u32>(S);
1009
1010     foo(S);
1011     foo::<u32>(S);
1012     foo::<u32, i32>(S); // we should ignore the extraneous i32
1013 }"#,
1014         expect![[r#"
1015             155..156 'x': impl Trait
1016             175..186 '{ loop {} }': T
1017             177..184 'loop {}': !
1018             182..184 '{}': ()
1019             199..200 'x': impl Trait
1020             219..230 '{ loop {} }': T
1021             221..228 'loop {}': !
1022             226..228 '{}': ()
1023             300..509 '{     ... i32 }': ()
1024             306..314 'Foo::bar': fn bar<{unknown}, {unknown}>(S) -> {unknown}
1025             306..317 'Foo::bar(S)': {unknown}
1026             315..316 'S': S
1027             323..338 '<F as Foo>::bar': fn bar<F, {unknown}>(S) -> {unknown}
1028             323..341 '<F as ...bar(S)': {unknown}
1029             339..340 'S': S
1030             347..353 'F::bar': fn bar<F, {unknown}>(S) -> {unknown}
1031             347..356 'F::bar(S)': {unknown}
1032             354..355 'S': S
1033             362..377 'Foo::bar::<u32>': fn bar<{unknown}, u32>(S) -> u32
1034             362..380 'Foo::b...32>(S)': u32
1035             378..379 'S': S
1036             386..408 '<F as ...:<u32>': fn bar<F, u32>(S) -> u32
1037             386..411 '<F as ...32>(S)': u32
1038             409..410 'S': S
1039             418..421 'foo': fn foo<{unknown}>(S) -> {unknown}
1040             418..424 'foo(S)': {unknown}
1041             422..423 'S': S
1042             430..440 'foo::<u32>': fn foo<u32>(S) -> u32
1043             430..443 'foo::<u32>(S)': u32
1044             441..442 'S': S
1045             449..464 'foo::<u32, i32>': fn foo<u32>(S) -> u32
1046             449..467 'foo::<...32>(S)': u32
1047             465..466 'S': S
1048         "#]],
1049     );
1050 }
1051
1052 #[test]
1053 fn argument_impl_trait_type_args_2() {
1054     check_infer_with_mismatches(
1055         r#"
1056 //- minicore: sized
1057 trait Trait {}
1058 struct S;
1059 impl Trait for S {}
1060 struct F<T>;
1061 impl<T> F<T> {
1062     fn foo<U>(self, x: impl Trait) -> (T, U) { loop {} }
1063 }
1064
1065 fn test() {
1066     F.foo(S);
1067     F::<u32>.foo(S);
1068     F::<u32>.foo::<i32>(S);
1069     F::<u32>.foo::<i32, u32>(S); // extraneous argument should be ignored
1070 }"#,
1071         expect![[r#"
1072             87..91 'self': F<T>
1073             93..94 'x': impl Trait
1074             118..129 '{ loop {} }': (T, U)
1075             120..127 'loop {}': !
1076             125..127 '{}': ()
1077             143..283 '{     ...ored }': ()
1078             149..150 'F': F<{unknown}>
1079             149..157 'F.foo(S)': ({unknown}, {unknown})
1080             155..156 'S': S
1081             163..171 'F::<u32>': F<u32>
1082             163..178 'F::<u32>.foo(S)': (u32, {unknown})
1083             176..177 'S': S
1084             184..192 'F::<u32>': F<u32>
1085             184..206 'F::<u3...32>(S)': (u32, i32)
1086             204..205 'S': S
1087             212..220 'F::<u32>': F<u32>
1088             212..239 'F::<u3...32>(S)': (u32, i32)
1089             237..238 'S': S
1090         "#]],
1091     );
1092 }
1093
1094 #[test]
1095 fn argument_impl_trait_to_fn_pointer() {
1096     check_infer_with_mismatches(
1097         r#"
1098 //- minicore: sized
1099 trait Trait {}
1100 fn foo(x: impl Trait) { loop {} }
1101 struct S;
1102 impl Trait for S {}
1103
1104 fn test() {
1105     let f: fn(S) -> () = foo;
1106 }"#,
1107         expect![[r#"
1108             22..23 'x': impl Trait
1109             37..48 '{ loop {} }': ()
1110             39..46 'loop {}': !
1111             44..46 '{}': ()
1112             90..123 '{     ...foo; }': ()
1113             100..101 'f': fn(S)
1114             117..120 'foo': fn foo(S)
1115         "#]],
1116     );
1117 }
1118
1119 #[test]
1120 fn impl_trait() {
1121     check_infer(
1122         r#"
1123 //- minicore: sized
1124 trait Trait<T> {
1125     fn foo(&self) -> T;
1126     fn foo2(&self) -> i64;
1127 }
1128 fn bar() -> impl Trait<u64> {}
1129
1130 fn test(x: impl Trait<u64>, y: &impl Trait<u64>) {
1131     x;
1132     y;
1133     let z = bar();
1134     x.foo();
1135     y.foo();
1136     z.foo();
1137     x.foo2();
1138     y.foo2();
1139     z.foo2();
1140 }"#,
1141         expect![[r#"
1142             29..33 'self': &Self
1143             54..58 'self': &Self
1144             98..100 '{}': ()
1145             110..111 'x': impl Trait<u64>
1146             130..131 'y': &impl Trait<u64>
1147             151..268 '{     ...2(); }': ()
1148             157..158 'x': impl Trait<u64>
1149             164..165 'y': &impl Trait<u64>
1150             175..176 'z': impl Trait<u64>
1151             179..182 'bar': fn bar() -> impl Trait<u64>
1152             179..184 'bar()': impl Trait<u64>
1153             190..191 'x': impl Trait<u64>
1154             190..197 'x.foo()': u64
1155             203..204 'y': &impl Trait<u64>
1156             203..210 'y.foo()': u64
1157             216..217 'z': impl Trait<u64>
1158             216..223 'z.foo()': u64
1159             229..230 'x': impl Trait<u64>
1160             229..237 'x.foo2()': i64
1161             243..244 'y': &impl Trait<u64>
1162             243..251 'y.foo2()': i64
1163             257..258 'z': impl Trait<u64>
1164             257..265 'z.foo2()': i64
1165         "#]],
1166     );
1167 }
1168
1169 #[test]
1170 fn simple_return_pos_impl_trait() {
1171     cov_mark::check!(lower_rpit);
1172     check_infer(
1173         r#"
1174 //- minicore: sized
1175 trait Trait<T> {
1176     fn foo(&self) -> T;
1177 }
1178 fn bar() -> impl Trait<u64> { loop {} }
1179
1180 fn test() {
1181     let a = bar();
1182     a.foo();
1183 }"#,
1184         expect![[r#"
1185             29..33 'self': &Self
1186             71..82 '{ loop {} }': !
1187             73..80 'loop {}': !
1188             78..80 '{}': ()
1189             94..129 '{     ...o(); }': ()
1190             104..105 'a': impl Trait<u64>
1191             108..111 'bar': fn bar() -> impl Trait<u64>
1192             108..113 'bar()': impl Trait<u64>
1193             119..120 'a': impl Trait<u64>
1194             119..126 'a.foo()': u64
1195         "#]],
1196     );
1197 }
1198
1199 #[test]
1200 fn more_return_pos_impl_trait() {
1201     check_infer(
1202         r#"
1203 //- minicore: sized
1204 trait Iterator {
1205     type Item;
1206     fn next(&mut self) -> Self::Item;
1207 }
1208 trait Trait<T> {
1209     fn foo(&self) -> T;
1210 }
1211 fn bar() -> (impl Iterator<Item = impl Trait<u32>>, impl Trait<u64>) { loop {} }
1212 fn baz<T>(t: T) -> (impl Iterator<Item = impl Trait<T>>, impl Trait<T>) { loop {} }
1213
1214 fn test() {
1215     let (a, b) = bar();
1216     a.next().foo();
1217     b.foo();
1218     let (c, d) = baz(1u128);
1219     c.next().foo();
1220     d.foo();
1221 }"#,
1222         expect![[r#"
1223             49..53 'self': &mut Self
1224             101..105 'self': &Self
1225             184..195 '{ loop {} }': ({unknown}, {unknown})
1226             186..193 'loop {}': !
1227             191..193 '{}': ()
1228             206..207 't': T
1229             268..279 '{ loop {} }': ({unknown}, {unknown})
1230             270..277 'loop {}': !
1231             275..277 '{}': ()
1232             291..413 '{     ...o(); }': ()
1233             301..307 '(a, b)': (impl Iterator<Item = impl Trait<u32>>, impl Trait<u64>)
1234             302..303 'a': impl Iterator<Item = impl Trait<u32>>
1235             305..306 'b': impl Trait<u64>
1236             310..313 'bar': fn bar() -> (impl Iterator<Item = impl Trait<u32>>, impl Trait<u64>)
1237             310..315 'bar()': (impl Iterator<Item = impl Trait<u32>>, impl Trait<u64>)
1238             321..322 'a': impl Iterator<Item = impl Trait<u32>>
1239             321..329 'a.next()': impl Trait<u32>
1240             321..335 'a.next().foo()': u32
1241             341..342 'b': impl Trait<u64>
1242             341..348 'b.foo()': u64
1243             358..364 '(c, d)': (impl Iterator<Item = impl Trait<u128>>, impl Trait<u128>)
1244             359..360 'c': impl Iterator<Item = impl Trait<u128>>
1245             362..363 'd': impl Trait<u128>
1246             367..370 'baz': fn baz<u128>(u128) -> (impl Iterator<Item = impl Trait<u128>>, impl Trait<u128>)
1247             367..377 'baz(1u128)': (impl Iterator<Item = impl Trait<u128>>, impl Trait<u128>)
1248             371..376 '1u128': u128
1249             383..384 'c': impl Iterator<Item = impl Trait<u128>>
1250             383..391 'c.next()': impl Trait<u128>
1251             383..397 'c.next().foo()': u128
1252             403..404 'd': impl Trait<u128>
1253             403..410 'd.foo()': u128
1254         "#]],
1255     );
1256 }
1257
1258 #[test]
1259 fn dyn_trait() {
1260     check_infer(
1261         r#"
1262 //- minicore: sized
1263 trait Trait<T> {
1264     fn foo(&self) -> T;
1265     fn foo2(&self) -> i64;
1266 }
1267 fn bar() -> dyn Trait<u64> {}
1268
1269 fn test(x: dyn Trait<u64>, y: &dyn Trait<u64>) {
1270     x;
1271     y;
1272     let z = bar();
1273     x.foo();
1274     y.foo();
1275     z.foo();
1276     x.foo2();
1277     y.foo2();
1278     z.foo2();
1279 }"#,
1280         expect![[r#"
1281             29..33 'self': &Self
1282             54..58 'self': &Self
1283             97..99 '{}': ()
1284             109..110 'x': dyn Trait<u64>
1285             128..129 'y': &dyn Trait<u64>
1286             148..265 '{     ...2(); }': ()
1287             154..155 'x': dyn Trait<u64>
1288             161..162 'y': &dyn Trait<u64>
1289             172..173 'z': dyn Trait<u64>
1290             176..179 'bar': fn bar() -> dyn Trait<u64>
1291             176..181 'bar()': dyn Trait<u64>
1292             187..188 'x': dyn Trait<u64>
1293             187..194 'x.foo()': u64
1294             200..201 'y': &dyn Trait<u64>
1295             200..207 'y.foo()': u64
1296             213..214 'z': dyn Trait<u64>
1297             213..220 'z.foo()': u64
1298             226..227 'x': dyn Trait<u64>
1299             226..234 'x.foo2()': i64
1300             240..241 'y': &dyn Trait<u64>
1301             240..248 'y.foo2()': i64
1302             254..255 'z': dyn Trait<u64>
1303             254..262 'z.foo2()': i64
1304         "#]],
1305     );
1306 }
1307
1308 #[test]
1309 fn dyn_trait_in_impl() {
1310     check_infer(
1311         r#"
1312 //- minicore: sized
1313 trait Trait<T, U> {
1314     fn foo(&self) -> (T, U);
1315 }
1316 struct S<T, U> {}
1317 impl<T, U> S<T, U> {
1318     fn bar(&self) -> &dyn Trait<T, U> { loop {} }
1319 }
1320 trait Trait2<T, U> {
1321     fn baz(&self) -> (T, U);
1322 }
1323 impl<T, U> Trait2<T, U> for dyn Trait<T, U> { }
1324
1325 fn test(s: S<u32, i32>) {
1326     s.bar().baz();
1327 }"#,
1328         expect![[r#"
1329             32..36 'self': &Self
1330             102..106 'self': &S<T, U>
1331             128..139 '{ loop {} }': &dyn Trait<T, U>
1332             130..137 'loop {}': !
1333             135..137 '{}': ()
1334             175..179 'self': &Self
1335             251..252 's': S<u32, i32>
1336             267..289 '{     ...z(); }': ()
1337             273..274 's': S<u32, i32>
1338             273..280 's.bar()': &dyn Trait<u32, i32>
1339             273..286 's.bar().baz()': (u32, i32)
1340         "#]],
1341     );
1342 }
1343
1344 #[test]
1345 fn dyn_trait_bare() {
1346     check_infer(
1347         r#"
1348 //- minicore: sized
1349 trait Trait {
1350     fn foo(&self) -> u64;
1351 }
1352 fn bar() -> Trait {}
1353
1354 fn test(x: Trait, y: &Trait) -> u64 {
1355     x;
1356     y;
1357     let z = bar();
1358     x.foo();
1359     y.foo();
1360     z.foo();
1361 }"#,
1362         expect![[r#"
1363             26..30 'self': &Self
1364             60..62 '{}': ()
1365             72..73 'x': dyn Trait
1366             82..83 'y': &dyn Trait
1367             100..175 '{     ...o(); }': ()
1368             106..107 'x': dyn Trait
1369             113..114 'y': &dyn Trait
1370             124..125 'z': dyn Trait
1371             128..131 'bar': fn bar() -> dyn Trait
1372             128..133 'bar()': dyn Trait
1373             139..140 'x': dyn Trait
1374             139..146 'x.foo()': u64
1375             152..153 'y': &dyn Trait
1376             152..159 'y.foo()': u64
1377             165..166 'z': dyn Trait
1378             165..172 'z.foo()': u64
1379         "#]],
1380     );
1381 }
1382
1383 #[test]
1384 fn weird_bounds() {
1385     check_infer(
1386         r#"
1387 //- minicore: sized
1388 trait Trait {}
1389 fn test(
1390     a: impl Trait + 'lifetime,
1391     b: impl 'lifetime,
1392     c: impl (Trait),
1393     d: impl ('lifetime),
1394     e: impl ?Sized,
1395     f: impl Trait + ?Sized
1396 ) {}
1397 "#,
1398         expect![[r#"
1399             28..29 'a': impl Trait
1400             59..60 'b': impl Sized
1401             82..83 'c': impl Trait
1402             103..104 'd': impl Sized
1403             128..129 'e': impl ?Sized
1404             148..149 'f': impl Trait + ?Sized
1405             173..175 '{}': ()
1406         "#]],
1407     );
1408 }
1409
1410 #[test]
1411 fn error_bound_chalk() {
1412     check_types(
1413         r#"
1414 trait Trait {
1415     fn foo(&self) -> u32 { 0 }
1416 }
1417
1418 fn test(x: (impl Trait + UnknownTrait)) {
1419     x.foo();
1420 } //^^^^^^^ u32
1421 "#,
1422     );
1423 }
1424
1425 #[test]
1426 fn assoc_type_bindings() {
1427     check_infer(
1428         r#"
1429 //- minicore: sized
1430 trait Trait {
1431     type Type;
1432 }
1433
1434 fn get<T: Trait>(t: T) -> <T as Trait>::Type {}
1435 fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
1436 fn set<T: Trait<Type = u64>>(t: T) -> T {t}
1437
1438 struct S<T>;
1439 impl<T> Trait for S<T> { type Type = T; }
1440
1441 fn test<T: Trait<Type = u32>>(x: T, y: impl Trait<Type = i64>) {
1442     get(x);
1443     get2(x);
1444     get(y);
1445     get2(y);
1446     get(set(S));
1447     get2(set(S));
1448     get2(S::<str>);
1449 }"#,
1450         expect![[r#"
1451             49..50 't': T
1452             77..79 '{}': ()
1453             111..112 't': T
1454             122..124 '{}': ()
1455             154..155 't': T
1456             165..168 '{t}': T
1457             166..167 't': T
1458             256..257 'x': T
1459             262..263 'y': impl Trait<Type = i64>
1460             289..397 '{     ...r>); }': ()
1461             295..298 'get': fn get<T>(T) -> <T as Trait>::Type
1462             295..301 'get(x)': u32
1463             299..300 'x': T
1464             307..311 'get2': fn get2<u32, T>(T) -> u32
1465             307..314 'get2(x)': u32
1466             312..313 'x': T
1467             320..323 'get': fn get<impl Trait<Type = i64>>(impl Trait<Type = i64>) -> <impl Trait<Type = i64> as Trait>::Type
1468             320..326 'get(y)': i64
1469             324..325 'y': impl Trait<Type = i64>
1470             332..336 'get2': fn get2<i64, impl Trait<Type = i64>>(impl Trait<Type = i64>) -> i64
1471             332..339 'get2(y)': i64
1472             337..338 'y': impl Trait<Type = i64>
1473             345..348 'get': fn get<S<u64>>(S<u64>) -> <S<u64> as Trait>::Type
1474             345..356 'get(set(S))': u64
1475             349..352 'set': fn set<S<u64>>(S<u64>) -> S<u64>
1476             349..355 'set(S)': S<u64>
1477             353..354 'S': S<u64>
1478             362..366 'get2': fn get2<u64, S<u64>>(S<u64>) -> u64
1479             362..374 'get2(set(S))': u64
1480             367..370 'set': fn set<S<u64>>(S<u64>) -> S<u64>
1481             367..373 'set(S)': S<u64>
1482             371..372 'S': S<u64>
1483             380..384 'get2': fn get2<str, S<str>>(S<str>) -> str
1484             380..394 'get2(S::<str>)': str
1485             385..393 'S::<str>': S<str>
1486         "#]],
1487     );
1488 }
1489
1490 #[test]
1491 fn impl_trait_assoc_binding_projection_bug() {
1492     check_types(
1493         r#"
1494 //- minicore: iterator
1495 pub trait Language {
1496     type Kind;
1497 }
1498 pub enum RustLanguage {}
1499 impl Language for RustLanguage {
1500     type Kind = SyntaxKind;
1501 }
1502 struct SyntaxNode<L> {}
1503 fn foo() -> impl Iterator<Item = SyntaxNode<RustLanguage>> {}
1504
1505 trait Clone {
1506     fn clone(&self) -> Self;
1507 }
1508
1509 fn api_walkthrough() {
1510     for node in foo() {
1511         node.clone();
1512     } //^^^^^^^^^^^^ {unknown}
1513 }
1514 "#,
1515     );
1516 }
1517
1518 #[test]
1519 fn projection_eq_within_chalk() {
1520     check_infer(
1521         r#"
1522 trait Trait1 {
1523     type Type;
1524 }
1525 trait Trait2<T> {
1526     fn foo(self) -> T;
1527 }
1528 impl<T, U> Trait2<T> for U where U: Trait1<Type = T> {}
1529
1530 fn test<T: Trait1<Type = u32>>(x: T) {
1531     x.foo();
1532 }"#,
1533         expect![[r#"
1534             61..65 'self': Self
1535             163..164 'x': T
1536             169..185 '{     ...o(); }': ()
1537             175..176 'x': T
1538             175..182 'x.foo()': u32
1539         "#]],
1540     );
1541 }
1542
1543 #[test]
1544 fn where_clause_trait_in_scope_for_method_resolution() {
1545     check_types(
1546         r#"
1547 mod foo {
1548     trait Trait {
1549         fn foo(&self) -> u32 { 0 }
1550     }
1551 }
1552
1553 fn test<T: foo::Trait>(x: T) {
1554     x.foo();
1555 } //^^^^^^^ u32
1556 "#,
1557     );
1558 }
1559
1560 #[test]
1561 fn super_trait_method_resolution() {
1562     check_infer(
1563         r#"
1564 mod foo {
1565     trait SuperTrait {
1566         fn foo(&self) -> u32 {}
1567     }
1568 }
1569 trait Trait1: foo::SuperTrait {}
1570 trait Trait2 where Self: foo::SuperTrait {}
1571
1572 fn test<T: Trait1, U: Trait2>(x: T, y: U) {
1573     x.foo();
1574     y.foo();
1575 }"#,
1576         expect![[r#"
1577             49..53 'self': &Self
1578             62..64 '{}': ()
1579             181..182 'x': T
1580             187..188 'y': U
1581             193..222 '{     ...o(); }': ()
1582             199..200 'x': T
1583             199..206 'x.foo()': u32
1584             212..213 'y': U
1585             212..219 'y.foo()': u32
1586         "#]],
1587     );
1588 }
1589
1590 #[test]
1591 fn super_trait_impl_trait_method_resolution() {
1592     check_infer(
1593         r#"
1594 //- minicore: sized
1595 mod foo {
1596     trait SuperTrait {
1597         fn foo(&self) -> u32 {}
1598     }
1599 }
1600 trait Trait1: foo::SuperTrait {}
1601
1602 fn test(x: &impl Trait1) {
1603     x.foo();
1604 }"#,
1605         expect![[r#"
1606             49..53 'self': &Self
1607             62..64 '{}': ()
1608             115..116 'x': &impl Trait1
1609             132..148 '{     ...o(); }': ()
1610             138..139 'x': &impl Trait1
1611             138..145 'x.foo()': u32
1612         "#]],
1613     );
1614 }
1615
1616 #[test]
1617 fn super_trait_cycle() {
1618     // This just needs to not crash
1619     check_infer(
1620         r#"
1621         trait A: B {}
1622         trait B: A {}
1623
1624         fn test<T: A>(x: T) {
1625             x.foo();
1626         }
1627         "#,
1628         expect![[r#"
1629             43..44 'x': T
1630             49..65 '{     ...o(); }': ()
1631             55..56 'x': T
1632             55..62 'x.foo()': {unknown}
1633         "#]],
1634     );
1635 }
1636
1637 #[test]
1638 fn super_trait_assoc_type_bounds() {
1639     check_infer(
1640         r#"
1641 trait SuperTrait { type Type; }
1642 trait Trait where Self: SuperTrait {}
1643
1644 fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
1645 fn set<T: Trait<Type = u64>>(t: T) -> T {t}
1646
1647 struct S<T>;
1648 impl<T> SuperTrait for S<T> { type Type = T; }
1649 impl<T> Trait for S<T> {}
1650
1651 fn test() {
1652     get2(set(S));
1653 }"#,
1654         expect![[r#"
1655             102..103 't': T
1656             113..115 '{}': ()
1657             145..146 't': T
1658             156..159 '{t}': T
1659             157..158 't': T
1660             258..279 '{     ...S)); }': ()
1661             264..268 'get2': fn get2<u64, S<u64>>(S<u64>) -> u64
1662             264..276 'get2(set(S))': u64
1663             269..272 'set': fn set<S<u64>>(S<u64>) -> S<u64>
1664             269..275 'set(S)': S<u64>
1665             273..274 'S': S<u64>
1666         "#]],
1667     );
1668 }
1669
1670 #[test]
1671 fn fn_trait() {
1672     check_infer_with_mismatches(
1673         r#"
1674 trait FnOnce<Args> {
1675     type Output;
1676
1677     fn call_once(self, args: Args) -> <Self as FnOnce<Args>>::Output;
1678 }
1679
1680 fn test<F: FnOnce(u32, u64) -> u128>(f: F) {
1681     f.call_once((1, 2));
1682 }"#,
1683         expect![[r#"
1684             56..60 'self': Self
1685             62..66 'args': Args
1686             149..150 'f': F
1687             155..183 '{     ...2)); }': ()
1688             161..162 'f': F
1689             161..180 'f.call...1, 2))': u128
1690             173..179 '(1, 2)': (u32, u64)
1691             174..175 '1': u32
1692             177..178 '2': u64
1693         "#]],
1694     );
1695 }
1696
1697 #[test]
1698 fn fn_ptr_and_item() {
1699     check_infer_with_mismatches(
1700         r#"
1701 #[lang="fn_once"]
1702 trait FnOnce<Args> {
1703     type Output;
1704
1705     fn call_once(self, args: Args) -> Self::Output;
1706 }
1707
1708 trait Foo<T> {
1709     fn foo(&self) -> T;
1710 }
1711
1712 struct Bar<T>(T);
1713
1714 impl<A1, R, F: FnOnce(A1) -> R> Foo<(A1, R)> for Bar<F> {
1715     fn foo(&self) -> (A1, R) { loop {} }
1716 }
1717
1718 enum Opt<T> { None, Some(T) }
1719 impl<T> Opt<T> {
1720     fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Opt<U> { loop {} }
1721 }
1722
1723 fn test() {
1724     let bar: Bar<fn(u8) -> u32>;
1725     bar.foo();
1726
1727     let opt: Opt<u8>;
1728     let f: fn(u8) -> u32;
1729     opt.map(f);
1730 }"#,
1731         expect![[r#"
1732             74..78 'self': Self
1733             80..84 'args': Args
1734             139..143 'self': &Self
1735             243..247 'self': &Bar<F>
1736             260..271 '{ loop {} }': (A1, R)
1737             262..269 'loop {}': !
1738             267..269 '{}': ()
1739             355..359 'self': Opt<T>
1740             361..362 'f': F
1741             377..388 '{ loop {} }': Opt<U>
1742             379..386 'loop {}': !
1743             384..386 '{}': ()
1744             402..518 '{     ...(f); }': ()
1745             412..415 'bar': Bar<fn(u8) -> u32>
1746             441..444 'bar': Bar<fn(u8) -> u32>
1747             441..450 'bar.foo()': (u8, u32)
1748             461..464 'opt': Opt<u8>
1749             483..484 'f': fn(u8) -> u32
1750             505..508 'opt': Opt<u8>
1751             505..515 'opt.map(f)': Opt<u32>
1752             513..514 'f': fn(u8) -> u32
1753         "#]],
1754     );
1755 }
1756
1757 #[test]
1758 fn fn_trait_deref_with_ty_default() {
1759     check_infer(
1760         r#"
1761 //- minicore: deref, fn
1762 struct Foo;
1763
1764 impl Foo {
1765     fn foo(&self) -> usize {}
1766 }
1767
1768 struct Lazy<T, F = fn() -> T>(F);
1769
1770 impl<T, F> Lazy<T, F> {
1771     pub fn new(f: F) -> Lazy<T, F> {}
1772 }
1773
1774 impl<T, F: FnOnce() -> T> core::ops::Deref for Lazy<T, F> {
1775     type Target = T;
1776 }
1777
1778 fn test() {
1779     let lazy1: Lazy<Foo, _> = Lazy::new(|| Foo);
1780     let r1 = lazy1.foo();
1781
1782     fn make_foo_fn() -> Foo {}
1783     let make_foo_fn_ptr: fn() -> Foo = make_foo_fn;
1784     let lazy2: Lazy<Foo, _> = Lazy::new(make_foo_fn_ptr);
1785     let r2 = lazy2.foo();
1786 }"#,
1787         expect![[r#"
1788             36..40 'self': &Foo
1789             51..53 '{}': ()
1790             131..132 'f': F
1791             151..153 '{}': ()
1792             251..497 '{     ...o(); }': ()
1793             261..266 'lazy1': Lazy<Foo, || -> Foo>
1794             283..292 'Lazy::new': fn new<Foo, || -> Foo>(|| -> Foo) -> Lazy<Foo, || -> Foo>
1795             283..300 'Lazy::...| Foo)': Lazy<Foo, || -> Foo>
1796             293..299 '|| Foo': || -> Foo
1797             296..299 'Foo': Foo
1798             310..312 'r1': usize
1799             315..320 'lazy1': Lazy<Foo, || -> Foo>
1800             315..326 'lazy1.foo()': usize
1801             368..383 'make_foo_fn_ptr': fn() -> Foo
1802             399..410 'make_foo_fn': fn make_foo_fn() -> Foo
1803             420..425 'lazy2': Lazy<Foo, fn() -> Foo>
1804             442..451 'Lazy::new': fn new<Foo, fn() -> Foo>(fn() -> Foo) -> Lazy<Foo, fn() -> Foo>
1805             442..468 'Lazy::...n_ptr)': Lazy<Foo, fn() -> Foo>
1806             452..467 'make_foo_fn_ptr': fn() -> Foo
1807             478..480 'r2': usize
1808             483..488 'lazy2': Lazy<Foo, fn() -> Foo>
1809             483..494 'lazy2.foo()': usize
1810             357..359 '{}': ()
1811         "#]],
1812     );
1813 }
1814
1815 #[test]
1816 fn closure_1() {
1817     check_infer_with_mismatches(
1818         r#"
1819 //- minicore: fn
1820 enum Option<T> { Some(T), None }
1821 impl<T> Option<T> {
1822     fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> { loop {} }
1823 }
1824
1825 fn test() {
1826     let x = Option::Some(1u32);
1827     x.map(|v| v + 1);
1828     x.map(|_v| 1u64);
1829     let y: Option<i64> = x.map(|_v| 1);
1830 }"#,
1831         expect![[r#"
1832             86..90 'self': Option<T>
1833             92..93 'f': F
1834             111..122 '{ loop {} }': Option<U>
1835             113..120 'loop {}': !
1836             118..120 '{}': ()
1837             136..255 '{     ... 1); }': ()
1838             146..147 'x': Option<u32>
1839             150..162 'Option::Some': Some<u32>(u32) -> Option<u32>
1840             150..168 'Option...(1u32)': Option<u32>
1841             163..167 '1u32': u32
1842             174..175 'x': Option<u32>
1843             174..190 'x.map(...v + 1)': Option<u32>
1844             180..189 '|v| v + 1': |u32| -> u32
1845             181..182 'v': u32
1846             184..185 'v': u32
1847             184..189 'v + 1': u32
1848             188..189 '1': u32
1849             196..197 'x': Option<u32>
1850             196..212 'x.map(... 1u64)': Option<u64>
1851             202..211 '|_v| 1u64': |u32| -> u64
1852             203..205 '_v': u32
1853             207..211 '1u64': u64
1854             222..223 'y': Option<i64>
1855             239..240 'x': Option<u32>
1856             239..252 'x.map(|_v| 1)': Option<i64>
1857             245..251 '|_v| 1': |u32| -> i64
1858             246..248 '_v': u32
1859             250..251 '1': i64
1860         "#]],
1861     );
1862 }
1863
1864 #[test]
1865 fn closure_2() {
1866     check_types(
1867         r#"
1868 //- minicore: add, fn
1869
1870 impl core::ops::Add for u64 {
1871     type Output = Self;
1872     fn add(self, rhs: u64) -> Self::Output {0}
1873 }
1874
1875 impl core::ops::Add for u128 {
1876     type Output = Self;
1877     fn add(self, rhs: u128) -> Self::Output {0}
1878 }
1879
1880 fn test<F: FnOnce(u32) -> u64>(f: F) {
1881     f(1);
1882   //  ^ u32
1883   //^^^^ u64
1884     let g = |v| v + 1;
1885               //^^^^^ u64
1886           //^^^^^^^^^ |u64| -> u64
1887     g(1u64);
1888   //^^^^^^^ u64
1889     let h = |v| 1u128 + v;
1890           //^^^^^^^^^^^^^ |u128| -> u128
1891 }"#,
1892     );
1893 }
1894
1895 #[test]
1896 fn closure_as_argument_inference_order() {
1897     check_infer_with_mismatches(
1898         r#"
1899 //- minicore: fn
1900 fn foo1<T, U, F: FnOnce(T) -> U>(x: T, f: F) -> U { loop {} }
1901 fn foo2<T, U, F: FnOnce(T) -> U>(f: F, x: T) -> U { loop {} }
1902
1903 struct S;
1904 impl S {
1905     fn method(self) -> u64;
1906
1907     fn foo1<T, U, F: FnOnce(T) -> U>(self, x: T, f: F) -> U { loop {} }
1908     fn foo2<T, U, F: FnOnce(T) -> U>(self, f: F, x: T) -> U { loop {} }
1909 }
1910
1911 fn test() {
1912     let x1 = foo1(S, |s| s.method());
1913     let x2 = foo2(|s| s.method(), S);
1914     let x3 = S.foo1(S, |s| s.method());
1915     let x4 = S.foo2(|s| s.method(), S);
1916 }"#,
1917         expect![[r#"
1918             33..34 'x': T
1919             39..40 'f': F
1920             50..61 '{ loop {} }': U
1921             52..59 'loop {}': !
1922             57..59 '{}': ()
1923             95..96 'f': F
1924             101..102 'x': T
1925             112..123 '{ loop {} }': U
1926             114..121 'loop {}': !
1927             119..121 '{}': ()
1928             158..162 'self': S
1929             210..214 'self': S
1930             216..217 'x': T
1931             222..223 'f': F
1932             233..244 '{ loop {} }': U
1933             235..242 'loop {}': !
1934             240..242 '{}': ()
1935             282..286 'self': S
1936             288..289 'f': F
1937             294..295 'x': T
1938             305..316 '{ loop {} }': U
1939             307..314 'loop {}': !
1940             312..314 '{}': ()
1941             330..489 '{     ... S); }': ()
1942             340..342 'x1': u64
1943             345..349 'foo1': fn foo1<S, u64, |S| -> u64>(S, |S| -> u64) -> u64
1944             345..368 'foo1(S...hod())': u64
1945             350..351 'S': S
1946             353..367 '|s| s.method()': |S| -> u64
1947             354..355 's': S
1948             357..358 's': S
1949             357..367 's.method()': u64
1950             378..380 'x2': u64
1951             383..387 'foo2': fn foo2<S, u64, |S| -> u64>(|S| -> u64, S) -> u64
1952             383..406 'foo2(|...(), S)': u64
1953             388..402 '|s| s.method()': |S| -> u64
1954             389..390 's': S
1955             392..393 's': S
1956             392..402 's.method()': u64
1957             404..405 'S': S
1958             416..418 'x3': u64
1959             421..422 'S': S
1960             421..446 'S.foo1...hod())': u64
1961             428..429 'S': S
1962             431..445 '|s| s.method()': |S| -> u64
1963             432..433 's': S
1964             435..436 's': S
1965             435..445 's.method()': u64
1966             456..458 'x4': u64
1967             461..462 'S': S
1968             461..486 'S.foo2...(), S)': u64
1969             468..482 '|s| s.method()': |S| -> u64
1970             469..470 's': S
1971             472..473 's': S
1972             472..482 's.method()': u64
1973             484..485 'S': S
1974         "#]],
1975     );
1976 }
1977
1978 #[test]
1979 fn fn_item_fn_trait() {
1980     check_types(
1981         r#"
1982 //- minicore: fn
1983 struct S;
1984
1985 fn foo() -> S { S }
1986
1987 fn takes_closure<U, F: FnOnce() -> U>(f: F) -> U { f() }
1988
1989 fn test() {
1990     takes_closure(foo);
1991 } //^^^^^^^^^^^^^^^^^^ S
1992 "#,
1993     );
1994 }
1995
1996 #[test]
1997 fn unselected_projection_in_trait_env_1() {
1998     check_types(
1999         r#"
2000 //- /main.rs
2001 trait Trait {
2002     type Item;
2003 }
2004
2005 trait Trait2 {
2006     fn foo(&self) -> u32;
2007 }
2008
2009 fn test<T: Trait>() where T::Item: Trait2 {
2010     let x: T::Item = no_matter;
2011     x.foo();
2012 } //^^^^^^^ u32
2013 "#,
2014     );
2015 }
2016
2017 #[test]
2018 fn unselected_projection_in_trait_env_2() {
2019     check_types(
2020         r#"
2021 trait Trait<T> {
2022     type Item;
2023 }
2024
2025 trait Trait2 {
2026     fn foo(&self) -> u32;
2027 }
2028
2029 fn test<T, U>() where T::Item: Trait2, T: Trait<U::Item>, U: Trait<()> {
2030     let x: T::Item = no_matter;
2031     x.foo();
2032 } //^^^^^^^ u32
2033 "#,
2034     );
2035 }
2036
2037 #[test]
2038 fn unselected_projection_on_impl_self() {
2039     check_infer(
2040         r#"
2041 //- /main.rs
2042 trait Trait {
2043     type Item;
2044
2045     fn f(&self, x: Self::Item);
2046 }
2047
2048 struct S;
2049
2050 impl Trait for S {
2051     type Item = u32;
2052     fn f(&self, x: Self::Item) { let y = x; }
2053 }
2054
2055 struct S2;
2056
2057 impl Trait for S2 {
2058     type Item = i32;
2059     fn f(&self, x: <Self>::Item) { let y = x; }
2060 }"#,
2061         expect![[r#"
2062             40..44 'self': &Self
2063             46..47 'x': Trait::Item<Self>
2064             126..130 'self': &S
2065             132..133 'x': u32
2066             147..161 '{ let y = x; }': ()
2067             153..154 'y': u32
2068             157..158 'x': u32
2069             228..232 'self': &S2
2070             234..235 'x': i32
2071             251..265 '{ let y = x; }': ()
2072             257..258 'y': i32
2073             261..262 'x': i32
2074         "#]],
2075     );
2076 }
2077
2078 #[test]
2079 fn unselected_projection_on_trait_self() {
2080     check_types(
2081         r#"
2082 trait Trait {
2083     type Item;
2084
2085     fn f(&self) -> Self::Item { loop {} }
2086 }
2087
2088 struct S;
2089 impl Trait for S {
2090     type Item = u32;
2091 }
2092
2093 fn test() {
2094     S.f();
2095 } //^^^^^ u32
2096 "#,
2097     );
2098 }
2099
2100 #[test]
2101 fn unselected_projection_chalk_fold() {
2102     check_types(
2103         r#"
2104 trait Interner {}
2105 trait Fold<I: Interner, TI = I> {
2106     type Result;
2107 }
2108
2109 struct Ty<I: Interner> {}
2110 impl<I: Interner, TI: Interner> Fold<I, TI> for Ty<I> {
2111     type Result = Ty<TI>;
2112 }
2113
2114 fn fold<I: Interner, T>(interner: &I, t: T) -> T::Result
2115 where
2116     T: Fold<I, I>,
2117 {
2118     loop {}
2119 }
2120
2121 fn foo<I: Interner>(interner: &I, t: Ty<I>) {
2122     fold(interner, t);
2123 } //^^^^^^^^^^^^^^^^^ Ty<I>
2124 "#,
2125     );
2126 }
2127
2128 #[test]
2129 fn trait_impl_self_ty() {
2130     check_types(
2131         r#"
2132 trait Trait<T> {
2133    fn foo(&self);
2134 }
2135
2136 struct S;
2137
2138 impl Trait<Self> for S {}
2139
2140 fn test() {
2141     S.foo();
2142 } //^^^^^^^ ()
2143 "#,
2144     );
2145 }
2146
2147 #[test]
2148 fn trait_impl_self_ty_cycle() {
2149     check_types(
2150         r#"
2151 trait Trait {
2152    fn foo(&self);
2153 }
2154
2155 struct S<T>;
2156
2157 impl Trait for S<Self> {}
2158
2159 fn test() {
2160     S.foo();
2161 } //^^^^^^^ {unknown}
2162 "#,
2163     );
2164 }
2165
2166 #[test]
2167 fn unselected_projection_in_trait_env_cycle_1() {
2168     // This is not a cycle, because the `T: Trait2<T::Item>` bound depends only on the `T: Trait`
2169     // bound, not on itself (since only `Trait` can define `Item`).
2170     check_types(
2171         r#"
2172 trait Trait {
2173     type Item;
2174 }
2175
2176 trait Trait2<T> {}
2177
2178 fn test<T: Trait>() where T: Trait2<T::Item> {
2179     let x: T::Item = no_matter;
2180 }                  //^^^^^^^^^ Trait::Item<T>
2181 "#,
2182     );
2183 }
2184
2185 #[test]
2186 fn unselected_projection_in_trait_env_cycle_2() {
2187     // this is a legitimate cycle
2188     check_types(
2189         r#"
2190 //- /main.rs
2191 trait Trait<T> {
2192     type Item;
2193 }
2194
2195 fn test<T, U>() where T: Trait<U::Item>, U: Trait<T::Item> {
2196     let x: T::Item = no_matter;
2197 }                  //^^^^^^^^^ {unknown}
2198 "#,
2199     );
2200 }
2201
2202 #[test]
2203 fn unselected_projection_in_trait_env_cycle_3() {
2204     // this is a cycle for rustc; we currently accept it
2205     check_types(
2206         r#"
2207 //- /main.rs
2208 trait Trait {
2209     type Item;
2210     type OtherItem;
2211 }
2212
2213 fn test<T>() where T: Trait<OtherItem = T::Item> {
2214     let x: T::Item = no_matter;
2215 }                  //^^^^^^^^^ Trait::Item<T>
2216 "#,
2217     );
2218 }
2219
2220 #[test]
2221 fn unselected_projection_in_trait_env_no_cycle() {
2222     // this is not a cycle
2223     check_types(
2224         r#"
2225 //- /main.rs
2226 trait Index {
2227     type Output;
2228 }
2229
2230 type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
2231
2232 pub trait UnificationStoreBase: Index<Output = Key<Self>> {
2233     type Key;
2234
2235     fn len(&self) -> usize;
2236 }
2237
2238 pub trait UnificationStoreMut: UnificationStoreBase {
2239     fn push(&mut self, value: Self::Key);
2240 }
2241
2242 fn test<T>(t: T) where T: UnificationStoreMut {
2243     let x;
2244     t.push(x);
2245     let y: Key<T>;
2246     (x, y);
2247 } //^^^^^^ (UnificationStoreBase::Key<T>, UnificationStoreBase::Key<T>)
2248 "#,
2249     );
2250 }
2251
2252 #[test]
2253 fn inline_assoc_type_bounds_1() {
2254     check_types(
2255         r#"
2256 trait Iterator {
2257     type Item;
2258 }
2259 trait OtherTrait<T> {
2260     fn foo(&self) -> T;
2261 }
2262
2263 // workaround for Chalk assoc type normalization problems
2264 pub struct S<T>;
2265 impl<T: Iterator> Iterator for S<T> {
2266     type Item = <T as Iterator>::Item;
2267 }
2268
2269 fn test<I: Iterator<Item: OtherTrait<u32>>>() {
2270     let x: <S<I> as Iterator>::Item;
2271     x.foo();
2272 } //^^^^^^^ u32
2273 "#,
2274     );
2275 }
2276
2277 #[test]
2278 fn inline_assoc_type_bounds_2() {
2279     check_types(
2280         r#"
2281 trait Iterator {
2282     type Item;
2283 }
2284
2285 fn test<I: Iterator<Item: Iterator<Item = u32>>>() {
2286     let x: <<I as Iterator>::Item as Iterator>::Item;
2287     x;
2288 } //^ u32
2289 "#,
2290     );
2291 }
2292
2293 #[test]
2294 fn proc_macro_server_types() {
2295     check_infer(
2296         r#"
2297 macro_rules! with_api {
2298     ($S:ident, $self:ident, $m:ident) => {
2299         $m! {
2300             TokenStream {
2301                 fn new() -> $S::TokenStream;
2302             },
2303             Group {
2304             },
2305         }
2306     };
2307 }
2308 macro_rules! associated_item {
2309     (type TokenStream) =>
2310         (type TokenStream: 'static;);
2311     (type Group) =>
2312         (type Group: 'static;);
2313     ($($item:tt)*) => ($($item)*;)
2314 }
2315 macro_rules! declare_server_traits {
2316     ($($name:ident {
2317         $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)*
2318     }),* $(,)?) => {
2319         pub trait Types {
2320             $(associated_item!(type $name);)*
2321         }
2322
2323         $(pub trait $name: Types {
2324             $(associated_item!(fn $method($($arg: $arg_ty),*) $(-> $ret_ty)?);)*
2325         })*
2326
2327         pub trait Server: Types $(+ $name)* {}
2328         impl<S: Types $(+ $name)*> Server for S {}
2329     }
2330 }
2331
2332 with_api!(Self, self_, declare_server_traits);
2333 struct G {}
2334 struct T {}
2335 struct Rustc;
2336 impl Types for Rustc {
2337     type TokenStream = T;
2338     type Group = G;
2339 }
2340
2341 fn make<T>() -> T { loop {} }
2342 impl TokenStream for Rustc {
2343     fn new() -> Self::TokenStream {
2344         let group: Self::Group = make();
2345         make()
2346     }
2347 }"#,
2348         expect![[r#"
2349             1061..1072 '{ loop {} }': T
2350             1063..1070 'loop {}': !
2351             1068..1070 '{}': ()
2352             1136..1199 '{     ...     }': T
2353             1150..1155 'group': G
2354             1171..1175 'make': fn make<G>() -> G
2355             1171..1177 'make()': G
2356             1187..1191 'make': fn make<T>() -> T
2357             1187..1193 'make()': T
2358         "#]],
2359     );
2360 }
2361
2362 #[test]
2363 fn unify_impl_trait() {
2364     check_infer_with_mismatches(
2365         r#"
2366 //- minicore: sized
2367 trait Trait<T> {}
2368
2369 fn foo(x: impl Trait<u32>) { loop {} }
2370 fn bar<T>(x: impl Trait<T>) -> T { loop {} }
2371
2372 struct S<T>(T);
2373 impl<T> Trait<T> for S<T> {}
2374
2375 fn default<T>() -> T { loop {} }
2376
2377 fn test() -> impl Trait<i32> {
2378     let s1 = S(default());
2379     foo(s1);
2380     let x: i32 = bar(S(default()));
2381     S(default())
2382 }"#,
2383         expect![[r#"
2384             26..27 'x': impl Trait<u32>
2385             46..57 '{ loop {} }': ()
2386             48..55 'loop {}': !
2387             53..55 '{}': ()
2388             68..69 'x': impl Trait<T>
2389             91..102 '{ loop {} }': T
2390             93..100 'loop {}': !
2391             98..100 '{}': ()
2392             171..182 '{ loop {} }': T
2393             173..180 'loop {}': !
2394             178..180 '{}': ()
2395             213..309 '{     ...t()) }': S<{unknown}>
2396             223..225 's1': S<u32>
2397             228..229 'S': S<u32>(u32) -> S<u32>
2398             228..240 'S(default())': S<u32>
2399             230..237 'default': fn default<u32>() -> u32
2400             230..239 'default()': u32
2401             246..249 'foo': fn foo(S<u32>)
2402             246..253 'foo(s1)': ()
2403             250..252 's1': S<u32>
2404             263..264 'x': i32
2405             272..275 'bar': fn bar<i32>(S<i32>) -> i32
2406             272..289 'bar(S(...lt()))': i32
2407             276..277 'S': S<i32>(i32) -> S<i32>
2408             276..288 'S(default())': S<i32>
2409             278..285 'default': fn default<i32>() -> i32
2410             278..287 'default()': i32
2411             295..296 'S': S<{unknown}>({unknown}) -> S<{unknown}>
2412             295..307 'S(default())': S<{unknown}>
2413             297..304 'default': fn default<{unknown}>() -> {unknown}
2414             297..306 'default()': {unknown}
2415         "#]],
2416     );
2417 }
2418
2419 #[test]
2420 fn assoc_types_from_bounds() {
2421     check_infer(
2422         r#"
2423 //- minicore: fn
2424 trait T {
2425     type O;
2426 }
2427
2428 impl T for () {
2429     type O = ();
2430 }
2431
2432 fn f<X, F>(_v: F)
2433 where
2434     X: T,
2435     F: FnOnce(&X::O),
2436 { }
2437
2438 fn main() {
2439     f::<(), _>(|z| { z; });
2440 }"#,
2441         expect![[r#"
2442             72..74 '_v': F
2443             117..120 '{ }': ()
2444             132..163 '{     ... }); }': ()
2445             138..148 'f::<(), _>': fn f<(), |&()| -> ()>(|&()| -> ())
2446             138..160 'f::<()... z; })': ()
2447             149..159 '|z| { z; }': |&()| -> ()
2448             150..151 'z': &()
2449             153..159 '{ z; }': ()
2450             155..156 'z': &()
2451         "#]],
2452     );
2453 }
2454
2455 #[test]
2456 fn associated_type_bound() {
2457     check_types(
2458         r#"
2459 pub trait Trait {
2460     type Item: OtherTrait<u32>;
2461 }
2462 pub trait OtherTrait<T> {
2463     fn foo(&self) -> T;
2464 }
2465
2466 // this is just a workaround for chalk#234
2467 pub struct S<T>;
2468 impl<T: Trait> Trait for S<T> {
2469     type Item = <T as Trait>::Item;
2470 }
2471
2472 fn test<T: Trait>() {
2473     let y: <S<T> as Trait>::Item = no_matter;
2474     y.foo();
2475 } //^^^^^^^ u32
2476 "#,
2477     );
2478 }
2479
2480 #[test]
2481 fn dyn_trait_through_chalk() {
2482     check_types(
2483         r#"
2484 //- minicore: deref
2485 struct Box<T: ?Sized> {}
2486 impl<T: ?Sized> core::ops::Deref for Box<T> {
2487     type Target = T;
2488 }
2489 trait Trait {
2490     fn foo(&self);
2491 }
2492
2493 fn test(x: Box<dyn Trait>) {
2494     x.foo();
2495 } //^^^^^^^ ()
2496 "#,
2497     );
2498 }
2499
2500 #[test]
2501 fn string_to_owned() {
2502     check_types(
2503         r#"
2504 struct String {}
2505 pub trait ToOwned {
2506     type Owned;
2507     fn to_owned(&self) -> Self::Owned;
2508 }
2509 impl ToOwned for str {
2510     type Owned = String;
2511 }
2512 fn test() {
2513     "foo".to_owned();
2514 } //^^^^^^^^^^^^^^^^ String
2515 "#,
2516     );
2517 }
2518
2519 #[test]
2520 fn iterator_chain() {
2521     check_infer_with_mismatches(
2522         r#"
2523 //- minicore: fn, option
2524 pub trait Iterator {
2525     type Item;
2526
2527     fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
2528     where
2529         F: FnMut(Self::Item) -> Option<B>,
2530     { loop {} }
2531
2532     fn for_each<F>(self, f: F)
2533     where
2534         F: FnMut(Self::Item),
2535     { loop {} }
2536 }
2537
2538 pub trait IntoIterator {
2539     type Item;
2540     type IntoIter: Iterator<Item = Self::Item>;
2541     fn into_iter(self) -> Self::IntoIter;
2542 }
2543
2544 pub struct FilterMap<I, F> { }
2545 impl<B, I: Iterator, F> Iterator for FilterMap<I, F>
2546 where
2547     F: FnMut(I::Item) -> Option<B>,
2548 {
2549     type Item = B;
2550 }
2551
2552 #[stable(feature = "rust1", since = "1.0.0")]
2553 impl<I: Iterator> IntoIterator for I {
2554     type Item = I::Item;
2555     type IntoIter = I;
2556
2557     fn into_iter(self) -> I {
2558         self
2559     }
2560 }
2561
2562 struct Vec<T> {}
2563 impl<T> Vec<T> {
2564     fn new() -> Self { loop {} }
2565 }
2566
2567 impl<T> IntoIterator for Vec<T> {
2568     type Item = T;
2569     type IntoIter = IntoIter<T>;
2570 }
2571
2572 pub struct IntoIter<T> { }
2573 impl<T> Iterator for IntoIter<T> {
2574     type Item = T;
2575 }
2576
2577 fn main() {
2578     Vec::<i32>::new().into_iter()
2579     .filter_map(|x| if x > 0 { Some(x as u32) } else { None })
2580     .for_each(|y| { y; });
2581 }"#,
2582         expect![[r#"
2583             61..65 'self': Self
2584             67..68 'f': F
2585             152..163 '{ loop {} }': FilterMap<Self, F>
2586             154..161 'loop {}': !
2587             159..161 '{}': ()
2588             184..188 'self': Self
2589             190..191 'f': F
2590             240..251 '{ loop {} }': ()
2591             242..249 'loop {}': !
2592             247..249 '{}': ()
2593             360..364 'self': Self
2594             689..693 'self': I
2595             700..720 '{     ...     }': I
2596             710..714 'self': I
2597             779..790 '{ loop {} }': Vec<T>
2598             781..788 'loop {}': !
2599             786..788 '{}': ()
2600             977..1104 '{     ... }); }': ()
2601             983..998 'Vec::<i32>::new': fn new<i32>() -> Vec<i32>
2602             983..1000 'Vec::<...:new()': Vec<i32>
2603             983..1012 'Vec::<...iter()': IntoIter<i32>
2604             983..1075 'Vec::<...one })': FilterMap<IntoIter<i32>, |i32| -> Option<u32>>
2605             983..1101 'Vec::<... y; })': ()
2606             1029..1074 '|x| if...None }': |i32| -> Option<u32>
2607             1030..1031 'x': i32
2608             1033..1074 'if x >...None }': Option<u32>
2609             1036..1037 'x': i32
2610             1036..1041 'x > 0': bool
2611             1040..1041 '0': i32
2612             1042..1060 '{ Some...u32) }': Option<u32>
2613             1044..1048 'Some': Some<u32>(u32) -> Option<u32>
2614             1044..1058 'Some(x as u32)': Option<u32>
2615             1049..1050 'x': i32
2616             1049..1057 'x as u32': u32
2617             1066..1074 '{ None }': Option<u32>
2618             1068..1072 'None': Option<u32>
2619             1090..1100 '|y| { y; }': |u32| -> ()
2620             1091..1092 'y': u32
2621             1094..1100 '{ y; }': ()
2622             1096..1097 'y': u32
2623         "#]],
2624     );
2625 }
2626
2627 #[test]
2628 fn nested_assoc() {
2629     check_types(
2630         r#"
2631 struct Bar;
2632 struct Foo;
2633
2634 trait A {
2635     type OutputA;
2636 }
2637
2638 impl A for Bar {
2639     type OutputA = Foo;
2640 }
2641
2642 trait B {
2643     type Output;
2644     fn foo() -> Self::Output;
2645 }
2646
2647 impl<T:A> B for T {
2648     type Output = T::OutputA;
2649     fn foo() -> Self::Output { loop {} }
2650 }
2651
2652 fn main() {
2653     Bar::foo();
2654 } //^^^^^^^^^^ Foo
2655 "#,
2656     );
2657 }
2658
2659 #[test]
2660 fn trait_object_no_coercion() {
2661     check_infer_with_mismatches(
2662         r#"
2663 trait Foo {}
2664
2665 fn foo(x: &dyn Foo) {}
2666
2667 fn test(x: &dyn Foo) {
2668     foo(x);
2669 }"#,
2670         expect![[r#"
2671             21..22 'x': &dyn Foo
2672             34..36 '{}': ()
2673             46..47 'x': &dyn Foo
2674             59..74 '{     foo(x); }': ()
2675             65..68 'foo': fn foo(&dyn Foo)
2676             65..71 'foo(x)': ()
2677             69..70 'x': &dyn Foo
2678         "#]],
2679     );
2680 }
2681
2682 #[test]
2683 fn builtin_copy() {
2684     check_infer_with_mismatches(
2685         r#"
2686 //- minicore: copy
2687 struct IsCopy;
2688 impl Copy for IsCopy {}
2689 struct NotCopy;
2690
2691 trait Test { fn test(&self) -> bool; }
2692 impl<T: Copy> Test for T {}
2693
2694 fn test() {
2695     IsCopy.test();
2696     NotCopy.test();
2697     (IsCopy, IsCopy).test();
2698     (IsCopy, NotCopy).test();
2699 }"#,
2700         expect![[r#"
2701             78..82 'self': &Self
2702             134..235 '{     ...t(); }': ()
2703             140..146 'IsCopy': IsCopy
2704             140..153 'IsCopy.test()': bool
2705             159..166 'NotCopy': NotCopy
2706             159..173 'NotCopy.test()': {unknown}
2707             179..195 '(IsCop...sCopy)': (IsCopy, IsCopy)
2708             179..202 '(IsCop...test()': bool
2709             180..186 'IsCopy': IsCopy
2710             188..194 'IsCopy': IsCopy
2711             208..225 '(IsCop...tCopy)': (IsCopy, NotCopy)
2712             208..232 '(IsCop...test()': {unknown}
2713             209..215 'IsCopy': IsCopy
2714             217..224 'NotCopy': NotCopy
2715         "#]],
2716     );
2717 }
2718
2719 #[test]
2720 fn builtin_fn_def_copy() {
2721     check_infer_with_mismatches(
2722         r#"
2723 //- minicore: copy
2724 fn foo() {}
2725 fn bar<T: Copy>(T) -> T {}
2726 struct Struct(usize);
2727 enum Enum { Variant(usize) }
2728
2729 trait Test { fn test(&self) -> bool; }
2730 impl<T: Copy> Test for T {}
2731
2732 fn test() {
2733     foo.test();
2734     bar.test();
2735     Struct.test();
2736     Enum::Variant.test();
2737 }"#,
2738         expect![[r#"
2739             9..11 '{}': ()
2740             28..29 'T': {unknown}
2741             36..38 '{}': ()
2742             36..38: expected T, got ()
2743             113..117 'self': &Self
2744             169..249 '{     ...t(); }': ()
2745             175..178 'foo': fn foo()
2746             175..185 'foo.test()': bool
2747             191..194 'bar': fn bar<{unknown}>({unknown}) -> {unknown}
2748             191..201 'bar.test()': bool
2749             207..213 'Struct': Struct(usize) -> Struct
2750             207..220 'Struct.test()': bool
2751             226..239 'Enum::Variant': Variant(usize) -> Enum
2752             226..246 'Enum::...test()': bool
2753         "#]],
2754     );
2755 }
2756
2757 #[test]
2758 fn builtin_fn_ptr_copy() {
2759     check_infer_with_mismatches(
2760         r#"
2761 //- minicore: copy
2762 trait Test { fn test(&self) -> bool; }
2763 impl<T: Copy> Test for T {}
2764
2765 fn test(f1: fn(), f2: fn(usize) -> u8, f3: fn(u8, u8) -> &u8) {
2766     f1.test();
2767     f2.test();
2768     f3.test();
2769 }"#,
2770         expect![[r#"
2771             22..26 'self': &Self
2772             76..78 'f1': fn()
2773             86..88 'f2': fn(usize) -> u8
2774             107..109 'f3': fn(u8, u8) -> &u8
2775             130..178 '{     ...t(); }': ()
2776             136..138 'f1': fn()
2777             136..145 'f1.test()': bool
2778             151..153 'f2': fn(usize) -> u8
2779             151..160 'f2.test()': bool
2780             166..168 'f3': fn(u8, u8) -> &u8
2781             166..175 'f3.test()': bool
2782         "#]],
2783     );
2784 }
2785
2786 #[test]
2787 fn builtin_sized() {
2788     check_infer_with_mismatches(
2789         r#"
2790 //- minicore: sized
2791 trait Test { fn test(&self) -> bool; }
2792 impl<T: Sized> Test for T {}
2793
2794 fn test() {
2795     1u8.test();
2796     (*"foo").test(); // not Sized
2797     (1u8, 1u8).test();
2798     (1u8, *"foo").test(); // not Sized
2799 }"#,
2800         expect![[r#"
2801             22..26 'self': &Self
2802             79..194 '{     ...ized }': ()
2803             85..88 '1u8': u8
2804             85..95 '1u8.test()': bool
2805             101..116 '(*"foo").test()': {unknown}
2806             102..108 '*"foo"': str
2807             103..108 '"foo"': &str
2808             135..145 '(1u8, 1u8)': (u8, u8)
2809             135..152 '(1u8, ...test()': bool
2810             136..139 '1u8': u8
2811             141..144 '1u8': u8
2812             158..171 '(1u8, *"foo")': (u8, str)
2813             158..178 '(1u8, ...test()': {unknown}
2814             159..162 '1u8': u8
2815             164..170 '*"foo"': str
2816             165..170 '"foo"': &str
2817         "#]],
2818     );
2819 }
2820
2821 #[test]
2822 fn integer_range_iterate() {
2823     check_types(
2824         r#"
2825 //- /main.rs crate:main deps:core
2826 fn test() {
2827     for x in 0..100 { x; }
2828 }                   //^ i32
2829
2830 //- /core.rs crate:core
2831 pub mod ops {
2832     pub struct Range<Idx> {
2833         pub start: Idx,
2834         pub end: Idx,
2835     }
2836 }
2837
2838 pub mod iter {
2839     pub trait Iterator {
2840         type Item;
2841     }
2842
2843     pub trait IntoIterator {
2844         type Item;
2845         type IntoIter: Iterator<Item = Self::Item>;
2846     }
2847
2848     impl<T> IntoIterator for T where T: Iterator {
2849         type Item = <T as Iterator>::Item;
2850         type IntoIter = Self;
2851     }
2852 }
2853
2854 trait Step {}
2855 impl Step for i32 {}
2856 impl Step for i64 {}
2857
2858 impl<A: Step> iter::Iterator for ops::Range<A> {
2859     type Item = A;
2860 }
2861 "#,
2862     );
2863 }
2864
2865 #[test]
2866 fn infer_closure_arg() {
2867     check_infer(
2868         r#"
2869 //- /lib.rs
2870
2871 enum Option<T> {
2872     None,
2873     Some(T)
2874 }
2875
2876 fn foo() {
2877     let s = Option::None;
2878     let f = |x: Option<i32>| {};
2879     (&f)(s)
2880 }"#,
2881         expect![[r#"
2882             52..126 '{     ...)(s) }': ()
2883             62..63 's': Option<i32>
2884             66..78 'Option::None': Option<i32>
2885             88..89 'f': |Option<i32>| -> ()
2886             92..111 '|x: Op...2>| {}': |Option<i32>| -> ()
2887             93..94 'x': Option<i32>
2888             109..111 '{}': ()
2889             117..124 '(&f)(s)': ()
2890             118..120 '&f': &|Option<i32>| -> ()
2891             119..120 'f': |Option<i32>| -> ()
2892             122..123 's': Option<i32>
2893         "#]],
2894     );
2895 }
2896
2897 #[test]
2898 fn dyn_fn_param_informs_call_site_closure_signature() {
2899     cov_mark::check!(dyn_fn_param_informs_call_site_closure_signature);
2900     check_types(
2901         r#"
2902 //- minicore: fn, coerce_unsized
2903 struct S;
2904 impl S {
2905     fn inherent(&self) -> u8 { 0 }
2906 }
2907 fn take_dyn_fn(f: &dyn Fn(S)) {}
2908
2909 fn f() {
2910     take_dyn_fn(&|x| { x.inherent(); });
2911                      //^^^^^^^^^^^^ u8
2912 }
2913         "#,
2914     );
2915 }
2916
2917 #[test]
2918 fn infer_fn_trait_arg() {
2919     check_infer_with_mismatches(
2920         r#"
2921 //- minicore: fn, option
2922 fn foo<F, T>(f: F) -> T
2923 where
2924     F: Fn(Option<i32>) -> T,
2925 {
2926     let s = None;
2927     f(s)
2928 }
2929 "#,
2930         expect![[r#"
2931             13..14 'f': F
2932             59..89 '{     ...f(s) }': T
2933             69..70 's': Option<i32>
2934             73..77 'None': Option<i32>
2935             83..84 'f': F
2936             83..87 'f(s)': T
2937             85..86 's': Option<i32>
2938         "#]],
2939     );
2940 }
2941
2942 #[test]
2943 fn infer_box_fn_arg() {
2944     // The type mismatch is because we don't define Unsize and CoerceUnsized
2945     check_infer_with_mismatches(
2946         r#"
2947 //- minicore: fn, deref, option
2948 #[lang = "owned_box"]
2949 pub struct Box<T: ?Sized> {
2950     inner: *mut T,
2951 }
2952
2953 impl<T: ?Sized> core::ops::Deref for Box<T> {
2954     type Target = T;
2955
2956     fn deref(&self) -> &T {
2957         &self.inner
2958     }
2959 }
2960
2961 fn foo() {
2962     let s = None;
2963     let f: Box<dyn FnOnce(&Option<i32>)> = box (|ps| {});
2964     f(&s);
2965 }"#,
2966         expect![[r#"
2967             154..158 'self': &Box<T>
2968             166..193 '{     ...     }': &T
2969             176..187 '&self.inner': &*mut T
2970             177..181 'self': &Box<T>
2971             177..187 'self.inner': *mut T
2972             206..296 '{     ...&s); }': ()
2973             216..217 's': Option<i32>
2974             220..224 'None': Option<i32>
2975             234..235 'f': Box<dyn FnOnce(&Option<i32>)>
2976             269..282 'box (|ps| {})': Box<|{unknown}| -> ()>
2977             274..281 '|ps| {}': |{unknown}| -> ()
2978             275..277 'ps': {unknown}
2979             279..281 '{}': ()
2980             288..289 'f': Box<dyn FnOnce(&Option<i32>)>
2981             288..293 'f(&s)': ()
2982             290..292 '&s': &Option<i32>
2983             291..292 's': Option<i32>
2984             269..282: expected Box<dyn FnOnce(&Option<i32>)>, got Box<|{unknown}| -> ()>
2985         "#]],
2986     );
2987 }
2988
2989 #[test]
2990 fn infer_dyn_fn_output() {
2991     check_types(
2992         r#"
2993 //- minicore: fn
2994 fn foo() {
2995     let f: &dyn Fn() -> i32;
2996     f();
2997   //^^^ i32
2998 }"#,
2999     );
3000 }
3001
3002 #[test]
3003 fn infer_dyn_fn_once_output() {
3004     check_types(
3005         r#"
3006 //- minicore: fn
3007 fn foo() {
3008     let f: dyn FnOnce() -> i32;
3009     f();
3010   //^^^ i32
3011 }"#,
3012     );
3013 }
3014
3015 #[test]
3016 fn variable_kinds_1() {
3017     check_types(
3018         r#"
3019 trait Trait<T> { fn get(self, t: T) -> T; }
3020 struct S;
3021 impl Trait<u128> for S {}
3022 impl Trait<f32> for S {}
3023 fn test() {
3024     S.get(1);
3025   //^^^^^^^^ u128
3026     S.get(1.);
3027   //^^^^^^^^^ f32
3028 }
3029         "#,
3030     );
3031 }
3032
3033 #[test]
3034 fn variable_kinds_2() {
3035     check_types(
3036         r#"
3037 trait Trait { fn get(self) -> Self; }
3038 impl Trait for u128 {}
3039 impl Trait for f32 {}
3040 fn test() {
3041     1.get();
3042   //^^^^^^^ u128
3043     (1.).get();
3044   //^^^^^^^^^^ f32
3045 }
3046         "#,
3047     );
3048 }
3049
3050 #[test]
3051 fn underscore_import() {
3052     check_types(
3053         r#"
3054 mod tr {
3055     pub trait Tr {
3056         fn method(&self) -> u8 { 0 }
3057     }
3058 }
3059
3060 struct Tr;
3061 impl crate::tr::Tr for Tr {}
3062
3063 use crate::tr::Tr as _;
3064 fn test() {
3065     Tr.method();
3066   //^^^^^^^^^^^ u8
3067 }
3068     "#,
3069     );
3070 }
3071
3072 #[test]
3073 fn inner_use() {
3074     check_types(
3075         r#"
3076 mod m {
3077     pub trait Tr {
3078         fn method(&self) -> u8 { 0 }
3079     }
3080
3081     impl Tr for () {}
3082 }
3083
3084 fn f() {
3085     use m::Tr;
3086
3087     ().method();
3088   //^^^^^^^^^^^ u8
3089 }
3090         "#,
3091     );
3092 }
3093
3094 #[test]
3095 fn trait_in_scope_with_inner_item() {
3096     check_infer(
3097         r#"
3098 mod m {
3099     pub trait Tr {
3100         fn method(&self) -> u8 { 0 }
3101     }
3102
3103     impl Tr for () {}
3104 }
3105
3106 use m::Tr;
3107
3108 fn f() {
3109     fn inner() {
3110         ().method();
3111       //^^^^^^^^^^^ u8
3112     }
3113 }"#,
3114         expect![[r#"
3115             46..50 'self': &Self
3116             58..63 '{ 0 }': u8
3117             60..61 '0': u8
3118             115..185 '{     ...   } }': ()
3119             132..183 '{     ...     }': ()
3120             142..144 '()': ()
3121             142..153 '().method()': u8
3122         "#]],
3123     );
3124 }
3125
3126 #[test]
3127 fn inner_use_in_block() {
3128     check_types(
3129         r#"
3130 mod m {
3131     pub trait Tr {
3132         fn method(&self) -> u8 { 0 }
3133     }
3134
3135     impl Tr for () {}
3136 }
3137
3138 fn f() {
3139     {
3140         use m::Tr;
3141
3142         ().method();
3143       //^^^^^^^^^^^ u8
3144     }
3145
3146     {
3147         ().method();
3148       //^^^^^^^^^^^ {unknown}
3149     }
3150
3151     ().method();
3152   //^^^^^^^^^^^ {unknown}
3153 }
3154         "#,
3155     );
3156 }
3157
3158 #[test]
3159 fn nested_inner_function_calling_self() {
3160     check_infer(
3161         r#"
3162 struct S;
3163 fn f() {
3164     fn inner() -> S {
3165         let s = inner();
3166     }
3167 }"#,
3168         expect![[r#"
3169             17..73 '{     ...   } }': ()
3170             39..71 '{     ...     }': ()
3171             53..54 's': S
3172             57..62 'inner': fn inner() -> S
3173             57..64 'inner()': S
3174         "#]],
3175     )
3176 }
3177
3178 #[test]
3179 fn infer_default_trait_type_parameter() {
3180     check_infer(
3181         r#"
3182 struct A;
3183
3184 trait Op<RHS=Self> {
3185     type Output;
3186
3187     fn do_op(self, rhs: RHS) -> Self::Output;
3188 }
3189
3190 impl Op for A {
3191     type Output = bool;
3192
3193     fn do_op(self, rhs: Self) -> Self::Output {
3194         true
3195     }
3196 }
3197
3198 fn test() {
3199     let x = A;
3200     let y = A;
3201     let r = x.do_op(y);
3202 }"#,
3203         expect![[r#"
3204             63..67 'self': Self
3205             69..72 'rhs': RHS
3206             153..157 'self': A
3207             159..162 'rhs': A
3208             186..206 '{     ...     }': bool
3209             196..200 'true': bool
3210             220..277 '{     ...(y); }': ()
3211             230..231 'x': A
3212             234..235 'A': A
3213             245..246 'y': A
3214             249..250 'A': A
3215             260..261 'r': bool
3216             264..265 'x': A
3217             264..274 'x.do_op(y)': bool
3218             272..273 'y': A
3219         "#]],
3220     )
3221 }
3222
3223 #[test]
3224 fn qualified_path_as_qualified_trait() {
3225     check_infer(
3226         r#"
3227 mod foo {
3228
3229     pub trait Foo {
3230         type Target;
3231     }
3232     pub trait Bar {
3233         type Output;
3234         fn boo() -> Self::Output {
3235             loop {}
3236         }
3237     }
3238 }
3239
3240 struct F;
3241 impl foo::Foo for F {
3242     type Target = ();
3243 }
3244 impl foo::Bar for F {
3245     type Output = <F as foo::Foo>::Target;
3246 }
3247
3248 fn foo() {
3249     use foo::Bar;
3250     let x = <F as Bar>::boo();
3251 }"#,
3252         expect![[r#"
3253             132..163 '{     ...     }': Bar::Output<Self>
3254             146..153 'loop {}': !
3255             151..153 '{}': ()
3256             306..358 '{     ...o(); }': ()
3257             334..335 'x': ()
3258             338..353 '<F as Bar>::boo': fn boo<F>() -> <F as Bar>::Output
3259             338..355 '<F as ...:boo()': ()
3260         "#]],
3261     );
3262 }
3263
3264 #[test]
3265 fn renamed_extern_crate_in_block() {
3266     check_types(
3267         r#"
3268 //- /lib.rs crate:lib deps:serde
3269 use serde::Deserialize;
3270
3271 struct Foo {}
3272
3273 const _ : () = {
3274     extern crate serde as _serde;
3275     impl _serde::Deserialize for Foo {
3276         fn deserialize() -> u8 { 0 }
3277     }
3278 };
3279
3280 fn foo() {
3281     Foo::deserialize();
3282   //^^^^^^^^^^^^^^^^^^ u8
3283 }
3284
3285 //- /serde.rs crate:serde
3286
3287 pub trait Deserialize {
3288     fn deserialize() -> u8;
3289 }"#,
3290     );
3291 }
3292
3293 #[test]
3294 fn bin_op_adt_with_rhs_primitive() {
3295     check_infer_with_mismatches(
3296         r#"
3297 #[lang = "add"]
3298 pub trait Add<Rhs = Self> {
3299     type Output;
3300     fn add(self, rhs: Rhs) -> Self::Output;
3301 }
3302
3303 struct Wrapper(u32);
3304 impl Add<u32> for Wrapper {
3305     type Output = Self;
3306     fn add(self, rhs: u32) -> Wrapper {
3307         Wrapper(rhs)
3308     }
3309 }
3310 fn main(){
3311     let wrapped = Wrapper(10);
3312     let num: u32 = 2;
3313     let res = wrapped + num;
3314
3315 }"#,
3316         expect![[r#"
3317             72..76 'self': Self
3318             78..81 'rhs': Rhs
3319             192..196 'self': Wrapper
3320             198..201 'rhs': u32
3321             219..247 '{     ...     }': Wrapper
3322             229..236 'Wrapper': Wrapper(u32) -> Wrapper
3323             229..241 'Wrapper(rhs)': Wrapper
3324             237..240 'rhs': u32
3325             259..345 '{     ...um;  }': ()
3326             269..276 'wrapped': Wrapper
3327             279..286 'Wrapper': Wrapper(u32) -> Wrapper
3328             279..290 'Wrapper(10)': Wrapper
3329             287..289 '10': u32
3330             300..303 'num': u32
3331             311..312 '2': u32
3332             322..325 'res': Wrapper
3333             328..335 'wrapped': Wrapper
3334             328..341 'wrapped + num': Wrapper
3335             338..341 'num': u32
3336         "#]],
3337     )
3338 }
3339
3340 #[test]
3341 fn array_length() {
3342     check_infer(
3343         r#"
3344 trait T {
3345     type Output;
3346     fn do_thing(&self) -> Self::Output;
3347 }
3348
3349 impl T for [u8; 4] {
3350     type Output = usize;
3351     fn do_thing(&self) -> Self::Output {
3352         2
3353     }
3354 }
3355
3356 impl T for [u8; 2] {
3357     type Output = u8;
3358     fn do_thing(&self) -> Self::Output {
3359         2
3360     }
3361 }
3362
3363 fn main() {
3364     let v = [0u8; 2];
3365     let v2 = v.do_thing();
3366     let v3 = [0u8; 4];
3367     let v4 = v3.do_thing();
3368 }
3369 "#,
3370         expect![[r#"
3371             44..48 'self': &Self
3372             133..137 'self': &[u8; 4]
3373             155..172 '{     ...     }': usize
3374             165..166 '2': usize
3375             236..240 'self': &[u8; 2]
3376             258..275 '{     ...     }': u8
3377             268..269 '2': u8
3378             289..392 '{     ...g(); }': ()
3379             299..300 'v': [u8; 2]
3380             303..311 '[0u8; 2]': [u8; 2]
3381             304..307 '0u8': u8
3382             309..310 '2': usize
3383             321..323 'v2': u8
3384             326..327 'v': [u8; 2]
3385             326..338 'v.do_thing()': u8
3386             348..350 'v3': [u8; 4]
3387             353..361 '[0u8; 4]': [u8; 4]
3388             354..357 '0u8': u8
3389             359..360 '4': usize
3390             371..373 'v4': usize
3391             376..378 'v3': [u8; 4]
3392             376..389 'v3.do_thing()': usize
3393         "#]],
3394     )
3395 }
3396
3397 #[test]
3398 fn const_generics() {
3399     check_infer(
3400         r#"
3401 trait T {
3402     type Output;
3403     fn do_thing(&self) -> Self::Output;
3404 }
3405
3406 impl<const L: usize> T for [u8; L] {
3407     type Output = [u8; L];
3408     fn do_thing(&self) -> Self::Output {
3409         *self
3410     }
3411 }
3412
3413 fn main() {
3414     let v = [0u8; 2];
3415     let v2 = v.do_thing();
3416 }
3417 "#,
3418         expect![[r#"
3419             44..48 'self': &Self
3420             151..155 'self': &[u8; L]
3421             173..194 '{     ...     }': [u8; L]
3422             183..188 '*self': [u8; L]
3423             184..188 'self': &[u8; L]
3424             208..260 '{     ...g(); }': ()
3425             218..219 'v': [u8; 2]
3426             222..230 '[0u8; 2]': [u8; 2]
3427             223..226 '0u8': u8
3428             228..229 '2': usize
3429             240..242 'v2': [u8; 2]
3430             245..246 'v': [u8; 2]
3431             245..257 'v.do_thing()': [u8; 2]
3432         "#]],
3433     )
3434 }
3435
3436 #[test]
3437 fn fn_returning_unit() {
3438     check_infer_with_mismatches(
3439         r#"
3440 //- minicore: fn
3441 fn test<F: FnOnce()>(f: F) {
3442     let _: () = f();
3443 }"#,
3444         expect![[r#"
3445             21..22 'f': F
3446             27..51 '{     ...f(); }': ()
3447             37..38 '_': ()
3448             45..46 'f': F
3449             45..48 'f()': ()
3450         "#]],
3451     );
3452 }
3453
3454 #[test]
3455 fn trait_in_scope_of_trait_impl() {
3456     check_infer(
3457         r#"
3458 mod foo {
3459     pub trait Foo {
3460         fn foo(self);
3461         fn bar(self) -> usize { 0 }
3462     }
3463 }
3464 impl foo::Foo for u32 {
3465     fn foo(self) {
3466         let _x = self.bar();
3467     }
3468 }
3469     "#,
3470         expect![[r#"
3471             45..49 'self': Self
3472             67..71 'self': Self
3473             82..87 '{ 0 }': usize
3474             84..85 '0': usize
3475             131..135 'self': u32
3476             137..173 '{     ...     }': ()
3477             151..153 '_x': usize
3478             156..160 'self': u32
3479             156..166 'self.bar()': usize
3480         "#]],
3481     );
3482 }
3483
3484 #[test]
3485 fn infer_async_ret_type() {
3486     check_types(
3487         r#"
3488 //- minicore: future, result
3489 struct Fooey;
3490
3491 impl Fooey {
3492     fn collect<B: Convert>(self) -> B {
3493         B::new()
3494     }
3495 }
3496
3497 trait Convert {
3498     fn new() -> Self;
3499 }
3500 impl Convert for u32 {
3501     fn new() -> Self { 0 }
3502 }
3503
3504 async fn get_accounts() -> Result<u32, ()> {
3505     let ret = Fooey.collect();
3506     //        ^^^^^^^^^^^^^^^ u32
3507     Ok(ret)
3508 }
3509 "#,
3510     );
3511 }
3512
3513 #[test]
3514 fn local_impl_1() {
3515     check!(block_local_impls);
3516     check_types(
3517         r#"
3518 trait Trait<T> {
3519     fn foo(&self) -> T;
3520 }
3521
3522 fn test() {
3523     struct S;
3524     impl Trait<u32> for S {
3525         fn foo(&self) -> u32 { 0 }
3526     }
3527
3528     S.foo();
3529  // ^^^^^^^ u32
3530 }
3531 "#,
3532     );
3533 }
3534
3535 #[test]
3536 fn local_impl_2() {
3537     check!(block_local_impls);
3538     check_types(
3539         r#"
3540 struct S;
3541
3542 fn test() {
3543     trait Trait<T> {
3544         fn foo(&self) -> T;
3545     }
3546     impl Trait<u32> for S {
3547         fn foo(&self) -> u32 { 0 }
3548     }
3549
3550     S.foo();
3551  // ^^^^^^^ u32
3552 }
3553 "#,
3554     );
3555 }
3556
3557 #[test]
3558 fn local_impl_3() {
3559     check!(block_local_impls);
3560     check_types(
3561         r#"
3562 trait Trait<T> {
3563     fn foo(&self) -> T;
3564 }
3565
3566 fn test() {
3567     struct S1;
3568     {
3569         struct S2;
3570
3571         impl Trait<S1> for S2 {
3572             fn foo(&self) -> S1 { S1 }
3573         }
3574
3575         S2.foo();
3576      // ^^^^^^^^ S1
3577     }
3578 }
3579 "#,
3580     );
3581 }
3582
3583 #[test]
3584 fn associated_type_sized_bounds() {
3585     check_infer(
3586         r#"
3587 //- minicore: sized
3588 struct Yes;
3589 trait IsSized { const IS_SIZED: Yes; }
3590 impl<T: Sized> IsSized for T { const IS_SIZED: Yes = Yes; }
3591
3592 trait Foo {
3593     type Explicit: Sized;
3594     type Implicit;
3595     type Relaxed: ?Sized;
3596 }
3597 fn f<F: Foo>() {
3598     F::Explicit::IS_SIZED;
3599     F::Implicit::IS_SIZED;
3600     F::Relaxed::IS_SIZED;
3601 }
3602 "#,
3603         expect![[r#"
3604             104..107 'Yes': Yes
3605             212..295 '{     ...ZED; }': ()
3606             218..239 'F::Exp..._SIZED': Yes
3607             245..266 'F::Imp..._SIZED': Yes
3608             272..292 'F::Rel..._SIZED': {unknown}
3609         "#]],
3610     );
3611 }