| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 'package:analyzer/dart/element/type.dart'; | 5 import 'package:analyzer/dart/element/type.dart'; |
| 6 import 'package:analyzer/src/dart/element/element.dart'; | 6 import 'package:analyzer/src/dart/element/element.dart'; |
| 7 import 'package:analyzer/src/summary/format.dart'; | 7 import 'package:analyzer/src/summary/format.dart'; |
| 8 import 'package:analyzer/src/summary/idl.dart'; | 8 import 'package:analyzer/src/summary/idl.dart'; |
| 9 import 'package:analyzer/src/summary/link.dart'; | 9 import 'package:analyzer/src/summary/link.dart'; |
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 152 } |
| 153 class C extends B { | 153 class C extends B { |
| 154 var c; | 154 var c; |
| 155 } | 155 } |
| 156 '''); | 156 '''); |
| 157 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 157 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 158 library.libraryCycleForLink.ensureLinked(); | 158 library.libraryCycleForLink.ensureLinked(); |
| 159 // No assertions--just make sure it doesn't crash. | 159 // No assertions--just make sure it doesn't crash. |
| 160 } | 160 } |
| 161 | 161 |
| 162 void test_bundle_refers_to_bundle() { |
| 163 var bundle1 = createPackageBundle( |
| 164 ''' |
| 165 var x = 0; |
| 166 ''', |
| 167 path: '/a.dart'); |
| 168 addBundle('/a.ds', bundle1); |
| 169 var bundle2 = createPackageBundle( |
| 170 ''' |
| 171 import "a.dart"; |
| 172 var y = x; |
| 173 ''', |
| 174 path: '/b.dart'); |
| 175 addBundle('/a.ds', bundle1); |
| 176 addBundle('/b.ds', bundle2); |
| 177 createLinker(''' |
| 178 import "b.dart"; |
| 179 var z = y; |
| 180 '''); |
| 181 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 182 expect(_getVariable(library.getContainedName('z')).inferredType.toString(), |
| 183 'int'); |
| 184 } |
| 185 |
| 162 void test_constCycle_viaLength() { | 186 void test_constCycle_viaLength() { |
| 163 createLinker(''' | 187 createLinker(''' |
| 164 class C { | 188 class C { |
| 165 final y; | 189 final y; |
| 166 const C() : y = x.length; | 190 const C() : y = x.length; |
| 167 } | 191 } |
| 168 const x = [const C()]; | 192 const x = [const C()]; |
| 169 '''); | 193 '''); |
| 170 testLibrary.libraryCycleForLink.ensureLinked(); | 194 testLibrary.libraryCycleForLink.ensureLinked(); |
| 171 ClassElementForLink classC = testLibrary.getContainedName('C'); | 195 ClassElementForLink classC = testLibrary.getContainedName('C'); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 199 ClassElementForLink_Class c = library.getContainedName('C'); | 223 ClassElementForLink_Class c = library.getContainedName('C'); |
| 200 expect(c.getContainedName('s='), isNot(isUndefined)); | 224 expect(c.getContainedName('s='), isNot(isUndefined)); |
| 201 } | 225 } |
| 202 | 226 |
| 203 void test_inferredType_closure_fromBundle() { | 227 void test_inferredType_closure_fromBundle() { |
| 204 var bundle = createPackageBundle( | 228 var bundle = createPackageBundle( |
| 205 ''' | 229 ''' |
| 206 var x = () {}; | 230 var x = () {}; |
| 207 ''', | 231 ''', |
| 208 path: '/a.dart'); | 232 path: '/a.dart'); |
| 209 addBundle(bundle); | 233 addBundle('/a.ds', bundle); |
| 210 createLinker(''' | 234 createLinker(''' |
| 211 import 'a.dart'; | 235 import 'a.dart'; |
| 212 var y = x; | 236 var y = x; |
| 213 '''); | 237 '''); |
| 214 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 238 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 215 expect(_getVariable(library.getContainedName('y')).inferredType.toString(), | 239 expect(_getVariable(library.getContainedName('y')).inferredType.toString(), |
| 216 '() → dynamic'); | 240 '() → dynamic'); |
| 217 } | 241 } |
| 218 | 242 |
| 219 void test_inferredType_closure_fromBundle_identifierSequence() { | 243 void test_inferredType_closure_fromBundle_identifierSequence() { |
| 220 var bundle = createPackageBundle( | 244 var bundle = createPackageBundle( |
| 221 ''' | 245 ''' |
| 222 class C { | 246 class C { |
| 223 static final x = (D d) => d.e; | 247 static final x = (D d) => d.e; |
| 224 } | 248 } |
| 225 class D { | 249 class D { |
| 226 E e; | 250 E e; |
| 227 } | 251 } |
| 228 class E {} | 252 class E {} |
| 229 ''', | 253 ''', |
| 230 path: '/a.dart'); | 254 path: '/a.dart'); |
| 231 addBundle(bundle); | 255 addBundle('/a.ds', bundle); |
| 232 createLinker(''' | 256 createLinker(''' |
| 233 import 'a.dart'; | 257 import 'a.dart'; |
| 234 var y = C.x; | 258 var y = C.x; |
| 235 '''); | 259 '''); |
| 236 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 260 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 237 expect(_getVariable(library.getContainedName('y')).inferredType.toString(), | 261 expect(_getVariable(library.getContainedName('y')).inferredType.toString(), |
| 238 '(D) → E'); | 262 '(D) → E'); |
| 239 } | 263 } |
| 240 | 264 |
| 241 void test_inferredType_instanceField_dynamic() { | 265 void test_inferredType_instanceField_dynamic() { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 'dynamic'); | 358 'dynamic'); |
| 335 } | 359 } |
| 336 | 360 |
| 337 void test_inferredTypeFromOutsideBuildUnit_dynamic() { | 361 void test_inferredTypeFromOutsideBuildUnit_dynamic() { |
| 338 var bundle = createPackageBundle( | 362 var bundle = createPackageBundle( |
| 339 ''' | 363 ''' |
| 340 var x; | 364 var x; |
| 341 var y = x; // Inferred type: dynamic | 365 var y = x; // Inferred type: dynamic |
| 342 ''', | 366 ''', |
| 343 path: '/a.dart'); | 367 path: '/a.dart'); |
| 344 addBundle(bundle); | 368 addBundle('/a.ds', bundle); |
| 345 createLinker(''' | 369 createLinker(''' |
| 346 import 'a.dart'; | 370 import 'a.dart'; |
| 347 var z = y; // Inferred type: dynamic | 371 var z = y; // Inferred type: dynamic |
| 348 '''); | 372 '''); |
| 349 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 373 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 350 expect(_getVariable(library.getContainedName('z')).inferredType.toString(), | 374 expect(_getVariable(library.getContainedName('z')).inferredType.toString(), |
| 351 'dynamic'); | 375 'dynamic'); |
| 352 } | 376 } |
| 353 | 377 |
| 354 void test_inferredTypeFromOutsideBuildUnit_instanceField() { | 378 void test_inferredTypeFromOutsideBuildUnit_instanceField() { |
| 355 var bundle = createPackageBundle( | 379 var bundle = createPackageBundle( |
| 356 ''' | 380 ''' |
| 357 class C { | 381 class C { |
| 358 var f = 0; // Inferred type: int | 382 var f = 0; // Inferred type: int |
| 359 } | 383 } |
| 360 ''', | 384 ''', |
| 361 path: '/a.dart'); | 385 path: '/a.dart'); |
| 362 addBundle(bundle); | 386 addBundle('/a.ds', bundle); |
| 363 createLinker(''' | 387 createLinker(''' |
| 364 import 'a.dart'; | 388 import 'a.dart'; |
| 365 var x = new C().f; // Inferred type: int | 389 var x = new C().f; // Inferred type: int |
| 366 '''); | 390 '''); |
| 367 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 391 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 368 expect(_getVariable(library.getContainedName('x')).inferredType.toString(), | 392 expect(_getVariable(library.getContainedName('x')).inferredType.toString(), |
| 369 'int'); | 393 'int'); |
| 370 } | 394 } |
| 371 | 395 |
| 372 void test_inferredTypeFromOutsideBuildUnit_instanceField_toInstanceField() { | 396 void test_inferredTypeFromOutsideBuildUnit_instanceField_toInstanceField() { |
| 373 var bundle = createPackageBundle( | 397 var bundle = createPackageBundle( |
| 374 ''' | 398 ''' |
| 375 class C { | 399 class C { |
| 376 var f = 0; // Inferred type: int | 400 var f = 0; // Inferred type: int |
| 377 } | 401 } |
| 378 ''', | 402 ''', |
| 379 path: '/a.dart'); | 403 path: '/a.dart'); |
| 380 addBundle(bundle); | 404 addBundle('/a.ds', bundle); |
| 381 createLinker(''' | 405 createLinker(''' |
| 382 import 'a.dart'; | 406 import 'a.dart'; |
| 383 class D { | 407 class D { |
| 384 var g = new C().f; // Inferred type: int | 408 var g = new C().f; // Inferred type: int |
| 385 } | 409 } |
| 386 '''); | 410 '''); |
| 387 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 411 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 388 ClassElementForLink_Class classD = library.getContainedName('D'); | 412 ClassElementForLink_Class classD = library.getContainedName('D'); |
| 389 expect(classD.fields[0].inferredType.toString(), 'int'); | 413 expect(classD.fields[0].inferredType.toString(), 'int'); |
| 390 } | 414 } |
| 391 | 415 |
| 392 void test_inferredTypeFromOutsideBuildUnit_methodParamType_viaGeneric() { | 416 void test_inferredTypeFromOutsideBuildUnit_methodParamType_viaGeneric() { |
| 393 var bundle = createPackageBundle( | 417 var bundle = createPackageBundle( |
| 394 ''' | 418 ''' |
| 395 class B { | 419 class B { |
| 396 T f<T>(T t) => t; | 420 T f<T>(T t) => t; |
| 397 } | 421 } |
| 398 class C extends B { | 422 class C extends B { |
| 399 f<T>(t) => t; // Inferred param type: T | 423 f<T>(t) => t; // Inferred param type: T |
| 400 } | 424 } |
| 401 ''', | 425 ''', |
| 402 path: '/a.dart'); | 426 path: '/a.dart'); |
| 403 addBundle(bundle); | 427 addBundle('/a.ds', bundle); |
| 404 createLinker(''' | 428 createLinker(''' |
| 405 import 'a.dart'; | 429 import 'a.dart'; |
| 406 var x = new C().f(0); // Inferred type: int | 430 var x = new C().f(0); // Inferred type: int |
| 407 '''); | 431 '''); |
| 408 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 432 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 409 expect(_getVariable(library.getContainedName('x')).inferredType.toString(), | 433 expect(_getVariable(library.getContainedName('x')).inferredType.toString(), |
| 410 'int'); | 434 'int'); |
| 411 } | 435 } |
| 412 | 436 |
| 413 void test_inferredTypeFromOutsideBuildUnit_methodParamType_viaInheritance() { | 437 void test_inferredTypeFromOutsideBuildUnit_methodParamType_viaInheritance() { |
| 414 var bundle = createPackageBundle( | 438 var bundle = createPackageBundle( |
| 415 ''' | 439 ''' |
| 416 class B { | 440 class B { |
| 417 void f(int i) {} | 441 void f(int i) {} |
| 418 } | 442 } |
| 419 class C extends B { | 443 class C extends B { |
| 420 f(i) {} // Inferred param type: int | 444 f(i) {} // Inferred param type: int |
| 421 } | 445 } |
| 422 ''', | 446 ''', |
| 423 path: '/a.dart'); | 447 path: '/a.dart'); |
| 424 addBundle(bundle); | 448 addBundle('/a.ds', bundle); |
| 425 createLinker(''' | 449 createLinker(''' |
| 426 import 'a.dart'; | 450 import 'a.dart'; |
| 427 class D extends C { | 451 class D extends C { |
| 428 f(i) {} // Inferred param type: int | 452 f(i) {} // Inferred param type: int |
| 429 } | 453 } |
| 430 '''); | 454 '''); |
| 431 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 455 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 432 library.libraryCycleForLink.ensureLinked(); | 456 library.libraryCycleForLink.ensureLinked(); |
| 433 ClassElementForLink_Class cls = library.getContainedName('D'); | 457 ClassElementForLink_Class cls = library.getContainedName('D'); |
| 434 expect(cls.methods, hasLength(1)); | 458 expect(cls.methods, hasLength(1)); |
| 435 var method = cls.methods[0]; | 459 var method = cls.methods[0]; |
| 436 expect(method.parameters, hasLength(1)); | 460 expect(method.parameters, hasLength(1)); |
| 437 expect(method.parameters[0].type.toString(), 'int'); | 461 expect(method.parameters[0].type.toString(), 'int'); |
| 438 } | 462 } |
| 439 | 463 |
| 440 void test_inferredTypeFromOutsideBuildUnit_methodReturnType_viaCall() { | 464 void test_inferredTypeFromOutsideBuildUnit_methodReturnType_viaCall() { |
| 441 var bundle = createPackageBundle( | 465 var bundle = createPackageBundle( |
| 442 ''' | 466 ''' |
| 443 class B { | 467 class B { |
| 444 int f() => 0; | 468 int f() => 0; |
| 445 } | 469 } |
| 446 class C extends B { | 470 class C extends B { |
| 447 f() => 1; // Inferred return type: int | 471 f() => 1; // Inferred return type: int |
| 448 } | 472 } |
| 449 ''', | 473 ''', |
| 450 path: '/a.dart'); | 474 path: '/a.dart'); |
| 451 addBundle(bundle); | 475 addBundle('/a.ds', bundle); |
| 452 createLinker(''' | 476 createLinker(''' |
| 453 import 'a.dart'; | 477 import 'a.dart'; |
| 454 var x = new C().f(); // Inferred type: int | 478 var x = new C().f(); // Inferred type: int |
| 455 '''); | 479 '''); |
| 456 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 480 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 457 expect(_getVariable(library.getContainedName('x')).inferredType.toString(), | 481 expect(_getVariable(library.getContainedName('x')).inferredType.toString(), |
| 458 'int'); | 482 'int'); |
| 459 } | 483 } |
| 460 | 484 |
| 461 void test_inferredTypeFromOutsideBuildUnit_methodReturnType_viaInheritance() { | 485 void test_inferredTypeFromOutsideBuildUnit_methodReturnType_viaInheritance() { |
| 462 var bundle = createPackageBundle( | 486 var bundle = createPackageBundle( |
| 463 ''' | 487 ''' |
| 464 class B { | 488 class B { |
| 465 int f() => 0; | 489 int f() => 0; |
| 466 } | 490 } |
| 467 class C extends B { | 491 class C extends B { |
| 468 f() => 1; // Inferred return type: int | 492 f() => 1; // Inferred return type: int |
| 469 } | 493 } |
| 470 ''', | 494 ''', |
| 471 path: '/a.dart'); | 495 path: '/a.dart'); |
| 472 addBundle(bundle); | 496 addBundle('/a.ds', bundle); |
| 473 createLinker(''' | 497 createLinker(''' |
| 474 import 'a.dart'; | 498 import 'a.dart'; |
| 475 class D extends C { | 499 class D extends C { |
| 476 f() => 2; //Inferred return type: int | 500 f() => 2; //Inferred return type: int |
| 477 } | 501 } |
| 478 '''); | 502 '''); |
| 479 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 503 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 480 library.libraryCycleForLink.ensureLinked(); | 504 library.libraryCycleForLink.ensureLinked(); |
| 481 ClassElementForLink_Class cls = library.getContainedName('D'); | 505 ClassElementForLink_Class cls = library.getContainedName('D'); |
| 482 expect(cls.methods, hasLength(1)); | 506 expect(cls.methods, hasLength(1)); |
| 483 expect(cls.methods[0].returnType.toString(), 'int'); | 507 expect(cls.methods[0].returnType.toString(), 'int'); |
| 484 } | 508 } |
| 485 | 509 |
| 486 void test_inferredTypeFromOutsideBuildUnit_staticField() { | 510 void test_inferredTypeFromOutsideBuildUnit_staticField() { |
| 487 var bundle = | 511 var bundle = |
| 488 createPackageBundle('class C { static var f = 0; }', path: '/a.dart'); | 512 createPackageBundle('class C { static var f = 0; }', path: '/a.dart'); |
| 489 addBundle(bundle); | 513 addBundle('/a.ds', bundle); |
| 490 createLinker('import "a.dart"; var x = C.f;', path: '/b.dart'); | 514 createLinker('import "a.dart"; var x = C.f;', path: '/b.dart'); |
| 491 expect( | 515 expect( |
| 492 _getVariable(linker | 516 _getVariable(linker |
| 493 .getLibrary(linkerInputs.testDartUri) | 517 .getLibrary(linkerInputs.testDartUri) |
| 494 .getContainedName('x')) | 518 .getContainedName('x')) |
| 495 .inferredType | 519 .inferredType |
| 496 .toString(), | 520 .toString(), |
| 497 'int'); | 521 'int'); |
| 498 } | 522 } |
| 499 | 523 |
| 500 void test_inferredTypeFromOutsideBuildUnit_topLevelVariable() { | 524 void test_inferredTypeFromOutsideBuildUnit_topLevelVariable() { |
| 501 var bundle = createPackageBundle('var a = 0;', path: '/a.dart'); | 525 var bundle = createPackageBundle('var a = 0;', path: '/a.dart'); |
| 502 addBundle(bundle); | 526 addBundle('/a.ds', bundle); |
| 503 createLinker('import "a.dart"; var b = a;', path: '/b.dart'); | 527 createLinker('import "a.dart"; var b = a;', path: '/b.dart'); |
| 504 expect( | 528 expect( |
| 505 _getVariable(linker | 529 _getVariable(linker |
| 506 .getLibrary(linkerInputs.testDartUri) | 530 .getLibrary(linkerInputs.testDartUri) |
| 507 .getContainedName('b')) | 531 .getContainedName('b')) |
| 508 .inferredType | 532 .inferredType |
| 509 .toString(), | 533 .toString(), |
| 510 'int'); | 534 'int'); |
| 511 } | 535 } |
| 512 | 536 |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 // refers to a non-existent local function. | 687 // refers to a non-existent local function. |
| 664 var bundle = createPackageBundle('var x = () {}', path: '/a.dart'); | 688 var bundle = createPackageBundle('var x = () {}', path: '/a.dart'); |
| 665 expect(bundle.linkedLibraries, hasLength(1)); | 689 expect(bundle.linkedLibraries, hasLength(1)); |
| 666 expect(bundle.linkedLibraries[0].units, hasLength(1)); | 690 expect(bundle.linkedLibraries[0].units, hasLength(1)); |
| 667 for (LinkedReferenceBuilder ref | 691 for (LinkedReferenceBuilder ref |
| 668 in bundle.linkedLibraries[0].units[0].references) { | 692 in bundle.linkedLibraries[0].units[0].references) { |
| 669 if (ref.kind == ReferenceKind.function) { | 693 if (ref.kind == ReferenceKind.function) { |
| 670 ref.localIndex = 1234; | 694 ref.localIndex = 1234; |
| 671 } | 695 } |
| 672 } | 696 } |
| 673 addBundle(bundle); | 697 addBundle('/a.ds', bundle); |
| 674 createLinker(''' | 698 createLinker(''' |
| 675 import 'a.dart'; | 699 import 'a.dart'; |
| 676 var y = x; | 700 var y = x; |
| 677 '''); | 701 '''); |
| 678 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); | 702 LibraryElementForLink library = linker.getLibrary(linkerInputs.testDartUri); |
| 679 expect(_getVariable(library.getContainedName('y')).inferredType.toString(), | 703 expect(_getVariable(library.getContainedName('y')).inferredType.toString(), |
| 680 'dynamic'); | 704 'dynamic'); |
| 681 } | 705 } |
| 682 | 706 |
| 683 @failingTest | 707 @failingTest |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 PropertyAccessorElementForLink_Variable j = library.getContainedName('j'); | 850 PropertyAccessorElementForLink_Variable j = library.getContainedName('j'); |
| 827 expect(j.variable.initializer, isNull); | 851 expect(j.variable.initializer, isNull); |
| 828 PropertyAccessorElementForLink_Variable v = library.getContainedName('v'); | 852 PropertyAccessorElementForLink_Variable v = library.getContainedName('v'); |
| 829 expect(v.variable.initializer, isNotNull); | 853 expect(v.variable.initializer, isNotNull); |
| 830 } | 854 } |
| 831 | 855 |
| 832 VariableElementForLink _getVariable(ReferenceableElementForLink element) { | 856 VariableElementForLink _getVariable(ReferenceableElementForLink element) { |
| 833 return (element as PropertyAccessorElementForLink_Variable).variable; | 857 return (element as PropertyAccessorElementForLink_Variable).variable; |
| 834 } | 858 } |
| 835 } | 859 } |
| OLD | NEW |