| 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 /// Tests for type inference. | 5 /// Tests for type inference. |
| 6 library dev_compiler.test.inferred_type_test; | 6 library dev_compiler.test.inferred_type_test; |
| 7 | 7 |
| 8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
| 9 | 9 |
| 10 import 'package:dev_compiler/src/testing.dart'; | 10 import 'package:dev_compiler/src/testing.dart'; |
| (...skipping 1161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 class T { | 1172 class T { |
| 1173 static final T foo = m1(m2(m3('', ''))); | 1173 static final T foo = m1(m2(m3('', ''))); |
| 1174 static T m1(String m) { return null; } | 1174 static T m1(String m) { return null; } |
| 1175 static String m2(e) { return ''; } | 1175 static String m2(e) { return ''; } |
| 1176 } | 1176 } |
| 1177 | 1177 |
| 1178 | 1178 |
| 1179 ''' | 1179 ''' |
| 1180 }, inferFromOverrides: true, inferTransitively: true); | 1180 }, inferFromOverrides: true, inferTransitively: true); |
| 1181 }); | 1181 }); |
| 1182 |
| 1183 test('downwards inference: miscellaneous', () { |
| 1184 testChecker({ |
| 1185 '/main.dart': ''' |
| 1186 typedef (T x); |
| 1187 class A<T> { |
| 1188 Function2<T> x; |
| 1189 A(this.x); |
| 1190 } |
| 1191 void main() { |
| 1192 { // Variables, nested literals |
| 1193 var x = "hello"; |
| 1194 var y = 3; |
| 1195 void f(List<Map<int, String>> l) {}; |
| 1196 f(/*info:InferredTypeLiteral*/[{y: x}]); |
| 1197 } |
| 1198 { |
| 1199 int f(int x) {}; |
| 1200 A<int> = new A(f); |
| 1201 } |
| 1202 } |
| 1203 '''}, inferDownwards: true); |
| 1204 }); |
| 1205 |
| 1206 test('downwards inference on instance creations', () { |
| 1207 String mk(String error) => ''' |
| 1208 class A<S, T> { |
| 1209 S x; |
| 1210 T y; |
| 1211 A(this.x, this.y); |
| 1212 A.named(this.x, this.y); |
| 1213 } |
| 1214 |
| 1215 class B<S, T> extends A<T, S> { |
| 1216 B(S y, T x) : super(x, y); |
| 1217 B.named(S y, T x) : super.named(x, y); |
| 1218 } |
| 1219 |
| 1220 class C<S> extends B<S, S> { |
| 1221 C(S a) : super(a, a); |
| 1222 C.named(S a) : super.named(a, a); |
| 1223 } |
| 1224 |
| 1225 class D<S, T> extends B<T, int> { |
| 1226 D(T a) : super(a, 3); |
| 1227 D.named(T a) : super.named(a, 3); |
| 1228 } |
| 1229 |
| 1230 class E<S, T> extends A<C<S>, T> { |
| 1231 E(T a) : super(null, a); |
| 1232 } |
| 1233 |
| 1234 class F<S, T> extends A<S, T> { |
| 1235 F(S x, T y, {S a, T b}) : super(a, b); |
| 1236 F.named(S x, T y, [S a, T b]) : super(a, b); |
| 1237 } |
| 1238 |
| 1239 void main() { |
| 1240 { |
| 1241 A<int, String> a0 = /*$error*/new A(3, "hello"); |
| 1242 A<int, String> a1 = /*$error*/new A.named(3, "hello"); |
| 1243 A<int, String> a2 = new A<int, String>(3, "hello"); |
| 1244 A<int, String> a3 = new A<int, String>.named(3, "hello"); |
| 1245 A<int, String> a4 = /*warning:InferableAllocation*/new A<int, dynamic>
(3, "hello"); |
| 1246 A<int, String> a5 = /*warning:InferableAllocation*/new A<dynamic, dyna
mic>.named(3, "hello"); |
| 1247 } |
| 1248 { |
| 1249 A<int, String> a0 = /*warning:InferableAllocation*/new A("hello", 3); |
| 1250 A<int, String> a1 = /*warning:InferableAllocation*/new A.named("hello"
, 3); |
| 1251 } |
| 1252 { |
| 1253 A<int, String> a0 = /*$error*/new B("hello", 3); |
| 1254 A<int, String> a1 = /*$error*/new B.named("hello", 3); |
| 1255 A<int, String> a2 = new B<String, int>("hello", 3); |
| 1256 A<int, String> a3 = new B<String, int>.named("hello", 3); |
| 1257 A<int, String> a4 = /*warning:InferableAllocation*/new B<String, dynam
ic>("hello", 3); |
| 1258 A<int, String> a5 = /*warning:InferableAllocation*/new B<dynamic, dyna
mic>.named("hello", 3); |
| 1259 } |
| 1260 { |
| 1261 A<int, String> a0 = /*warning:InferableAllocation*/new B(3, "hello"); |
| 1262 A<int, String> a1 = /*warning:InferableAllocation*/new B.named(3, "hel
lo"); |
| 1263 } |
| 1264 { |
| 1265 A<int, int> a0 = /*$error*/new C(3); |
| 1266 A<int, int> a1 = /*$error*/new C.named(3); |
| 1267 A<int, int> a2 = new C<int>(3); |
| 1268 A<int, int> a3 = new C<int>.named(3); |
| 1269 A<int, int> a4 = /*warning:InferableAllocation*/new C<dynamic>(3); |
| 1270 A<int, int> a5 = /*warning:InferableAllocation*/new C<dynamic>.named(3
); |
| 1271 } |
| 1272 { |
| 1273 A<int, int> a0 = /*warning:InferableAllocation*/new C("hello"); |
| 1274 A<int, int> a1 = /*warning:InferableAllocation*/new C.named("hello"); |
| 1275 } |
| 1276 { |
| 1277 A<int, String> a0 = /*$error*/new D("hello"); |
| 1278 A<int, String> a1 = /*$error*/new D.named("hello"); |
| 1279 A<int, String> a2 = new D<int, String>("hello"); |
| 1280 A<int, String> a3 = new D<String, String>.named("hello"); |
| 1281 A<int, String> a4 = /*warning:InferableAllocation*/new D<num, dynamic>
("hello"); |
| 1282 A<int, String> a5 = /*warning:InferableAllocation*/new D<dynamic, dyna
mic>.named("hello"); |
| 1283 } |
| 1284 { |
| 1285 A<int, String> a0 = /*warning:InferableAllocation*/new D(3); |
| 1286 A<int, String> a1 = /*warning:InferableAllocation*/new D.named(3); |
| 1287 } |
| 1288 { // Currently we only allow variable constraints. Test that we reject. |
| 1289 A<C<int>, String> a0 = /*warning:InferableAllocation*/new E("hello"); |
| 1290 } |
| 1291 { // Check named and optional arguments |
| 1292 A<int, String> a0 = /*$error*/new F(3, "hello", a: 3, b: "hello"); |
| 1293 A<int, String> a1 = /*warning:InferableAllocation*/new F(3, "hello", a
:"hello", b:3); |
| 1294 A<int, String> a2 = /*$error*/new F.named(3, "hello", 3, "hello"); |
| 1295 A<int, String> a3 = /*$error*/new F.named(3, "hello"); |
| 1296 A<int, String> a4 = /*warning:InferableAllocation*/new F.named(3, "hel
lo", "hello", 3); |
| 1297 A<int, String> a5 = /*warning:InferableAllocation*/new F.named(3, "hel
lo", "hello"); |
| 1298 } |
| 1299 } |
| 1300 '''; |
| 1301 testChecker({'/main.dart': mk("info:InferredTypeExact")}, |
| 1302 inferDownwards: true); |
| 1303 testChecker({'/main.dart': mk("warning:InferableAllocation")}, |
| 1304 inferDownwards: false); |
| 1305 }); |
| 1306 |
| 1307 test('downwards inference on list literals', () { |
| 1308 String mk(String error) => ''' |
| 1309 List<int> l0 = /*$error*/[]; |
| 1310 List<int> l1 = /*$error*/[3]; |
| 1311 List<int> l2 = /*warning:InferableLiteral*/["hello"]; |
| 1312 List<int> l3 = /*warning:InferableLiteral*/["hello", 3]; |
| 1313 |
| 1314 List<dynamic> l0 = []; |
| 1315 List<dynamic> l1 = [3]; |
| 1316 List<dynamic> l2 = ["hello"]; |
| 1317 List<dynamic> l3 = ["hello", 3]; |
| 1318 |
| 1319 List<int> l0 = /*warning:InferableLiteral*/<num>[]; |
| 1320 List<int> l1 = /*warning:InferableLiteral*/<num>[3]; |
| 1321 List<int> l2 = /*warning:InferableLiteral*/<num>[/*severe:StaticTypeError*
/"hello"]; |
| 1322 List<int> l3 = /*warning:InferableLiteral*/<num>[/*severe:StaticTypeError*
/"hello", 3]; |
| 1323 |
| 1324 Iterable<int> i0 = /*$error*/[]; |
| 1325 Iterable<int> i1 = /*$error*/[3]; |
| 1326 Iterable<int> i2 = /*warning:InferableLiteral*/["hello"]; |
| 1327 Iterable<int> i3 = /*warning:InferableLiteral*/["hello", 3]; |
| 1328 |
| 1329 const List<int> c0 = /*$error*/const []; |
| 1330 const List<int> c1 = /*$error*/const [3]; |
| 1331 const List<int> c2 = /*warning:InferableLiteral*/const ["hello"]; |
| 1332 const List<int> c3 = /*warning:InferableLiteral*/const ["hello", 3]; |
| 1333 '''; |
| 1334 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, |
| 1335 inferDownwards: true); |
| 1336 testChecker({'/main.dart': mk("warning:InferableLiteral")}, |
| 1337 inferDownwards: false); |
| 1338 }); |
| 1339 |
| 1340 test('downwards inference on function arguments', () { |
| 1341 String mk(String error) => ''' |
| 1342 void f0(List<int> a) {}; |
| 1343 void f1({List<int> a}) {}; |
| 1344 void f2(Iterable<int> a) {}; |
| 1345 void f3(Iterable<Iterable<int>> a) {}; |
| 1346 void main() { |
| 1347 f0(/*$error*/[]); |
| 1348 f0(/*$error*/[3]); |
| 1349 f0(/*warning:InferableLiteral*/["hello"]); |
| 1350 f0(/*warning:InferableLiteral*/["hello", 3]); |
| 1351 |
| 1352 f1(a: /*$error*/[]); |
| 1353 f1(a: /*$error*/[3]); |
| 1354 f1(a: /*warning:InferableLiteral*/["hello"]); |
| 1355 f1(a: /*warning:InferableLiteral*/["hello", 3]); |
| 1356 |
| 1357 f2(/*$error*/[]); |
| 1358 f2(/*$error*/[3]); |
| 1359 f2(/*warning:InferableLiteral*/["hello"]); |
| 1360 f2(/*warning:InferableLiteral*/["hello", 3]); |
| 1361 |
| 1362 f3(/*$error*/[]); |
| 1363 f3(/*$error*/[[3]]); |
| 1364 f3(/*warning:InferableLiteral*/[["hello"]]); |
| 1365 f3(/*warning:InferableLiteral*/[["hello"], [3]]); |
| 1366 } |
| 1367 '''; |
| 1368 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, |
| 1369 inferDownwards: true); |
| 1370 testChecker({'/main.dart': mk("warning:InferableLiteral")}, |
| 1371 inferDownwards: false); |
| 1372 }); |
| 1373 |
| 1374 test('downwards inference on map literals', () { |
| 1375 String mk(String error) => ''' |
| 1376 Map<int, String> l0 = /*$error*/{}; |
| 1377 Map<int, String> l1 = /*$error*/{3: "hello"}; |
| 1378 Map<int, String> l2 = /*warning:InferableLiteral*/{"hello": "hello"}; |
| 1379 Map<int, String> l3 = /*warning:InferableLiteral*/{3: 3}; |
| 1380 Map<int, String> l4 = /*warning:InferableLiteral*/{3:"hello", "hello": 3}; |
| 1381 |
| 1382 Map<dynamic, dynamic> l0 = {}; |
| 1383 Map<dynamic, dynamic> l1 = {3: "hello"}; |
| 1384 Map<dynamic, dynamic> l2 = {"hello": "hello"}; |
| 1385 Map<dynamic, dynamic> l3 = {3: 3}; |
| 1386 Map<dynamic, dynamic> l4 = {3:"hello", "hello": 3}; |
| 1387 |
| 1388 Map<dynamic, String> l0 = /*$error*/{}; |
| 1389 Map<dynamic, String> l1 = /*$error*/{3: "hello"}; |
| 1390 Map<dynamic, String> l2 = /*$error*/{"hello": "hello"}; |
| 1391 Map<dynamic, String> l3 = /*warning:InferableLiteral*/{3: 3}; |
| 1392 Map<dynamic, String> l4 = /*warning:InferableLiteral*/{3:"hello", "hello":
3}; |
| 1393 |
| 1394 Map<int, dynamic> l0 = /*$error*/{}; |
| 1395 Map<int, dynamic> l1 = /*$error*/{3: "hello"}; |
| 1396 Map<int, dynamic> l2 = /*warning:InferableLiteral*/{"hello": "hello"}; |
| 1397 Map<int, dynamic> l3 = /*$error*/{3: 3}; |
| 1398 Map<int, dynamic> l3 = /*warning:InferableLiteral*/{3:"hello", "hello": 3}
; |
| 1399 |
| 1400 Map<int, String> l0 = /*warning:InferableLiteral*/<num, dynamic>{}; |
| 1401 Map<int, String> l1 = /*warning:InferableLiteral*/<num, dynamic>{3: "hello
"}; |
| 1402 Map<int, String> l3 = /*warning:InferableLiteral*/<num, dynamic>{3: 3}; |
| 1403 |
| 1404 const Map<int, String> l0 = /*$error*/const {}; |
| 1405 const Map<int, String> l1 = /*$error*/const {3: "hello"}; |
| 1406 const Map<int, String> l2 = /*warning:InferableLiteral*/const {"hello": "h
ello"}; |
| 1407 const Map<int, String> l3 = /*warning:InferableLiteral*/const {3: 3}; |
| 1408 const Map<int, String> l4 = /*warning:InferableLiteral*/const {3:"hello",
"hello": 3}; |
| 1409 '''; |
| 1410 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, |
| 1411 inferDownwards: true); |
| 1412 testChecker({'/main.dart': mk("warning:InferableLiteral")}, |
| 1413 inferDownwards: false); |
| 1414 }); |
| 1182 } | 1415 } |
| OLD | NEW |