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