diff options
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 271 |
1 files changed, 215 insertions, 56 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 6076e4025..2ce0039b1 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -806,14 +806,14 @@ fn infer_argument_autoderef() { | |||
806 | infer(r#" | 806 | infer(r#" |
807 | #[lang = "deref"] | 807 | #[lang = "deref"] |
808 | pub trait Deref { | 808 | pub trait Deref { |
809 | type Target: ?Sized; | 809 | type Target; |
810 | fn deref(&self) -> &Self::Target; | 810 | fn deref(&self) -> &Self::Target; |
811 | } | 811 | } |
812 | 812 | ||
813 | struct A<T>(T); | 813 | struct A<T>(T); |
814 | 814 | ||
815 | impl<T: Copy> A<T> { | 815 | impl<T> A<T> { |
816 | fn foo(&self) -> T { | 816 | fn foo(&self) -> &T { |
817 | self.0 | 817 | self.0 |
818 | } | 818 | } |
819 | } | 819 | } |
@@ -828,32 +828,33 @@ impl<T> Deref for B<T> { | |||
828 | } | 828 | } |
829 | 829 | ||
830 | fn test() { | 830 | fn test() { |
831 | A::foo(&&B(B(A(42)))); | 831 | let t = A::foo(&&B(B(A(42)))); |
832 | } | 832 | } |
833 | "#), | 833 | "#), |
834 | @r###" | 834 | @r###" |
835 | [76; 80) 'self': &Self | 835 | [68; 72) 'self': &Self |
836 | [153; 157) 'self': &A<T> | 836 | [139; 143) 'self': &A<T> |
837 | [164; 186) '{ ... }': T | 837 | [151; 173) '{ ... }': T |
838 | [174; 178) 'self': &A<T> | 838 | [161; 165) 'self': &A<T> |
839 | [174; 180) 'self.0': T | 839 | [161; 167) 'self.0': T |
840 | [267; 271) 'self': &B<T> | 840 | [254; 258) 'self': &B<T> |
841 | [290; 313) '{ ... }': &T | 841 | [277; 300) '{ ... }': &T |
842 | [300; 307) '&self.0': &T | 842 | [287; 294) '&self.0': &T |
843 | [301; 305) 'self': &B<T> | 843 | [288; 292) 'self': &B<T> |
844 | [301; 307) 'self.0': T | 844 | [288; 294) 'self.0': T |
845 | [327; 357) '{ ...))); }': () | 845 | [314; 352) '{ ...))); }': () |
846 | [333; 339) 'A::foo': fn foo<i32>(&A<T>) -> T | 846 | [324; 325) 't': &i32 |
847 | [333; 354) 'A::foo...42))))': i32 | 847 | [328; 334) 'A::foo': fn foo<i32>(&A<T>) -> &T |
848 | [340; 353) '&&B(B(A(42)))': &&B<B<A<i32>>> | 848 | [328; 349) 'A::foo...42))))': &i32 |
849 | [341; 353) '&B(B(A(42)))': &B<B<A<i32>>> | 849 | [335; 348) '&&B(B(A(42)))': &&B<B<A<i32>>> |
850 | [342; 343) 'B': B<B<A<i32>>>(T) -> B<T> | 850 | [336; 348) '&B(B(A(42)))': &B<B<A<i32>>> |
851 | [342; 353) 'B(B(A(42)))': B<B<A<i32>>> | 851 | [337; 338) 'B': B<B<A<i32>>>(T) -> B<T> |
852 | [344; 345) 'B': B<A<i32>>(T) -> B<T> | 852 | [337; 348) 'B(B(A(42)))': B<B<A<i32>>> |
853 | [344; 352) 'B(A(42))': B<A<i32>> | 853 | [339; 340) 'B': B<A<i32>>(T) -> B<T> |
854 | [346; 347) 'A': A<i32>(T) -> A<T> | 854 | [339; 347) 'B(A(42))': B<A<i32>> |
855 | [346; 351) 'A(42)': A<i32> | 855 | [341; 342) 'A': A<i32>(T) -> A<T> |
856 | [348; 350) '42': i32 | 856 | [341; 346) 'A(42)': A<i32> |
857 | [343; 345) '42': i32 | ||
857 | "### | 858 | "### |
858 | ); | 859 | ); |
859 | } | 860 | } |
@@ -864,15 +865,15 @@ fn infer_method_argument_autoderef() { | |||
864 | infer(r#" | 865 | infer(r#" |
865 | #[lang = "deref"] | 866 | #[lang = "deref"] |
866 | pub trait Deref { | 867 | pub trait Deref { |
867 | type Target: ?Sized; | 868 | type Target; |
868 | fn deref(&self) -> &Self::Target; | 869 | fn deref(&self) -> &Self::Target; |
869 | } | 870 | } |
870 | 871 | ||
871 | struct A<T>(*mut T); | 872 | struct A<T>(*mut T); |
872 | 873 | ||
873 | impl<T: Copy> A<T> { | 874 | impl<T> A<T> { |
874 | fn foo(&self, x: &A<T>) -> T { | 875 | fn foo(&self, x: &A<T>) -> &T { |
875 | x | 876 | &*x.0 |
876 | } | 877 | } |
877 | } | 878 | } |
878 | 879 | ||
@@ -886,39 +887,197 @@ impl<T> Deref for B<T> { | |||
886 | } | 887 | } |
887 | 888 | ||
888 | fn test(a: A<i32>) { | 889 | fn test(a: A<i32>) { |
889 | A(0 as *mut _).foo(&&B(B(a))); | 890 | let t = A(0 as *mut _).foo(&&B(B(a))); |
891 | } | ||
892 | "#), | ||
893 | @r###" | ||
894 | [68; 72) 'self': &Self | ||
895 | [144; 148) 'self': &A<T> | ||
896 | [150; 151) 'x': &A<T> | ||
897 | [166; 187) '{ ... }': &T | ||
898 | [176; 181) '&*x.0': &T | ||
899 | [177; 181) '*x.0': T | ||
900 | [178; 179) 'x': &A<T> | ||
901 | [178; 181) 'x.0': *mut T | ||
902 | [268; 272) 'self': &B<T> | ||
903 | [291; 314) '{ ... }': &T | ||
904 | [301; 308) '&self.0': &T | ||
905 | [302; 306) 'self': &B<T> | ||
906 | [302; 308) 'self.0': T | ||
907 | [326; 327) 'a': A<i32> | ||
908 | [337; 383) '{ ...))); }': () | ||
909 | [347; 348) 't': &i32 | ||
910 | [351; 352) 'A': A<i32>(*mut T) -> A<T> | ||
911 | [351; 365) 'A(0 as *mut _)': A<i32> | ||
912 | [351; 380) 'A(0 as...B(a)))': &i32 | ||
913 | [353; 354) '0': i32 | ||
914 | [353; 364) '0 as *mut _': *mut i32 | ||
915 | [370; 379) '&&B(B(a))': &&B<B<A<i32>>> | ||
916 | [371; 379) '&B(B(a))': &B<B<A<i32>>> | ||
917 | [372; 373) 'B': B<B<A<i32>>>(T) -> B<T> | ||
918 | [372; 379) 'B(B(a))': B<B<A<i32>>> | ||
919 | [374; 375) 'B': B<A<i32>>(T) -> B<T> | ||
920 | [374; 378) 'B(a)': B<A<i32>> | ||
921 | [376; 377) 'a': A<i32> | ||
922 | "### | ||
923 | ); | ||
924 | } | ||
925 | |||
926 | #[test] | ||
927 | fn infer_if_coerce() { | ||
928 | assert_snapshot!( | ||
929 | infer(r#" | ||
930 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | ||
931 | fn test() { | ||
932 | let x = if true { | ||
933 | foo(&[1]) | ||
934 | } else { | ||
935 | &[1] | ||
936 | }; | ||
890 | } | 937 | } |
891 | "#), | 938 | "#), |
892 | @r###" | 939 | @r###" |
893 | [76; 80) 'self': &Self | 940 | [11; 12) 'x': &[T] |
894 | [158; 162) 'self': &A<T> | 941 | [28; 39) '{ loop {} }': ! |
895 | [164; 165) 'x': &A<T> | 942 | [30; 37) 'loop {}': ! |
896 | [179; 196) '{ ... }': &A<T> | 943 | [35; 37) '{}': () |
897 | [189; 190) 'x': &A<T> | 944 | [50; 126) '{ ... }; }': () |
898 | [277; 281) 'self': &B<T> | 945 | [60; 61) 'x': &[i32] |
899 | [300; 323) '{ ... }': &T | 946 | [64; 123) 'if tru... }': &[i32] |
900 | [310; 317) '&self.0': &T | 947 | [67; 71) 'true': bool |
901 | [311; 315) 'self': &B<T> | 948 | [72; 97) '{ ... }': &[i32] |
902 | [311; 317) 'self.0': T | 949 | [82; 85) 'foo': fn foo<i32>(&[T]) -> &[T] |
903 | [335; 336) 'a': A<i32> | 950 | [82; 91) 'foo(&[1])': &[i32] |
904 | [346; 384) '{ ...))); }': () | 951 | [86; 90) '&[1]': &[i32;_] |
905 | [352; 353) 'A': A<i32>(*mut T) -> A<T> | 952 | [87; 90) '[1]': [i32;_] |
906 | [352; 366) 'A(0 as *mut _)': A<i32> | 953 | [88; 89) '1': i32 |
907 | [352; 381) 'A(0 as...B(a)))': i32 | 954 | [103; 123) '{ ... }': &[i32;_] |
908 | [354; 355) '0': i32 | 955 | [113; 117) '&[1]': &[i32;_] |
909 | [354; 365) '0 as *mut _': *mut i32 | 956 | [114; 117) '[1]': [i32;_] |
910 | [371; 380) '&&B(B(a))': &&B<B<A<i32>>> | 957 | [115; 116) '1': i32 |
911 | [372; 380) '&B(B(a))': &B<B<A<i32>>> | ||
912 | [373; 374) 'B': B<B<A<i32>>>(T) -> B<T> | ||
913 | [373; 380) 'B(B(a))': B<B<A<i32>>> | ||
914 | [375; 376) 'B': B<A<i32>>(T) -> B<T> | ||
915 | [375; 379) 'B(a)': B<A<i32>> | ||
916 | [377; 378) 'a': A<i32> | ||
917 | "### | 958 | "### |
918 | ); | 959 | ); |
919 | } | 960 | } |
920 | 961 | ||
921 | #[test] | 962 | #[test] |
963 | fn infer_if_else_coerce() { | ||
964 | assert_snapshot!( | ||
965 | infer(r#" | ||
966 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | ||
967 | fn test() { | ||
968 | let x = if true { | ||
969 | &[1] | ||
970 | } else { | ||
971 | foo(&[1]) | ||
972 | }; | ||
973 | } | ||
974 | "#), | ||
975 | @r###" | ||
976 | [11; 12) 'x': &[T] | ||
977 | [28; 39) '{ loop {} }': ! | ||
978 | [30; 37) 'loop {}': ! | ||
979 | [35; 37) '{}': () | ||
980 | [50; 126) '{ ... }; }': () | ||
981 | [60; 61) 'x': &[i32] | ||
982 | [64; 123) 'if tru... }': &[i32] | ||
983 | [67; 71) 'true': bool | ||
984 | [72; 92) '{ ... }': &[i32;_] | ||
985 | [82; 86) '&[1]': &[i32;_] | ||
986 | [83; 86) '[1]': [i32;_] | ||
987 | [84; 85) '1': i32 | ||
988 | [98; 123) '{ ... }': &[i32] | ||
989 | [108; 111) 'foo': fn foo<i32>(&[T]) -> &[T] | ||
990 | [108; 117) 'foo(&[1])': &[i32] | ||
991 | [112; 116) '&[1]': &[i32;_] | ||
992 | [113; 116) '[1]': [i32;_] | ||
993 | [114; 115) '1': i32 | ||
994 | "### | ||
995 | ); | ||
996 | } | ||
997 | |||
998 | #[test] | ||
999 | fn infer_match_first_coerce() { | ||
1000 | assert_snapshot!( | ||
1001 | infer(r#" | ||
1002 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | ||
1003 | fn test(i: i32) { | ||
1004 | let x = match i { | ||
1005 | 2 => foo(&[2]), | ||
1006 | 1 => &[1], | ||
1007 | _ => &[3], | ||
1008 | }; | ||
1009 | } | ||
1010 | "#), | ||
1011 | @r###" | ||
1012 | [11; 12) 'x': &[T] | ||
1013 | [28; 39) '{ loop {} }': ! | ||
1014 | [30; 37) 'loop {}': ! | ||
1015 | [35; 37) '{}': () | ||
1016 | [48; 49) 'i': i32 | ||
1017 | [56; 150) '{ ... }; }': () | ||
1018 | [66; 67) 'x': &[i32] | ||
1019 | [70; 147) 'match ... }': &[i32] | ||
1020 | [76; 77) 'i': i32 | ||
1021 | [88; 89) '2': i32 | ||
1022 | [93; 96) 'foo': fn foo<i32>(&[T]) -> &[T] | ||
1023 | [93; 102) 'foo(&[2])': &[i32] | ||
1024 | [97; 101) '&[2]': &[i32;_] | ||
1025 | [98; 101) '[2]': [i32;_] | ||
1026 | [99; 100) '2': i32 | ||
1027 | [112; 113) '1': i32 | ||
1028 | [117; 121) '&[1]': &[i32;_] | ||
1029 | [118; 121) '[1]': [i32;_] | ||
1030 | [119; 120) '1': i32 | ||
1031 | [131; 132) '_': i32 | ||
1032 | [136; 140) '&[3]': &[i32;_] | ||
1033 | [137; 140) '[3]': [i32;_] | ||
1034 | [138; 139) '3': i32 | ||
1035 | "### | ||
1036 | ); | ||
1037 | } | ||
1038 | |||
1039 | #[test] | ||
1040 | fn infer_match_second_coerce() { | ||
1041 | assert_snapshot!( | ||
1042 | infer(r#" | ||
1043 | fn foo<T>(x: &[T]) -> &[T] { loop {} } | ||
1044 | fn test(i: i32) { | ||
1045 | let x = match i { | ||
1046 | 1 => &[1], | ||
1047 | 2 => foo(&[2]), | ||
1048 | _ => &[3], | ||
1049 | }; | ||
1050 | } | ||
1051 | "#), | ||
1052 | @r###" | ||
1053 | [11; 12) 'x': &[T] | ||
1054 | [28; 39) '{ loop {} }': ! | ||
1055 | [30; 37) 'loop {}': ! | ||
1056 | [35; 37) '{}': () | ||
1057 | [48; 49) 'i': i32 | ||
1058 | [56; 150) '{ ... }; }': () | ||
1059 | [66; 67) 'x': &[i32] | ||
1060 | [70; 147) 'match ... }': &[i32] | ||
1061 | [76; 77) 'i': i32 | ||
1062 | [88; 89) '1': i32 | ||
1063 | [93; 97) '&[1]': &[i32;_] | ||
1064 | [94; 97) '[1]': [i32;_] | ||
1065 | [95; 96) '1': i32 | ||
1066 | [107; 108) '2': i32 | ||
1067 | [112; 115) 'foo': fn foo<i32>(&[T]) -> &[T] | ||
1068 | [112; 121) 'foo(&[2])': &[i32] | ||
1069 | [116; 120) '&[2]': &[i32;_] | ||
1070 | [117; 120) '[2]': [i32;_] | ||
1071 | [118; 119) '2': i32 | ||
1072 | [131; 132) '_': i32 | ||
1073 | [136; 140) '&[3]': &[i32;_] | ||
1074 | [137; 140) '[3]': [i32;_] | ||
1075 | [138; 139) '3': i32 | ||
1076 | "### | ||
1077 | ); | ||
1078 | } | ||
1079 | |||
1080 | #[test] | ||
922 | fn bug_484() { | 1081 | fn bug_484() { |
923 | assert_snapshot!( | 1082 | assert_snapshot!( |
924 | infer(r#" | 1083 | infer(r#" |
@@ -3474,7 +3633,7 @@ trait Deref { | |||
3474 | } | 3633 | } |
3475 | 3634 | ||
3476 | struct Arc<T>; | 3635 | struct Arc<T>; |
3477 | impl<T: ?Sized> Deref for Arc<T> { | 3636 | impl<T> Deref for Arc<T> { |
3478 | type Target = T; | 3637 | type Target = T; |
3479 | } | 3638 | } |
3480 | 3639 | ||