| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// General type checking tests | 5 /// General type checking tests |
| 6 library dev_compiler.test.checker_test; | 6 library dev_compiler.test.checker_test; |
| 7 | 7 |
| 8 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 | 9 |
| 10 import '../testing.dart'; | 10 import '../testing.dart'; |
| (...skipping 918 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 929 testChecker('Function typing and subtyping: void', { | 929 testChecker('Function typing and subtyping: void', { |
| 930 '/main.dart': ''' | 930 '/main.dart': ''' |
| 931 | 931 |
| 932 class A { | 932 class A { |
| 933 void bar() => null; | 933 void bar() => null; |
| 934 void foo() => bar; // allowed | 934 void foo() => bar; // allowed |
| 935 } | 935 } |
| 936 ''' | 936 ''' |
| 937 }); | 937 }); |
| 938 | 938 |
| 939 testChecker( | 939 testChecker('Relaxed casts', { |
| 940 'Covariant generic subtyping: invariance', | 940 '/main.dart': ''' |
| 941 { | |
| 942 '/main.dart': ''' | |
| 943 | |
| 944 class A {} | |
| 945 class B extends A {} | |
| 946 class C implements A {} | |
| 947 | |
| 948 class L<T> {} | |
| 949 class M<T> extends L<T> {} | |
| 950 class N extends M<A> {} | |
| 951 | |
| 952 void main() { | |
| 953 L<A> lOfAs; | |
| 954 L<B> lOfBs; | |
| 955 L<C> lOfCs; | |
| 956 | |
| 957 M<A> mOfAs; | |
| 958 M<B> mOfBs; | |
| 959 M<C> mOfCs; | |
| 960 | |
| 961 N ns; | |
| 962 | |
| 963 // L<T> <: L<S> iff S <: T | |
| 964 lOfAs = lOfAs; | |
| 965 lOfAs = lOfBs; | |
| 966 lOfAs = lOfCs; | |
| 967 | |
| 968 // M<T> <: L<S> iff T <: S | |
| 969 lOfAs = mOfAs; | |
| 970 lOfAs = mOfBs; | |
| 971 lOfAs = mOfCs; | |
| 972 | |
| 973 // N <: L<A> | |
| 974 lOfAs = ns; | |
| 975 | |
| 976 // L<T> <: L<S> iff S <: T | |
| 977 lOfBs = /*warning:DownCastComposite*/lOfAs; | |
| 978 lOfBs = lOfBs; | |
| 979 lOfBs = /*severe:StaticTypeError*/lOfCs; | |
| 980 | |
| 981 // M<T> <: L<S> iff T <: S | |
| 982 lOfBs = /*severe:StaticTypeError*/mOfAs; | |
| 983 lOfBs = mOfBs; | |
| 984 lOfBs = /*severe:StaticTypeError*/mOfCs; | |
| 985 | |
| 986 // N </: L<B> | |
| 987 lOfBs = /*severe:StaticTypeError*/ns; | |
| 988 | |
| 989 // L<T> <: L<S> iff S <: T | |
| 990 lOfCs = /*warning:DownCastComposite*/lOfAs; | |
| 991 lOfCs = /*severe:StaticTypeError*/lOfBs; | |
| 992 lOfCs = lOfCs; | |
| 993 | |
| 994 // M<T> <: L<S> iff T <: S | |
| 995 lOfCs = /*severe:StaticTypeError*/mOfAs; | |
| 996 lOfCs = /*severe:StaticTypeError*/mOfBs; | |
| 997 lOfCs = mOfCs; | |
| 998 | |
| 999 // N </: L<C> | |
| 1000 lOfCs = /*severe:StaticTypeError*/ns; | |
| 1001 | |
| 1002 // M<T> <: L<S> iff T <: S | |
| 1003 mOfAs = /*warning:DownCastComposite*/lOfAs; | |
| 1004 mOfAs = /*severe:StaticTypeError*/lOfBs; | |
| 1005 mOfAs = /*severe:StaticTypeError*/lOfCs; | |
| 1006 | |
| 1007 // M<T> <: M<S> iff T <: S | |
| 1008 mOfAs = mOfAs; | |
| 1009 mOfAs = mOfBs; | |
| 1010 mOfAs = mOfCs; | |
| 1011 | |
| 1012 // N <: M<A> | |
| 1013 mOfAs = ns; | |
| 1014 | |
| 1015 // M<T> <: L<S> iff T <: S | |
| 1016 mOfBs = /*warning:DownCastComposite*/lOfAs; | |
| 1017 mOfBs = /*warning:DownCastComposite*/lOfBs; | |
| 1018 mOfBs = /*severe:StaticTypeError*/lOfCs; | |
| 1019 | |
| 1020 // M<T> <: M<S> iff T <: S | |
| 1021 mOfBs = /*warning:DownCastComposite*/mOfAs; | |
| 1022 mOfBs = mOfBs; | |
| 1023 mOfBs = /*severe:StaticTypeError*/mOfCs; | |
| 1024 | |
| 1025 // N </: M<B> | |
| 1026 mOfBs = /*severe:StaticTypeError*/ns; | |
| 1027 | |
| 1028 // M<T> <: L<S> iff T <: S | |
| 1029 mOfCs = /*warning:DownCastComposite*/lOfAs; | |
| 1030 mOfCs = /*severe:StaticTypeError*/lOfBs; | |
| 1031 mOfCs = /*warning:DownCastComposite*/lOfCs; | |
| 1032 | |
| 1033 // M<T> <: M<S> iff T :< S | |
| 1034 mOfCs = /*warning:DownCastComposite*/mOfAs; | |
| 1035 mOfCs = /*severe:StaticTypeError*/mOfBs; | |
| 1036 mOfCs = mOfCs; | |
| 1037 | |
| 1038 // N </: L<C> | |
| 1039 mOfCs = /*severe:StaticTypeError*/ns; | |
| 1040 | |
| 1041 // Concrete subclass subtyping | |
| 1042 ns = /*info:DownCastImplicit*/lOfAs; | |
| 1043 ns = /*severe:StaticTypeError*/lOfBs; | |
| 1044 ns = /*severe:StaticTypeError*/lOfCs; | |
| 1045 ns = /*info:DownCastImplicit*/mOfAs; | |
| 1046 ns = /*severe:StaticTypeError*/mOfBs; | |
| 1047 ns = /*severe:StaticTypeError*/mOfCs; | |
| 1048 ns = ns; | |
| 1049 } | |
| 1050 ''' | |
| 1051 }, | |
| 1052 relaxedCasts: false); | |
| 1053 | |
| 1054 testChecker( | |
| 1055 'Relaxed casts', | |
| 1056 { | |
| 1057 '/main.dart': ''' | |
| 1058 | 941 |
| 1059 class A {} | 942 class A {} |
| 1060 | 943 |
| 1061 class L<T> {} | 944 class L<T> {} |
| 1062 class M<T> extends L<T> {} | 945 class M<T> extends L<T> {} |
| 1063 // L<dynamic|Object> | 946 // L<dynamic|Object> |
| 1064 // / \ | 947 // / \ |
| 1065 // M<dynamic|Object> L<A> | 948 // M<dynamic|Object> L<A> |
| 1066 // \ / | 949 // \ / |
| 1067 // M<A> | 950 // M<A> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 mOfAs = /*warning:DownCastComposite*/mOfDs; | 1005 mOfAs = /*warning:DownCastComposite*/mOfDs; |
| 1123 mOfAs = /*warning:DownCastComposite*/mOfOs; | 1006 mOfAs = /*warning:DownCastComposite*/mOfOs; |
| 1124 mOfAs = mOfAs; | 1007 mOfAs = mOfAs; |
| 1125 mOfAs = /*warning:DownCastComposite*/lOfDs; | 1008 mOfAs = /*warning:DownCastComposite*/lOfDs; |
| 1126 mOfAs = /*warning:DownCastComposite*/lOfOs; | 1009 mOfAs = /*warning:DownCastComposite*/lOfOs; |
| 1127 mOfAs = /*warning:DownCastComposite*/lOfAs; | 1010 mOfAs = /*warning:DownCastComposite*/lOfAs; |
| 1128 } | 1011 } |
| 1129 | 1012 |
| 1130 } | 1013 } |
| 1131 ''' | 1014 ''' |
| 1132 }, | 1015 }); |
| 1133 relaxedCasts: true); | |
| 1134 testChecker( | |
| 1135 'Subtyping literals', | |
| 1136 { | |
| 1137 '/main.dart': ''' | |
| 1138 test() { | |
| 1139 Iterable i1 = [1, 2, 3]; | |
| 1140 i1 = <int>[1, 2, 3]; | |
| 1141 | |
| 1142 List l1 = [1, 2, 3]; | |
| 1143 l1 = <int>[1, 2, 3]; | |
| 1144 | |
| 1145 Iterable<int> i2 = /*severe:StaticTypeError*/[1, 2, 3]; | |
| 1146 i2 = /*warning:DownCastComposite*/i1; | |
| 1147 i2 = /*warning:DownCastComposite*/l1; | |
| 1148 i2 = <int>[1, 2, 3]; | |
| 1149 | |
| 1150 List<int> l2 = /*severe:StaticTypeError*/[1, 2, 3]; | |
| 1151 l2 = /*warning:DownCastComposite*/i1; | |
| 1152 l2 = /*warning:DownCastComposite*/l1; | |
| 1153 | |
| 1154 l2 = /*severe:StaticTypeError*/new List(); | |
| 1155 l2 = /*severe:StaticTypeError*/new List(10); | |
| 1156 l2 = /*severe:StaticTypeError*/new List.filled(10, 42); | |
| 1157 } | |
| 1158 ''' | |
| 1159 }, | |
| 1160 inferDownwards: false); | |
| 1161 | 1016 |
| 1162 testChecker('Type checking literals', { | 1017 testChecker('Type checking literals', { |
| 1163 '/main.dart': ''' | 1018 '/main.dart': ''' |
| 1164 test() { | 1019 test() { |
| 1165 num n = 3; | 1020 num n = 3; |
| 1166 int i = 3; | 1021 int i = 3; |
| 1167 String s = "hello"; | 1022 String s = "hello"; |
| 1168 { | 1023 { |
| 1169 List<int> l = <int>[i]; | 1024 List<int> l = <int>[i]; |
| 1170 l = <int>[/*severe:StaticTypeError*/s]; | 1025 l = <int>[/*severe:StaticTypeError*/s]; |
| (...skipping 1284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2455 | 2310 |
| 2456 baz1() sync* { yield* (/*info:DynamicCast*/x); } | 2311 baz1() sync* { yield* (/*info:DynamicCast*/x); } |
| 2457 Iterable baz2() sync* { yield* (/*info:DynamicCast*/x); } | 2312 Iterable baz2() sync* { yield* (/*info:DynamicCast*/x); } |
| 2458 Iterable<int> baz3() sync* { yield* (/*warning:DownCastComposite*/x); } | 2313 Iterable<int> baz3() sync* { yield* (/*warning:DownCastComposite*/x); } |
| 2459 Iterable<int> baz4() sync* { yield* new Iterable<int>(); } | 2314 Iterable<int> baz4() sync* { yield* new Iterable<int>(); } |
| 2460 Iterable<int> baz5() sync* { yield* (/*info:InferredTypeAllocation*/new
Iterable()); } | 2315 Iterable<int> baz5() sync* { yield* (/*info:InferredTypeAllocation*/new
Iterable()); } |
| 2461 ''' | 2316 ''' |
| 2462 }); | 2317 }); |
| 2463 }); | 2318 }); |
| 2464 } | 2319 } |
| OLD | NEW |