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

Side by Side Diff: test/checker/inferred_type_test.dart

Issue 1038213003: Downward inference (Closed) Base URL: git@github.com:dart-lang/dart-dev-compiler.git@master
Patch Set: Small fixes Created 5 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) 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
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 on instance creations', () {
1184 String mk(String error) => '''
vsm 2015/03/27 21:20:59 minor nit: error->info since it's not an error in
1185 class A<S, T> {
1186 S x;
1187 T y;
1188 A(this.x, this.y);
1189 A.named(this.x, this.y);
1190 }
1191
1192 class B<S, T> extends A<T, S> {
1193 B(S y, T x) : super(x, y);
1194 B.named(S y, T x) : super.named(x, y);
1195 }
1196
1197 class C<S> extends B<S, S> {
1198 C(S a) : super(a, a);
1199 C.named(S a) : super.named(a, a);
1200 }
1201
1202 class D<S, T> extends B<T, int> {
1203 D(T a) : super(a, 3);
1204 D.named(T a) : super.named(a, 3);
1205 }
1206
1207 class E<S, T> extends A<C<S>, T> {
1208 E(T a) : super(null, a);
1209 }
1210
1211 void main() {
1212 {
1213 A<int, String> a0 = /*$error*/new A(3, "hello");
1214 A<int, String> a1 = /*$error*/new A.named(3, "hello");
1215 A<int, String> a2 = new A<int, String>(3, "hello");
1216 A<int, String> a3 = new A<int, String>.named(3, "hello");
1217 A<int, String> a4 = /*warning:InferableAllocation*/new A<int, dynamic> (3, "hello");
1218 A<int, String> a5 = /*warning:InferableAllocation*/new A<dynamic, dyna mic>.named(3, "hello");
1219 }
1220 {
1221 A<int, String> a0 = /*warning:InferableAllocation*/new A("hello", 3);
1222 A<int, String> a1 = /*warning:InferableAllocation*/new A.named("hello" , 3);
1223 }
1224 {
1225 A<int, String> a0 = /*$error*/new B("hello", 3);
1226 A<int, String> a1 = /*$error*/new B.named("hello", 3);
1227 A<int, String> a2 = new B<String, int>("hello", 3);
1228 A<int, String> a3 = new B<String, int>.named("hello", 3);
1229 A<int, String> a4 = /*warning:InferableAllocation*/new B<String, dynam ic>("hello", 3);
1230 A<int, String> a5 = /*warning:InferableAllocation*/new B<dynamic, dyna mic>.named("hello", 3);
1231 }
1232 {
1233 A<int, String> a0 = /*warning:InferableAllocation*/new B(3, "hello");
1234 A<int, String> a1 = /*warning:InferableAllocation*/new B.named(3, "hel lo");
1235 }
1236 {
1237 A<int, int> a0 = /*$error*/new C(3);
1238 A<int, int> a1 = /*$error*/new C.named(3);
1239 A<int, int> a2 = new C<int>(3);
1240 A<int, int> a3 = new C<int>.named(3);
1241 A<int, int> a4 = /*warning:InferableAllocation*/new C<dynamic>(3);
1242 A<int, int> a5 = /*warning:InferableAllocation*/new C<dynamic>.named(3 );
1243 }
1244 {
1245 A<int, int> a0 = /*warning:InferableAllocation*/new C("hello");
1246 A<int, int> a1 = /*warning:InferableAllocation*/new C.named("hello");
1247 }
1248 {
1249 A<int, String> a0 = /*$error*/new D("hello");
1250 A<int, String> a1 = /*$error*/new D.named("hello");
1251 A<int, String> a2 = new D<int, String>("hello");
1252 A<int, String> a3 = new D<String, String>.named("hello");
1253 A<int, String> a4 = /*warning:InferableAllocation*/new D<num, dynamic> ("hello");
1254 A<int, String> a5 = /*warning:InferableAllocation*/new D<dynamic, dyna mic>.named("hello");
1255 }
1256 {
1257 A<int, String> a0 = /*warning:InferableAllocation*/new D(3);
1258 A<int, String> a1 = /*warning:InferableAllocation*/new D.named(3);
1259 }
1260 { // Currently we only allow variable constraints. Test that we reject.
1261 A<C<int>, String> a0 = /*warning:InferableAllocation*/new E("hello");
1262 }
1263 }
1264 ''';
1265 testChecker({'/main.dart': mk("info:InferredTypeExact")},
1266 inferDownwards: true);
1267 testChecker({'/main.dart': mk("warning:InferableAllocation")},
1268 inferDownwards: false);
1269 });
1270
1271 test('downwards inference on list literals', () {
1272 String mk(String error) => '''
1273 List<int> l0 = /*$error*/[];
1274 List<int> l1 = /*$error*/[3];
1275 List<int> l2 = /*warning:InferableLiteral*/["hello"];
1276 List<int> l3 = /*warning:InferableLiteral*/["hello", 3];
vsm 2015/03/27 21:20:59 Perhaps some tests with variables / expressions in
1277
1278 List<dynamic> l0 = [];
1279 List<dynamic> l1 = [3];
1280 List<dynamic> l2 = ["hello"];
1281 List<dynamic> l3 = ["hello", 3];
1282
1283 List<int> l0 = /*warning:InferableLiteral*/<num>[];
1284 List<int> l1 = /*warning:InferableLiteral*/<num>[3];
1285 List<int> l2 = /*warning:InferableLiteral*/<num>[/*severe:StaticTypeErro r*/"hello"];
1286 List<int> l3 = /*warning:InferableLiteral*/<num>[/*severe:StaticTypeErro r*/"hello", 3];
1287
1288 Iterable<int> i0 = /*$error*/[];
1289 Iterable<int> i1 = /*$error*/[3];
1290 Iterable<int> i2 = /*warning:InferableLiteral*/["hello"];
1291 Iterable<int> i3 = /*warning:InferableLiteral*/["hello", 3];
1292
1293 const List<int> c0 = /*$error*/const [];
1294 const List<int> c1 = /*$error*/const [3];
1295 const List<int> c2 = /*warning:InferableLiteral*/const ["hello"];
1296 const List<int> c3 = /*warning:InferableLiteral*/const ["hello", 3];
1297 ''';
1298 testChecker({'/main.dart': mk("info:InferredTypeLiteral")},
1299 inferDownwards: true);
1300 testChecker({'/main.dart': mk("warning:InferableLiteral")},
1301 inferDownwards: false);
1302 });
1303
1304 test('downwards inference on function arguments', () {
1305 String mk(String error) => '''
1306 void f0(List<int> a) {};
1307 void f1({List<int> a}) {};
1308 f0(/*$error*/[]);
1309 f0(/*$error*/[3]);
1310 f0(/*warning:InferableLiteral*/["hello"]);
1311 f0(/*warning:InferableLiteral*/["hello", 3]);
1312
1313 /// TODO(leafp): What's going on here?
1314 f1(a: /*pass should be $error*/[]);
1315 f1(a: /*pass should be $error*/[3]);
1316 f1(a: /*pass should be warning:InferableLiteral*/["hello"]);
1317 f1(a: /*pass should be warning:InferableLiteral*/["hello", 3]);
1318
1319 ''';
1320 testChecker({'/main.dart': mk("info:InferredTypeLiteral")},
1321 inferDownwards: true);
1322 testChecker({'/main.dart': mk("warning:InferableLiteral")},
1323 inferDownwards: false);
1324 });
1325
1326 test('downwards inference on map literals', () {
1327 String mk(String error) => '''
1328 Map<int, String> l0 = /*$error*/{};
1329 Map<int, String> l1 = /*$error*/{3: "hello"};
1330 Map<int, String> l2 = /*warning:InferableLiteral*/{"hello": "hello"};
1331 Map<int, String> l3 = /*warning:InferableLiteral*/{3: 3};
1332 Map<int, String> l4 = /*warning:InferableLiteral*/{3:"hello", "hello": 3 };
1333
1334 Map<dynamic, dynamic> l0 = {};
1335 Map<dynamic, dynamic> l1 = {3: "hello"};
1336 Map<dynamic, dynamic> l2 = {"hello": "hello"};
1337 Map<dynamic, dynamic> l3 = {3: 3};
1338 Map<dynamic, dynamic> l4 = {3:"hello", "hello": 3};
1339
1340 Map<dynamic, String> l0 = /*$error*/{};
1341 Map<dynamic, String> l1 = /*$error*/{3: "hello"};
1342 Map<dynamic, String> l2 = /*$error*/{"hello": "hello"};
1343 Map<dynamic, String> l3 = /*warning:InferableLiteral*/{3: 3};
1344 Map<dynamic, String> l4 = /*warning:InferableLiteral*/{3:"hello", "hello ": 3};
1345
1346 Map<int, dynamic> l0 = /*$error*/{};
1347 Map<int, dynamic> l1 = /*$error*/{3: "hello"};
1348 Map<int, dynamic> l2 = /*warning:InferableLiteral*/{"hello": "hello"};
1349 Map<int, dynamic> l3 = /*$error*/{3: 3};
1350 Map<int, dynamic> l3 = /*warning:InferableLiteral*/{3:"hello", "hello": 3};
1351
1352 Map<int, String> l0 = /*warning:InferableLiteral*/<num, dynamic>{};
1353 Map<int, String> l1 = /*warning:InferableLiteral*/<num, dynamic>{3: "hel lo"};
1354 Map<int, String> l3 = /*warning:InferableLiteral*/<num, dynamic>{3: 3};
1355
1356 const Map<int, String> l0 = /*$error*/const {};
1357 const Map<int, String> l1 = /*$error*/const {3: "hello"};
1358 const Map<int, String> l2 = /*warning:InferableLiteral*/const {"hello": "hello"};
1359 const Map<int, String> l3 = /*warning:InferableLiteral*/const {3: 3};
1360 const Map<int, String> l4 = /*warning:InferableLiteral*/const {3:"hello" , "hello": 3};
1361 ''';
1362 testChecker({'/main.dart': mk("info:InferredTypeLiteral")},
1363 inferDownwards: true);
1364 testChecker({'/main.dart': mk("warning:InferableLiteral")},
1365 inferDownwards: false);
1366 });
1182 } 1367 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698