| 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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 ''', | 229 ''', |
| 230 '/main.dart': ''' | 230 '/main.dart': ''' |
| 231 import 'a.dart'; | 231 import 'a.dart'; |
| 232 var y = x; | 232 var y = x; |
| 233 | 233 |
| 234 test1() { | 234 test1() { |
| 235 x = /*severe:StaticTypeError*/"hi"; | 235 x = /*severe:StaticTypeError*/"hi"; |
| 236 y = /*severe:StaticTypeError*/"hi"; | 236 y = /*severe:StaticTypeError*/"hi"; |
| 237 } | 237 } |
| 238 ''' | 238 ''' |
| 239 }, inferStaticsFromIdentifiers: true); | 239 }, inferTransitively: true); |
| 240 | 240 |
| 241 testChecker({ | 241 testChecker({ |
| 242 '/a.dart': ''' | 242 '/a.dart': ''' |
| 243 class A { static var x = 2; } | 243 class A { static var x = 2; } |
| 244 ''', | 244 ''', |
| 245 '/main.dart': ''' | 245 '/main.dart': ''' |
| 246 import 'a.dart'; | 246 import 'a.dart'; |
| 247 class B { static var y = A.x; } | 247 class B { static var y = A.x; } |
| 248 | 248 |
| 249 test1() { | 249 test1() { |
| 250 A.x = /*severe:StaticTypeError*/"hi"; | 250 A.x = /*severe:StaticTypeError*/"hi"; |
| 251 B.y = /*severe:StaticTypeError*/"hi"; | 251 B.y = /*severe:StaticTypeError*/"hi"; |
| 252 } | 252 } |
| 253 ''' | 253 ''' |
| 254 }, inferStaticsFromIdentifiers: true); | 254 }, inferTransitively: true); |
| 255 }); | 255 }); |
| 256 | 256 |
| 257 test('do not infer from variables in cycle libs', () { | 257 test('do not infer from variables in cycle libs', () { |
| 258 testChecker({ | 258 testChecker({ |
| 259 '/a.dart': ''' | 259 '/a.dart': ''' |
| 260 import 'main.dart'; | 260 import 'main.dart'; |
| 261 var x = 2; // ok to infer | 261 var x = 2; // ok to infer |
| 262 ''', | 262 ''', |
| 263 '/main.dart': ''' | 263 '/main.dart': ''' |
| 264 import 'a.dart'; | 264 import 'a.dart'; |
| 265 var y = x; // not ok to infer yet | 265 var y = x; // not ok to infer yet |
| 266 | 266 |
| 267 test1() { | 267 test1() { |
| 268 int t = 3; | 268 int t = 3; |
| 269 t = x; | 269 t = x; |
| 270 t = /*info:DownCast*/y; | 270 t = /*info:DownCast*/y; |
| 271 } | 271 } |
| 272 ''' | 272 ''' |
| 273 }, inferStaticsFromIdentifiers: true); | 273 }, inferTransitively: true); |
| 274 | 274 |
| 275 testChecker({ | 275 testChecker({ |
| 276 '/a.dart': ''' | 276 '/a.dart': ''' |
| 277 import 'main.dart'; | 277 import 'main.dart'; |
| 278 class A { static var x = 2; } | 278 class A { static var x = 2; } |
| 279 ''', | 279 ''', |
| 280 '/main.dart': ''' | 280 '/main.dart': ''' |
| 281 import 'a.dart'; | 281 import 'a.dart'; |
| 282 class B { static var y = A.x; } | 282 class B { static var y = A.x; } |
| 283 | 283 |
| 284 test1() { | 284 test1() { |
| 285 int t = 3; | 285 int t = 3; |
| 286 t = A.x; | 286 t = A.x; |
| 287 t = /*info:DownCast*/A.y; | 287 t = /*info:DownCast*/A.y; |
| 288 } | 288 } |
| 289 ''' | 289 ''' |
| 290 }, inferStaticsFromIdentifiers: true); | 290 }, inferTransitively: true); |
| 291 }); | 291 }); |
| 292 | 292 |
| 293 test('do not infer from static and instance fields', () { | 293 test('do not infer from static and instance fields', () { |
| 294 testChecker({ | 294 testChecker({ |
| 295 '/a.dart': ''' | 295 '/a.dart': ''' |
| 296 import 'b.dart'; | 296 import 'b.dart'; |
| 297 class A { | 297 class A { |
| 298 static final a1 = B.b1; | 298 static final a1 = B.b1; |
| 299 final a2 = new B().b2; | 299 final a2 = new B().b2; |
| 300 } | 300 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 '/main.dart': ''' | 336 '/main.dart': ''' |
| 337 import "a.dart"; | 337 import "a.dart"; |
| 338 | 338 |
| 339 test1() { | 339 test1() { |
| 340 int x = 0; | 340 int x = 0; |
| 341 // inference in A now works. | 341 // inference in A now works. |
| 342 x = A.a1; | 342 x = A.a1; |
| 343 x = new A().a2; | 343 x = new A().a2; |
| 344 } | 344 } |
| 345 ''' | 345 ''' |
| 346 }, inferStaticsFromIdentifiers: true); | 346 }, inferTransitively: true); |
| 347 }); | 347 }); |
| 348 | 348 |
| 349 test('inference uses declared types', () { | 349 test('inference uses declared types', () { |
| 350 testChecker({ | 350 testChecker({ |
| 351 '/main.dart': ''' | 351 '/main.dart': ''' |
| 352 int w = 0; | 352 int w = 0; |
| 353 var x = 0; | 353 var x = 0; |
| 354 | 354 |
| 355 var y = w; // y can be inferred because w is typed int. | 355 var y = w; // y can be inferred because w is typed int. |
| 356 var z = x; // z cannot, because x would be inferred. | 356 var z = x; // z cannot, because x would be inferred. |
| 357 | 357 |
| 358 test1() { | 358 test1() { |
| 359 int a; | 359 int a; |
| 360 a = w; | 360 a = w; |
| 361 a = x; | 361 a = x; |
| 362 a = y; | 362 a = y; |
| 363 a = /*info:DownCast*/z; | 363 a = /*info:DownCast*/z; |
| 364 } | 364 } |
| 365 ''' | 365 ''' |
| 366 }, inferStaticsFromIdentifiers: true); | 366 }, inferTransitively: true); |
| 367 }); | 367 }); |
| 368 | 368 |
| 369 test('inference in cycles is deterministic', () { | 369 test('inference in cycles is deterministic', () { |
| 370 testChecker({ | 370 testChecker({ |
| 371 '/a.dart': ''' | 371 '/a.dart': ''' |
| 372 import 'b.dart'; | 372 import 'b.dart'; |
| 373 class A { | 373 class A { |
| 374 static final a1 = B.b1; | 374 static final a1 = B.b1; |
| 375 final a2 = new B().b2; | 375 final a2 = new B().b2; |
| 376 } | 376 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 x = E.e1; | 443 x = E.e1; |
| 444 x = /*info:DownCast*/E.e2; | 444 x = /*info:DownCast*/E.e2; |
| 445 x = E.e3; | 445 x = E.e3; |
| 446 x = new E().e4; | 446 x = new E().e4; |
| 447 x = /*info:DownCast*/new E().e5; | 447 x = /*info:DownCast*/new E().e5; |
| 448 x = new E().e6; | 448 x = new E().e6; |
| 449 x = F.f1; | 449 x = F.f1; |
| 450 x = new F().f2; | 450 x = new F().f2; |
| 451 } | 451 } |
| 452 ''' | 452 ''' |
| 453 }, inferStaticsFromIdentifiers: true); | 453 }, inferTransitively: true); |
| 454 }); | 454 }); |
| 455 | 455 |
| 456 test('infer from complex expressions if the outer-most value is precise', () { | 456 test('infer from complex expressions if the outer-most value is precise', () { |
| 457 testChecker({ | 457 testChecker({ |
| 458 '/main.dart': ''' | 458 '/main.dart': ''' |
| 459 class A { int x; B operator+(other) {} } | 459 class A { int x; B operator+(other) {} } |
| 460 class B extends A { B(ignore); } | 460 class B extends A { B(ignore); } |
| 461 var a = new A(); | 461 var a = new A(); |
| 462 // Note: it doesn't matter that some of these refer to 'x'. | 462 // Note: it doesn't matter that some of these refer to 'x'. |
| 463 var b = new B(x); // allocations | 463 var b = new B(x); // allocations |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 int i = 0; | 552 int i = 0; |
| 553 A a; | 553 A a; |
| 554 B b; | 554 B b; |
| 555 a = t1; | 555 a = t1; |
| 556 i = t2; | 556 i = t2; |
| 557 b = t3; | 557 b = t3; |
| 558 i = /*info:DownCast*/t4; | 558 i = /*info:DownCast*/t4; |
| 559 i = new B().y; // B.y was inferred though | 559 i = new B().y; // B.y was inferred though |
| 560 } | 560 } |
| 561 ''' | 561 ''' |
| 562 }, inferStaticsFromIdentifiers: true); | 562 }, inferTransitively: true); |
| 563 }); | 563 }); |
| 564 | 564 |
| 565 test('infer types on loop indices', () { | 565 test('infer types on loop indices', () { |
| 566 // foreach loop | 566 // foreach loop |
| 567 testChecker({ | 567 testChecker({ |
| 568 '/main.dart': ''' | 568 '/main.dart': ''' |
| 569 class Foo { | 569 class Foo { |
| 570 int bar = 42; | 570 int bar = 42; |
| 571 } | 571 } |
| 572 | 572 |
| (...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1046 | 1046 |
| 1047 i = /*info:DownCast*/new B().x; | 1047 i = /*info:DownCast*/new B().x; |
| 1048 i = new B().y; | 1048 i = new B().y; |
| 1049 i = /*severe:StaticTypeError*/new B().z; | 1049 i = /*severe:StaticTypeError*/new B().z; |
| 1050 i = new B().w; | 1050 i = new B().w; |
| 1051 } | 1051 } |
| 1052 ''' | 1052 ''' |
| 1053 }, inferFromOverrides: true); | 1053 }, inferFromOverrides: true); |
| 1054 }); | 1054 }); |
| 1055 } | 1055 } |
| OLD | NEW |