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