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