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