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