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