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