]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests/traits.rs
Ignore type bindings in generic_predicates_for_param
[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
1416             123..124 'f': impl Trait
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 unselected_projection_in_trait_env_cycle_3() {
2276     // this is a cycle for rustc; we currently accept it
2277     check_types(
2278         r#"
2279 //- /main.rs
2280 trait Trait {
2281     type Item;
2282     type OtherItem;
2283 }
2284
2285 fn test<T>() where T: Trait<OtherItem = T::Item> {
2286     let x: T::Item = no_matter;
2287 }                   //^ Trait::Item<T>
2288 "#,
2289     );
2290 }
2291
2292 #[test]
2293 fn unselected_projection_in_trait_env_no_cycle() {
2294     // this is not a cycle
2295     check_types(
2296         r#"
2297 //- /main.rs
2298 trait Index {
2299     type Output;
2300 }
2301
2302 type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
2303
2304 pub trait UnificationStoreBase: Index<Output = Key<Self>> {
2305     type Key;
2306
2307     fn len(&self) -> usize;
2308 }
2309
2310 pub trait UnificationStoreMut: UnificationStoreBase {
2311     fn push(&mut self, value: Self::Key);
2312 }
2313
2314 fn test<T>(t: T) where T: UnificationStoreMut {
2315     let x;
2316     t.push(x);
2317     let y: Key<T>;
2318     (x, y);
2319 }      //^ (UnificationStoreBase::Key<T>, UnificationStoreBase::Key<T>)
2320 "#,
2321     );
2322 }
2323
2324 #[test]
2325 fn inline_assoc_type_bounds_1() {
2326     check_types(
2327         r#"
2328 trait Iterator {
2329     type Item;
2330 }
2331 trait OtherTrait<T> {
2332     fn foo(&self) -> T;
2333 }
2334
2335 // workaround for Chalk assoc type normalization problems
2336 pub struct S<T>;
2337 impl<T: Iterator> Iterator for S<T> {
2338     type Item = <T as Iterator>::Item;
2339 }
2340
2341 fn test<I: Iterator<Item: OtherTrait<u32>>>() {
2342     let x: <S<I> as Iterator>::Item;
2343     x.foo();
2344 }       //^ u32
2345 "#,
2346     );
2347 }
2348
2349 #[test]
2350 fn inline_assoc_type_bounds_2() {
2351     check_types(
2352         r#"
2353 trait Iterator {
2354     type Item;
2355 }
2356
2357 fn test<I: Iterator<Item: Iterator<Item = u32>>>() {
2358     let x: <<I as Iterator>::Item as Iterator>::Item;
2359     x;
2360 } //^ u32
2361 "#,
2362     );
2363 }
2364
2365 #[test]
2366 fn proc_macro_server_types() {
2367     check_infer(
2368         r#"
2369         macro_rules! with_api {
2370             ($S:ident, $self:ident, $m:ident) => {
2371                 $m! {
2372                     TokenStream {
2373                         fn new() -> $S::TokenStream;
2374                     },
2375                     Group {
2376                     },
2377                 }
2378             };
2379         }
2380         macro_rules! associated_item {
2381             (type TokenStream) =>
2382                 (type TokenStream: 'static;);
2383             (type Group) =>
2384                 (type Group: 'static;);
2385             ($($item:tt)*) => ($($item)*;)
2386         }
2387         macro_rules! declare_server_traits {
2388             ($($name:ident {
2389                 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)*
2390             }),* $(,)?) => {
2391                 pub trait Types {
2392                     $(associated_item!(type $name);)*
2393                 }
2394
2395                 $(pub trait $name: Types {
2396                     $(associated_item!(fn $method($($arg: $arg_ty),*) $(-> $ret_ty)?);)*
2397                 })*
2398
2399                 pub trait Server: Types $(+ $name)* {}
2400                 impl<S: Types $(+ $name)*> Server for S {}
2401             }
2402         }
2403
2404         with_api!(Self, self_, declare_server_traits);
2405         struct G {}
2406         struct T {}
2407         struct Rustc;
2408         impl Types for Rustc {
2409             type TokenStream = T;
2410             type Group = G;
2411         }
2412
2413         fn make<T>() -> T { loop {} }
2414         impl TokenStream for Rustc {
2415             fn new() -> Self::TokenStream {
2416                 let group: Self::Group = make();
2417                 make()
2418             }
2419         }
2420         "#,
2421         expect![[r#"
2422             1061..1072 '{ loop {} }': T
2423             1063..1070 'loop {}': !
2424             1068..1070 '{}': ()
2425             1136..1199 '{     ...     }': T
2426             1150..1155 'group': G
2427             1171..1175 'make': fn make<G>() -> G
2428             1171..1177 'make()': G
2429             1187..1191 'make': fn make<T>() -> T
2430             1187..1193 'make()': T
2431         "#]],
2432     );
2433 }
2434
2435 #[test]
2436 fn unify_impl_trait() {
2437     check_infer_with_mismatches(
2438         r#"
2439         trait Trait<T> {}
2440
2441         fn foo(x: impl Trait<u32>) { loop {} }
2442         fn bar<T>(x: impl Trait<T>) -> T { loop {} }
2443
2444         struct S<T>(T);
2445         impl<T> Trait<T> for S<T> {}
2446
2447         fn default<T>() -> T { loop {} }
2448
2449         fn test() -> impl Trait<i32> {
2450             let s1 = S(default());
2451             foo(s1);
2452             let x: i32 = bar(S(default()));
2453             S(default())
2454         }
2455         "#,
2456         expect![[r#"
2457             26..27 'x': impl Trait<u32>
2458             46..57 '{ loop {} }': ()
2459             48..55 'loop {}': !
2460             53..55 '{}': ()
2461             68..69 'x': impl Trait<T>
2462             91..102 '{ loop {} }': T
2463             93..100 'loop {}': !
2464             98..100 '{}': ()
2465             171..182 '{ loop {} }': T
2466             173..180 'loop {}': !
2467             178..180 '{}': ()
2468             213..309 '{     ...t()) }': S<{unknown}>
2469             223..225 's1': S<u32>
2470             228..229 'S': S<u32>(u32) -> S<u32>
2471             228..240 'S(default())': S<u32>
2472             230..237 'default': fn default<u32>() -> u32
2473             230..239 'default()': u32
2474             246..249 'foo': fn foo(S<u32>)
2475             246..253 'foo(s1)': ()
2476             250..252 's1': S<u32>
2477             263..264 'x': i32
2478             272..275 'bar': fn bar<i32>(S<i32>) -> i32
2479             272..289 'bar(S(...lt()))': i32
2480             276..277 'S': S<i32>(i32) -> S<i32>
2481             276..288 'S(default())': S<i32>
2482             278..285 'default': fn default<i32>() -> i32
2483             278..287 'default()': i32
2484             295..296 'S': S<{unknown}>({unknown}) -> S<{unknown}>
2485             295..307 'S(default())': S<{unknown}>
2486             297..304 'default': fn default<{unknown}>() -> {unknown}
2487             297..306 'default()': {unknown}
2488         "#]],
2489     );
2490 }
2491
2492 #[test]
2493 fn assoc_types_from_bounds() {
2494     check_infer(
2495         r#"
2496         //- /main.rs
2497         #[lang = "fn_once"]
2498         trait FnOnce<Args> {
2499             type Output;
2500         }
2501
2502         trait T {
2503             type O;
2504         }
2505
2506         impl T for () {
2507             type O = ();
2508         }
2509
2510         fn f<X, F>(_v: F)
2511         where
2512             X: T,
2513             F: FnOnce(&X::O),
2514         { }
2515
2516         fn main() {
2517             f::<(), _>(|z| { z; });
2518         }
2519         "#,
2520         expect![[r#"
2521             133..135 '_v': F
2522             178..181 '{ }': ()
2523             193..224 '{     ... }); }': ()
2524             199..209 'f::<(), _>': fn f<(), |&()| -> ()>(|&()| -> ())
2525             199..221 'f::<()... z; })': ()
2526             210..220 '|z| { z; }': |&()| -> ()
2527             211..212 'z': &()
2528             214..220 '{ z; }': ()
2529             216..217 'z': &()
2530         "#]],
2531     );
2532 }
2533
2534 #[test]
2535 fn associated_type_bound() {
2536     check_types(
2537         r#"
2538 pub trait Trait {
2539     type Item: OtherTrait<u32>;
2540 }
2541 pub trait OtherTrait<T> {
2542     fn foo(&self) -> T;
2543 }
2544
2545 // this is just a workaround for chalk#234
2546 pub struct S<T>;
2547 impl<T: Trait> Trait for S<T> {
2548     type Item = <T as Trait>::Item;
2549 }
2550
2551 fn test<T: Trait>() {
2552     let y: <S<T> as Trait>::Item = no_matter;
2553     y.foo();
2554 }       //^ u32
2555 "#,
2556     );
2557 }
2558
2559 #[test]
2560 fn dyn_trait_through_chalk() {
2561     check_types(
2562         r#"
2563 struct Box<T> {}
2564 #[lang = "deref"]
2565 trait Deref {
2566     type Target;
2567 }
2568 impl<T> Deref for Box<T> {
2569     type Target = T;
2570 }
2571 trait Trait {
2572     fn foo(&self);
2573 }
2574
2575 fn test(x: Box<dyn Trait>) {
2576     x.foo();
2577 }       //^ ()
2578 "#,
2579     );
2580 }
2581
2582 #[test]
2583 fn string_to_owned() {
2584     check_types(
2585         r#"
2586 struct String {}
2587 pub trait ToOwned {
2588     type Owned;
2589     fn to_owned(&self) -> Self::Owned;
2590 }
2591 impl ToOwned for str {
2592     type Owned = String;
2593 }
2594 fn test() {
2595     "foo".to_owned();
2596 }               //^ String
2597 "#,
2598     );
2599 }
2600
2601 #[test]
2602 fn iterator_chain() {
2603     check_infer_with_mismatches(
2604         r#"
2605         //- /main.rs
2606         #[lang = "fn_once"]
2607         trait FnOnce<Args> {
2608             type Output;
2609         }
2610         #[lang = "fn_mut"]
2611         trait FnMut<Args>: FnOnce<Args> { }
2612
2613         enum Option<T> { Some(T), None }
2614         use Option::*;
2615
2616         pub trait Iterator {
2617             type Item;
2618
2619             fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
2620             where
2621                 F: FnMut(Self::Item) -> Option<B>,
2622             { loop {} }
2623
2624             fn for_each<F>(self, f: F)
2625             where
2626                 F: FnMut(Self::Item),
2627             { loop {} }
2628         }
2629
2630         pub trait IntoIterator {
2631             type Item;
2632             type IntoIter: Iterator<Item = Self::Item>;
2633             fn into_iter(self) -> Self::IntoIter;
2634         }
2635
2636         pub struct FilterMap<I, F> { }
2637         impl<B, I: Iterator, F> Iterator for FilterMap<I, F>
2638         where
2639             F: FnMut(I::Item) -> Option<B>,
2640         {
2641             type Item = B;
2642         }
2643
2644         #[stable(feature = "rust1", since = "1.0.0")]
2645         impl<I: Iterator> IntoIterator for I {
2646             type Item = I::Item;
2647             type IntoIter = I;
2648
2649             fn into_iter(self) -> I {
2650                 self
2651             }
2652         }
2653
2654         struct Vec<T> {}
2655         impl<T> Vec<T> {
2656             fn new() -> Self { loop {} }
2657         }
2658
2659         impl<T> IntoIterator for Vec<T> {
2660             type Item = T;
2661             type IntoIter = IntoIter<T>;
2662         }
2663
2664         pub struct IntoIter<T> { }
2665         impl<T> Iterator for IntoIter<T> {
2666             type Item = T;
2667         }
2668
2669         fn main() {
2670             Vec::<i32>::new().into_iter()
2671             .filter_map(|x| if x > 0 { Some(x as u32) } else { None })
2672             .for_each(|y| { y; });
2673         }
2674         "#,
2675         expect![[r#"
2676             226..230 'self': Self
2677             232..233 'f': F
2678             317..328 '{ loop {} }': FilterMap<Self, F>
2679             319..326 'loop {}': !
2680             324..326 '{}': ()
2681             349..353 'self': Self
2682             355..356 'f': F
2683             405..416 '{ loop {} }': ()
2684             407..414 'loop {}': !
2685             412..414 '{}': ()
2686             525..529 'self': Self
2687             854..858 'self': I
2688             865..885 '{     ...     }': I
2689             875..879 'self': I
2690             944..955 '{ loop {} }': Vec<T>
2691             946..953 'loop {}': !
2692             951..953 '{}': ()
2693             1142..1269 '{     ... }); }': ()
2694             1148..1163 'Vec::<i32>::new': fn new<i32>() -> Vec<i32>
2695             1148..1165 'Vec::<...:new()': Vec<i32>
2696             1148..1177 'Vec::<...iter()': IntoIter<i32>
2697             1148..1240 'Vec::<...one })': FilterMap<IntoIter<i32>, |i32| -> Option<u32>>
2698             1148..1266 'Vec::<... y; })': ()
2699             1194..1239 '|x| if...None }': |i32| -> Option<u32>
2700             1195..1196 'x': i32
2701             1198..1239 'if x >...None }': Option<u32>
2702             1201..1202 'x': i32
2703             1201..1206 'x > 0': bool
2704             1205..1206 '0': i32
2705             1207..1225 '{ Some...u32) }': Option<u32>
2706             1209..1213 'Some': Some<u32>(u32) -> Option<u32>
2707             1209..1223 'Some(x as u32)': Option<u32>
2708             1214..1215 'x': i32
2709             1214..1222 'x as u32': u32
2710             1231..1239 '{ None }': Option<u32>
2711             1233..1237 'None': Option<u32>
2712             1255..1265 '|y| { y; }': |u32| -> ()
2713             1256..1257 'y': u32
2714             1259..1265 '{ y; }': ()
2715             1261..1262 'y': u32
2716         "#]],
2717     );
2718 }
2719
2720 #[test]
2721 fn nested_assoc() {
2722     check_types(
2723         r#"
2724 struct Bar;
2725 struct Foo;
2726
2727 trait A {
2728     type OutputA;
2729 }
2730
2731 impl A for Bar {
2732     type OutputA = Foo;
2733 }
2734
2735 trait B {
2736     type Output;
2737     fn foo() -> Self::Output;
2738 }
2739
2740 impl<T:A> B for T {
2741     type Output = T::OutputA;
2742     fn foo() -> Self::Output { loop {} }
2743 }
2744
2745 fn main() {
2746     Bar::foo();
2747 }          //^ Foo
2748 "#,
2749     );
2750 }
2751
2752 #[test]
2753 fn trait_object_no_coercion() {
2754     check_infer_with_mismatches(
2755         r#"
2756         trait Foo {}
2757
2758         fn foo(x: &dyn Foo) {}
2759
2760         fn test(x: &dyn Foo) {
2761             foo(x);
2762         }
2763         "#,
2764         expect![[r#"
2765             21..22 'x': &dyn Foo
2766             34..36 '{}': ()
2767             46..47 'x': &dyn Foo
2768             59..74 '{     foo(x); }': ()
2769             65..68 'foo': fn foo(&dyn Foo)
2770             65..71 'foo(x)': ()
2771             69..70 'x': &dyn Foo
2772         "#]],
2773     );
2774 }
2775
2776 #[test]
2777 fn builtin_copy() {
2778     check_infer_with_mismatches(
2779         r#"
2780         #[lang = "copy"]
2781         trait Copy {}
2782
2783         struct IsCopy;
2784         impl Copy for IsCopy {}
2785         struct NotCopy;
2786
2787         trait Test { fn test(&self) -> bool; }
2788         impl<T: Copy> Test for T {}
2789
2790         fn test() {
2791             IsCopy.test();
2792             NotCopy.test();
2793             (IsCopy, IsCopy).test();
2794             (IsCopy, NotCopy).test();
2795         }
2796         "#,
2797         expect![[r#"
2798             110..114 'self': &Self
2799             166..267 '{     ...t(); }': ()
2800             172..178 'IsCopy': IsCopy
2801             172..185 'IsCopy.test()': bool
2802             191..198 'NotCopy': NotCopy
2803             191..205 'NotCopy.test()': {unknown}
2804             211..227 '(IsCop...sCopy)': (IsCopy, IsCopy)
2805             211..234 '(IsCop...test()': bool
2806             212..218 'IsCopy': IsCopy
2807             220..226 'IsCopy': IsCopy
2808             240..257 '(IsCop...tCopy)': (IsCopy, NotCopy)
2809             240..264 '(IsCop...test()': {unknown}
2810             241..247 'IsCopy': IsCopy
2811             249..256 'NotCopy': NotCopy
2812         "#]],
2813     );
2814 }
2815
2816 #[test]
2817 fn builtin_fn_def_copy() {
2818     check_infer_with_mismatches(
2819         r#"
2820         #[lang = "copy"]
2821         trait Copy {}
2822
2823         fn foo() {}
2824         fn bar<T: Copy>(T) -> T {}
2825         struct Struct(usize);
2826         enum Enum { Variant(usize) }
2827
2828         trait Test { fn test(&self) -> bool; }
2829         impl<T: Copy> Test for T {}
2830
2831         fn test() {
2832             foo.test();
2833             bar.test();
2834             Struct.test();
2835             Enum::Variant.test();
2836         }
2837         "#,
2838         expect![[r#"
2839             41..43 '{}': ()
2840             60..61 'T': {unknown}
2841             68..70 '{}': ()
2842             68..70: expected T, got ()
2843             145..149 'self': &Self
2844             201..281 '{     ...t(); }': ()
2845             207..210 'foo': fn foo()
2846             207..217 'foo.test()': bool
2847             223..226 'bar': fn bar<{unknown}>({unknown}) -> {unknown}
2848             223..233 'bar.test()': bool
2849             239..245 'Struct': Struct(usize) -> Struct
2850             239..252 'Struct.test()': bool
2851             258..271 'Enum::Variant': Variant(usize) -> Enum
2852             258..278 'Enum::...test()': bool
2853         "#]],
2854     );
2855 }
2856
2857 #[test]
2858 fn builtin_fn_ptr_copy() {
2859     check_infer_with_mismatches(
2860         r#"
2861         #[lang = "copy"]
2862         trait Copy {}
2863
2864         trait Test { fn test(&self) -> bool; }
2865         impl<T: Copy> Test for T {}
2866
2867         fn test(f1: fn(), f2: fn(usize) -> u8, f3: fn(u8, u8) -> &u8) {
2868             f1.test();
2869             f2.test();
2870             f3.test();
2871         }
2872         "#,
2873         expect![[r#"
2874             54..58 'self': &Self
2875             108..110 'f1': fn()
2876             118..120 'f2': fn(usize) -> u8
2877             139..141 'f3': fn(u8, u8) -> &u8
2878             162..210 '{     ...t(); }': ()
2879             168..170 'f1': fn()
2880             168..177 'f1.test()': bool
2881             183..185 'f2': fn(usize) -> u8
2882             183..192 'f2.test()': bool
2883             198..200 'f3': fn(u8, u8) -> &u8
2884             198..207 'f3.test()': bool
2885         "#]],
2886     );
2887 }
2888
2889 #[test]
2890 fn builtin_sized() {
2891     check_infer_with_mismatches(
2892         r#"
2893         #[lang = "sized"]
2894         trait Sized {}
2895
2896         trait Test { fn test(&self) -> bool; }
2897         impl<T: Sized> Test for T {}
2898
2899         fn test() {
2900             1u8.test();
2901             (*"foo").test(); // not Sized
2902             (1u8, 1u8).test();
2903             (1u8, *"foo").test(); // not Sized
2904         }
2905         "#,
2906         expect![[r#"
2907             56..60 'self': &Self
2908             113..228 '{     ...ized }': ()
2909             119..122 '1u8': u8
2910             119..129 '1u8.test()': bool
2911             135..150 '(*"foo").test()': {unknown}
2912             136..142 '*"foo"': str
2913             137..142 '"foo"': &str
2914             169..179 '(1u8, 1u8)': (u8, u8)
2915             169..186 '(1u8, ...test()': bool
2916             170..173 '1u8': u8
2917             175..178 '1u8': u8
2918             192..205 '(1u8, *"foo")': (u8, str)
2919             192..212 '(1u8, ...test()': {unknown}
2920             193..196 '1u8': u8
2921             198..204 '*"foo"': str
2922             199..204 '"foo"': &str
2923         "#]],
2924     );
2925 }
2926
2927 #[test]
2928 fn integer_range_iterate() {
2929     check_types(
2930         r#"
2931 //- /main.rs crate:main deps:core
2932 fn test() {
2933     for x in 0..100 { x; }
2934 }                   //^ i32
2935
2936 //- /core.rs crate:core
2937 pub mod ops {
2938     pub struct Range<Idx> {
2939         pub start: Idx,
2940         pub end: Idx,
2941     }
2942 }
2943
2944 pub mod iter {
2945     pub trait Iterator {
2946         type Item;
2947     }
2948
2949     pub trait IntoIterator {
2950         type Item;
2951         type IntoIter: Iterator<Item = Self::Item>;
2952     }
2953
2954     impl<T> IntoIterator for T where T: Iterator {
2955         type Item = <T as Iterator>::Item;
2956         type IntoIter = Self;
2957     }
2958 }
2959
2960 trait Step {}
2961 impl Step for i32 {}
2962 impl Step for i64 {}
2963
2964 impl<A: Step> iter::Iterator for ops::Range<A> {
2965     type Item = A;
2966 }
2967 "#,
2968     );
2969 }
2970
2971 #[test]
2972 fn infer_closure_arg() {
2973     check_infer(
2974         r#"
2975         //- /lib.rs
2976
2977         enum Option<T> {
2978             None,
2979             Some(T)
2980         }
2981
2982         fn foo() {
2983             let s = Option::None;
2984             let f = |x: Option<i32>| {};
2985             (&f)(s)
2986         }
2987         "#,
2988         expect![[r#"
2989             52..126 '{     ...)(s) }': ()
2990             62..63 's': Option<i32>
2991             66..78 'Option::None': Option<i32>
2992             88..89 'f': |Option<i32>| -> ()
2993             92..111 '|x: Op...2>| {}': |Option<i32>| -> ()
2994             93..94 'x': Option<i32>
2995             109..111 '{}': ()
2996             117..124 '(&f)(s)': ()
2997             118..120 '&f': &|Option<i32>| -> ()
2998             119..120 'f': |Option<i32>| -> ()
2999             122..123 's': Option<i32>
3000         "#]],
3001     );
3002 }
3003
3004 #[test]
3005 fn infer_fn_trait_arg() {
3006     check_infer_with_mismatches(
3007         r#"
3008         //- /lib.rs deps:std
3009
3010         #[lang = "fn_once"]
3011         pub trait FnOnce<Args> {
3012             type Output;
3013
3014             extern "rust-call" fn call_once(&self, args: Args) -> Self::Output;
3015         }
3016
3017         #[lang = "fn"]
3018         pub trait Fn<Args>:FnOnce<Args> {
3019             extern "rust-call" fn call(&self, args: Args) -> Self::Output;
3020         }
3021
3022         enum Option<T> {
3023             None,
3024             Some(T)
3025         }
3026
3027         fn foo<F, T>(f: F) -> T
3028         where
3029             F: Fn(Option<i32>) -> T,
3030         {
3031             let s = None;
3032             f(s)
3033         }
3034         "#,
3035         expect![[r#"
3036             101..105 'self': &Self
3037             107..111 'args': Args
3038             220..224 'self': &Self
3039             226..230 'args': Args
3040             313..314 'f': F
3041             359..389 '{     ...f(s) }': T
3042             369..370 's': Option<i32>
3043             373..377 'None': Option<i32>
3044             383..384 'f': F
3045             383..387 'f(s)': T
3046             385..386 's': Option<i32>
3047         "#]],
3048     );
3049 }
3050
3051 #[test]
3052 fn infer_box_fn_arg() {
3053     // The type mismatch is a bug
3054     check_infer_with_mismatches(
3055         r#"
3056         //- /lib.rs deps:std
3057
3058         #[lang = "fn_once"]
3059         pub trait FnOnce<Args> {
3060             type Output;
3061
3062             extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
3063         }
3064
3065         #[lang = "deref"]
3066         pub trait Deref {
3067             type Target: ?Sized;
3068
3069             fn deref(&self) -> &Self::Target;
3070         }
3071
3072         #[lang = "owned_box"]
3073         pub struct Box<T: ?Sized> {
3074             inner: *mut T,
3075         }
3076
3077         impl<T: ?Sized> Deref for Box<T> {
3078             type Target = T;
3079
3080             fn deref(&self) -> &T {
3081                 &self.inner
3082             }
3083         }
3084
3085         enum Option<T> {
3086             None,
3087             Some(T)
3088         }
3089
3090         fn foo() {
3091             let s = Option::None;
3092             let f: Box<dyn FnOnce(&Option<i32>)> = box (|ps| {});
3093             f(&s);
3094         }
3095         "#,
3096         expect![[r#"
3097             100..104 'self': Self
3098             106..110 'args': Args
3099             214..218 'self': &Self
3100             384..388 'self': &Box<T>
3101             396..423 '{     ...     }': &T
3102             406..417 '&self.inner': &*mut T
3103             407..411 'self': &Box<T>
3104             407..417 'self.inner': *mut T
3105             478..576 '{     ...&s); }': ()
3106             488..489 's': Option<i32>
3107             492..504 'Option::None': Option<i32>
3108             514..515 'f': Box<dyn FnOnce(&Option<i32>)>
3109             549..562 'box (|ps| {})': Box<|{unknown}| -> ()>
3110             554..561 '|ps| {}': |{unknown}| -> ()
3111             555..557 'ps': {unknown}
3112             559..561 '{}': ()
3113             568..569 'f': Box<dyn FnOnce(&Option<i32>)>
3114             568..573 'f(&s)': FnOnce::Output<dyn FnOnce(&Option<i32>), (&Option<i32>,)>
3115             570..572 '&s': &Option<i32>
3116             571..572 's': Option<i32>
3117             549..562: expected Box<dyn FnOnce(&Option<i32>)>, got Box<|_| -> ()>
3118         "#]],
3119     );
3120 }
3121
3122 #[test]
3123 fn infer_dyn_fn_output() {
3124     check_types(
3125         r#"
3126 #[lang = "fn_once"]
3127 pub trait FnOnce<Args> {
3128     type Output;
3129     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
3130 }
3131
3132 #[lang = "fn"]
3133 pub trait Fn<Args>: FnOnce<Args> {
3134     extern "rust-call" fn call(&self, args: Args) -> Self::Output;
3135 }
3136
3137 fn foo() {
3138     let f: &dyn Fn() -> i32;
3139     f();
3140   //^^^ i32
3141 }"#,
3142     );
3143 }
3144
3145 #[test]
3146 fn infer_dyn_fn_once_output() {
3147     check_types(
3148         r#"
3149 #[lang = "fn_once"]
3150 pub trait FnOnce<Args> {
3151     type Output;
3152     extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
3153 }
3154
3155 fn foo() {
3156     let f: dyn FnOnce() -> i32;
3157     f();
3158   //^^^ i32
3159 }"#,
3160     );
3161 }
3162
3163 #[test]
3164 fn variable_kinds_1() {
3165     check_types(
3166         r#"
3167 trait Trait<T> { fn get(self, t: T) -> T; }
3168 struct S;
3169 impl Trait<u128> for S {}
3170 impl Trait<f32> for S {}
3171 fn test() {
3172     S.get(1);
3173   //^^^^^^^^ u128
3174     S.get(1.);
3175   //^^^^^^^^ f32
3176 }
3177         "#,
3178     );
3179 }
3180
3181 #[test]
3182 fn variable_kinds_2() {
3183     check_types(
3184         r#"
3185 trait Trait { fn get(self) -> Self; }
3186 impl Trait for u128 {}
3187 impl Trait for f32 {}
3188 fn test() {
3189     1.get();
3190   //^^^^^^^ u128
3191     (1.).get();
3192   //^^^^^^^^^^ f32
3193 }
3194         "#,
3195     );
3196 }
3197
3198 #[test]
3199 fn underscore_import() {
3200     check_types(
3201         r#"
3202 mod tr {
3203     pub trait Tr {
3204         fn method(&self) -> u8 { 0 }
3205     }
3206 }
3207
3208 struct Tr;
3209 impl crate::tr::Tr for Tr {}
3210
3211 use crate::tr::Tr as _;
3212 fn test() {
3213     Tr.method();
3214   //^^^^^^^^^^^ u8
3215 }
3216     "#,
3217     );
3218 }
3219
3220 #[test]
3221 fn inner_use() {
3222     check_types(
3223         r#"
3224 mod m {
3225     pub trait Tr {
3226         fn method(&self) -> u8 { 0 }
3227     }
3228
3229     impl Tr for () {}
3230 }
3231
3232 fn f() {
3233     use m::Tr;
3234
3235     ().method();
3236   //^^^^^^^^^^^ u8
3237 }
3238         "#,
3239     );
3240 }
3241
3242 #[test]
3243 fn trait_in_scope_with_inner_item() {
3244     check_infer(
3245         r#"
3246 mod m {
3247     pub trait Tr {
3248         fn method(&self) -> u8 { 0 }
3249     }
3250
3251     impl Tr for () {}
3252 }
3253
3254 use m::Tr;
3255
3256 fn f() {
3257     fn inner() {
3258         ().method();
3259       //^^^^^^^^^^^ u8
3260     }
3261 }
3262         "#,
3263         expect![[r#"
3264             46..50 'self': &Self
3265             58..63 '{ 0 }': u8
3266             60..61 '0': u8
3267             115..185 '{     ...   } }': ()
3268             132..183 '{     ...     }': ()
3269             142..144 '()': ()
3270             142..153 '().method()': u8
3271         "#]],
3272     );
3273 }
3274
3275 #[test]
3276 fn inner_use_in_block() {
3277     check_types(
3278         r#"
3279 mod m {
3280     pub trait Tr {
3281         fn method(&self) -> u8 { 0 }
3282     }
3283
3284     impl Tr for () {}
3285 }
3286
3287 fn f() {
3288     {
3289         use m::Tr;
3290
3291         ().method();
3292       //^^^^^^^^^^^ u8
3293     }
3294
3295     {
3296         ().method();
3297       //^^^^^^^^^^^ {unknown}
3298     }
3299
3300     ().method();
3301   //^^^^^^^^^^^ {unknown}
3302 }
3303         "#,
3304     );
3305 }
3306
3307 #[test]
3308 fn nested_inner_function_calling_self() {
3309     check_infer(
3310         r#"
3311 struct S;
3312 fn f() {
3313     fn inner() -> S {
3314         let s = inner();
3315     }
3316 }
3317         "#,
3318         expect![[r#"
3319             17..73 '{     ...   } }': ()
3320             39..71 '{     ...     }': ()
3321             53..54 's': S
3322             57..62 'inner': fn inner() -> S
3323             57..64 'inner()': S
3324         "#]],
3325     )
3326 }