aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/tests/coercion.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/tests/coercion.rs')
-rw-r--r--crates/hir_ty/src/tests/coercion.rs310
1 files changed, 129 insertions, 181 deletions
diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs
index 58d454a0f..eca6ae1fe 100644
--- a/crates/hir_ty/src/tests/coercion.rs
+++ b/crates/hir_ty/src/tests/coercion.rs
@@ -630,25 +630,19 @@ fn coerce_placeholder_ref() {
630fn coerce_unsize_array() { 630fn coerce_unsize_array() {
631 check_infer_with_mismatches( 631 check_infer_with_mismatches(
632 r#" 632 r#"
633 #[lang = "unsize"] 633//- minicore: coerce_unsized
634 pub trait Unsize<T> {} 634fn test() {
635 #[lang = "coerce_unsized"] 635 let f: &[usize] = &[1, 2, 3];
636 pub trait CoerceUnsized<T> {} 636}
637
638 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
639
640 fn test() {
641 let f: &[usize] = &[1, 2, 3];
642 }
643 "#, 637 "#,
644 expect![[r#" 638 expect![[r#"
645 161..198 '{ ... 3]; }': () 639 10..47 '{ ... 3]; }': ()
646 171..172 'f': &[usize] 640 20..21 'f': &[usize]
647 185..195 '&[1, 2, 3]': &[usize; 3] 641 34..44 '&[1, 2, 3]': &[usize; 3]
648 186..195 '[1, 2, 3]': [usize; 3] 642 35..44 '[1, 2, 3]': [usize; 3]
649 187..188 '1': usize 643 36..37 '1': usize
650 190..191 '2': usize 644 39..40 '2': usize
651 193..194 '3': usize 645 42..43 '3': usize
652 "#]], 646 "#]],
653 ); 647 );
654} 648}
@@ -657,42 +651,34 @@ fn coerce_unsize_array() {
657fn coerce_unsize_trait_object_simple() { 651fn coerce_unsize_trait_object_simple() {
658 check_infer_with_mismatches( 652 check_infer_with_mismatches(
659 r#" 653 r#"
660 #[lang = "sized"] 654//- minicore: coerce_unsized
661 pub trait Sized {} 655trait Foo<T, U> {}
662 #[lang = "unsize"] 656trait Bar<U, T, X>: Foo<T, U> {}
663 pub trait Unsize<T> {} 657trait Baz<T, X>: Bar<usize, T, X> {}
664 #[lang = "coerce_unsized"]
665 pub trait CoerceUnsized<T> {}
666
667 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
668
669 trait Foo<T, U> {}
670 trait Bar<U, T, X>: Foo<T, U> {}
671 trait Baz<T, X>: Bar<usize, T, X> {}
672 658
673 struct S<T, X>; 659struct S<T, X>;
674 impl<T, X> Foo<T, usize> for S<T, X> {} 660impl<T, X> Foo<T, usize> for S<T, X> {}
675 impl<T, X> Bar<usize, T, X> for S<T, X> {} 661impl<T, X> Bar<usize, T, X> for S<T, X> {}
676 impl<T, X> Baz<T, X> for S<T, X> {} 662impl<T, X> Baz<T, X> for S<T, X> {}
677 663
678 fn test() { 664fn test() {
679 let obj: &dyn Baz<i8, i16> = &S; 665 let obj: &dyn Baz<i8, i16> = &S;
680 let obj: &dyn Bar<_, i8, i16> = &S; 666 let obj: &dyn Bar<_, i8, i16> = &S;
681 let obj: &dyn Foo<i8, _> = &S; 667 let obj: &dyn Foo<i8, _> = &S;
682 } 668}
683 "#, 669"#,
684 expect![[r" 670 expect![[r#"
685 424..539 '{ ... &S; }': () 671 236..351 '{ ... &S; }': ()
686 434..437 'obj': &dyn Baz<i8, i16> 672 246..249 'obj': &dyn Baz<i8, i16>
687 459..461 '&S': &S<i8, i16> 673 271..273 '&S': &S<i8, i16>
688 460..461 'S': S<i8, i16> 674 272..273 'S': S<i8, i16>
689 471..474 'obj': &dyn Bar<usize, i8, i16> 675 283..286 'obj': &dyn Bar<usize, i8, i16>
690 499..501 '&S': &S<i8, i16> 676 311..313 '&S': &S<i8, i16>
691 500..501 'S': S<i8, i16> 677 312..313 'S': S<i8, i16>
692 511..514 'obj': &dyn Foo<i8, usize> 678 323..326 'obj': &dyn Foo<i8, usize>
693 534..536 '&S': &S<i8, {unknown}> 679 346..348 '&S': &S<i8, {unknown}>
694 535..536 'S': S<i8, {unknown}> 680 347..348 'S': S<i8, {unknown}>
695 "]], 681 "#]],
696 ); 682 );
697} 683}
698 684
@@ -717,49 +703,41 @@ fn coerce_unsize_trait_object_to_trait_object() {
717 // 602..606 'obj2': &dyn Baz<i8, i16> 703 // 602..606 'obj2': &dyn Baz<i8, i16>
718 check_infer_with_mismatches( 704 check_infer_with_mismatches(
719 r#" 705 r#"
720 #[lang = "sized"] 706//- minicore: coerce_unsized
721 pub trait Sized {} 707trait Foo<T, U> {}
722 #[lang = "unsize"] 708trait Bar<U, T, X>: Foo<T, U> {}
723 pub trait Unsize<T> {} 709trait Baz<T, X>: Bar<usize, T, X> {}
724 #[lang = "coerce_unsized"]
725 pub trait CoerceUnsized<T> {}
726
727 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
728
729 trait Foo<T, U> {}
730 trait Bar<U, T, X>: Foo<T, U> {}
731 trait Baz<T, X>: Bar<usize, T, X> {}
732 710
733 struct S<T, X>; 711struct S<T, X>;
734 impl<T, X> Foo<T, usize> for S<T, X> {} 712impl<T, X> Foo<T, usize> for S<T, X> {}
735 impl<T, X> Bar<usize, T, X> for S<T, X> {} 713impl<T, X> Bar<usize, T, X> for S<T, X> {}
736 impl<T, X> Baz<T, X> for S<T, X> {} 714impl<T, X> Baz<T, X> for S<T, X> {}
737 715
738 fn test() { 716fn test() {
739 let obj: &dyn Baz<i8, i16> = &S; 717 let obj: &dyn Baz<i8, i16> = &S;
740 let obj: &dyn Bar<_, _, _> = obj; 718 let obj: &dyn Bar<_, _, _> = obj;
741 let obj: &dyn Foo<_, _> = obj; 719 let obj: &dyn Foo<_, _> = obj;
742 let obj2: &dyn Baz<i8, i16> = &S; 720 let obj2: &dyn Baz<i8, i16> = &S;
743 let _: &dyn Foo<_, _> = obj2; 721 let _: &dyn Foo<_, _> = obj2;
744 } 722}
745 "#, 723"#,
746 expect![[r#" 724 expect![[r#"
747 424..609 '{ ...bj2; }': () 725 236..421 '{ ...bj2; }': ()
748 434..437 'obj': &dyn Baz<i8, i16> 726 246..249 'obj': &dyn Baz<i8, i16>
749 459..461 '&S': &S<i8, i16> 727 271..273 '&S': &S<i8, i16>
750 460..461 'S': S<i8, i16> 728 272..273 'S': S<i8, i16>
751 471..474 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> 729 283..286 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}>
752 496..499 'obj': &dyn Baz<i8, i16> 730 308..311 'obj': &dyn Baz<i8, i16>
753 509..512 'obj': &dyn Foo<{unknown}, {unknown}> 731 321..324 'obj': &dyn Foo<{unknown}, {unknown}>
754 531..534 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}> 732 343..346 'obj': &dyn Bar<{unknown}, {unknown}, {unknown}>
755 544..548 'obj2': &dyn Baz<i8, i16> 733 356..360 'obj2': &dyn Baz<i8, i16>
756 570..572 '&S': &S<i8, i16> 734 382..384 '&S': &S<i8, i16>
757 571..572 'S': S<i8, i16> 735 383..384 'S': S<i8, i16>
758 582..583 '_': &dyn Foo<{unknown}, {unknown}> 736 394..395 '_': &dyn Foo<{unknown}, {unknown}>
759 602..606 'obj2': &dyn Baz<i8, i16> 737 414..418 'obj2': &dyn Baz<i8, i16>
760 496..499: expected &dyn Bar<{unknown}, {unknown}, {unknown}>, got &dyn Baz<i8, i16> 738 308..311: expected &dyn Bar<{unknown}, {unknown}, {unknown}>, got &dyn Baz<i8, i16>
761 531..534: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Bar<{unknown}, {unknown}, {unknown}> 739 343..346: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Bar<{unknown}, {unknown}, {unknown}>
762 602..606: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Baz<i8, i16> 740 414..418: expected &dyn Foo<{unknown}, {unknown}>, got &dyn Baz<i8, i16>
763 "#]], 741 "#]],
764 ); 742 );
765} 743}
@@ -768,40 +746,32 @@ fn coerce_unsize_trait_object_to_trait_object() {
768fn coerce_unsize_super_trait_cycle() { 746fn coerce_unsize_super_trait_cycle() {
769 check_infer_with_mismatches( 747 check_infer_with_mismatches(
770 r#" 748 r#"
771 #[lang = "sized"] 749//- minicore: coerce_unsized
772 pub trait Sized {} 750trait A {}
773 #[lang = "unsize"] 751trait B: C + A {}
774 pub trait Unsize<T> {} 752trait C: B {}
775 #[lang = "coerce_unsized"] 753trait D: C
776 pub trait CoerceUnsized<T> {}
777
778 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
779
780 trait A {}
781 trait B: C + A {}
782 trait C: B {}
783 trait D: C
784
785 struct S;
786 impl A for S {}
787 impl B for S {}
788 impl C for S {}
789 impl D for S {}
790 754
791 fn test() { 755struct S;
792 let obj: &dyn D = &S; 756impl A for S {}
793 let obj: &dyn A = &S; 757impl B for S {}
794 } 758impl C for S {}
795 "#, 759impl D for S {}
796 expect![[r" 760
797 328..383 '{ ... &S; }': () 761fn test() {
798 338..341 'obj': &dyn D 762 let obj: &dyn D = &S;
799 352..354 '&S': &S 763 let obj: &dyn A = &S;
800 353..354 'S': S 764}
801 364..367 'obj': &dyn A 765"#,
802 378..380 '&S': &S 766 expect![[r#"
803 379..380 'S': S 767 140..195 '{ ... &S; }': ()
804 "]], 768 150..153 'obj': &dyn D
769 164..166 '&S': &S
770 165..166 'S': S
771 176..179 'obj': &dyn A
772 190..192 '&S': &S
773 191..192 'S': S
774 "#]],
805 ); 775 );
806} 776}
807 777
@@ -810,41 +780,35 @@ fn coerce_unsize_generic() {
810 // FIXME: fix the type mismatches here 780 // FIXME: fix the type mismatches here
811 check_infer_with_mismatches( 781 check_infer_with_mismatches(
812 r#" 782 r#"
813 #[lang = "unsize"] 783//- minicore: coerce_unsized
814 pub trait Unsize<T> {} 784struct Foo<T> { t: T };
815 #[lang = "coerce_unsized"] 785struct Bar<T>(Foo<T>);
816 pub trait CoerceUnsized<T> {}
817
818 impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
819
820 struct Foo<T> { t: T };
821 struct Bar<T>(Foo<T>);
822 786
823 fn test() { 787fn test() {
824 let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] }; 788 let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
825 let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] }); 789 let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
826 } 790}
827 "#, 791"#,
828 expect![[r#" 792 expect![[r#"
829 209..317 '{ ... }); }': () 793 58..166 '{ ... }); }': ()
830 219..220 '_': &Foo<[usize]> 794 68..69 '_': &Foo<[usize]>
831 238..259 '&Foo {..., 3] }': &Foo<[usize]> 795 87..108 '&Foo {..., 3] }': &Foo<[usize]>
832 239..259 'Foo { ..., 3] }': Foo<[usize]> 796 88..108 'Foo { ..., 3] }': Foo<[usize]>
833 248..257 '[1, 2, 3]': [usize; 3] 797 97..106 '[1, 2, 3]': [usize; 3]
834 249..250 '1': usize 798 98..99 '1': usize
835 252..253 '2': usize 799 101..102 '2': usize
836 255..256 '3': usize 800 104..105 '3': usize
837 269..270 '_': &Bar<[usize]> 801 118..119 '_': &Bar<[usize]>
838 288..314 '&Bar(F... 3] })': &Bar<[i32; 3]> 802 137..163 '&Bar(F... 3] })': &Bar<[i32; 3]>
839 289..292 'Bar': Bar<[i32; 3]>(Foo<[i32; 3]>) -> Bar<[i32; 3]> 803 138..141 'Bar': Bar<[i32; 3]>(Foo<[i32; 3]>) -> Bar<[i32; 3]>
840 289..314 'Bar(Fo... 3] })': Bar<[i32; 3]> 804 138..163 'Bar(Fo... 3] })': Bar<[i32; 3]>
841 293..313 'Foo { ..., 3] }': Foo<[i32; 3]> 805 142..162 'Foo { ..., 3] }': Foo<[i32; 3]>
842 302..311 '[1, 2, 3]': [i32; 3] 806 151..160 '[1, 2, 3]': [i32; 3]
843 303..304 '1': i32 807 152..153 '1': i32
844 306..307 '2': i32 808 155..156 '2': i32
845 309..310 '3': i32 809 158..159 '3': i32
846 248..257: expected [usize], got [usize; 3] 810 97..106: expected [usize], got [usize; 3]
847 288..314: expected &Bar<[usize]>, got &Bar<[i32; 3]> 811 137..163: expected &Bar<[usize]>, got &Bar<[i32; 3]>
848 "#]], 812 "#]],
849 ); 813 );
850} 814}
@@ -854,15 +818,7 @@ fn coerce_unsize_apit() {
854 // FIXME: #8984 818 // FIXME: #8984
855 check_infer_with_mismatches( 819 check_infer_with_mismatches(
856 r#" 820 r#"
857#[lang = "sized"] 821//- minicore: coerce_unsized
858pub trait Sized {}
859#[lang = "unsize"]
860pub trait Unsize<T> {}
861#[lang = "coerce_unsized"]
862pub trait CoerceUnsized<T> {}
863
864impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
865
866trait Foo {} 822trait Foo {}
867 823
868fn test(f: impl Foo) { 824fn test(f: impl Foo) {
@@ -870,12 +826,12 @@ fn test(f: impl Foo) {
870} 826}
871 "#, 827 "#,
872 expect![[r#" 828 expect![[r#"
873 210..211 'f': impl Foo 829 22..23 'f': impl Foo
874 223..252 '{ ... &f; }': () 830 35..64 '{ ... &f; }': ()
875 233..234 '_': &dyn Foo 831 45..46 '_': &dyn Foo
876 247..249 '&f': &impl Foo 832 59..61 '&f': &impl Foo
877 248..249 'f': impl Foo 833 60..61 'f': impl Foo
878 247..249: expected &dyn Foo, got &impl Foo 834 59..61: expected &dyn Foo, got &impl Foo
879 "#]], 835 "#]],
880 ); 836 );
881} 837}
@@ -971,15 +927,7 @@ fn main() {
971fn coerce_unsize_expected_type() { 927fn coerce_unsize_expected_type() {
972 check_no_mismatches( 928 check_no_mismatches(
973 r#" 929 r#"
974#[lang = "sized"] 930//- minicore: coerce_unsized
975pub trait Sized {}
976#[lang = "unsize"]
977pub trait Unsize<T> {}
978#[lang = "coerce_unsized"]
979pub trait CoerceUnsized<T> {}
980
981impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
982
983fn main() { 931fn main() {
984 let foo: &[u32] = &[1, 2]; 932 let foo: &[u32] = &[1, 2];
985 let foo: &[u32] = match true { 933 let foo: &[u32] = match true {