| 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 library analyzer.test.src.summary.summary_common; | 5 library analyzer.test.src.summary.summary_common; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/dart/ast/ast.dart'; | 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/src/dart/scanner/reader.dart'; | 10 import 'package:analyzer/src/dart/scanner/reader.dart'; |
| (...skipping 7715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7726 serializeClassText('class C { static var v = 0; }').fields[0]; | 7726 serializeClassText('class C { static var v = 0; }').fields[0]; |
| 7727 checkInferredTypeSlot(v.inferredTypeSlot, 'dart:core', 'dart:core', 'int'); | 7727 checkInferredTypeSlot(v.inferredTypeSlot, 'dart:core', 'dart:core', 'int'); |
| 7728 } | 7728 } |
| 7729 | 7729 |
| 7730 test_field_inferred_type_static_implicit_uninitialized() { | 7730 test_field_inferred_type_static_implicit_uninitialized() { |
| 7731 UnlinkedVariable v = | 7731 UnlinkedVariable v = |
| 7732 serializeClassText('class C { static var v; }').fields[0]; | 7732 serializeClassText('class C { static var v; }').fields[0]; |
| 7733 expect(v.inferredTypeSlot, 0); | 7733 expect(v.inferredTypeSlot, 0); |
| 7734 } | 7734 } |
| 7735 | 7735 |
| 7736 test_field_propagated_type_final_immediate() { | |
| 7737 UnlinkedVariable v = | |
| 7738 serializeClassText('class C { final v = 0; }').fields[0]; | |
| 7739 checkLinkedTypeSlot(v.propagatedTypeSlot, 'dart:core', 'dart:core', 'int'); | |
| 7740 } | |
| 7741 | |
| 7742 test_field_static() { | 7736 test_field_static() { |
| 7743 UnlinkedVariable variable = | 7737 UnlinkedVariable variable = |
| 7744 serializeClassText('class C { static int i; }').fields[0]; | 7738 serializeClassText('class C { static int i; }').fields[0]; |
| 7745 expect(variable.isStatic, isTrue); | 7739 expect(variable.isStatic, isTrue); |
| 7746 expect(variable.initializer, isNull); | 7740 expect(variable.initializer, isNull); |
| 7747 expect(unlinkedUnits[0].publicNamespace.names, hasLength(1)); | 7741 expect(unlinkedUnits[0].publicNamespace.names, hasLength(1)); |
| 7748 expect(unlinkedUnits[0].publicNamespace.names[0].name, 'C'); | 7742 expect(unlinkedUnits[0].publicNamespace.names[0].name, 'C'); |
| 7749 expect(unlinkedUnits[0].publicNamespace.names[0].members, hasLength(1)); | 7743 expect(unlinkedUnits[0].publicNamespace.names[0].members, hasLength(1)); |
| 7750 expect(unlinkedUnits[0].publicNamespace.names[0].members[0].name, 'i'); | 7744 expect(unlinkedUnits[0].publicNamespace.names[0].members[0].name, 'i'); |
| 7751 expect(unlinkedUnits[0].publicNamespace.names[0].members[0].kind, | 7745 expect(unlinkedUnits[0].publicNamespace.names[0].members[0].kind, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 7781 // The element model doesn't contain the initializer expressions needed | 7775 // The element model doesn't contain the initializer expressions needed |
| 7782 // for type inference. TODO(paulberry): fix. | 7776 // for type inference. TODO(paulberry): fix. |
| 7783 return; | 7777 return; |
| 7784 } | 7778 } |
| 7785 UnlinkedVariable variable = | 7779 UnlinkedVariable variable = |
| 7786 serializeClassText('class C { var x = 0; }').fields[0]; | 7780 serializeClassText('class C { var x = 0; }').fields[0]; |
| 7787 expect(variable.initializer.bodyExpr, isNotNull); | 7781 expect(variable.initializer.bodyExpr, isNotNull); |
| 7788 } | 7782 } |
| 7789 | 7783 |
| 7790 test_fully_linked_references_follow_other_references() { | 7784 test_fully_linked_references_follow_other_references() { |
| 7791 if (skipFullyLinkedData) { | 7785 if (!strongMode || skipFullyLinkedData) { |
| 7792 return; | 7786 return; |
| 7793 } | 7787 } |
| 7794 serializeLibraryText('final x = 0; String y;'); | 7788 serializeLibraryText('final x = 0; String y;'); |
| 7795 checkLinkedTypeSlot(unlinkedUnits[0].variables[0].propagatedTypeSlot, | 7789 checkLinkedTypeSlot(unlinkedUnits[0].variables[0].inferredTypeSlot, |
| 7796 'dart:core', 'dart:core', 'int'); | 7790 'dart:core', 'dart:core', 'int'); |
| 7797 checkTypeRef( | 7791 checkTypeRef( |
| 7798 unlinkedUnits[0].variables[1].type, 'dart:core', 'dart:core', 'String'); | 7792 unlinkedUnits[0].variables[1].type, 'dart:core', 'dart:core', 'String'); |
| 7799 // Even though the definition of y follows the definition of x, the linked | 7793 // Even though the definition of y follows the definition of x, the linked |
| 7800 // type reference for x should use a higher numbered reference than the | 7794 // type reference for x should use a higher numbered reference than the |
| 7801 // unlinked type reference for y. | 7795 // unlinked type reference for y. |
| 7802 EntityRef propagatedType = | 7796 EntityRef propagatedType = |
| 7803 getTypeRefForSlot(unlinkedUnits[0].variables[0].propagatedTypeSlot); | 7797 getTypeRefForSlot(unlinkedUnits[0].variables[0].inferredTypeSlot); |
| 7804 expect(unlinkedUnits[0].variables[1].type.reference, | 7798 expect(unlinkedUnits[0].variables[1].type.reference, |
| 7805 lessThan(propagatedType.reference)); | 7799 lessThan(propagatedType.reference)); |
| 7806 } | 7800 } |
| 7807 | 7801 |
| 7808 test_function_documented() { | 7802 test_function_documented() { |
| 7809 String text = ''' | 7803 String text = ''' |
| 7810 // Extra comment so doc comment offset != 0 | 7804 // Extra comment so doc comment offset != 0 |
| 7811 /** | 7805 /** |
| 7812 * Docs | 7806 * Docs |
| 7813 */ | 7807 */ |
| (...skipping 891 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8705 int foo; | 8699 int foo; |
| 8706 class Test {} | 8700 class Test {} |
| 8707 | 8701 |
| 8708 int bar;''' | 8702 int bar;''' |
| 8709 .replaceAll('\r\n', '\n'); | 8703 .replaceAll('\r\n', '\n'); |
| 8710 serializeLibraryText(text); | 8704 serializeLibraryText(text); |
| 8711 expect(unlinkedUnits[0].lineStarts, [0, 9, 23, 24]); | 8705 expect(unlinkedUnits[0].lineStarts, [0, 9, 23, 24]); |
| 8712 } | 8706 } |
| 8713 | 8707 |
| 8714 test_linked_reference_reuse() { | 8708 test_linked_reference_reuse() { |
| 8715 if (skipFullyLinkedData) { | 8709 if (!strongMode || skipFullyLinkedData) { |
| 8716 return; | 8710 return; |
| 8717 } | 8711 } |
| 8718 // When the reference for a linked type is the same as an explicitly | 8712 // When the reference for a linked type is the same as an explicitly |
| 8719 // referenced type, the explicit reference should be re-used. | 8713 // referenced type, the explicit reference should be re-used. |
| 8720 addNamedSource('/a.dart', 'class C {}'); | 8714 addNamedSource('/a.dart', 'class C {}'); |
| 8721 addNamedSource('/b.dart', 'import "a.dart"; C f() => null;'); | 8715 addNamedSource('/b.dart', 'import "a.dart"; C f() => null;'); |
| 8722 serializeLibraryText( | 8716 serializeLibraryText( |
| 8723 'import "a.dart"; import "b.dart"; C c1; final c2 = f();'); | 8717 'import "a.dart"; import "b.dart"; C c1; final c2 = f();'); |
| 8724 int explicitReference = findVariable('c1').type.reference; | 8718 int explicitReference = findVariable('c1').type.reference; |
| 8725 expect(getTypeRefForSlot(findVariable('c2').propagatedTypeSlot).reference, | 8719 expect(getTypeRefForSlot(findVariable('c2').inferredTypeSlot).reference, |
| 8726 explicitReference); | 8720 explicitReference); |
| 8727 } | 8721 } |
| 8728 | 8722 |
| 8729 test_linked_type_dependency_reuse() { | 8723 test_linked_type_dependency_reuse() { |
| 8730 if (skipFullyLinkedData) { | 8724 if (!strongMode || skipFullyLinkedData) { |
| 8731 return; | 8725 return; |
| 8732 } | 8726 } |
| 8733 // When the dependency for a linked type is the same as an explicit | 8727 // When the dependency for a linked type is the same as an explicit |
| 8734 // dependency, the explicit dependency should be re-used. | 8728 // dependency, the explicit dependency should be re-used. |
| 8735 addNamedSource('/a.dart', 'class C {} class D {}'); | 8729 addNamedSource('/a.dart', 'class C {} class D {}'); |
| 8736 addNamedSource('/b.dart', 'import "a.dart"; D f() => null;'); | 8730 addNamedSource('/b.dart', 'import "a.dart"; D f() => null;'); |
| 8737 serializeLibraryText( | 8731 serializeLibraryText( |
| 8738 'import "a.dart"; import "b.dart"; C c; final d = f();'); | 8732 'import "a.dart"; import "b.dart"; C c; final d = f();'); |
| 8739 int cReference = findVariable('c').type.reference; | 8733 int cReference = findVariable('c').type.reference; |
| 8740 int explicitDependency = linked.units[0].references[cReference].dependency; | 8734 int explicitDependency = linked.units[0].references[cReference].dependency; |
| 8741 int dReference = | 8735 int dReference = |
| 8742 getTypeRefForSlot(findVariable('d').propagatedTypeSlot).reference; | 8736 getTypeRefForSlot(findVariable('d').inferredTypeSlot).reference; |
| 8743 expect( | 8737 expect( |
| 8744 linked.units[0].references[dReference].dependency, explicitDependency); | 8738 linked.units[0].references[dReference].dependency, explicitDependency); |
| 8745 } | 8739 } |
| 8746 | 8740 |
| 8747 test_local_names_take_precedence_over_imported_names() { | 8741 test_local_names_take_precedence_over_imported_names() { |
| 8748 addNamedSource('/a.dart', 'class C {} class D {}'); | 8742 addNamedSource('/a.dart', 'class C {} class D {}'); |
| 8749 serializeLibraryText(''' | 8743 serializeLibraryText(''' |
| 8750 import 'a.dart'; | 8744 import 'a.dart'; |
| 8751 class C {} | 8745 class C {} |
| 8752 C c; | 8746 C c; |
| (...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9493 UnlinkedExecutable f = | 9487 UnlinkedExecutable f = |
| 9494 serializeExecutableText('set f(int value) {}', executableName: 'f='); | 9488 serializeExecutableText('set f(int value) {}', executableName: 'f='); |
| 9495 expect(f.inferredReturnTypeSlot, 0); | 9489 expect(f.inferredReturnTypeSlot, 0); |
| 9496 } | 9490 } |
| 9497 | 9491 |
| 9498 test_slot_reuse() { | 9492 test_slot_reuse() { |
| 9499 // Different compilation units have independent notions of slot id, so slot | 9493 // Different compilation units have independent notions of slot id, so slot |
| 9500 // ids should be reused. | 9494 // ids should be reused. |
| 9501 addNamedSource('/a.dart', 'part of foo; final v = 0;'); | 9495 addNamedSource('/a.dart', 'part of foo; final v = 0;'); |
| 9502 serializeLibraryText('library foo; part "a.dart"; final w = 0;'); | 9496 serializeLibraryText('library foo; part "a.dart"; final w = 0;'); |
| 9503 expect(unlinkedUnits[1].variables[0].propagatedTypeSlot, | 9497 expect(unlinkedUnits[1].variables[0].inferredTypeSlot, |
| 9504 unlinkedUnits[0].variables[0].propagatedTypeSlot); | 9498 unlinkedUnits[0].variables[0].inferredTypeSlot); |
| 9505 } | 9499 } |
| 9506 | 9500 |
| 9507 test_syntheticFunctionType_genericClosure() { | 9501 test_syntheticFunctionType_genericClosure() { |
| 9508 if (skipFullyLinkedData) { | 9502 if (skipFullyLinkedData) { |
| 9509 return; | 9503 return; |
| 9510 } | 9504 } |
| 9511 if (!strongMode) { | 9505 if (!strongMode) { |
| 9512 // The test below uses generic comment syntax because proper generic | 9506 // The test below uses generic comment syntax because proper generic |
| 9513 // method syntax doesn't support generic closures. So it can only run in | 9507 // method syntax doesn't support generic closures. So it can only run in |
| 9514 // strong mode. | 9508 // strong mode. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9585 } | 9579 } |
| 9586 ''').localVariables[0]; | 9580 ''').localVariables[0]; |
| 9587 EntityRef inferredType = | 9581 EntityRef inferredType = |
| 9588 getTypeRefForSlot(variable.initializer.inferredReturnTypeSlot); | 9582 getTypeRefForSlot(variable.initializer.inferredReturnTypeSlot); |
| 9589 checkLinkedTypeRef( | 9583 checkLinkedTypeRef( |
| 9590 inferredType.syntheticReturnType, 'dart:core', 'dart:core', 'int'); | 9584 inferredType.syntheticReturnType, 'dart:core', 'dart:core', 'int'); |
| 9591 checkParamTypeRef(inferredType.syntheticParams[0].type, 2); | 9585 checkParamTypeRef(inferredType.syntheticParams[0].type, 2); |
| 9592 checkParamTypeRef(inferredType.syntheticParams[1].type, 1); | 9586 checkParamTypeRef(inferredType.syntheticParams[1].type, 1); |
| 9593 } | 9587 } |
| 9594 | 9588 |
| 9595 test_syntheticFunctionType_noArguments() { | |
| 9596 if (skipFullyLinkedData) { | |
| 9597 return; | |
| 9598 } | |
| 9599 UnlinkedVariable variable = serializeVariableText(''' | |
| 9600 final v = f() ? () => 0 : () => 1; | |
| 9601 bool f() => true; | |
| 9602 '''); | |
| 9603 EntityRef propagatedType = getTypeRefForSlot(variable.propagatedTypeSlot); | |
| 9604 checkLinkedTypeRef( | |
| 9605 propagatedType.syntheticReturnType, 'dart:core', 'dart:core', 'int'); | |
| 9606 expect(propagatedType.syntheticParams, isEmpty); | |
| 9607 } | |
| 9608 | |
| 9609 test_syntheticFunctionType_withArguments() { | |
| 9610 if (skipFullyLinkedData) { | |
| 9611 return; | |
| 9612 } | |
| 9613 UnlinkedVariable variable = serializeVariableText(''' | |
| 9614 final v = f() ? (int x, String y) => 0 : (int x, String y) => 1; | |
| 9615 bool f() => true; | |
| 9616 '''); | |
| 9617 EntityRef propagatedType = getTypeRefForSlot(variable.propagatedTypeSlot); | |
| 9618 checkTypeRef( | |
| 9619 propagatedType.syntheticReturnType, 'dart:core', 'dart:core', 'int'); | |
| 9620 expect(propagatedType.syntheticParams, hasLength(2)); | |
| 9621 checkTypeRef(propagatedType.syntheticParams[0].type, 'dart:core', | |
| 9622 'dart:core', 'int'); | |
| 9623 checkTypeRef(propagatedType.syntheticParams[1].type, 'dart:core', | |
| 9624 'dart:core', 'String'); | |
| 9625 } | |
| 9626 | |
| 9627 test_type_arguments_explicit() { | 9589 test_type_arguments_explicit() { |
| 9628 EntityRef typeRef = serializeTypeText('List<int>'); | 9590 EntityRef typeRef = serializeTypeText('List<int>'); |
| 9629 checkTypeRef(typeRef, 'dart:core', 'dart:core', 'List', | 9591 checkTypeRef(typeRef, 'dart:core', 'dart:core', 'List', |
| 9630 numTypeParameters: 1, numTypeArguments: 1); | 9592 numTypeParameters: 1, numTypeArguments: 1); |
| 9631 checkTypeRef(typeRef.typeArguments[0], 'dart:core', 'dart:core', 'int'); | 9593 checkTypeRef(typeRef.typeArguments[0], 'dart:core', 'dart:core', 'int'); |
| 9632 } | 9594 } |
| 9633 | 9595 |
| 9634 test_type_arguments_explicit_dynamic() { | 9596 test_type_arguments_explicit_dynamic() { |
| 9635 EntityRef typeRef = serializeTypeText('List<dynamic>'); | 9597 EntityRef typeRef = serializeTypeText('List<dynamic>'); |
| 9636 checkTypeRef(typeRef, 'dart:core', 'dart:core', 'List', | 9598 checkTypeRef(typeRef, 'dart:core', 'dart:core', 'List', |
| (...skipping 691 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10328 UnlinkedVariable variable = | 10290 UnlinkedVariable variable = |
| 10329 serializeVariableText('int i;', variableName: 'i'); | 10291 serializeVariableText('int i;', variableName: 'i'); |
| 10330 expect(variable.isStatic, isFalse); | 10292 expect(variable.isStatic, isFalse); |
| 10331 } | 10293 } |
| 10332 | 10294 |
| 10333 test_variable_private() { | 10295 test_variable_private() { |
| 10334 serializeVariableText('int _i;', variableName: '_i'); | 10296 serializeVariableText('int _i;', variableName: '_i'); |
| 10335 expect(unlinkedUnits[0].publicNamespace.names, isEmpty); | 10297 expect(unlinkedUnits[0].publicNamespace.names, isEmpty); |
| 10336 } | 10298 } |
| 10337 | 10299 |
| 10338 test_variable_propagated_type_final_immediate() { | |
| 10339 UnlinkedVariable v = serializeVariableText('final v = 0;'); | |
| 10340 checkLinkedTypeSlot(v.propagatedTypeSlot, 'dart:core', 'dart:core', 'int'); | |
| 10341 } | |
| 10342 | |
| 10343 test_variable_propagated_type_new_reference() { | |
| 10344 if (skipFullyLinkedData) { | |
| 10345 return; | |
| 10346 } | |
| 10347 UnlinkedVariable v = serializeVariableText('final v = 0;'); | |
| 10348 // Since the propagated type of `v` is `int`, and there are no references | |
| 10349 // to `int` elsewhere in the source file, a new linked reference should | |
| 10350 // have been created for it, with no associated unlinked reference. | |
| 10351 expect(v.propagatedTypeSlot, isNot(0)); | |
| 10352 EntityRef type = getTypeRefForSlot(v.propagatedTypeSlot); | |
| 10353 expect(type, isNotNull); | |
| 10354 expect(type.reference, | |
| 10355 greaterThanOrEqualTo(unlinkedUnits[0].references.length)); | |
| 10356 } | |
| 10357 | |
| 10358 test_variable_propagated_type_omit_dynamic() { | |
| 10359 if (skipFullyLinkedData) { | |
| 10360 return; | |
| 10361 } | |
| 10362 UnlinkedVariable v = serializeVariableText('final v = <int, dynamic>{};'); | |
| 10363 EntityRef type = getTypeRefForSlot(v.propagatedTypeSlot); | |
| 10364 checkLinkedTypeRef(type, 'dart:core', 'dart:core', 'Map', | |
| 10365 numTypeParameters: 2, numTypeArguments: 2); | |
| 10366 checkLinkedTypeRef(type.typeArguments[0], 'dart:core', 'dart:core', 'int'); | |
| 10367 checkLinkedDynamicTypeRef(type.typeArguments[1]); | |
| 10368 } | |
| 10369 | |
| 10370 test_variable_propagatedTypeSlot_const() { | |
| 10371 // Const variables are propagable so they have a nonzero | |
| 10372 // propagatedTypeSlot. | |
| 10373 UnlinkedVariable variable = serializeVariableText('const v = 0;'); | |
| 10374 expect(variable.propagatedTypeSlot, isNot(0)); | |
| 10375 } | |
| 10376 | |
| 10377 test_variable_propagatedTypeSlot_final() { | |
| 10378 // Final variables are propagable so they have a nonzero | |
| 10379 // propagatedTypeSlot. | |
| 10380 UnlinkedVariable variable = serializeVariableText('final v = 0;'); | |
| 10381 expect(variable.propagatedTypeSlot, isNot(0)); | |
| 10382 } | |
| 10383 | |
| 10384 test_variable_propagatedTypeSlot_non_propagable() { | |
| 10385 // Non-final non-const variables aren't propagable so they don't have a | |
| 10386 // propagatedTypeSlot. | |
| 10387 UnlinkedVariable variable = serializeVariableText('var v;'); | |
| 10388 expect(variable.propagatedTypeSlot, 0); | |
| 10389 } | |
| 10390 | |
| 10391 test_variable_static() { | 10300 test_variable_static() { |
| 10392 UnlinkedVariable variable = | 10301 UnlinkedVariable variable = |
| 10393 serializeClassText('class C { static int i; }').fields[0]; | 10302 serializeClassText('class C { static int i; }').fields[0]; |
| 10394 expect(variable.isStatic, isTrue); | 10303 expect(variable.isStatic, isTrue); |
| 10395 } | 10304 } |
| 10396 | 10305 |
| 10397 test_variable_type() { | 10306 test_variable_type() { |
| 10398 UnlinkedVariable variable = | 10307 UnlinkedVariable variable = |
| 10399 serializeVariableText('int i;', variableName: 'i'); | 10308 serializeVariableText('int i;', variableName: 'i'); |
| 10400 checkTypeRef(variable.type, 'dart:core', 'dart:core', 'int'); | 10309 checkTypeRef(variable.type, 'dart:core', 'dart:core', 'int'); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10578 class _PrefixExpectation { | 10487 class _PrefixExpectation { |
| 10579 final ReferenceKind kind; | 10488 final ReferenceKind kind; |
| 10580 final String name; | 10489 final String name; |
| 10581 final String absoluteUri; | 10490 final String absoluteUri; |
| 10582 final String relativeUri; | 10491 final String relativeUri; |
| 10583 final int numTypeParameters; | 10492 final int numTypeParameters; |
| 10584 | 10493 |
| 10585 _PrefixExpectation(this.kind, this.name, | 10494 _PrefixExpectation(this.kind, this.name, |
| 10586 {this.absoluteUri, this.relativeUri, this.numTypeParameters: 0}); | 10495 {this.absoluteUri, this.relativeUri, this.numTypeParameters: 0}); |
| 10587 } | 10496 } |
| OLD | NEW |