Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: pkg/analyzer/test/src/summary/top_level_inference_test.dart

Issue 2761633002: Infer fields/getters/setters types according to the new top-level inference rules. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 import 'dart:async'; 5 import 'dart:async';
6 6
7 import 'package:analyzer/dart/element/element.dart'; 7 import 'package:analyzer/dart/element/element.dart';
8 import 'package:analyzer/src/dart/analysis/driver.dart'; 8 import 'package:analyzer/src/dart/analysis/driver.dart';
9 import 'package:test_reflective_loader/test_reflective_loader.dart'; 9 import 'package:test_reflective_loader/test_reflective_loader.dart';
10 10
(...skipping 972 matching lines...) Expand 10 before | Expand all | Expand 10 after
983 var library = await _encodeDecodeLibrary(r''' 983 var library = await _encodeDecodeLibrary(r'''
984 var V = throw 42; 984 var V = throw 42;
985 '''); 985 ''');
986 checkElementText( 986 checkElementText(
987 library, 987 library,
988 r''' 988 r'''
989 Null V; 989 Null V;
990 '''); 990 ''');
991 } 991 }
992 992
993 /** 993 test_instanceField_fieldFormal() async {
994 * A getter, setter or field which overrides/implements only a getter is 994 var library = await _encodeDecodeLibrary(r'''
995 * inferred to have the type taken from the overridden getter result type. 995 class A {
996 * 996 var f = 0;
997 * Note that overriding a field is addressed via the implicit induced 997 A([this.f = 'hello']);
998 * getter/setter pair (or just getter in the case of a final field). 998 }
999 */ 999 ''');
1000 checkElementText(
1001 library,
1002 r'''
1003 class A {
1004 int f;
1005 A([int this.f]);
1006 }
1007 ''');
1008 }
1009
1010 test_instanceField_fromAccessors_multiple_different() async {
1011 var library = await _encodeDecodeLibrary(r'''
1012 abstract class A {
1013 int get x;
1014 }
1015 abstract class B {
1016 void set x(String _);
1017 }
1018 class C implements A, B {
1019 var x;
1020 }
1021 ''');
1022 // TODO(scheglov) test for inference failure error
1023 checkElementText(
1024 library,
1025 r'''
1026 abstract class A {
1027 int get x;
1028 }
1029 abstract class B {
1030 void set x(String _);
1031 }
1032 class C implements A, B {
1033 dynamic x;
1034 }
1035 ''');
1036 }
1037
1038 test_instanceField_fromAccessors_multiple_same() async {
1039 var library = await _encodeDecodeLibrary(r'''
1040 abstract class A {
1041 int get x;
1042 }
1043 abstract class B {
1044 void set x(int _);
1045 }
1046 class C implements A, B {
1047 var x;
1048 }
1049 ''');
1050 checkElementText(
1051 library,
1052 r'''
1053 abstract class A {
1054 int get x;
1055 }
1056 abstract class B {
1057 void set x(int _);
1058 }
1059 class C implements A, B {
1060 int x;
1061 }
1062 ''');
1063 }
1064
1000 test_instanceField_fromField() async { 1065 test_instanceField_fromField() async {
1001 var library = await _encodeDecodeLibrary(r''' 1066 var library = await _encodeDecodeLibrary(r'''
1002 abstract class A { 1067 abstract class A {
1003 int x; 1068 int x;
1004 int y; 1069 int y;
1005 int z; 1070 int z;
1006 } 1071 }
1007 class B implements A { 1072 class B implements A {
1008 var x; 1073 var x;
1009 get y => null; 1074 get y => null;
1010 set z(_) {} 1075 set z(_) {}
1011 } 1076 }
1012 '''); 1077 ''');
1013 checkElementText( 1078 checkElementText(
1014 library, 1079 library,
1015 r''' 1080 r'''
1016 abstract class A { 1081 abstract class A {
1017 int x; 1082 int x;
1018 int y; 1083 int y;
1019 int z; 1084 int z;
1020 } 1085 }
1021 class B implements A { 1086 class B implements A {
1022 int x; 1087 int x;
1088 synthetic final int y;
1089 synthetic int z;
1023 int get y {} 1090 int get y {}
1024 void set z(int _) {} 1091 void set z(int _) {}
1025 } 1092 }
1093 ''',
1094 withSyntheticFields: true);
1095 }
1096
1097 test_instanceField_fromField_explicitDynamic() async {
1098 var library = await _encodeDecodeLibrary(r'''
1099 abstract class A {
1100 dynamic x;
1101 }
1102 class B implements A {
1103 var x = 1;
1104 }
1105 ''');
1106 checkElementText(
1107 library,
1108 r'''
1109 abstract class A {
1110 dynamic x;
1111 }
1112 class B implements A {
1113 dynamic x;
1114 }
1026 '''); 1115 ''');
1027 } 1116 }
1028 1117
1029 /** 1118 test_instanceField_fromField_generic() async {
1030 * A getter, setter or field which overrides/implements only a getter is 1119 var library = await _encodeDecodeLibrary(r'''
1031 * inferred to have the type taken from the overridden getter result type. 1120 abstract class A<E> {
1032 */ 1121 E x;
1033 @failingTest 1122 E y;
1123 E z;
1124 }
1125 class B<T> implements A<T> {
1126 var x;
1127 get y => null;
1128 set z(_) {}
1129 }
1130 ''');
1131 checkElementText(
1132 library,
1133 r'''
1134 abstract class A<E> {
1135 E x;
1136 E y;
1137 E z;
1138 }
1139 class B<T> implements A<T> {
1140 T x;
1141 synthetic final T y;
1142 synthetic T z;
1143 T get y {}
1144 void set z(T _) {}
1145 }
1146 ''',
1147 withSyntheticFields: true);
1148 }
1149
1150 test_instanceField_fromField_implicitDynamic() async {
1151 var library = await _encodeDecodeLibrary(r'''
1152 abstract class A {
1153 var x;
1154 }
1155 class B implements A {
1156 var x = 1;
1157 }
1158 ''');
1159 checkElementText(
1160 library,
1161 r'''
1162 abstract class A {
1163 dynamic x;
1164 }
1165 class B implements A {
1166 dynamic x;
1167 }
1168 ''');
1169 }
1170
1171 test_instanceField_fromField_narrowType() async {
1172 var library = await _encodeDecodeLibrary(r'''
1173 abstract class A {
1174 num x;
1175 }
1176 class B implements A {
1177 var x = 1;
1178 }
1179 ''');
1180 checkElementText(
1181 library,
1182 r'''
1183 abstract class A {
1184 num x;
1185 }
1186 class B implements A {
1187 num x;
1188 }
1189 ''');
1190 }
1191
1034 test_instanceField_fromGetter() async { 1192 test_instanceField_fromGetter() async {
1035 var library = await _encodeDecodeLibrary(r''' 1193 var library = await _encodeDecodeLibrary(r'''
1036 abstract class A { 1194 abstract class A {
1037 int get x; 1195 int get x;
1038 int get y; 1196 int get y;
1039 int get z; 1197 int get z;
1040 } 1198 }
1041 class B implements A { 1199 class B implements A {
1042 var x; 1200 var x;
1043 get y => null; 1201 get y => null;
1044 set z(_) {} 1202 set z(_) {}
1045 } 1203 }
1046 '''); 1204 ''');
1047 checkElementText( 1205 checkElementText(
1048 library, 1206 library,
1049 r''' 1207 r'''
1050 abstract class A { 1208 abstract class A {
1051 int get x; 1209 int get x;
1052 int get y; 1210 int get y;
1053 int get z; 1211 int get z;
1054 } 1212 }
1055 class B implements A { 1213 class B implements A {
1056 int x; 1214 int x;
1057 int get y {} 1215 int get y {}
1058 void set z(int _) {} 1216 void set z(int _) {}
1059 } 1217 }
1060 '''); 1218 ''');
1061 } 1219 }
1062 1220
1063 /** 1221 test_instanceField_fromGetter_generic() async {
1064 * A field which overrides/implements both a setter and a getter is inferred 1222 var library = await _encodeDecodeLibrary(r'''
1065 * to have the type taken from the overridden setter parameter type if this 1223 abstract class A<E> {
1066 * type is the same as the return type of the overridden getter (if the types 1224 E get x;
1067 * are not the same then inference fails with an error). 1225 E get y;
1068 */ 1226 E get z;
1069 test_instanceField_fromGetterSetter_field_differentType() async { 1227 }
1228 class B<T> implements A<T> {
1229 var x;
1230 get y => null;
1231 set z(_) {}
1232 }
1233 ''');
1234 checkElementText(
1235 library,
1236 r'''
1237 abstract class A<E> {
1238 E get x;
1239 E get y;
1240 E get z;
1241 }
1242 class B<T> implements A<T> {
1243 T x;
1244 T get y {}
1245 void set z(T _) {}
1246 }
1247 ''');
1248 }
1249
1250 test_instanceField_fromGetter_multiple_different() async {
1070 var library = await _encodeDecodeLibrary(r''' 1251 var library = await _encodeDecodeLibrary(r'''
1071 abstract class A { 1252 abstract class A {
1072 int get x; 1253 int get x;
1073 } 1254 }
1074 abstract class B { 1255 abstract class B {
1075 void set x(String _); 1256 String get x;
1076 } 1257 }
1077 class C implements A, B { 1258 class C implements A, B {
1078 var x; 1259 get x => null;
1079 } 1260 }
1080 '''); 1261 ''');
1081 // TODO(scheglov) test for inference failure error 1262 // TODO(scheglov) test for inference failure error
1082 checkElementText( 1263 checkElementText(
1083 library, 1264 library,
1084 r''' 1265 r'''
1085 abstract class A { 1266 abstract class A {
1086 int get x; 1267 int get x;
1087 } 1268 }
1088 abstract class B { 1269 abstract class B {
1089 void set x(String _); 1270 String get x;
1090 } 1271 }
1091 class C implements A, B { 1272 class C implements A, B {
1092 dynamic x; 1273 dynamic get x {}
1093 } 1274 }
1094 '''); 1275 ''');
1095 } 1276 }
1096 1277
1097 /** 1278 test_instanceField_fromGetter_multiple_different_dynamic() async {
1098 * A field which overrides/implements both a setter and a getter is inferred
1099 * to have the type taken from the overridden setter parameter type if this
1100 * type is the same as the return type of the overridden getter (if the types
1101 * are not the same then inference fails with an error).
1102 */
1103 test_instanceField_fromGetterSetter_field_sameType() async {
1104 var library = await _encodeDecodeLibrary(r''' 1279 var library = await _encodeDecodeLibrary(r'''
1105 abstract class A { 1280 abstract class A {
1106 int get x; 1281 int get x;
1282 }
1283 abstract class B {
1284 dynamic get x;
1285 }
1286 class C implements A, B {
1287 get x => null;
1288 }
1289 ''');
1290 // TODO(scheglov) test for inference failure error
1291 checkElementText(
1292 library,
1293 r'''
1294 abstract class A {
1295 int get x;
1296 }
1297 abstract class B {
1298 dynamic get x;
1299 }
1300 class C implements A, B {
1301 dynamic get x {}
1302 }
1303 ''');
1304 }
1305
1306 test_instanceField_fromGetter_multiple_different_generic() async {
1307 var library = await _encodeDecodeLibrary(r'''
1308 abstract class A<T> {
1309 T get x;
1310 }
1311 abstract class B<T> {
1312 T get x;
1313 }
1314 class C implements A<int>, B<String> {
1315 get x => null;
1316 }
1317 ''');
1318 // TODO(scheglov) test for inference failure error
1319 checkElementText(
1320 library,
1321 r'''
1322 abstract class A<T> {
1323 T get x;
1324 }
1325 abstract class B<T> {
1326 T get x;
1327 }
1328 class C implements A<int>, B<String> {
1329 dynamic get x {}
1330 }
1331 ''');
1332 }
1333
1334 test_instanceField_fromGetter_multiple_same() async {
1335 var library = await _encodeDecodeLibrary(r'''
1336 abstract class A {
1337 int get x;
1338 }
1339 abstract class B {
1340 int get x;
1341 }
1342 class C implements A, B {
1343 get x => null;
1344 }
1345 ''');
1346 checkElementText(
1347 library,
1348 r'''
1349 abstract class A {
1350 int get x;
1351 }
1352 abstract class B {
1353 int get x;
1354 }
1355 class C implements A, B {
1356 int get x {}
1357 }
1358 ''');
1359 }
1360
1361 test_instanceField_fromGetterSetter_field() async {
1362 var library = await _encodeDecodeLibrary(r'''
1363 abstract class A {
1364 int get x;
1107 } 1365 }
1108 abstract class B { 1366 abstract class B {
1109 void set x(int _); 1367 void set x(int _);
1110 } 1368 }
1111 class C implements A, B { 1369 class C implements A, B {
1112 var x; 1370 var x;
1113 } 1371 }
1114 '''); 1372 ''');
1115 checkElementText( 1373 checkElementText(
1116 library, 1374 library,
1117 r''' 1375 r'''
1118 abstract class A { 1376 abstract class A {
1119 int get x; 1377 int get x;
1120 } 1378 }
1121 abstract class B { 1379 abstract class B {
1122 void set x(int _); 1380 void set x(int _);
1123 } 1381 }
1124 class C implements A, B { 1382 class C implements A, B {
1125 int x; 1383 int x;
1126 } 1384 }
1127 '''); 1385 ''');
1128 } 1386 }
1129 1387
1130 /**
1131 * A getter which overrides/implements both a setter and a getter is inferred
1132 * to have the type taken from the overridden getter result type.
1133 */
1134 test_instanceField_fromGetterSetter_getter() async { 1388 test_instanceField_fromGetterSetter_getter() async {
1135 var library = await _encodeDecodeLibrary(r''' 1389 var library = await _encodeDecodeLibrary(r'''
1136 abstract class A { 1390 abstract class A {
1137 int get x; 1391 int get x;
1138 } 1392 }
1139 abstract class B { 1393 abstract class B {
1140 void set x(String _); 1394 void set x(int _);
1141 } 1395 }
1142 class C implements A, B { 1396 class C implements A, B {
1143 get x => null; 1397 get x => null;
1144 } 1398 }
1145 '''); 1399 ''');
1146 checkElementText( 1400 checkElementText(
1147 library, 1401 library,
1148 r''' 1402 r'''
1149 abstract class A { 1403 abstract class A {
1404 synthetic final int x;
1150 int get x; 1405 int get x;
1151 } 1406 }
1152 abstract class B { 1407 abstract class B {
1153 void set x(String _); 1408 synthetic int x;
1409 void set x(int _);
1154 } 1410 }
1155 class C implements A, B { 1411 class C implements A, B {
1412 synthetic final int x;
1156 int get x {} 1413 int get x {}
1157 } 1414 }
1158 '''); 1415 ''',
1416 withSyntheticFields: true);
1159 } 1417 }
1160 1418
1161 /**
1162 * A setter which overrides/implements both a setter and a getter is inferred
1163 * to have the type taken from the overridden setter parameter type.
1164 */
1165 test_instanceField_fromGetterSetter_setter() async { 1419 test_instanceField_fromGetterSetter_setter() async {
1166 var library = await _encodeDecodeLibrary(r''' 1420 var library = await _encodeDecodeLibrary(r'''
1167 abstract class A { 1421 abstract class A {
1168 int get x; 1422 int get x;
1169 } 1423 }
1170 abstract class B { 1424 abstract class B {
1171 void set x(String _); 1425 void set x(String _);
1172 } 1426 }
1173 class C implements A, B { 1427 class C implements A, B {
1174 set x(_); 1428 set x(_);
1175 } 1429 }
1176 '''); 1430 ''');
1177 checkElementText( 1431 checkElementText(
1178 library, 1432 library,
1179 r''' 1433 r'''
1180 abstract class A { 1434 abstract class A {
1435 synthetic final int x;
1181 int get x; 1436 int get x;
1182 } 1437 }
1183 abstract class B { 1438 abstract class B {
1439 synthetic String x;
1184 void set x(String _); 1440 void set x(String _);
1185 } 1441 }
1186 class C implements A, B { 1442 class C implements A, B {
1187 void set x(String _); 1443 synthetic dynamic x;
1444 void set x(dynamic _);
1188 } 1445 }
1189 '''); 1446 ''',
1447 withSyntheticFields: true);
1190 } 1448 }
1191 1449
1192 /**
1193 * A getter, setter or field which overrides/implements only a setter is
1194 * inferred to have the type taken from the overridden setter parameter.
1195 */
1196 @failingTest
1197 test_instanceField_fromSetter() async { 1450 test_instanceField_fromSetter() async {
1198 var library = await _encodeDecodeLibrary(r''' 1451 var library = await _encodeDecodeLibrary(r'''
1199 abstract class A { 1452 abstract class A {
1200 void set x(int _); 1453 void set x(int _);
1201 void set y(int _); 1454 void set y(int _);
1202 void set z(int _); 1455 void set z(int _);
1203 } 1456 }
1204 class B implements A { 1457 class B implements A {
1205 var x; 1458 var x;
1206 get y => null; 1459 get y => null;
1207 set z(_) {} 1460 set z(_) {}
1208 } 1461 }
1209 '''); 1462 ''');
1210 checkElementText( 1463 checkElementText(
1211 library, 1464 library,
1212 r''' 1465 r'''
1213 abstract class A { 1466 abstract class A {
1214 void set x(int _); 1467 void set x(int _);
1215 void set y(int _); 1468 void set y(int _);
1216 void set z(int _); 1469 void set z(int _);
1217 } 1470 }
1218 class B implements A { 1471 class B implements A {
1219 int x; 1472 int x;
1220 int get y {} 1473 int get y {}
1221 void set z(int _) {} 1474 void set z(int _) {}
1222 } 1475 }
1223 '''); 1476 ''');
1224 } 1477 }
1225 1478
1226 /** 1479 test_instanceField_fromSetter_multiple_different() async {
1227 * A getter, setter or field which overrides/implements only a getter is
1228 * inferred to have the type taken from the overridden getter result type.
1229 *
1230 * Note that overriding a field is addressed via the implicit induced
1231 * getter/setter pair (or just getter in the case of a final field).
1232 *
1233 * A field with no annotated type that does not override anything has the
1234 * type inferred from its initializer.
1235 */
1236 test_instanceField_preferOverride() async {
1237 var library = await _encodeDecodeLibrary(r''' 1480 var library = await _encodeDecodeLibrary(r'''
1238 abstract class A { 1481 abstract class A {
1239 num x; 1482 void set x(int _);
1240 var y = 2;
1241 } 1483 }
1242 class B implements A { 1484 abstract class B {
1243 var x = 1; 1485 void set x(String _);
1244 var y = 2; 1486 }
1245 var z = 3; 1487 class C implements A, B {
1488 get x => null;
1246 } 1489 }
1247 '''); 1490 ''');
1248 checkElementText( 1491 checkElementText(
1249 library, 1492 library,
1250 r''' 1493 r'''
1251 abstract class A { 1494 abstract class A {
1252 num x; 1495 void set x(int _);
1253 int y;
1254 } 1496 }
1255 class B implements A { 1497 abstract class B {
1256 num x; 1498 void set x(String _);
1257 int y; 1499 }
1258 int z; 1500 class C implements A, B {
1501 dynamic get x {}
1259 } 1502 }
1260 '''); 1503 ''');
1261 } 1504 }
1505
1506 test_instanceField_fromSetter_multiple_same() async {
1507 var library = await _encodeDecodeLibrary(r'''
1508 abstract class A {
1509 void set x(int _);
1510 }
1511 abstract class B {
1512 void set x(int _);
1513 }
1514 class C implements A, B {
1515 get x => null;
1516 }
1517 ''');
1518 checkElementText(
1519 library,
1520 r'''
1521 abstract class A {
1522 void set x(int _);
1523 }
1524 abstract class B {
1525 void set x(int _);
1526 }
1527 class C implements A, B {
1528 int get x {}
1529 }
1530 ''');
1531 }
1532
1533 @failingTest
1534 test_instanceField_inheritsCovariant_fromSetter_field() async {
1535 var library = await _encodeDecodeLibrary(r'''
1536 abstract class A {
1537 num get x;
1538 void set x(covariant num _);
1539 }
1540 class B implements A {
1541 int x;
1542 }
1543 ''');
1544 checkElementText(
1545 library,
1546 r'''
1547 abstract class A {
1548 num get x;
1549 void set x(covariant num _);
1550 }
1551 class B implements A {
1552 int x;
1553 synthetic int get x {}
1554 synthetic void set x(covariant int _x) {}
1555 }
1556 ''',
1557 withSyntheticAccessors: true);
1558 }
1559
1560 test_instanceField_inheritsCovariant_fromSetter_setter() async {
1561 var library = await _encodeDecodeLibrary(r'''
1562 abstract class A {
1563 num get x;
1564 void set x(covariant num _);
1565 }
1566 class B implements A {
1567 set x(int _) {}
1568 }
1569 ''');
1570 checkElementText(
1571 library,
1572 r'''
1573 abstract class A {
1574 num get x;
1575 void set x(covariant num _);
1576 }
1577 class B implements A {
1578 void set x(covariant int _) {}
1579 }
1580 ''');
1581 }
1582
1583 test_instanceField_initializer() async {
1584 var library = await _encodeDecodeLibrary(r'''
1585 class A {
1586 var t1 = 1;
1587 var t2 = 2.0;
1588 var t3 = null;
1589 }
1590 ''');
1591 checkElementText(
1592 library,
1593 r'''
1594 class A {
1595 int t1;
1596 double t2;
1597 dynamic t3;
1598 }
1599 ''');
1600 }
1262 1601
1263 test_method_error_conflict_parameterType_generic() async { 1602 test_method_error_conflict_parameterType_generic() async {
1264 var library = await _encodeDecodeLibrary(r''' 1603 var library = await _encodeDecodeLibrary(r'''
1265 class A<T> { 1604 class A<T> {
1266 void m(T a) {} 1605 void m(T a) {}
1267 } 1606 }
1268 class B<E> { 1607 class B<E> {
1269 void m(E a) {} 1608 void m(E a) {}
1270 } 1609 }
1271 class C extends A<int> implements B<double> { 1610 class C extends A<int> implements B<double> {
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after
1857 2196
1858 Future<LibraryElement> _encodeDecodeLibrary(String text) async { 2197 Future<LibraryElement> _encodeDecodeLibrary(String text) async {
1859 String path = _p('/test.dart'); 2198 String path = _p('/test.dart');
1860 provider.newFile(path, text); 2199 provider.newFile(path, text);
1861 UnitElementResult result = await driver.getUnitElement(path); 2200 UnitElementResult result = await driver.getUnitElement(path);
1862 return result.element.library; 2201 return result.element.library;
1863 } 2202 }
1864 2203
1865 String _p(String path) => provider.convertPath(path); 2204 String _p(String path) => provider.convertPath(path);
1866 } 2205 }
OLDNEW
« no previous file with comments | « pkg/analyzer/test/src/summary/resynthesize_common.dart ('k') | pkg/analyzer/test/src/task/strong/inferred_type_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698