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/context/context.dart' as newContext; | 9 import 'package:analyzer/src/context/context.dart' as newContext; |
10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
(...skipping 977 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
988 ]); | 988 ]); |
989 verify([source]); | 989 verify([source]); |
990 } | 990 } |
991 | 991 |
992 void test_fieldTypeMismatch_generic() { | 992 void test_fieldTypeMismatch_generic() { |
993 Source source = addSource(r''' | 993 Source source = addSource(r''' |
994 class C<T> { | 994 class C<T> { |
995 final T x = y; | 995 final T x = y; |
996 const C(); | 996 const C(); |
997 } | 997 } |
998 const y = 1; | 998 const int y = 1; |
999 var v = const C<String>(); | 999 var v = const C<String>(); |
1000 '''); | 1000 '''); |
1001 computeLibrarySourceErrors(source); | 1001 computeLibrarySourceErrors(source); |
1002 assertErrors(source, [ | 1002 assertErrors(source, [ |
1003 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH, | 1003 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH, |
1004 HintCode.INVALID_ASSIGNMENT | 1004 StaticTypeWarningCode.INVALID_ASSIGNMENT |
1005 ]); | 1005 ]); |
1006 verify([source]); | 1006 verify([source]); |
1007 } | 1007 } |
1008 | 1008 |
1009 void test_fieldTypeMismatch_unresolved() { | 1009 void test_fieldTypeMismatch_unresolved() { |
1010 Source source = addSource(r''' | 1010 Source source = addSource(r''' |
1011 class A { | 1011 class A { |
1012 const A(x) : y = x; | 1012 const A(x) : y = x; |
1013 final Unresolved y; | 1013 final Unresolved y; |
1014 } | 1014 } |
1015 var v = const A('foo');'''); | 1015 var v = const A('foo');'''); |
1016 computeLibrarySourceErrors(source); | 1016 computeLibrarySourceErrors(source); |
1017 assertErrors(source, [ | 1017 assertErrors(source, [ |
1018 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH, | 1018 CheckedModeCompileTimeErrorCode.CONST_CONSTRUCTOR_FIELD_TYPE_MISMATCH, |
1019 StaticWarningCode.UNDEFINED_CLASS | 1019 StaticWarningCode.UNDEFINED_CLASS |
1020 ]); | 1020 ]); |
1021 verify([source]); | 1021 verify([source]); |
1022 } | 1022 } |
1023 | 1023 |
1024 void test_fieldTypeOk_generic() { | 1024 void test_fieldTypeOk_generic() { |
1025 Source source = addSource(r''' | 1025 Source source = addSource(r''' |
1026 class C<T> { | 1026 class C<T> { |
1027 final T x = y; | 1027 final T x = y; |
1028 const C(); | 1028 const C(); |
1029 } | 1029 } |
1030 const y = 1; | 1030 const int y = 1; |
1031 var v = const C<int>(); | 1031 var v = const C<int>(); |
1032 '''); | 1032 '''); |
1033 computeLibrarySourceErrors(source); | 1033 computeLibrarySourceErrors(source); |
1034 assertErrors(source, [HintCode.INVALID_ASSIGNMENT]); | 1034 assertErrors(source, [StaticTypeWarningCode.INVALID_ASSIGNMENT]); |
1035 verify([source]); | 1035 verify([source]); |
1036 } | 1036 } |
1037 | 1037 |
1038 void test_fieldTypeOk_null() { | 1038 void test_fieldTypeOk_null() { |
1039 Source source = addSource(r''' | 1039 Source source = addSource(r''' |
1040 class A { | 1040 class A { |
1041 const A(x) : y = x; | 1041 const A(x) : y = x; |
1042 final int y; | 1042 final int y; |
1043 } | 1043 } |
1044 var v = const A(null);'''); | 1044 var v = const A(null);'''); |
(...skipping 8922 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9967 } | 9967 } |
9968 } | 9968 } |
9969 } | 9969 } |
9970 | 9970 |
9971 class SourceContainer_ChangeSetTest_test_toString implements SourceContainer { | 9971 class SourceContainer_ChangeSetTest_test_toString implements SourceContainer { |
9972 @override | 9972 @override |
9973 bool contains(Source source) => false; | 9973 bool contains(Source source) => false; |
9974 } | 9974 } |
9975 | 9975 |
9976 /** | 9976 /** |
9977 * Shared infrastructure for [StaticTypeAnalyzer2Test] and | |
9978 * [StrongModeStaticTypeAnalyzer2Test]. | |
9979 */ | |
9980 class _StaticTypeAnalyzer2TestShared extends ResolverTestCase { | |
9981 String testCode; | |
9982 Source testSource; | |
9983 CompilationUnit testUnit; | |
9984 | |
9985 SimpleIdentifier _findIdentifier(String search) { | |
9986 SimpleIdentifier identifier = EngineTestCase.findNode( | |
9987 testUnit, testCode, search, (node) => node is SimpleIdentifier); | |
9988 return identifier; | |
9989 } | |
9990 | |
9991 void _resolveTestUnit(String code) { | |
9992 testCode = code; | |
9993 testSource = addSource(testCode); | |
9994 LibraryElement library = resolve2(testSource); | |
9995 assertNoErrors(testSource); | |
9996 verify([testSource]); | |
9997 testUnit = resolveCompilationUnit(testSource, library); | |
9998 } | |
9999 } | |
10000 | |
10001 /** | |
10002 * Like [StaticTypeAnalyzerTest], but as end-to-end tests. | 9977 * Like [StaticTypeAnalyzerTest], but as end-to-end tests. |
10003 */ | 9978 */ |
10004 @reflectiveTest | 9979 @reflectiveTest |
10005 class StaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { | 9980 class StaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { |
10006 void test_FunctionExpressionInvocation_block() { | 9981 void test_FunctionExpressionInvocation_block() { |
10007 String code = r''' | 9982 String code = r''' |
10008 main() { | 9983 main() { |
10009 var foo = (() { return 1; })(); | 9984 var foo = (() { return 1; })(); |
10010 } | 9985 } |
10011 '''; | 9986 '''; |
(...skipping 1896 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11908 * Strong mode static analyzer end to end tests | 11883 * Strong mode static analyzer end to end tests |
11909 */ | 11884 */ |
11910 @reflectiveTest | 11885 @reflectiveTest |
11911 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { | 11886 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { |
11912 void setUp() { | 11887 void setUp() { |
11913 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 11888 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
11914 options.strongMode = true; | 11889 options.strongMode = true; |
11915 resetWithOptions(options); | 11890 resetWithOptions(options); |
11916 } | 11891 } |
11917 | 11892 |
11918 void test_ternaryOperator_null_right() { | 11893 void test_dynamicObjectGetter_hashCode() { |
11919 String code = r''' | 11894 String code = r''' |
11920 main() { | 11895 main() { |
11921 var foo = (true) ? 3 : null; | 11896 dynamic a = null; |
| 11897 var foo = a.hashCode; |
11922 } | 11898 } |
11923 '''; | 11899 '''; |
11924 _resolveTestUnit(code); | 11900 _resolveTestUnit(code); |
11925 | 11901 |
11926 SimpleIdentifier identifier = _findIdentifier('foo'); | 11902 SimpleIdentifier identifier = _findIdentifier('foo'); |
11927 VariableDeclaration declaration = | 11903 VariableDeclaration declaration = |
11928 identifier.getAncestor((node) => node is VariableDeclaration); | 11904 identifier.getAncestor((node) => node is VariableDeclaration); |
11929 expect(declaration.initializer.staticType.name, 'int'); | 11905 expect(declaration.initializer.staticType.name, 'int'); |
11930 expect(declaration.initializer.propagatedType, isNull); | 11906 expect(declaration.initializer.propagatedType, isNull); |
11931 } | 11907 } |
11932 | 11908 |
11933 void test_ternaryOperator_null_left() { | 11909 void test_dynamicObjectMethod_toString() { |
11934 String code = r''' | 11910 String code = r''' |
11935 main() { | 11911 main() { |
11936 var foo = (true) ? null : 3; | 11912 dynamic a = null; |
| 11913 var foo = a.toString(); |
11937 } | 11914 } |
11938 '''; | 11915 '''; |
11939 _resolveTestUnit(code); | 11916 _resolveTestUnit(code); |
11940 | 11917 |
11941 SimpleIdentifier identifier = _findIdentifier('foo'); | 11918 SimpleIdentifier identifier = _findIdentifier('foo'); |
11942 VariableDeclaration declaration = | 11919 VariableDeclaration declaration = |
11943 identifier.getAncestor((node) => node is VariableDeclaration); | 11920 identifier.getAncestor((node) => node is VariableDeclaration); |
11944 expect(declaration.initializer.staticType.name, 'int'); | 11921 expect(declaration.initializer.staticType.name, 'String'); |
11945 expect(declaration.initializer.propagatedType, isNull); | 11922 expect(declaration.initializer.propagatedType, isNull); |
11946 } | 11923 } |
11947 | 11924 |
11948 void test_dynamicObjectMethod_toString() { | 11925 void test_pseudoGeneric_max_doubleDouble() { |
11949 String code = r''' | 11926 String code = r''' |
| 11927 import 'dart:math'; |
11950 main() { | 11928 main() { |
11951 dynamic a = null; | 11929 var foo = max(1.0, 2.0); |
11952 var foo = a.toString(); | |
11953 } | 11930 } |
11954 '''; | 11931 '''; |
11955 _resolveTestUnit(code); | 11932 _resolveTestUnit(code); |
11956 | 11933 |
11957 SimpleIdentifier identifier = _findIdentifier('foo'); | 11934 SimpleIdentifier identifier = _findIdentifier('foo'); |
11958 VariableDeclaration declaration = | 11935 VariableDeclaration declaration = |
11959 identifier.getAncestor((node) => node is VariableDeclaration); | 11936 identifier.getAncestor((node) => node is VariableDeclaration); |
11960 expect(declaration.initializer.staticType.name, 'String'); | 11937 expect(declaration.initializer.staticType.name, 'double'); |
11961 expect(declaration.initializer.propagatedType, isNull); | 11938 expect(declaration.initializer.propagatedType, isNull); |
11962 } | 11939 } |
11963 | 11940 |
11964 void test_dynamicObjectGetter_hashCode() { | 11941 void test_pseudoGeneric_max_doubleInt() { |
11965 String code = r''' | 11942 String code = r''' |
| 11943 import 'dart:math'; |
11966 main() { | 11944 main() { |
11967 dynamic a = null; | 11945 var foo = max(1.0, 2); |
11968 var foo = a.hashCode; | |
11969 } | 11946 } |
11970 '''; | 11947 '''; |
11971 _resolveTestUnit(code); | 11948 _resolveTestUnit(code); |
11972 | 11949 |
11973 SimpleIdentifier identifier = _findIdentifier('foo'); | 11950 SimpleIdentifier identifier = _findIdentifier('foo'); |
11974 VariableDeclaration declaration = | 11951 VariableDeclaration declaration = |
11975 identifier.getAncestor((node) => node is VariableDeclaration); | 11952 identifier.getAncestor((node) => node is VariableDeclaration); |
11976 expect(declaration.initializer.staticType.name, 'int'); | 11953 expect(declaration.initializer.staticType.name, 'num'); |
11977 expect(declaration.initializer.propagatedType, isNull); | 11954 expect(declaration.initializer.propagatedType, isNull); |
11978 } | 11955 } |
11979 | 11956 |
11980 void test_pseudoGeneric_max_intInt() { | 11957 void test_pseudoGeneric_max_intDouble() { |
11981 String code = r''' | 11958 String code = r''' |
11982 import 'dart:math'; | 11959 import 'dart:math'; |
11983 main() { | 11960 main() { |
11984 var foo = max(1, 2); | 11961 var foo = max(1, 2.0); |
11985 } | 11962 } |
11986 '''; | 11963 '''; |
11987 _resolveTestUnit(code); | 11964 _resolveTestUnit(code); |
11988 | 11965 |
11989 SimpleIdentifier identifier = _findIdentifier('foo'); | 11966 SimpleIdentifier identifier = _findIdentifier('foo'); |
11990 VariableDeclaration declaration = | 11967 VariableDeclaration declaration = |
11991 identifier.getAncestor((node) => node is VariableDeclaration); | 11968 identifier.getAncestor((node) => node is VariableDeclaration); |
11992 expect(declaration.initializer.staticType.name, 'int'); | 11969 expect(declaration.initializer.staticType.name, 'num'); |
11993 expect(declaration.initializer.propagatedType, isNull); | 11970 expect(declaration.initializer.propagatedType, isNull); |
11994 } | 11971 } |
11995 | 11972 |
11996 void test_pseudoGeneric_max_doubleDouble() { | 11973 void test_pseudoGeneric_max_intInt() { |
11997 String code = r''' | 11974 String code = r''' |
11998 import 'dart:math'; | 11975 import 'dart:math'; |
11999 main() { | 11976 main() { |
12000 var foo = max(1.0, 2.0); | 11977 var foo = max(1, 2); |
12001 } | 11978 } |
12002 '''; | 11979 '''; |
12003 _resolveTestUnit(code); | 11980 _resolveTestUnit(code); |
12004 | 11981 |
12005 SimpleIdentifier identifier = _findIdentifier('foo'); | 11982 SimpleIdentifier identifier = _findIdentifier('foo'); |
12006 VariableDeclaration declaration = | 11983 VariableDeclaration declaration = |
12007 identifier.getAncestor((node) => node is VariableDeclaration); | 11984 identifier.getAncestor((node) => node is VariableDeclaration); |
12008 expect(declaration.initializer.staticType.name, 'double'); | 11985 expect(declaration.initializer.staticType.name, 'int'); |
12009 expect(declaration.initializer.propagatedType, isNull); | 11986 expect(declaration.initializer.propagatedType, isNull); |
12010 } | 11987 } |
12011 | 11988 |
12012 void test_pseudoGeneric_max_intDouble() { | 11989 void test_pseudoGeneric_then() { |
12013 String code = r''' | 11990 String code = r''' |
12014 import 'dart:math'; | 11991 import 'dart:async'; |
| 11992 String toString(int x) => x.toString(); |
12015 main() { | 11993 main() { |
12016 var foo = max(1, 2.0); | 11994 Future<int> bar = null; |
| 11995 var foo = bar.then(toString); |
12017 } | 11996 } |
12018 '''; | 11997 '''; |
12019 _resolveTestUnit(code); | 11998 _resolveTestUnit(code); |
12020 | 11999 |
12021 SimpleIdentifier identifier = _findIdentifier('foo'); | 12000 SimpleIdentifier identifier = _findIdentifier('foo'); |
12022 VariableDeclaration declaration = | 12001 VariableDeclaration declaration = |
12023 identifier.getAncestor((node) => node is VariableDeclaration); | 12002 identifier.getAncestor((node) => node is VariableDeclaration); |
12024 expect(declaration.initializer.staticType.name, 'num'); | 12003 |
| 12004 expect(declaration.initializer.staticType.toString(), "Future<String>"); |
12025 expect(declaration.initializer.propagatedType, isNull); | 12005 expect(declaration.initializer.propagatedType, isNull); |
12026 } | 12006 } |
12027 | 12007 |
12028 void test_pseudoGeneric_max_doubleInt() { | 12008 void test_ternaryOperator_null_left() { |
12029 String code = r''' | 12009 String code = r''' |
12030 import 'dart:math'; | |
12031 main() { | 12010 main() { |
12032 var foo = max(1.0, 2); | 12011 var foo = (true) ? null : 3; |
12033 } | 12012 } |
12034 '''; | 12013 '''; |
12035 _resolveTestUnit(code); | 12014 _resolveTestUnit(code); |
12036 | 12015 |
12037 SimpleIdentifier identifier = _findIdentifier('foo'); | 12016 SimpleIdentifier identifier = _findIdentifier('foo'); |
12038 VariableDeclaration declaration = | 12017 VariableDeclaration declaration = |
12039 identifier.getAncestor((node) => node is VariableDeclaration); | 12018 identifier.getAncestor((node) => node is VariableDeclaration); |
12040 expect(declaration.initializer.staticType.name, 'num'); | 12019 expect(declaration.initializer.staticType.name, 'int'); |
12041 expect(declaration.initializer.propagatedType, isNull); | 12020 expect(declaration.initializer.propagatedType, isNull); |
12042 } | 12021 } |
12043 | 12022 |
12044 void test_pseudoGeneric_then() { | 12023 void test_ternaryOperator_null_right() { |
12045 String code = r''' | 12024 String code = r''' |
12046 import 'dart:async'; | |
12047 String toString(int x) => x.toString(); | |
12048 main() { | 12025 main() { |
12049 Future<int> bar = null; | 12026 var foo = (true) ? 3 : null; |
12050 var foo = bar.then(toString); | |
12051 } | 12027 } |
12052 '''; | 12028 '''; |
12053 _resolveTestUnit(code); | 12029 _resolveTestUnit(code); |
12054 | 12030 |
12055 SimpleIdentifier identifier = _findIdentifier('foo'); | 12031 SimpleIdentifier identifier = _findIdentifier('foo'); |
12056 VariableDeclaration declaration = | 12032 VariableDeclaration declaration = |
12057 identifier.getAncestor((node) => node is VariableDeclaration); | 12033 identifier.getAncestor((node) => node is VariableDeclaration); |
12058 | 12034 expect(declaration.initializer.staticType.name, 'int'); |
12059 expect(declaration.initializer.staticType.toString(), "Future<String>"); | |
12060 expect(declaration.initializer.propagatedType, isNull); | 12035 expect(declaration.initializer.propagatedType, isNull); |
12061 } | 12036 } |
12062 } | 12037 } |
12063 | 12038 |
12064 @reflectiveTest | 12039 @reflectiveTest |
12065 class StrongModeTypePropagationTest extends ResolverTestCase { | 12040 class StrongModeTypePropagationTest extends ResolverTestCase { |
12066 @override | 12041 @override |
12067 void setUp() { | 12042 void setUp() { |
12068 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 12043 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
12069 options.strongMode = true; | 12044 options.strongMode = true; |
(...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12548 import 'lib.dart'; | 12523 import 'lib.dart'; |
12549 f(p) { | 12524 f(p) { |
12550 if (p is A) { | 12525 if (p is A) { |
12551 return p.v; // marker | 12526 return p.v; // marker |
12552 } | 12527 } |
12553 }'''; | 12528 }'''; |
12554 _assertTypeOfMarkedExpression( | 12529 _assertTypeOfMarkedExpression( |
12555 code, typeProvider.dynamicType, typeProvider.intType); | 12530 code, typeProvider.dynamicType, typeProvider.intType); |
12556 } | 12531 } |
12557 | 12532 |
| 12533 void fail_finalPropertyInducingVariable_classMember_instance_unprefixed() { |
| 12534 String code = r''' |
| 12535 class A { |
| 12536 final v = 0; |
| 12537 m() { |
| 12538 v; // marker |
| 12539 } |
| 12540 }'''; |
| 12541 _assertTypeOfMarkedExpression( |
| 12542 code, typeProvider.dynamicType, typeProvider.intType); |
| 12543 } |
| 12544 |
12558 void fail_finalPropertyInducingVariable_classMember_static() { | 12545 void fail_finalPropertyInducingVariable_classMember_static() { |
12559 addNamedSource( | 12546 addNamedSource( |
12560 "/lib.dart", | 12547 "/lib.dart", |
12561 r''' | 12548 r''' |
12562 class A { | 12549 class A { |
12563 static final V = 0; | 12550 static final V = 0; |
12564 }'''); | 12551 }'''); |
12565 String code = r''' | 12552 String code = r''' |
12566 import 'lib.dart'; | 12553 import 'lib.dart'; |
12567 f() { | 12554 f() { |
(...skipping 1274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13842 Base x = null; | 13829 Base x = null; |
13843 if (x is Derived) { | 13830 if (x is Derived) { |
13844 print(x.y); // GOOD | 13831 print(x.y); // GOOD |
13845 } | 13832 } |
13846 x = null; | 13833 x = null; |
13847 }'''); | 13834 }'''); |
13848 computeLibrarySourceErrors(source); | 13835 computeLibrarySourceErrors(source); |
13849 assertNoErrors(source); | 13836 assertNoErrors(source); |
13850 } | 13837 } |
13851 | 13838 |
| 13839 void test_objectAccessInference_disabled_for_library_prefix() { |
| 13840 String name = 'hashCode'; |
| 13841 addNamedSource( |
| 13842 '/helper.dart', |
| 13843 ''' |
| 13844 library helper; |
| 13845 dynamic get $name => 42; |
| 13846 '''); |
| 13847 String code = ''' |
| 13848 import 'helper.dart' as helper; |
| 13849 main() { |
| 13850 helper.$name; // marker |
| 13851 }'''; |
| 13852 |
| 13853 SimpleIdentifier id = _findMarkedIdentifier(code, "; // marker"); |
| 13854 PrefixedIdentifier prefixedId = id.parent; |
| 13855 expect(id.staticType, typeProvider.dynamicType); |
| 13856 expect(prefixedId.staticType, typeProvider.dynamicType); |
| 13857 } |
| 13858 |
| 13859 void test_objectAccessInference_disabled_for_local_getter() { |
| 13860 String name = 'hashCode'; |
| 13861 String code = ''' |
| 13862 dynamic get $name => null; |
| 13863 main() { |
| 13864 $name; // marker |
| 13865 }'''; |
| 13866 |
| 13867 SimpleIdentifier getter = _findMarkedIdentifier(code, "; // marker"); |
| 13868 expect(getter.staticType, typeProvider.dynamicType); |
| 13869 } |
| 13870 |
| 13871 void test_objectAccessInference_enabled_for_cascades() { |
| 13872 String name = 'hashCode'; |
| 13873 String code = ''' |
| 13874 main() { |
| 13875 dynamic obj; |
| 13876 obj..$name..$name; // marker |
| 13877 }'''; |
| 13878 PropertyAccess access = _findMarkedIdentifier(code, "; // marker").parent; |
| 13879 expect(access.staticType, typeProvider.dynamicType); |
| 13880 expect(access.realTarget.staticType, typeProvider.dynamicType); |
| 13881 } |
| 13882 |
| 13883 void test_objectMethodInference_disabled_for_library_prefix() { |
| 13884 String name = 'toString'; |
| 13885 addNamedSource( |
| 13886 '/helper.dart', |
| 13887 ''' |
| 13888 library helper; |
| 13889 dynamic $name = (int x) => x + 42'); |
| 13890 '''); |
| 13891 String code = ''' |
| 13892 import 'helper.dart' as helper; |
| 13893 main() { |
| 13894 helper.$name(); // marker |
| 13895 }'''; |
| 13896 SimpleIdentifier methodName = _findMarkedIdentifier(code, "(); // marker"); |
| 13897 MethodInvocation methodInvoke = methodName.parent; |
| 13898 expect(methodName.staticType, null, reason: 'library prefix has no type'); |
| 13899 expect(methodInvoke.staticType, typeProvider.dynamicType); |
| 13900 } |
| 13901 |
| 13902 void test_objectMethodInference_disabled_for_local_function() { |
| 13903 String name = 'toString'; |
| 13904 String code = ''' |
| 13905 main() { |
| 13906 dynamic $name = () => null; |
| 13907 $name(); // marker |
| 13908 }'''; |
| 13909 SimpleIdentifier identifier = _findMarkedIdentifier(code, "$name = "); |
| 13910 expect(identifier.staticType, typeProvider.dynamicType); |
| 13911 |
| 13912 SimpleIdentifier methodName = _findMarkedIdentifier(code, "(); // marker"); |
| 13913 MethodInvocation methodInvoke = methodName.parent; |
| 13914 expect(methodName.staticType, typeProvider.dynamicType); |
| 13915 expect(methodInvoke.staticType, typeProvider.dynamicType); |
| 13916 } |
| 13917 |
| 13918 void test_objectMethodInference_enabled_for_cascades() { |
| 13919 String name = 'toString'; |
| 13920 String code = ''' |
| 13921 main() { |
| 13922 dynamic obj; |
| 13923 obj..$name()..$name(); // marker |
| 13924 }'''; |
| 13925 SimpleIdentifier methodName = _findMarkedIdentifier(code, "(); // marker"); |
| 13926 MethodInvocation methodInvoke = methodName.parent; |
| 13927 |
| 13928 expect(methodInvoke.staticType, typeProvider.dynamicType); |
| 13929 expect(methodInvoke.realTarget.staticType, typeProvider.dynamicType); |
| 13930 } |
| 13931 |
13852 void test_objectMethodOnDynamicExpression_doubleEquals() { | 13932 void test_objectMethodOnDynamicExpression_doubleEquals() { |
13853 // https://code.google.com/p/dart/issues/detail?id=20342 | 13933 // https://code.google.com/p/dart/issues/detail?id=20342 |
13854 // | 13934 // |
13855 // This was not actually part of Issue 20342, since the spec specifies a | 13935 // This was not actually part of Issue 20342, since the spec specifies a |
13856 // static type of [bool] for [==] comparison and the implementation | 13936 // static type of [bool] for [==] comparison and the implementation |
13857 // was already consistent with the spec there. But, it's another | 13937 // was already consistent with the spec there. But, it's another |
13858 // [Object] method, so it's included here. | 13938 // [Object] method, so it's included here. |
13859 _assertTypeOfMarkedExpression( | 13939 _assertTypeOfMarkedExpression( |
13860 r''' | 13940 r''' |
13861 f1(x) { | 13941 f1(x) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13895 _assertTypeOfMarkedExpression( | 13975 _assertTypeOfMarkedExpression( |
13896 r''' | 13976 r''' |
13897 f1(x) { | 13977 f1(x) { |
13898 var v = x.toString(); | 13978 var v = x.toString(); |
13899 return v; // marker | 13979 return v; // marker |
13900 }''', | 13980 }''', |
13901 null, | 13981 null, |
13902 typeProvider.stringType); | 13982 typeProvider.stringType); |
13903 } | 13983 } |
13904 | 13984 |
13905 void test_objectMethodInference_disabled_for_local_function() { | |
13906 String name = 'toString'; | |
13907 String code = ''' | |
13908 main() { | |
13909 dynamic $name = () => null; | |
13910 $name(); // marker | |
13911 }'''; | |
13912 SimpleIdentifier identifier = _findMarkedIdentifier(code, "$name = "); | |
13913 expect(identifier.staticType, typeProvider.dynamicType); | |
13914 | |
13915 SimpleIdentifier methodName = _findMarkedIdentifier(code, "(); // marker"); | |
13916 MethodInvocation methodInvoke = methodName.parent; | |
13917 expect(methodName.staticType, typeProvider.dynamicType); | |
13918 expect(methodInvoke.staticType, typeProvider.dynamicType); | |
13919 } | |
13920 | |
13921 void test_objectMethodInference_disabled_for_library_prefix() { | |
13922 String name = 'toString'; | |
13923 addNamedSource('/helper.dart', ''' | |
13924 library helper; | |
13925 dynamic $name = (int x) => x + 42'); | |
13926 '''); | |
13927 String code = ''' | |
13928 import 'helper.dart' as helper; | |
13929 main() { | |
13930 helper.$name(); // marker | |
13931 }'''; | |
13932 SimpleIdentifier methodName = _findMarkedIdentifier(code, "(); // marker"); | |
13933 MethodInvocation methodInvoke = methodName.parent; | |
13934 expect(methodName.staticType, null, reason: 'library prefix has no type'); | |
13935 expect(methodInvoke.staticType, typeProvider.dynamicType); | |
13936 } | |
13937 | |
13938 void test_objectMethodInference_enabled_for_cascades() { | |
13939 String name = 'toString'; | |
13940 String code = ''' | |
13941 main() { | |
13942 dynamic obj; | |
13943 obj..$name()..$name(); // marker | |
13944 }'''; | |
13945 SimpleIdentifier methodName = _findMarkedIdentifier(code, "(); // marker"); | |
13946 MethodInvocation methodInvoke = methodName.parent; | |
13947 | |
13948 expect(methodInvoke.staticType, typeProvider.dynamicType); | |
13949 expect(methodInvoke.realTarget.staticType, typeProvider.dynamicType); | |
13950 } | |
13951 | |
13952 | |
13953 void test_objectAccessInference_disabled_for_local_getter() { | |
13954 String name = 'hashCode'; | |
13955 String code = ''' | |
13956 dynamic get $name => null; | |
13957 main() { | |
13958 $name; // marker | |
13959 }'''; | |
13960 | |
13961 SimpleIdentifier getter = _findMarkedIdentifier(code, "; // marker"); | |
13962 expect(getter.staticType, typeProvider.dynamicType); | |
13963 } | |
13964 | |
13965 void test_objectAccessInference_disabled_for_library_prefix() { | |
13966 String name = 'hashCode'; | |
13967 addNamedSource('/helper.dart', ''' | |
13968 library helper; | |
13969 dynamic get $name => 42; | |
13970 '''); | |
13971 String code = ''' | |
13972 import 'helper.dart' as helper; | |
13973 main() { | |
13974 helper.$name; // marker | |
13975 }'''; | |
13976 | |
13977 SimpleIdentifier id = _findMarkedIdentifier(code, "; // marker"); | |
13978 PrefixedIdentifier prefixedId = id.parent; | |
13979 expect(id.staticType, typeProvider.dynamicType); | |
13980 expect(prefixedId.staticType, typeProvider.dynamicType); | |
13981 } | |
13982 | |
13983 void test_objectAccessInference_enabled_for_cascades() { | |
13984 String name = 'hashCode'; | |
13985 String code = ''' | |
13986 main() { | |
13987 dynamic obj; | |
13988 obj..$name..$name; // marker | |
13989 }'''; | |
13990 PropertyAccess access = _findMarkedIdentifier(code, "; // marker").parent; | |
13991 expect(access.staticType, typeProvider.dynamicType); | |
13992 expect(access.realTarget.staticType, typeProvider.dynamicType); | |
13993 } | |
13994 | |
13995 void test_propagatedReturnType_localFunction() { | 13985 void test_propagatedReturnType_localFunction() { |
13996 String code = r''' | 13986 String code = r''' |
13997 main() { | 13987 main() { |
13998 f() => 42; | 13988 f() => 42; |
13999 var v = f(); | 13989 var v = f(); |
14000 }'''; | 13990 }'''; |
14001 _assertPropagatedAssignedType( | 13991 _assertPropagatedAssignedType( |
14002 code, typeProvider.dynamicType, typeProvider.intType); | 13992 code, typeProvider.dynamicType, typeProvider.intType); |
14003 } | 13993 } |
14004 | 13994 |
(...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14822 // check propagated type | 14812 // check propagated type |
14823 FunctionType propagatedType = node.propagatedType as FunctionType; | 14813 FunctionType propagatedType = node.propagatedType as FunctionType; |
14824 expect(propagatedType.returnType, test.typeProvider.stringType); | 14814 expect(propagatedType.returnType, test.typeProvider.stringType); |
14825 } on AnalysisException catch (e, stackTrace) { | 14815 } on AnalysisException catch (e, stackTrace) { |
14826 thrownException[0] = new CaughtException(e, stackTrace); | 14816 thrownException[0] = new CaughtException(e, stackTrace); |
14827 } | 14817 } |
14828 } | 14818 } |
14829 return null; | 14819 return null; |
14830 } | 14820 } |
14831 } | 14821 } |
| 14822 |
| 14823 /** |
| 14824 * Shared infrastructure for [StaticTypeAnalyzer2Test] and |
| 14825 * [StrongModeStaticTypeAnalyzer2Test]. |
| 14826 */ |
| 14827 class _StaticTypeAnalyzer2TestShared extends ResolverTestCase { |
| 14828 String testCode; |
| 14829 Source testSource; |
| 14830 CompilationUnit testUnit; |
| 14831 |
| 14832 SimpleIdentifier _findIdentifier(String search) { |
| 14833 SimpleIdentifier identifier = EngineTestCase.findNode( |
| 14834 testUnit, testCode, search, (node) => node is SimpleIdentifier); |
| 14835 return identifier; |
| 14836 } |
| 14837 |
| 14838 void _resolveTestUnit(String code) { |
| 14839 testCode = code; |
| 14840 testSource = addSource(testCode); |
| 14841 LibraryElement library = resolve2(testSource); |
| 14842 assertNoErrors(testSource); |
| 14843 verify([testSource]); |
| 14844 testUnit = resolveCompilationUnit(testSource, library); |
| 14845 } |
| 14846 } |
OLD | NEW |