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