| 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 1196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1207 class T { | 1207 class T { |
| 1208 static final T foo = m1(m2(m3('', ''))); | 1208 static final T foo = m1(m2(m3('', ''))); |
| 1209 static T m1(String m) { return null; } | 1209 static T m1(String m) { return null; } |
| 1210 static String m2(e) { return ''; } | 1210 static String m2(e) { return ''; } |
| 1211 } | 1211 } |
| 1212 | 1212 |
| 1213 | 1213 |
| 1214 ''' | 1214 ''' |
| 1215 }, inferFromOverrides: true, inferTransitively: true); | 1215 }, inferFromOverrides: true, inferTransitively: true); |
| 1216 }); | 1216 }); |
| 1217 |
| 1218 test('downwards inference: miscellaneous', () { |
| 1219 testChecker({ |
| 1220 '/main.dart': ''' |
| 1221 typedef (T x); |
| 1222 class A<T> { |
| 1223 Function2<T> x; |
| 1224 A(this.x); |
| 1225 } |
| 1226 void main() { |
| 1227 { // Variables, nested literals |
| 1228 var x = "hello"; |
| 1229 var y = 3; |
| 1230 void f(List<Map<int, String>> l) {}; |
| 1231 f(/*info:InferredTypeLiteral*/[{y: x}]); |
| 1232 } |
| 1233 { |
| 1234 int f(int x) {}; |
| 1235 A<int> = new A(f); |
| 1236 } |
| 1237 } |
| 1238 ''' |
| 1239 }, inferDownwards: true); |
| 1240 }); |
| 1241 |
| 1242 test('downwards inference on instance creations', () { |
| 1243 String mk(String error) => ''' |
| 1244 class A<S, T> { |
| 1245 S x; |
| 1246 T y; |
| 1247 A(this.x, this.y); |
| 1248 A.named(this.x, this.y); |
| 1249 } |
| 1250 |
| 1251 class B<S, T> extends A<T, S> { |
| 1252 B(S y, T x) : super(x, y); |
| 1253 B.named(S y, T x) : super.named(x, y); |
| 1254 } |
| 1255 |
| 1256 class C<S> extends B<S, S> { |
| 1257 C(S a) : super(a, a); |
| 1258 C.named(S a) : super.named(a, a); |
| 1259 } |
| 1260 |
| 1261 class D<S, T> extends B<T, int> { |
| 1262 D(T a) : super(a, 3); |
| 1263 D.named(T a) : super.named(a, 3); |
| 1264 } |
| 1265 |
| 1266 class E<S, T> extends A<C<S>, T> { |
| 1267 E(T a) : super(null, a); |
| 1268 } |
| 1269 |
| 1270 class F<S, T> extends A<S, T> { |
| 1271 F(S x, T y, {List<S> a, List<T> b}) : super(x, y); |
| 1272 F.named(S x, T y, [S a, T b]) : super(a, b); |
| 1273 } |
| 1274 |
| 1275 void main() { |
| 1276 { |
| 1277 A<int, String> a0 = /*$error*/new A(3, "hello"); |
| 1278 A<int, String> a1 = /*$error*/new A.named(3, "hello"); |
| 1279 A<int, String> a2 = new A<int, String>(3, "hello"); |
| 1280 A<int, String> a3 = new A<int, String>.named(3, "hello"); |
| 1281 A<int, String> a4 = /*severe:StaticTypeError*/new A<int, dynamic>(3, "
hello"); |
| 1282 A<int, String> a5 = /*severe:StaticTypeError*/new A<dynamic, dynamic>.
named(3, "hello"); |
| 1283 } |
| 1284 { |
| 1285 A<int, String> a0 = /*severe:StaticTypeError*/new A("hello", 3); |
| 1286 A<int, String> a1 = /*severe:StaticTypeError*/new A.named("hello", 3); |
| 1287 } |
| 1288 { |
| 1289 A<int, String> a0 = /*$error*/new B("hello", 3); |
| 1290 A<int, String> a1 = /*$error*/new B.named("hello", 3); |
| 1291 A<int, String> a2 = new B<String, int>("hello", 3); |
| 1292 A<int, String> a3 = new B<String, int>.named("hello", 3); |
| 1293 A<int, String> a4 = /*severe:StaticTypeError*/new B<String, dynamic>("
hello", 3); |
| 1294 A<int, String> a5 = /*severe:StaticTypeError*/new B<dynamic, dynamic>.
named("hello", 3); |
| 1295 } |
| 1296 { |
| 1297 A<int, String> a0 = /*severe:StaticTypeError*/new B(3, "hello"); |
| 1298 A<int, String> a1 = /*severe:StaticTypeError*/new B.named(3, "hello"); |
| 1299 } |
| 1300 { |
| 1301 A<int, int> a0 = /*$error*/new C(3); |
| 1302 A<int, int> a1 = /*$error*/new C.named(3); |
| 1303 A<int, int> a2 = new C<int>(3); |
| 1304 A<int, int> a3 = new C<int>.named(3); |
| 1305 A<int, int> a4 = /*severe:StaticTypeError*/new C<dynamic>(3); |
| 1306 A<int, int> a5 = /*severe:StaticTypeError*/new C<dynamic>.named(3); |
| 1307 } |
| 1308 { |
| 1309 A<int, int> a0 = /*severe:StaticTypeError*/new C("hello"); |
| 1310 A<int, int> a1 = /*severe:StaticTypeError*/new C.named("hello"); |
| 1311 } |
| 1312 { |
| 1313 A<int, String> a0 = /*$error*/new D("hello"); |
| 1314 A<int, String> a1 = /*$error*/new D.named("hello"); |
| 1315 A<int, String> a2 = new D<int, String>("hello"); |
| 1316 A<int, String> a3 = new D<String, String>.named("hello"); |
| 1317 A<int, String> a4 = /*severe:StaticTypeError*/new D<num, dynamic>("hel
lo"); |
| 1318 A<int, String> a5 = /*severe:StaticTypeError*/new D<dynamic, dynamic>.
named("hello"); |
| 1319 } |
| 1320 { |
| 1321 A<int, String> a0 = /*severe:StaticTypeError*/new D(3); |
| 1322 A<int, String> a1 = /*severe:StaticTypeError*/new D.named(3); |
| 1323 } |
| 1324 { // Currently we only allow variable constraints. Test that we reject. |
| 1325 A<C<int>, String> a0 = /*severe:StaticTypeError*/new E("hello"); |
| 1326 } |
| 1327 { // Check named and optional arguments |
| 1328 A<int, String> a0 = /*$error*/new F(3, "hello", a: [3], b: ["hello"]); |
| 1329 A<int, String> a1 = /*severe:StaticTypeError*/new F(3, "hello", a: ["h
ello"], b:[3]); |
| 1330 A<int, String> a2 = /*$error*/new F.named(3, "hello", 3, "hello"); |
| 1331 A<int, String> a3 = /*$error*/new F.named(3, "hello"); |
| 1332 A<int, String> a4 = /*severe:StaticTypeError*/new F.named(3, "hello",
"hello", 3); |
| 1333 A<int, String> a5 = /*severe:StaticTypeError*/new F.named(3, "hello",
"hello"); |
| 1334 } |
| 1335 } |
| 1336 '''; |
| 1337 testChecker({'/main.dart': mk("info:InferredTypeAllocation")}, |
| 1338 inferDownwards: true); |
| 1339 testChecker({'/main.dart': mk("severe:StaticTypeError")}, |
| 1340 inferDownwards: false); |
| 1341 }); |
| 1342 |
| 1343 test('downwards inference on list literals', () { |
| 1344 String mk(String error) => ''' |
| 1345 List<int> l0 = /*$error*/[]; |
| 1346 List<int> l1 = /*$error*/[3]; |
| 1347 List<int> l2 = /*severe:StaticTypeError*/["hello"]; |
| 1348 List<int> l3 = /*severe:StaticTypeError*/["hello", 3]; |
| 1349 |
| 1350 List<dynamic> l0 = []; |
| 1351 List<dynamic> l1 = [3]; |
| 1352 List<dynamic> l2 = ["hello"]; |
| 1353 List<dynamic> l3 = ["hello", 3]; |
| 1354 |
| 1355 List<int> l0 = /*severe:StaticTypeError*/<num>[]; |
| 1356 List<int> l1 = /*severe:StaticTypeError*/<num>[3]; |
| 1357 List<int> l2 = /*severe:StaticTypeError*/<num>[/*severe:StaticTypeError*/"
hello"]; |
| 1358 List<int> l3 = /*severe:StaticTypeError*/<num>[/*severe:StaticTypeError*/"
hello", 3]; |
| 1359 |
| 1360 Iterable<int> i0 = /*$error*/[]; |
| 1361 Iterable<int> i1 = /*$error*/[3]; |
| 1362 Iterable<int> i2 = /*severe:StaticTypeError*/["hello"]; |
| 1363 Iterable<int> i3 = /*severe:StaticTypeError*/["hello", 3]; |
| 1364 |
| 1365 const List<int> c0 = /*$error*/const []; |
| 1366 const List<int> c1 = /*$error*/const [3]; |
| 1367 const List<int> c2 = /*severe:StaticTypeError*/const ["hello"]; |
| 1368 const List<int> c3 = /*severe:StaticTypeError*/const ["hello", 3]; |
| 1369 '''; |
| 1370 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, |
| 1371 inferDownwards: true); |
| 1372 testChecker({'/main.dart': mk("severe:StaticTypeError")}, |
| 1373 inferDownwards: false); |
| 1374 }); |
| 1375 |
| 1376 test('downwards inference on function arguments', () { |
| 1377 String mk(String error) => ''' |
| 1378 void f0(List<int> a) {}; |
| 1379 void f1({List<int> a}) {}; |
| 1380 void f2(Iterable<int> a) {}; |
| 1381 void f3(Iterable<Iterable<int>> a) {}; |
| 1382 void f4({Iterable<Iterable<int>> a}) {}; |
| 1383 void main() { |
| 1384 f0(/*$error*/[]); |
| 1385 f0(/*$error*/[3]); |
| 1386 f0(/*severe:StaticTypeError*/["hello"]); |
| 1387 f0(/*severe:StaticTypeError*/["hello", 3]); |
| 1388 |
| 1389 f1(a: /*$error*/[]); |
| 1390 f1(a: /*$error*/[3]); |
| 1391 f1(a: /*severe:StaticTypeError*/["hello"]); |
| 1392 f1(a: /*severe:StaticTypeError*/["hello", 3]); |
| 1393 |
| 1394 f2(/*$error*/[]); |
| 1395 f2(/*$error*/[3]); |
| 1396 f2(/*severe:StaticTypeError*/["hello"]); |
| 1397 f2(/*severe:StaticTypeError*/["hello", 3]); |
| 1398 |
| 1399 f3(/*$error*/[]); |
| 1400 f3(/*$error*/[[3]]); |
| 1401 f3(/*severe:StaticTypeError*/[["hello"]]); |
| 1402 f3(/*severe:StaticTypeError*/[["hello"], [3]]); |
| 1403 |
| 1404 f4(a: /*$error*/[]); |
| 1405 f4(a: /*$error*/[[3]]); |
| 1406 f4(a: /*severe:StaticTypeError*/[["hello"]]); |
| 1407 f4(a: /*severe:StaticTypeError*/[["hello"], [3]]); |
| 1408 } |
| 1409 '''; |
| 1410 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, |
| 1411 inferDownwards: true); |
| 1412 testChecker({'/main.dart': mk("severe:StaticTypeError")}, |
| 1413 inferDownwards: false); |
| 1414 }); |
| 1415 |
| 1416 test('downwards inference on map literals', () { |
| 1417 String mk(String error) => ''' |
| 1418 Map<int, String> l0 = /*$error*/{}; |
| 1419 Map<int, String> l1 = /*$error*/{3: "hello"}; |
| 1420 Map<int, String> l2 = /*severe:StaticTypeError*/{"hello": "hello"}; |
| 1421 Map<int, String> l3 = /*severe:StaticTypeError*/{3: 3}; |
| 1422 Map<int, String> l4 = /*severe:StaticTypeError*/{3:"hello", "hello": 3}; |
| 1423 |
| 1424 Map<dynamic, dynamic> l0 = {}; |
| 1425 Map<dynamic, dynamic> l1 = {3: "hello"}; |
| 1426 Map<dynamic, dynamic> l2 = {"hello": "hello"}; |
| 1427 Map<dynamic, dynamic> l3 = {3: 3}; |
| 1428 Map<dynamic, dynamic> l4 = {3:"hello", "hello": 3}; |
| 1429 |
| 1430 Map<dynamic, String> l0 = /*$error*/{}; |
| 1431 Map<dynamic, String> l1 = /*$error*/{3: "hello"}; |
| 1432 Map<dynamic, String> l2 = /*$error*/{"hello": "hello"}; |
| 1433 Map<dynamic, String> l3 = /*severe:StaticTypeError*/{3: 3}; |
| 1434 Map<dynamic, String> l4 = /*severe:StaticTypeError*/{3:"hello", "hello": 3
}; |
| 1435 |
| 1436 Map<int, dynamic> l0 = /*$error*/{}; |
| 1437 Map<int, dynamic> l1 = /*$error*/{3: "hello"}; |
| 1438 Map<int, dynamic> l2 = /*severe:StaticTypeError*/{"hello": "hello"}; |
| 1439 Map<int, dynamic> l3 = /*$error*/{3: 3}; |
| 1440 Map<int, dynamic> l3 = /*severe:StaticTypeError*/{3:"hello", "hello": 3}; |
| 1441 |
| 1442 Map<int, String> l0 = /*severe:StaticTypeError*/<num, dynamic>{}; |
| 1443 Map<int, String> l1 = /*severe:StaticTypeError*/<num, dynamic>{3: "hello"}
; |
| 1444 Map<int, String> l3 = /*severe:StaticTypeError*/<num, dynamic>{3: 3}; |
| 1445 |
| 1446 const Map<int, String> l0 = /*$error*/const {}; |
| 1447 const Map<int, String> l1 = /*$error*/const {3: "hello"}; |
| 1448 const Map<int, String> l2 = /*severe:StaticTypeError*/const {"hello": "hel
lo"}; |
| 1449 const Map<int, String> l3 = /*severe:StaticTypeError*/const {3: 3}; |
| 1450 const Map<int, String> l4 = /*severe:StaticTypeError*/const {3:"hello", "h
ello": 3}; |
| 1451 '''; |
| 1452 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, |
| 1453 inferDownwards: true); |
| 1454 testChecker({'/main.dart': mk("severe:StaticTypeError")}, |
| 1455 inferDownwards: false); |
| 1456 }); |
| 1217 } | 1457 } |
| OLD | NEW |