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