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