| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 engine.resolver_test; | 5 library engine.resolver_test; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| 11 import 'package:analyzer/src/generated/element_resolver.dart'; | 11 import 'package:analyzer/src/generated/element_resolver.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/error.dart'; | 13 import 'package:analyzer/src/generated/error.dart'; |
| 14 import 'package:analyzer/src/generated/java_core.dart'; | 14 import 'package:analyzer/src/generated/java_core.dart'; |
| 15 import 'package:analyzer/src/generated/java_engine.dart'; | 15 import 'package:analyzer/src/generated/java_engine.dart'; |
| 16 import 'package:analyzer/src/generated/java_engine_io.dart'; | 16 import 'package:analyzer/src/generated/java_engine_io.dart'; |
| 17 import 'package:analyzer/src/generated/java_io.dart'; | 17 import 'package:analyzer/src/generated/java_io.dart'; |
| 18 import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode; | 18 import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode; |
| 19 import 'package:analyzer/src/generated/resolver.dart'; | 19 import 'package:analyzer/src/generated/resolver.dart'; |
| 20 import 'package:analyzer/src/generated/scanner.dart'; | 20 import 'package:analyzer/src/generated/scanner.dart'; |
| 21 import 'package:analyzer/src/generated/sdk.dart'; | 21 import 'package:analyzer/src/generated/sdk.dart'; |
| 22 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | 22 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; |
| 23 import 'package:analyzer/src/generated/source_io.dart'; | 23 import 'package:analyzer/src/generated/source_io.dart'; |
| 24 import 'package:analyzer/src/generated/static_type_analyzer.dart'; | 24 import 'package:analyzer/src/generated/static_type_analyzer.dart'; |
| 25 import 'package:analyzer/src/generated/testing/ast_factory.dart'; | 25 import 'package:analyzer/src/generated/testing/ast_factory.dart'; |
| 26 import 'package:analyzer/src/generated/testing/element_factory.dart'; |
| 26 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; | 27 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; |
| 27 import 'package:analyzer/src/generated/testing/element_factory.dart'; | |
| 28 import 'package:analyzer/src/generated/utilities_dart.dart'; | 28 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 29 import 'package:unittest/unittest.dart'; | 29 import 'package:unittest/unittest.dart'; |
| 30 | 30 |
| 31 import '../reflective_tests.dart'; | 31 import '../reflective_tests.dart'; |
| 32 import 'test_support.dart'; | 32 import 'test_support.dart'; |
| 33 | 33 |
| 34 | 34 |
| 35 main() { | 35 main() { |
| 36 groupSep = ' | '; | 36 groupSep = ' | '; |
| 37 runReflectiveTests(AnalysisDeltaTest); | 37 runReflectiveTests(AnalysisDeltaTest); |
| (...skipping 4561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4599 _inheritanceManager.getMapOfMembersInheritedFromClasses(classB); | 4599 _inheritanceManager.getMapOfMembersInheritedFromClasses(classB); |
| 4600 MemberMap mapA = | 4600 MemberMap mapA = |
| 4601 _inheritanceManager.getMapOfMembersInheritedFromClasses(classA); | 4601 _inheritanceManager.getMapOfMembersInheritedFromClasses(classA); |
| 4602 expect(mapA.size, _numOfMembersInObject); | 4602 expect(mapA.size, _numOfMembersInObject); |
| 4603 expect(mapB.size, _numOfMembersInObject + 1); | 4603 expect(mapB.size, _numOfMembersInObject + 1); |
| 4604 expect(mapB.get(methodName), same(methodM)); | 4604 expect(mapB.get(methodName), same(methodM)); |
| 4605 _assertNoErrors(classA); | 4605 _assertNoErrors(classA); |
| 4606 _assertNoErrors(classB); | 4606 _assertNoErrors(classB); |
| 4607 } | 4607 } |
| 4608 | 4608 |
| 4609 void test_getMapOfMembersInheritedFromClasses_method_with_two_mixins() { |
| 4610 // class A1 { int m(); } |
| 4611 // class A2 { int m(); } |
| 4612 // class B extends Object with A1, A2 {} |
| 4613 ClassElementImpl classA1 = ElementFactory.classElement2("A1"); |
| 4614 String methodName = "m"; |
| 4615 MethodElement methodA1M = |
| 4616 ElementFactory.methodElement(methodName, _typeProvider.intType); |
| 4617 classA1.methods = <MethodElement>[methodA1M]; |
| 4618 ClassElementImpl classA2 = ElementFactory.classElement2("A2"); |
| 4619 MethodElement methodA2M = |
| 4620 ElementFactory.methodElement(methodName, _typeProvider.intType); |
| 4621 classA2.methods = <MethodElement>[methodA2M]; |
| 4622 ClassElementImpl classB = ElementFactory.classElement2("B"); |
| 4623 classB.mixins = <InterfaceType>[classA1.type, classA2.type]; |
| 4624 MemberMap mapB = |
| 4625 _inheritanceManager.getMapOfMembersInheritedFromClasses(classB); |
| 4626 expect(mapB.get(methodName), same(methodA2M)); |
| 4627 _assertNoErrors(classA1); |
| 4628 _assertNoErrors(classA2); |
| 4629 _assertNoErrors(classB); |
| 4630 } |
| 4631 |
| 4609 void test_getMapOfMembersInheritedFromInterfaces_accessor_extends() { | 4632 void test_getMapOfMembersInheritedFromInterfaces_accessor_extends() { |
| 4610 // class A { int get g; } | 4633 // class A { int get g; } |
| 4611 // class B extends A {} | 4634 // class B extends A {} |
| 4612 ClassElementImpl classA = ElementFactory.classElement2("A"); | 4635 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 4613 String getterName = "g"; | 4636 String getterName = "g"; |
| 4614 PropertyAccessorElement getterG = | 4637 PropertyAccessorElement getterG = |
| 4615 ElementFactory.getterElement(getterName, false, _typeProvider.intType); | 4638 ElementFactory.getterElement(getterName, false, _typeProvider.intType); |
| 4616 classA.accessors = <PropertyAccessorElement>[getterG]; | 4639 classA.accessors = <PropertyAccessorElement>[getterG]; |
| 4617 ClassElementImpl classB = ElementFactory.classElement("B", classA.type); | 4640 ClassElementImpl classB = ElementFactory.classElement("B", classA.type); |
| 4618 MemberMap mapB = | 4641 MemberMap mapB = |
| (...skipping 1703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6322 MemberMap map = new MemberMap(); | 6345 MemberMap map = new MemberMap(); |
| 6323 expect(map.size, 0); | 6346 expect(map.size, 0); |
| 6324 map.put(m1.name, m1); | 6347 map.put(m1.name, m1); |
| 6325 expect(map.size, 1); | 6348 expect(map.size, 1); |
| 6326 expect(map.get("m1"), m1); | 6349 expect(map.get("m1"), m1); |
| 6327 } | 6350 } |
| 6328 } | 6351 } |
| 6329 | 6352 |
| 6330 @reflectiveTest | 6353 @reflectiveTest |
| 6331 class NonHintCodeTest extends ResolverTestCase { | 6354 class NonHintCodeTest extends ResolverTestCase { |
| 6355 void fail_propagatedFieldType() { |
| 6356 // From dartbug.com/20019 |
| 6357 Source source = addSource(r''' |
| 6358 class A { } |
| 6359 class X<T> { |
| 6360 final x = new List<T>(); |
| 6361 } |
| 6362 class Z { |
| 6363 final X<A> y = new X<A>(); |
| 6364 foo() { |
| 6365 y.x.add(new A()); |
| 6366 } |
| 6367 }'''); |
| 6368 resolve(source); |
| 6369 assertNoErrors(source); |
| 6370 verify([source]); |
| 6371 } |
| 6372 |
| 6332 void test_deadCode_deadBlock_conditionalElse_debugConst() { | 6373 void test_deadCode_deadBlock_conditionalElse_debugConst() { |
| 6333 Source source = addSource(r''' | 6374 Source source = addSource(r''' |
| 6334 const bool DEBUG = true; | 6375 const bool DEBUG = true; |
| 6335 f() { | 6376 f() { |
| 6336 DEBUG ? 1 : 2; | 6377 DEBUG ? 1 : 2; |
| 6337 }'''); | 6378 }'''); |
| 6338 resolve(source); | 6379 resolve(source); |
| 6339 assertNoErrors(source); | 6380 assertNoErrors(source); |
| 6340 verify([source]); | 6381 verify([source]); |
| 6341 } | 6382 } |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6544 assertNoErrors(source); | 6585 assertNoErrors(source); |
| 6545 verify([source]); | 6586 verify([source]); |
| 6546 } | 6587 } |
| 6547 | 6588 |
| 6548 void test_importDeferredLibraryWithLoadFunction() { | 6589 void test_importDeferredLibraryWithLoadFunction() { |
| 6549 resolveWithErrors(<String>[r''' | 6590 resolveWithErrors(<String>[r''' |
| 6550 library lib1; | 6591 library lib1; |
| 6551 f() {}''', r''' | 6592 f() {}''', r''' |
| 6552 library root; | 6593 library root; |
| 6553 import 'lib1.dart' deferred as lib1; | 6594 import 'lib1.dart' deferred as lib1; |
| 6554 main() { lib1.f(); }'''], | 6595 main() { lib1.f(); }'''], ErrorCode.EMPTY_LIST); |
| 6555 ErrorCode.EMPTY_LIST); | |
| 6556 } | 6596 } |
| 6557 | 6597 |
| 6558 void test_issue20904BuggyTypePromotionAtIfJoin_1() { | 6598 void test_issue20904BuggyTypePromotionAtIfJoin_1() { |
| 6559 // https://code.google.com/p/dart/issues/detail?id=20904 | 6599 // https://code.google.com/p/dart/issues/detail?id=20904 |
| 6560 Source source = addSource(r''' | 6600 Source source = addSource(r''' |
| 6561 f(var message, var dynamic_) { | 6601 f(var message, var dynamic_) { |
| 6562 if (message is Function) { | 6602 if (message is Function) { |
| 6563 message = dynamic_; | 6603 message = dynamic_; |
| 6564 } | 6604 } |
| 6565 int s = message; | 6605 int s = message; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6748 } | 6788 } |
| 6749 class B extends A { | 6789 class B extends A { |
| 6750 @override | 6790 @override |
| 6751 set m(int x) {} | 6791 set m(int x) {} |
| 6752 }'''); | 6792 }'''); |
| 6753 resolve(source); | 6793 resolve(source); |
| 6754 assertNoErrors(source); | 6794 assertNoErrors(source); |
| 6755 verify([source]); | 6795 verify([source]); |
| 6756 } | 6796 } |
| 6757 | 6797 |
| 6758 void fail_propagatedFieldType() { | |
| 6759 // From dartbug.com/20019 | |
| 6760 Source source = addSource(r''' | |
| 6761 class A { } | |
| 6762 class X<T> { | |
| 6763 final x = new List<T>(); | |
| 6764 } | |
| 6765 class Z { | |
| 6766 final X<A> y = new X<A>(); | |
| 6767 foo() { | |
| 6768 y.x.add(new A()); | |
| 6769 } | |
| 6770 }'''); | |
| 6771 resolve(source); | |
| 6772 assertNoErrors(source); | |
| 6773 verify([source]); | |
| 6774 } | |
| 6775 | |
| 6776 void test_proxy_annotation_prefixed() { | 6798 void test_proxy_annotation_prefixed() { |
| 6777 Source source = addSource(r''' | 6799 Source source = addSource(r''' |
| 6778 library L; | 6800 library L; |
| 6779 @proxy | 6801 @proxy |
| 6780 class A {} | 6802 class A {} |
| 6781 f(var a) { | 6803 f(var a) { |
| 6782 a = new A(); | 6804 a = new A(); |
| 6783 a.m(); | 6805 a.m(); |
| 6784 var x = a.g; | 6806 var x = a.g; |
| 6785 a.s = 1; | 6807 a.s = 1; |
| (...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7873 CompilationUnit unit = | 7895 CompilationUnit unit = |
| 7874 resolveSource2("/lib${i + 1}.dart", sourceTexts[i]); | 7896 resolveSource2("/lib${i + 1}.dart", sourceTexts[i]); |
| 7875 // reference the source if this is the last source | 7897 // reference the source if this is the last source |
| 7876 if (i + 1 == sourceTexts.length) { | 7898 if (i + 1 == sourceTexts.length) { |
| 7877 return unit.element.source; | 7899 return unit.element.source; |
| 7878 } | 7900 } |
| 7879 } | 7901 } |
| 7880 return null; | 7902 return null; |
| 7881 } | 7903 } |
| 7882 | 7904 |
| 7883 void resolveWithErrors(List<String> strSources, List<ErrorCode> codes) { | |
| 7884 // Analysis and assertions | |
| 7885 Source source = resolveSources(strSources); | |
| 7886 assertErrors(source, codes); | |
| 7887 verify([source]); | |
| 7888 } | |
| 7889 | |
| 7890 void resolveWithAndWithoutExperimental(List<String> strSources, | 7905 void resolveWithAndWithoutExperimental(List<String> strSources, |
| 7891 List<ErrorCode> codesWithoutExperimental, | 7906 List<ErrorCode> codesWithoutExperimental, |
| 7892 List<ErrorCode> codesWithExperimental) { | 7907 List<ErrorCode> codesWithExperimental) { |
| 7893 // Setup analysis context as non-experimental | 7908 // Setup analysis context as non-experimental |
| 7894 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 7909 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
| 7895 // options.enableDeferredLoading = false; | 7910 // options.enableDeferredLoading = false; |
| 7896 resetWithOptions(options); | 7911 resetWithOptions(options); |
| 7897 // Analysis and assertions | 7912 // Analysis and assertions |
| 7898 Source source = resolveSources(strSources); | 7913 Source source = resolveSources(strSources); |
| 7899 assertErrors(source, codesWithoutExperimental); | 7914 assertErrors(source, codesWithoutExperimental); |
| 7900 verify([source]); | 7915 verify([source]); |
| 7901 // Setup analysis context as experimental | 7916 // Setup analysis context as experimental |
| 7902 reset(); | 7917 reset(); |
| 7903 // Analysis and assertions | 7918 // Analysis and assertions |
| 7904 source = resolveSources(strSources); | 7919 source = resolveSources(strSources); |
| 7905 assertErrors(source, codesWithExperimental); | 7920 assertErrors(source, codesWithExperimental); |
| 7906 verify([source]); | 7921 verify([source]); |
| 7907 } | 7922 } |
| 7908 | 7923 |
| 7924 void resolveWithErrors(List<String> strSources, List<ErrorCode> codes) { |
| 7925 // Analysis and assertions |
| 7926 Source source = resolveSources(strSources); |
| 7927 assertErrors(source, codes); |
| 7928 verify([source]); |
| 7929 } |
| 7930 |
| 7909 @override | 7931 @override |
| 7910 void setUp() { | 7932 void setUp() { |
| 7911 reset(); | 7933 reset(); |
| 7912 } | 7934 } |
| 7913 | 7935 |
| 7914 @override | 7936 @override |
| 7915 void tearDown() { | 7937 void tearDown() { |
| 7916 analysisContext2 = null; | 7938 analysisContext2 = null; |
| 7917 super.tearDown(); | 7939 super.tearDown(); |
| 7918 } | 7940 } |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8029 ScopeTest_TestScope(this.errorListener); | 8051 ScopeTest_TestScope(this.errorListener); |
| 8030 | 8052 |
| 8031 @override | 8053 @override |
| 8032 Element internalLookup(Identifier identifier, String name, | 8054 Element internalLookup(Identifier identifier, String name, |
| 8033 LibraryElement referencingLibrary) => | 8055 LibraryElement referencingLibrary) => |
| 8034 localLookup(name, referencingLibrary); | 8056 localLookup(name, referencingLibrary); |
| 8035 } | 8057 } |
| 8036 | 8058 |
| 8037 @reflectiveTest | 8059 @reflectiveTest |
| 8038 class SimpleResolverTest extends ResolverTestCase { | 8060 class SimpleResolverTest extends ResolverTestCase { |
| 8061 void fail_getter_and_setter_fromMixins_property_access() { |
| 8062 // TODO(paulberry): it appears that auxiliaryElements isn't properly set on |
| 8063 // a SimpleIdentifier that's inside a property access. This bug should be |
| 8064 // fixed. |
| 8065 Source source = addSource(''' |
| 8066 class B {} |
| 8067 class M1 { |
| 8068 get x => null; |
| 8069 set x(value) {} |
| 8070 } |
| 8071 class M2 { |
| 8072 get x => null; |
| 8073 set x(value) {} |
| 8074 } |
| 8075 class C extends B with M1, M2 {} |
| 8076 void main() { |
| 8077 new C().x += 1; |
| 8078 } |
| 8079 '''); |
| 8080 LibraryElement library = resolve(source); |
| 8081 assertNoErrors(source); |
| 8082 verify([source]); |
| 8083 // Verify that both the getter and setter for "x" in "new C().x" refer to |
| 8084 // the accessors defined in M2. |
| 8085 FunctionDeclaration main = |
| 8086 library.definingCompilationUnit.functions[0].node; |
| 8087 BlockFunctionBody body = main.functionExpression.body; |
| 8088 ExpressionStatement stmt = body.block.statements[0]; |
| 8089 AssignmentExpression assignment = stmt.expression; |
| 8090 PropertyAccess propertyAccess = assignment.leftHandSide; |
| 8091 expect( |
| 8092 propertyAccess.propertyName.staticElement.enclosingElement.name, |
| 8093 'M2'); |
| 8094 expect( |
| 8095 propertyAccess.propertyName.auxiliaryElements.staticElement.enclosingEle
ment.name, |
| 8096 'M2'); |
| 8097 } |
| 8098 |
| 8039 void fail_staticInvocation() { | 8099 void fail_staticInvocation() { |
| 8040 Source source = addSource(r''' | 8100 Source source = addSource(r''' |
| 8041 class A { | 8101 class A { |
| 8042 static int get g => (a,b) => 0; | 8102 static int get g => (a,b) => 0; |
| 8043 } | 8103 } |
| 8044 class B { | 8104 class B { |
| 8045 f() { | 8105 f() { |
| 8046 A.g(1,0); | 8106 A.g(1,0); |
| 8047 } | 8107 } |
| 8048 }'''); | 8108 }'''); |
| (...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8727 P p; | 8787 P p; |
| 8728 m(e) { | 8788 m(e) { |
| 8729 if (p(e)) {} | 8789 if (p(e)) {} |
| 8730 } | 8790 } |
| 8731 }'''); | 8791 }'''); |
| 8732 resolve(source); | 8792 resolve(source); |
| 8733 assertNoErrors(source); | 8793 assertNoErrors(source); |
| 8734 verify([source]); | 8794 verify([source]); |
| 8735 } | 8795 } |
| 8736 | 8796 |
| 8797 void test_getter_and_setter_fromMixins_bare_identifier() { |
| 8798 Source source = addSource(''' |
| 8799 class B {} |
| 8800 class M1 { |
| 8801 get x => null; |
| 8802 set x(value) {} |
| 8803 } |
| 8804 class M2 { |
| 8805 get x => null; |
| 8806 set x(value) {} |
| 8807 } |
| 8808 class C extends B with M1, M2 { |
| 8809 void f() { |
| 8810 x += 1; |
| 8811 } |
| 8812 } |
| 8813 '''); |
| 8814 LibraryElement library = resolve(source); |
| 8815 assertNoErrors(source); |
| 8816 verify([source]); |
| 8817 // Verify that both the getter and setter for "x" in C.f() refer to the |
| 8818 // accessors defined in M2. |
| 8819 ClassElement classC = library.definingCompilationUnit.types[3]; |
| 8820 MethodDeclaration f = classC.getMethod('f').node; |
| 8821 BlockFunctionBody body = f.body; |
| 8822 ExpressionStatement stmt = body.block.statements[0]; |
| 8823 AssignmentExpression assignment = stmt.expression; |
| 8824 SimpleIdentifier leftHandSide = assignment.leftHandSide; |
| 8825 expect(leftHandSide.staticElement.enclosingElement.name, 'M2'); |
| 8826 expect( |
| 8827 leftHandSide.auxiliaryElements.staticElement.enclosingElement.name, |
| 8828 'M2'); |
| 8829 } |
| 8830 |
| 8831 void test_getter_fromMixins_bare_identifier() { |
| 8832 Source source = addSource(''' |
| 8833 class B {} |
| 8834 class M1 { |
| 8835 get x => null; |
| 8836 } |
| 8837 class M2 { |
| 8838 get x => null; |
| 8839 } |
| 8840 class C extends B with M1, M2 { |
| 8841 f() { |
| 8842 return x; |
| 8843 } |
| 8844 } |
| 8845 '''); |
| 8846 LibraryElement library = resolve(source); |
| 8847 assertNoErrors(source); |
| 8848 verify([source]); |
| 8849 // Verify that the getter for "x" in C.f() refers to the getter defined in |
| 8850 // M2. |
| 8851 ClassElement classC = library.definingCompilationUnit.types[3]; |
| 8852 MethodDeclaration f = classC.getMethod('f').node; |
| 8853 BlockFunctionBody body = f.body; |
| 8854 ReturnStatement stmt = body.block.statements[0]; |
| 8855 SimpleIdentifier x = stmt.expression; |
| 8856 expect(x.staticElement.enclosingElement.name, 'M2'); |
| 8857 } |
| 8858 |
| 8859 void test_getter_fromMixins_property_access() { |
| 8860 Source source = addSource(''' |
| 8861 class B {} |
| 8862 class M1 { |
| 8863 get x => null; |
| 8864 } |
| 8865 class M2 { |
| 8866 get x => null; |
| 8867 } |
| 8868 class C extends B with M1, M2 {} |
| 8869 void main() { |
| 8870 var y = new C().x; |
| 8871 } |
| 8872 '''); |
| 8873 LibraryElement library = resolve(source); |
| 8874 assertNoErrors(source); |
| 8875 verify([source]); |
| 8876 // Verify that the getter for "x" in "new C().x" refers to the getter |
| 8877 // defined in M2. |
| 8878 FunctionDeclaration main = |
| 8879 library.definingCompilationUnit.functions[0].node; |
| 8880 BlockFunctionBody body = main.functionExpression.body; |
| 8881 VariableDeclarationStatement stmt = body.block.statements[0]; |
| 8882 PropertyAccess propertyAccess = stmt.variables.variables[0].initializer; |
| 8883 expect( |
| 8884 propertyAccess.propertyName.staticElement.enclosingElement.name, |
| 8885 'M2'); |
| 8886 } |
| 8887 |
| 8737 void test_getterAndSetterWithDifferentTypes() { | 8888 void test_getterAndSetterWithDifferentTypes() { |
| 8738 Source source = addSource(r''' | 8889 Source source = addSource(r''' |
| 8739 class A { | 8890 class A { |
| 8740 int get f => 0; | 8891 int get f => 0; |
| 8741 void set f(String s) {} | 8892 void set f(String s) {} |
| 8742 } | 8893 } |
| 8743 g (A a) { | 8894 g (A a) { |
| 8744 a.f = a.f.toString(); | 8895 a.f = a.f.toString(); |
| 8745 }'''); | 8896 }'''); |
| 8746 resolve(source); | 8897 resolve(source); |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9221 | 9372 |
| 9222 class C extends B with A { | 9373 class C extends B with A { |
| 9223 bar() => super.bar(); | 9374 bar() => super.bar(); |
| 9224 foo() => super.foo(); | 9375 foo() => super.foo(); |
| 9225 }'''); | 9376 }'''); |
| 9226 resolve(source); | 9377 resolve(source); |
| 9227 assertNoErrors(source); | 9378 assertNoErrors(source); |
| 9228 verify([source]); | 9379 verify([source]); |
| 9229 } | 9380 } |
| 9230 | 9381 |
| 9382 void test_method_fromMixins() { |
| 9383 Source source = addSource(''' |
| 9384 class B {} |
| 9385 class M1 { |
| 9386 void f() {} |
| 9387 } |
| 9388 class M2 { |
| 9389 void f() {} |
| 9390 } |
| 9391 class C extends B with M1, M2 {} |
| 9392 void main() { |
| 9393 new C().f(); |
| 9394 } |
| 9395 '''); |
| 9396 LibraryElement library = resolve(source); |
| 9397 assertNoErrors(source); |
| 9398 verify([source]); |
| 9399 // Verify that the "f" in "new C().f()" refers to the "f" defined in M2. |
| 9400 FunctionDeclaration main = |
| 9401 library.definingCompilationUnit.functions[0].node; |
| 9402 BlockFunctionBody body = main.functionExpression.body; |
| 9403 ExpressionStatement stmt = body.block.statements[0]; |
| 9404 MethodInvocation expr = stmt.expression; |
| 9405 expect(expr.methodName.staticElement.enclosingElement.name, 'M2'); |
| 9406 } |
| 9407 |
| 9408 void test_method_fromMixins_bare_identifier() { |
| 9409 Source source = addSource(''' |
| 9410 class B {} |
| 9411 class M1 { |
| 9412 void f() {} |
| 9413 } |
| 9414 class M2 { |
| 9415 void f() {} |
| 9416 } |
| 9417 class C extends B with M1, M2 { |
| 9418 void g() { |
| 9419 f(); |
| 9420 } |
| 9421 } |
| 9422 '''); |
| 9423 LibraryElement library = resolve(source); |
| 9424 assertNoErrors(source); |
| 9425 verify([source]); |
| 9426 // Verify that the call to f() in C.g() refers to the method defined in M2. |
| 9427 ClassElement classC = library.definingCompilationUnit.types[3]; |
| 9428 MethodDeclaration g = classC.getMethod('g').node; |
| 9429 BlockFunctionBody body = g.body; |
| 9430 ExpressionStatement stmt = body.block.statements[0]; |
| 9431 MethodInvocation invocation = stmt.expression; |
| 9432 SimpleIdentifier methodName = invocation.methodName; |
| 9433 expect(methodName.staticElement.enclosingElement.name, 'M2'); |
| 9434 } |
| 9435 |
| 9436 void test_method_fromMixins_invked_from_outside_class() { |
| 9437 Source source = addSource(''' |
| 9438 class B {} |
| 9439 class M1 { |
| 9440 void f() {} |
| 9441 } |
| 9442 class M2 { |
| 9443 void f() {} |
| 9444 } |
| 9445 class C extends B with M1, M2 {} |
| 9446 void main() { |
| 9447 new C().f(); |
| 9448 } |
| 9449 '''); |
| 9450 LibraryElement library = resolve(source); |
| 9451 assertNoErrors(source); |
| 9452 verify([source]); |
| 9453 // Verify that the call to f() in "new C().f()" refers to the method |
| 9454 // defined in M2. |
| 9455 FunctionDeclaration main = |
| 9456 library.definingCompilationUnit.functions[0].node; |
| 9457 BlockFunctionBody body = main.functionExpression.body; |
| 9458 ExpressionStatement stmt = body.block.statements[0]; |
| 9459 MethodInvocation invocation = stmt.expression; |
| 9460 expect(invocation.methodName.staticElement.enclosingElement.name, 'M2'); |
| 9461 } |
| 9462 |
| 9231 void test_method_fromSuperclassMixin() { | 9463 void test_method_fromSuperclassMixin() { |
| 9232 Source source = addSource(r''' | 9464 Source source = addSource(r''' |
| 9233 class A { | 9465 class A { |
| 9234 void m1() {} | 9466 void m1() {} |
| 9235 } | 9467 } |
| 9236 class B extends Object with A { | 9468 class B extends Object with A { |
| 9237 } | 9469 } |
| 9238 class C extends B { | 9470 class C extends B { |
| 9239 } | 9471 } |
| 9240 f(C c) { | 9472 f(C c) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9282 | 9514 |
| 9283 void test_resolveAgainstNull() { | 9515 void test_resolveAgainstNull() { |
| 9284 Source source = addSource(r''' | 9516 Source source = addSource(r''' |
| 9285 f(var p) { | 9517 f(var p) { |
| 9286 return null == p; | 9518 return null == p; |
| 9287 }'''); | 9519 }'''); |
| 9288 resolve(source); | 9520 resolve(source); |
| 9289 assertNoErrors(source); | 9521 assertNoErrors(source); |
| 9290 } | 9522 } |
| 9291 | 9523 |
| 9524 void test_setter_fromMixins_bare_identifier() { |
| 9525 Source source = addSource(''' |
| 9526 class B {} |
| 9527 class M1 { |
| 9528 set x(value) {} |
| 9529 } |
| 9530 class M2 { |
| 9531 set x(value) {} |
| 9532 } |
| 9533 class C extends B with M1, M2 { |
| 9534 void f() { |
| 9535 x = 1; |
| 9536 } |
| 9537 } |
| 9538 '''); |
| 9539 LibraryElement library = resolve(source); |
| 9540 assertNoErrors(source); |
| 9541 verify([source]); |
| 9542 // Verify that the setter for "x" in C.f() refers to the setter defined in |
| 9543 // M2. |
| 9544 ClassElement classC = library.definingCompilationUnit.types[3]; |
| 9545 MethodDeclaration f = classC.getMethod('f').node; |
| 9546 BlockFunctionBody body = f.body; |
| 9547 ExpressionStatement stmt = body.block.statements[0]; |
| 9548 AssignmentExpression assignment = stmt.expression; |
| 9549 SimpleIdentifier leftHandSide = assignment.leftHandSide; |
| 9550 expect(leftHandSide.staticElement.enclosingElement.name, 'M2'); |
| 9551 } |
| 9552 |
| 9553 void test_setter_fromMixins_property_access() { |
| 9554 Source source = addSource(''' |
| 9555 class B {} |
| 9556 class M1 { |
| 9557 set x(value) {} |
| 9558 } |
| 9559 class M2 { |
| 9560 set x(value) {} |
| 9561 } |
| 9562 class C extends B with M1, M2 {} |
| 9563 void main() { |
| 9564 new C().x = 1; |
| 9565 } |
| 9566 '''); |
| 9567 LibraryElement library = resolve(source); |
| 9568 assertNoErrors(source); |
| 9569 verify([source]); |
| 9570 // Verify that the setter for "x" in "new C().x" refers to the setter |
| 9571 // defined in M2. |
| 9572 FunctionDeclaration main = |
| 9573 library.definingCompilationUnit.functions[0].node; |
| 9574 BlockFunctionBody body = main.functionExpression.body; |
| 9575 ExpressionStatement stmt = body.block.statements[0]; |
| 9576 AssignmentExpression assignment = stmt.expression; |
| 9577 PropertyAccess propertyAccess = assignment.leftHandSide; |
| 9578 expect( |
| 9579 propertyAccess.propertyName.staticElement.enclosingElement.name, |
| 9580 'M2'); |
| 9581 } |
| 9582 |
| 9292 void test_setter_inherited() { | 9583 void test_setter_inherited() { |
| 9293 Source source = addSource(r''' | 9584 Source source = addSource(r''' |
| 9294 class A { | 9585 class A { |
| 9295 int get x => 0; | 9586 int get x => 0; |
| 9296 set x(int p) {} | 9587 set x(int p) {} |
| 9297 } | 9588 } |
| 9298 class B extends A { | 9589 class B extends A { |
| 9299 int get x => super.x == null ? 0 : super.x; | 9590 int get x => super.x == null ? 0 : super.x; |
| 9300 int f() => x = 1; | 9591 int f() => x = 1; |
| 9301 }'''); | 9592 }'''); |
| (...skipping 1882 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 11184 } | 11475 } |
| 11185 | 11476 |
| 11186 void test_getType_noScope() { | 11477 void test_getType_noScope() { |
| 11187 TypeOverrideManager manager = new TypeOverrideManager(); | 11478 TypeOverrideManager manager = new TypeOverrideManager(); |
| 11188 expect(manager.getType(ElementFactory.localVariableElement2("v")), isNull); | 11479 expect(manager.getType(ElementFactory.localVariableElement2("v")), isNull); |
| 11189 } | 11480 } |
| 11190 } | 11481 } |
| 11191 | 11482 |
| 11192 @reflectiveTest | 11483 @reflectiveTest |
| 11193 class TypePropagationTest extends ResolverTestCase { | 11484 class TypePropagationTest extends ResolverTestCase { |
| 11485 void fail_finalPropertyInducingVariable_classMember_instance() { |
| 11486 addNamedSource("/lib.dart", r''' |
| 11487 class A { |
| 11488 final v = 0; |
| 11489 }'''); |
| 11490 String code = r''' |
| 11491 import 'lib.dart'; |
| 11492 f(A a) { |
| 11493 return a.v; // marker |
| 11494 }'''; |
| 11495 _assertTypeOfMarkedExpression( |
| 11496 code, |
| 11497 typeProvider.dynamicType, |
| 11498 typeProvider.intType); |
| 11499 } |
| 11500 |
| 11501 void fail_finalPropertyInducingVariable_classMember_instance_inherited() { |
| 11502 addNamedSource("/lib.dart", r''' |
| 11503 class A { |
| 11504 final v = 0; |
| 11505 }'''); |
| 11506 String code = r''' |
| 11507 import 'lib.dart'; |
| 11508 class B extends A { |
| 11509 m() { |
| 11510 return v; // marker |
| 11511 } |
| 11512 }'''; |
| 11513 _assertTypeOfMarkedExpression( |
| 11514 code, |
| 11515 typeProvider.dynamicType, |
| 11516 typeProvider.intType); |
| 11517 } |
| 11518 |
| 11519 void |
| 11520 fail_finalPropertyInducingVariable_classMember_instance_propagatedTarget()
{ |
| 11521 addNamedSource("/lib.dart", r''' |
| 11522 class A { |
| 11523 final v = 0; |
| 11524 }'''); |
| 11525 String code = r''' |
| 11526 import 'lib.dart'; |
| 11527 f(p) { |
| 11528 if (p is A) { |
| 11529 return p.v; // marker |
| 11530 } |
| 11531 }'''; |
| 11532 _assertTypeOfMarkedExpression( |
| 11533 code, |
| 11534 typeProvider.dynamicType, |
| 11535 typeProvider.intType); |
| 11536 } |
| 11537 |
| 11538 void fail_finalPropertyInducingVariable_classMember_static() { |
| 11539 addNamedSource("/lib.dart", r''' |
| 11540 class A { |
| 11541 static final V = 0; |
| 11542 }'''); |
| 11543 String code = r''' |
| 11544 import 'lib.dart'; |
| 11545 f() { |
| 11546 return A.V; // marker |
| 11547 }'''; |
| 11548 _assertTypeOfMarkedExpression( |
| 11549 code, |
| 11550 typeProvider.dynamicType, |
| 11551 typeProvider.intType); |
| 11552 } |
| 11553 |
| 11554 void fail_finalPropertyInducingVariable_topLevelVaraible_prefixed() { |
| 11555 addNamedSource("/lib.dart", "final V = 0;"); |
| 11556 String code = r''' |
| 11557 import 'lib.dart' as p; |
| 11558 f() { |
| 11559 var v2 = p.V; // marker prefixed |
| 11560 }'''; |
| 11561 _assertTypeOfMarkedExpression( |
| 11562 code, |
| 11563 typeProvider.dynamicType, |
| 11564 typeProvider.intType); |
| 11565 } |
| 11566 |
| 11567 void fail_finalPropertyInducingVariable_topLevelVaraible_simple() { |
| 11568 addNamedSource("/lib.dart", "final V = 0;"); |
| 11569 String code = r''' |
| 11570 import 'lib.dart'; |
| 11571 f() { |
| 11572 return V; // marker simple |
| 11573 }'''; |
| 11574 _assertTypeOfMarkedExpression( |
| 11575 code, |
| 11576 typeProvider.dynamicType, |
| 11577 typeProvider.intType); |
| 11578 } |
| 11579 |
| 11194 void fail_mergePropagatedTypesAtJoinPoint_1() { | 11580 void fail_mergePropagatedTypesAtJoinPoint_1() { |
| 11195 // https://code.google.com/p/dart/issues/detail?id=19929 | 11581 // https://code.google.com/p/dart/issues/detail?id=19929 |
| 11196 _assertTypeOfMarkedExpression(r''' | 11582 _assertTypeOfMarkedExpression(r''' |
| 11197 f1(x) { | 11583 f1(x) { |
| 11198 var y = []; | 11584 var y = []; |
| 11199 if (x) { | 11585 if (x) { |
| 11200 y = 0; | 11586 y = 0; |
| 11201 } else { | 11587 } else { |
| 11202 y = ''; | 11588 y = ''; |
| 11203 } | 11589 } |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 11470 verify([source]); | 11856 verify([source]); |
| 11471 CompilationUnit unit = resolveCompilationUnit(source, library); | 11857 CompilationUnit unit = resolveCompilationUnit(source, library); |
| 11472 SimpleIdentifier identifier = EngineTestCase.findNode( | 11858 SimpleIdentifier identifier = EngineTestCase.findNode( |
| 11473 unit, | 11859 unit, |
| 11474 code, | 11860 code, |
| 11475 "context", | 11861 "context", |
| 11476 (node) => node is SimpleIdentifier); | 11862 (node) => node is SimpleIdentifier); |
| 11477 expect(identifier.propagatedType.name, "CanvasRenderingContext2D"); | 11863 expect(identifier.propagatedType.name, "CanvasRenderingContext2D"); |
| 11478 } | 11864 } |
| 11479 | 11865 |
| 11480 void fail_finalPropertyInducingVariable_classMember_instance() { | |
| 11481 addNamedSource("/lib.dart", r''' | |
| 11482 class A { | |
| 11483 final v = 0; | |
| 11484 }'''); | |
| 11485 String code = r''' | |
| 11486 import 'lib.dart'; | |
| 11487 f(A a) { | |
| 11488 return a.v; // marker | |
| 11489 }'''; | |
| 11490 _assertTypeOfMarkedExpression( | |
| 11491 code, | |
| 11492 typeProvider.dynamicType, | |
| 11493 typeProvider.intType); | |
| 11494 } | |
| 11495 | |
| 11496 void fail_finalPropertyInducingVariable_classMember_instance_inherited() { | |
| 11497 addNamedSource("/lib.dart", r''' | |
| 11498 class A { | |
| 11499 final v = 0; | |
| 11500 }'''); | |
| 11501 String code = r''' | |
| 11502 import 'lib.dart'; | |
| 11503 class B extends A { | |
| 11504 m() { | |
| 11505 return v; // marker | |
| 11506 } | |
| 11507 }'''; | |
| 11508 _assertTypeOfMarkedExpression( | |
| 11509 code, | |
| 11510 typeProvider.dynamicType, | |
| 11511 typeProvider.intType); | |
| 11512 } | |
| 11513 | |
| 11514 void | |
| 11515 fail_finalPropertyInducingVariable_classMember_instance_propagatedTarget()
{ | |
| 11516 addNamedSource("/lib.dart", r''' | |
| 11517 class A { | |
| 11518 final v = 0; | |
| 11519 }'''); | |
| 11520 String code = r''' | |
| 11521 import 'lib.dart'; | |
| 11522 f(p) { | |
| 11523 if (p is A) { | |
| 11524 return p.v; // marker | |
| 11525 } | |
| 11526 }'''; | |
| 11527 _assertTypeOfMarkedExpression( | |
| 11528 code, | |
| 11529 typeProvider.dynamicType, | |
| 11530 typeProvider.intType); | |
| 11531 } | |
| 11532 | |
| 11533 void fail_finalPropertyInducingVariable_classMember_static() { | |
| 11534 addNamedSource("/lib.dart", r''' | |
| 11535 class A { | |
| 11536 static final V = 0; | |
| 11537 }'''); | |
| 11538 String code = r''' | |
| 11539 import 'lib.dart'; | |
| 11540 f() { | |
| 11541 return A.V; // marker | |
| 11542 }'''; | |
| 11543 _assertTypeOfMarkedExpression( | |
| 11544 code, | |
| 11545 typeProvider.dynamicType, | |
| 11546 typeProvider.intType); | |
| 11547 } | |
| 11548 | |
| 11549 void fail_finalPropertyInducingVariable_topLevelVaraible_prefixed() { | |
| 11550 addNamedSource("/lib.dart", "final V = 0;"); | |
| 11551 String code = r''' | |
| 11552 import 'lib.dart' as p; | |
| 11553 f() { | |
| 11554 var v2 = p.V; // marker prefixed | |
| 11555 }'''; | |
| 11556 _assertTypeOfMarkedExpression( | |
| 11557 code, | |
| 11558 typeProvider.dynamicType, | |
| 11559 typeProvider.intType); | |
| 11560 } | |
| 11561 | |
| 11562 void fail_finalPropertyInducingVariable_topLevelVaraible_simple() { | |
| 11563 addNamedSource("/lib.dart", "final V = 0;"); | |
| 11564 String code = r''' | |
| 11565 import 'lib.dart'; | |
| 11566 f() { | |
| 11567 return V; // marker simple | |
| 11568 }'''; | |
| 11569 _assertTypeOfMarkedExpression( | |
| 11570 code, | |
| 11571 typeProvider.dynamicType, | |
| 11572 typeProvider.intType); | |
| 11573 } | |
| 11574 | |
| 11575 void test_forEach() { | 11866 void test_forEach() { |
| 11576 String code = r''' | 11867 String code = r''' |
| 11577 main() { | 11868 main() { |
| 11578 var list = <String> []; | 11869 var list = <String> []; |
| 11579 for (var e in list) { | 11870 for (var e in list) { |
| 11580 e; | 11871 e; |
| 11581 } | 11872 } |
| 11582 }'''; | 11873 }'''; |
| 11583 Source source = addSource(code); | 11874 Source source = addSource(code); |
| 11584 LibraryElement library = resolve(source); | 11875 LibraryElement library = resolve(source); |
| (...skipping 1759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 13344 // check propagated type | 13635 // check propagated type |
| 13345 FunctionType propagatedType = node.propagatedType as FunctionType; | 13636 FunctionType propagatedType = node.propagatedType as FunctionType; |
| 13346 expect(propagatedType.returnType, test.typeProvider.stringType); | 13637 expect(propagatedType.returnType, test.typeProvider.stringType); |
| 13347 } on AnalysisException catch (e, stackTrace) { | 13638 } on AnalysisException catch (e, stackTrace) { |
| 13348 thrownException[0] = new CaughtException(e, stackTrace); | 13639 thrownException[0] = new CaughtException(e, stackTrace); |
| 13349 } | 13640 } |
| 13350 } | 13641 } |
| 13351 return null; | 13642 return null; |
| 13352 } | 13643 } |
| 13353 } | 13644 } |
| OLD | NEW |