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