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