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