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