Chromium Code Reviews| 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 ''' | |
| 1204 }, inferDownwards: true); | |
| 1205 }); | |
| 1206 | |
| 1207 test('downwards inference on instance creations', () { | |
| 1208 String mk(String error) => ''' | |
| 1209 class A<S, T> { | |
| 1210 S x; | |
| 1211 T y; | |
| 1212 A(this.x, this.y); | |
| 1213 A.named(this.x, this.y); | |
| 1214 } | |
| 1215 | |
| 1216 class B<S, T> extends A<T, S> { | |
| 1217 B(S y, T x) : super(x, y); | |
| 1218 B.named(S y, T x) : super.named(x, y); | |
| 1219 } | |
| 1220 | |
| 1221 class C<S> extends B<S, S> { | |
| 1222 C(S a) : super(a, a); | |
| 1223 C.named(S a) : super.named(a, a); | |
| 1224 } | |
| 1225 | |
| 1226 class D<S, T> extends B<T, int> { | |
| 1227 D(T a) : super(a, 3); | |
| 1228 D.named(T a) : super.named(a, 3); | |
| 1229 } | |
| 1230 | |
| 1231 class E<S, T> extends A<C<S>, T> { | |
| 1232 E(T a) : super(null, a); | |
| 1233 } | |
| 1234 | |
| 1235 class F<S, T> extends A<S, T> { | |
| 1236 F(S x, T y, {List<S> a, List<T> b}) : super(x, y); | |
| 1237 F.named(S x, T y, [S a, T b]) : super(a, b); | |
| 1238 } | |
| 1239 | |
| 1240 void main() { | |
| 1241 { | |
| 1242 A<int, String> a0 = /*$error*/new A(3, "hello"); | |
| 1243 A<int, String> a1 = /*$error*/new A.named(3, "hello"); | |
| 1244 A<int, String> a2 = new A<int, String>(3, "hello"); | |
| 1245 A<int, String> a3 = new A<int, String>.named(3, "hello"); | |
| 1246 A<int, String> a4 = /*warning:InferableAllocation*/new A<int, dynamic> (3, "hello"); | |
|
vsm
2015/03/31 17:11:28
" ... should be severe:StaticTypeError" here and b
Leaf
2015/04/03 19:24:51
Yes. I was leaving this to address separately, bu
| |
| 1247 A<int, String> a5 = /*warning:InferableAllocation*/new A<dynamic, dyna mic>.named(3, "hello"); | |
| 1248 } | |
| 1249 { | |
| 1250 A<int, String> a0 = /*warning:InferableAllocation*/new A("hello", 3); | |
| 1251 A<int, String> a1 = /*warning:InferableAllocation*/new A.named("hello" , 3); | |
| 1252 } | |
| 1253 { | |
| 1254 A<int, String> a0 = /*$error*/new B("hello", 3); | |
| 1255 A<int, String> a1 = /*$error*/new B.named("hello", 3); | |
| 1256 A<int, String> a2 = new B<String, int>("hello", 3); | |
| 1257 A<int, String> a3 = new B<String, int>.named("hello", 3); | |
| 1258 A<int, String> a4 = /*warning:InferableAllocation*/new B<String, dynam ic>("hello", 3); | |
| 1259 A<int, String> a5 = /*warning:InferableAllocation*/new B<dynamic, dyna mic>.named("hello", 3); | |
| 1260 } | |
| 1261 { | |
| 1262 A<int, String> a0 = /*warning:InferableAllocation*/new B(3, "hello"); | |
| 1263 A<int, String> a1 = /*warning:InferableAllocation*/new B.named(3, "hel lo"); | |
| 1264 } | |
| 1265 { | |
| 1266 A<int, int> a0 = /*$error*/new C(3); | |
| 1267 A<int, int> a1 = /*$error*/new C.named(3); | |
| 1268 A<int, int> a2 = new C<int>(3); | |
| 1269 A<int, int> a3 = new C<int>.named(3); | |
| 1270 A<int, int> a4 = /*warning:InferableAllocation*/new C<dynamic>(3); | |
| 1271 A<int, int> a5 = /*warning:InferableAllocation*/new C<dynamic>.named(3 ); | |
| 1272 } | |
| 1273 { | |
| 1274 A<int, int> a0 = /*warning:InferableAllocation*/new C("hello"); | |
| 1275 A<int, int> a1 = /*warning:InferableAllocation*/new C.named("hello"); | |
| 1276 } | |
| 1277 { | |
| 1278 A<int, String> a0 = /*$error*/new D("hello"); | |
| 1279 A<int, String> a1 = /*$error*/new D.named("hello"); | |
| 1280 A<int, String> a2 = new D<int, String>("hello"); | |
| 1281 A<int, String> a3 = new D<String, String>.named("hello"); | |
| 1282 A<int, String> a4 = /*warning:InferableAllocation*/new D<num, dynamic> ("hello"); | |
| 1283 A<int, String> a5 = /*warning:InferableAllocation*/new D<dynamic, dyna mic>.named("hello"); | |
| 1284 } | |
| 1285 { | |
| 1286 A<int, String> a0 = /*warning:InferableAllocation*/new D(3); | |
| 1287 A<int, String> a1 = /*warning:InferableAllocation*/new D.named(3); | |
| 1288 } | |
| 1289 { // Currently we only allow variable constraints. Test that we reject. | |
| 1290 A<C<int>, String> a0 = /*warning:InferableAllocation*/new E("hello"); | |
| 1291 } | |
| 1292 { // Check named and optional arguments | |
| 1293 A<int, String> a0 = /*$error*/new F(3, "hello", a: [3], b: ["hello"]); | |
| 1294 A<int, String> a1 = /*warning:InferableAllocation*/new F(3, "hello", a : ["hello"], b:[3]); | |
| 1295 A<int, String> a2 = /*$error*/new F.named(3, "hello", 3, "hello"); | |
| 1296 A<int, String> a3 = /*$error*/new F.named(3, "hello"); | |
| 1297 A<int, String> a4 = /*warning:InferableAllocation*/new F.named(3, "hel lo", "hello", 3); | |
| 1298 A<int, String> a5 = /*warning:InferableAllocation*/new F.named(3, "hel lo", "hello"); | |
| 1299 } | |
| 1300 } | |
| 1301 '''; | |
| 1302 testChecker({'/main.dart': mk("info:InferredTypeAllocation")}, | |
| 1303 inferDownwards: true); | |
| 1304 testChecker({'/main.dart': mk("warning:InferableAllocation")}, | |
| 1305 inferDownwards: false); | |
| 1306 }); | |
| 1307 | |
| 1308 test('downwards inference on list literals', () { | |
| 1309 String mk(String error) => ''' | |
| 1310 List<int> l0 = /*$error*/[]; | |
| 1311 List<int> l1 = /*$error*/[3]; | |
| 1312 List<int> l2 = /*warning:InferableLiteral*/["hello"]; | |
| 1313 List<int> l3 = /*warning:InferableLiteral*/["hello", 3]; | |
| 1314 | |
| 1315 List<dynamic> l0 = []; | |
| 1316 List<dynamic> l1 = [3]; | |
| 1317 List<dynamic> l2 = ["hello"]; | |
| 1318 List<dynamic> l3 = ["hello", 3]; | |
| 1319 | |
| 1320 List<int> l0 = /*warning:InferableLiteral*/<num>[]; | |
| 1321 List<int> l1 = /*warning:InferableLiteral*/<num>[3]; | |
| 1322 List<int> l2 = /*warning:InferableLiteral*/<num>[/*severe:StaticTypeError* /"hello"]; | |
| 1323 List<int> l3 = /*warning:InferableLiteral*/<num>[/*severe:StaticTypeError* /"hello", 3]; | |
| 1324 | |
| 1325 Iterable<int> i0 = /*$error*/[]; | |
| 1326 Iterable<int> i1 = /*$error*/[3]; | |
| 1327 Iterable<int> i2 = /*warning:InferableLiteral*/["hello"]; | |
| 1328 Iterable<int> i3 = /*warning:InferableLiteral*/["hello", 3]; | |
| 1329 | |
| 1330 const List<int> c0 = /*$error*/const []; | |
| 1331 const List<int> c1 = /*$error*/const [3]; | |
| 1332 const List<int> c2 = /*warning:InferableLiteral*/const ["hello"]; | |
| 1333 const List<int> c3 = /*warning:InferableLiteral*/const ["hello", 3]; | |
| 1334 '''; | |
| 1335 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, | |
| 1336 inferDownwards: true); | |
| 1337 testChecker({'/main.dart': mk("warning:InferableLiteral")}, | |
| 1338 inferDownwards: false); | |
| 1339 }); | |
| 1340 | |
| 1341 test('downwards inference on function arguments', () { | |
| 1342 String mk(String error) => ''' | |
| 1343 void f0(List<int> a) {}; | |
| 1344 void f1({List<int> a}) {}; | |
| 1345 void f2(Iterable<int> a) {}; | |
| 1346 void f3(Iterable<Iterable<int>> a) {}; | |
| 1347 void f4({Iterable<Iterable<int>> a}) {}; | |
| 1348 void main() { | |
| 1349 f0(/*$error*/[]); | |
| 1350 f0(/*$error*/[3]); | |
| 1351 f0(/*warning:InferableLiteral*/["hello"]); | |
| 1352 f0(/*warning:InferableLiteral*/["hello", 3]); | |
| 1353 | |
| 1354 f1(a: /*$error*/[]); | |
| 1355 f1(a: /*$error*/[3]); | |
| 1356 f1(a: /*warning:InferableLiteral*/["hello"]); | |
| 1357 f1(a: /*warning:InferableLiteral*/["hello", 3]); | |
| 1358 | |
| 1359 f2(/*$error*/[]); | |
| 1360 f2(/*$error*/[3]); | |
| 1361 f2(/*warning:InferableLiteral*/["hello"]); | |
| 1362 f2(/*warning:InferableLiteral*/["hello", 3]); | |
| 1363 | |
| 1364 f3(/*$error*/[]); | |
| 1365 f3(/*$error*/[[3]]); | |
| 1366 f3(/*warning:InferableLiteral*/[["hello"]]); | |
| 1367 f3(/*warning:InferableLiteral*/[["hello"], [3]]); | |
| 1368 | |
| 1369 f4(a: /*$error*/[]); | |
| 1370 f4(a: /*$error*/[[3]]); | |
| 1371 f4(a: /*warning:InferableLiteral*/[["hello"]]); | |
| 1372 f4(a: /*warning:InferableLiteral*/[["hello"], [3]]); | |
| 1373 } | |
| 1374 '''; | |
| 1375 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, | |
| 1376 inferDownwards: true); | |
| 1377 testChecker({'/main.dart': mk("warning:InferableLiteral")}, | |
| 1378 inferDownwards: false); | |
| 1379 }); | |
| 1380 | |
| 1381 test('downwards inference on map literals', () { | |
| 1382 String mk(String error) => ''' | |
| 1383 Map<int, String> l0 = /*$error*/{}; | |
| 1384 Map<int, String> l1 = /*$error*/{3: "hello"}; | |
| 1385 Map<int, String> l2 = /*warning:InferableLiteral*/{"hello": "hello"}; | |
| 1386 Map<int, String> l3 = /*warning:InferableLiteral*/{3: 3}; | |
| 1387 Map<int, String> l4 = /*warning:InferableLiteral*/{3:"hello", "hello": 3}; | |
| 1388 | |
| 1389 Map<dynamic, dynamic> l0 = {}; | |
| 1390 Map<dynamic, dynamic> l1 = {3: "hello"}; | |
| 1391 Map<dynamic, dynamic> l2 = {"hello": "hello"}; | |
| 1392 Map<dynamic, dynamic> l3 = {3: 3}; | |
| 1393 Map<dynamic, dynamic> l4 = {3:"hello", "hello": 3}; | |
| 1394 | |
| 1395 Map<dynamic, String> l0 = /*$error*/{}; | |
| 1396 Map<dynamic, String> l1 = /*$error*/{3: "hello"}; | |
| 1397 Map<dynamic, String> l2 = /*$error*/{"hello": "hello"}; | |
| 1398 Map<dynamic, String> l3 = /*warning:InferableLiteral*/{3: 3}; | |
| 1399 Map<dynamic, String> l4 = /*warning:InferableLiteral*/{3:"hello", "hello": 3}; | |
| 1400 | |
| 1401 Map<int, dynamic> l0 = /*$error*/{}; | |
| 1402 Map<int, dynamic> l1 = /*$error*/{3: "hello"}; | |
| 1403 Map<int, dynamic> l2 = /*warning:InferableLiteral*/{"hello": "hello"}; | |
| 1404 Map<int, dynamic> l3 = /*$error*/{3: 3}; | |
| 1405 Map<int, dynamic> l3 = /*warning:InferableLiteral*/{3:"hello", "hello": 3} ; | |
| 1406 | |
| 1407 Map<int, String> l0 = /*warning:InferableLiteral*/<num, dynamic>{}; | |
| 1408 Map<int, String> l1 = /*warning:InferableLiteral*/<num, dynamic>{3: "hello "}; | |
| 1409 Map<int, String> l3 = /*warning:InferableLiteral*/<num, dynamic>{3: 3}; | |
| 1410 | |
| 1411 const Map<int, String> l0 = /*$error*/const {}; | |
| 1412 const Map<int, String> l1 = /*$error*/const {3: "hello"}; | |
| 1413 const Map<int, String> l2 = /*warning:InferableLiteral*/const {"hello": "h ello"}; | |
| 1414 const Map<int, String> l3 = /*warning:InferableLiteral*/const {3: 3}; | |
| 1415 const Map<int, String> l4 = /*warning:InferableLiteral*/const {3:"hello", "hello": 3}; | |
| 1416 '''; | |
| 1417 testChecker({'/main.dart': mk("info:InferredTypeLiteral")}, | |
| 1418 inferDownwards: true); | |
| 1419 testChecker({'/main.dart': mk("warning:InferableLiteral")}, | |
| 1420 inferDownwards: false); | |
| 1421 }); | |
| 1182 } | 1422 } |
| OLD | NEW |