Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: pkg/analyzer/test/src/task/dart_test.dart

Issue 1460213005: Propagate types of final/const top-level variables and fields. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: tweaks Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/task/dart.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 test.src.task.dart_test; 5 library test.src.task.dart_test;
6 6
7 import 'package:analyzer/src/context/cache.dart'; 7 import 'package:analyzer/src/context/cache.dart';
8 import 'package:analyzer/src/generated/ast.dart'; 8 import 'package:analyzer/src/generated/ast.dart';
9 import 'package:analyzer/src/generated/constant.dart'; 9 import 'package:analyzer/src/generated/constant.dart';
10 import 'package:analyzer/src/generated/element.dart'; 10 import 'package:analyzer/src/generated/element.dart';
(...skipping 26 matching lines...) Expand all
37 runReflectiveTests(BuildEnumMemberElementsTaskTest); 37 runReflectiveTests(BuildEnumMemberElementsTaskTest);
38 runReflectiveTests(BuildExportNamespaceTaskTest); 38 runReflectiveTests(BuildExportNamespaceTaskTest);
39 runReflectiveTests(BuildLibraryElementTaskTest); 39 runReflectiveTests(BuildLibraryElementTaskTest);
40 runReflectiveTests(BuildPublicNamespaceTaskTest); 40 runReflectiveTests(BuildPublicNamespaceTaskTest);
41 runReflectiveTests(BuildSourceExportClosureTaskTest); 41 runReflectiveTests(BuildSourceExportClosureTaskTest);
42 runReflectiveTests(BuildTypeProviderTaskTest); 42 runReflectiveTests(BuildTypeProviderTaskTest);
43 runReflectiveTests(ComputeConstantDependenciesTaskTest); 43 runReflectiveTests(ComputeConstantDependenciesTaskTest);
44 runReflectiveTests(ComputeConstantValueTaskTest); 44 runReflectiveTests(ComputeConstantValueTaskTest);
45 runReflectiveTests(ComputeInferableStaticVariableDependenciesTaskTest); 45 runReflectiveTests(ComputeInferableStaticVariableDependenciesTaskTest);
46 runReflectiveTests(ComputeLibraryCycleTaskTest); 46 runReflectiveTests(ComputeLibraryCycleTaskTest);
47 runReflectiveTests(ComputePropagableVariableDependenciesTaskTest);
47 runReflectiveTests(ContainingLibrariesTaskTest); 48 runReflectiveTests(ContainingLibrariesTaskTest);
48 runReflectiveTests(DartErrorsTaskTest); 49 runReflectiveTests(DartErrorsTaskTest);
49 runReflectiveTests(ErrorFilterTest); 50 runReflectiveTests(ErrorFilterTest);
50 runReflectiveTests(EvaluateUnitConstantsTaskTest); 51 runReflectiveTests(EvaluateUnitConstantsTaskTest);
51 runReflectiveTests(GatherUsedImportedElementsTaskTest); 52 runReflectiveTests(GatherUsedImportedElementsTaskTest);
52 runReflectiveTests(GatherUsedLocalElementsTaskTest); 53 runReflectiveTests(GatherUsedLocalElementsTaskTest);
53 runReflectiveTests(GenerateHintsTaskTest); 54 runReflectiveTests(GenerateHintsTaskTest);
54 runReflectiveTests(GenerateLintsTaskTest); 55 runReflectiveTests(GenerateLintsTaskTest);
55 runReflectiveTests(InferInstanceMembersInUnitTaskTest); 56 runReflectiveTests(InferInstanceMembersInUnitTaskTest);
56 runReflectiveTests(InferStaticVariableTypesInUnitTaskTest); 57 runReflectiveTests(InferStaticVariableTypesInUnitTaskTest);
57 runReflectiveTests(InferStaticVariableTypeTaskTest); 58 runReflectiveTests(InferStaticVariableTypeTaskTest);
58 runReflectiveTests(LibraryErrorsReadyTaskTest); 59 runReflectiveTests(LibraryErrorsReadyTaskTest);
59 runReflectiveTests(LibraryUnitErrorsTaskTest); 60 runReflectiveTests(LibraryUnitErrorsTaskTest);
60 runReflectiveTests(ParseDartTaskTest); 61 runReflectiveTests(ParseDartTaskTest);
61 runReflectiveTests(PartiallyResolveUnitReferencesTaskTest); 62 runReflectiveTests(PartiallyResolveUnitReferencesTaskTest);
63 runReflectiveTests(PropagateVariableTypesInUnitTaskTest);
64 runReflectiveTests(PropagateVariableTypeTaskTest);
62 runReflectiveTests(ResolveInstanceFieldsInUnitTaskTest); 65 runReflectiveTests(ResolveInstanceFieldsInUnitTaskTest);
63 runReflectiveTests(ResolveLibraryTypeNamesTaskTest); 66 runReflectiveTests(ResolveLibraryTypeNamesTaskTest);
64 runReflectiveTests(ResolveUnitTaskTest); 67 runReflectiveTests(ResolveUnitTaskTest);
65 runReflectiveTests(ResolveUnitTypeNamesTaskTest); 68 runReflectiveTests(ResolveUnitTypeNamesTaskTest);
66 runReflectiveTests(ResolveVariableReferencesTaskTest); 69 runReflectiveTests(ResolveVariableReferencesTaskTest);
67 runReflectiveTests(ScanDartTaskTest); 70 runReflectiveTests(ScanDartTaskTest);
68 runReflectiveTests(StrongModeInferenceTest); 71 runReflectiveTests(StrongModeInferenceTest);
69 runReflectiveTests(StrongModeVerifyUnitTaskTest); 72 runReflectiveTests(StrongModeVerifyUnitTaskTest);
70 runReflectiveTests(VerifyUnitTaskTest); 73 runReflectiveTests(VerifyUnitTaskTest);
71 } 74 }
(...skipping 13 matching lines...) Expand all
85 isInstanceOf isBuildSourceExportClosureTask = 88 isInstanceOf isBuildSourceExportClosureTask =
86 new isInstanceOf<BuildSourceExportClosureTask>(); 89 new isInstanceOf<BuildSourceExportClosureTask>();
87 isInstanceOf isBuildTypeProviderTask = 90 isInstanceOf isBuildTypeProviderTask =
88 new isInstanceOf<BuildTypeProviderTask>(); 91 new isInstanceOf<BuildTypeProviderTask>();
89 isInstanceOf isComputeConstantDependenciesTask = 92 isInstanceOf isComputeConstantDependenciesTask =
90 new isInstanceOf<ComputeConstantDependenciesTask>(); 93 new isInstanceOf<ComputeConstantDependenciesTask>();
91 isInstanceOf isComputeConstantValueTask = 94 isInstanceOf isComputeConstantValueTask =
92 new isInstanceOf<ComputeConstantValueTask>(); 95 new isInstanceOf<ComputeConstantValueTask>();
93 isInstanceOf isComputeInferableStaticVariableDependenciesTask = 96 isInstanceOf isComputeInferableStaticVariableDependenciesTask =
94 new isInstanceOf<ComputeInferableStaticVariableDependenciesTask>(); 97 new isInstanceOf<ComputeInferableStaticVariableDependenciesTask>();
98 isInstanceOf isComputePropagableVariableDependenciesTask =
99 new isInstanceOf<ComputePropagableVariableDependenciesTask>();
95 isInstanceOf isContainingLibrariesTask = 100 isInstanceOf isContainingLibrariesTask =
96 new isInstanceOf<ContainingLibrariesTask>(); 101 new isInstanceOf<ContainingLibrariesTask>();
97 isInstanceOf isDartErrorsTask = new isInstanceOf<DartErrorsTask>(); 102 isInstanceOf isDartErrorsTask = new isInstanceOf<DartErrorsTask>();
98 isInstanceOf isEvaluateUnitConstantsTask = 103 isInstanceOf isEvaluateUnitConstantsTask =
99 new isInstanceOf<EvaluateUnitConstantsTask>(); 104 new isInstanceOf<EvaluateUnitConstantsTask>();
100 isInstanceOf isGatherUsedImportedElementsTask = 105 isInstanceOf isGatherUsedImportedElementsTask =
101 new isInstanceOf<GatherUsedImportedElementsTask>(); 106 new isInstanceOf<GatherUsedImportedElementsTask>();
102 isInstanceOf isGatherUsedLocalElementsTask = 107 isInstanceOf isGatherUsedLocalElementsTask =
103 new isInstanceOf<GatherUsedLocalElementsTask>(); 108 new isInstanceOf<GatherUsedLocalElementsTask>();
104 isInstanceOf isGenerateHintsTask = new isInstanceOf<GenerateHintsTask>(); 109 isInstanceOf isGenerateHintsTask = new isInstanceOf<GenerateHintsTask>();
105 isInstanceOf isGenerateLintsTask = new isInstanceOf<GenerateLintsTask>(); 110 isInstanceOf isGenerateLintsTask = new isInstanceOf<GenerateLintsTask>();
106 isInstanceOf isInferInstanceMembersInUnitTask = 111 isInstanceOf isInferInstanceMembersInUnitTask =
107 new isInstanceOf<InferInstanceMembersInUnitTask>(); 112 new isInstanceOf<InferInstanceMembersInUnitTask>();
108 isInstanceOf isInferStaticVariableTypesInUnitTask = 113 isInstanceOf isInferStaticVariableTypesInUnitTask =
109 new isInstanceOf<InferStaticVariableTypesInUnitTask>(); 114 new isInstanceOf<InferStaticVariableTypesInUnitTask>();
110 isInstanceOf isInferStaticVariableTypeTask = 115 isInstanceOf isInferStaticVariableTypeTask =
111 new isInstanceOf<InferStaticVariableTypeTask>(); 116 new isInstanceOf<InferStaticVariableTypeTask>();
112 isInstanceOf isLibraryErrorsReadyTask = 117 isInstanceOf isLibraryErrorsReadyTask =
113 new isInstanceOf<LibraryErrorsReadyTask>(); 118 new isInstanceOf<LibraryErrorsReadyTask>();
114 isInstanceOf isLibraryUnitErrorsTask = 119 isInstanceOf isLibraryUnitErrorsTask =
115 new isInstanceOf<LibraryUnitErrorsTask>(); 120 new isInstanceOf<LibraryUnitErrorsTask>();
116 isInstanceOf isParseDartTask = new isInstanceOf<ParseDartTask>(); 121 isInstanceOf isParseDartTask = new isInstanceOf<ParseDartTask>();
117 isInstanceOf isPartiallyResolveUnitReferencesTask = 122 isInstanceOf isPartiallyResolveUnitReferencesTask =
118 new isInstanceOf<PartiallyResolveUnitReferencesTask>(); 123 new isInstanceOf<PartiallyResolveUnitReferencesTask>();
124 isInstanceOf isPropagateVariableTypesInUnitTask =
125 new isInstanceOf<PropagateVariableTypesInUnitTask>();
126 isInstanceOf isPropagateVariableTypeTask =
127 new isInstanceOf<PropagateVariableTypeTask>();
119 isInstanceOf isResolveLibraryTypeNamesTask = 128 isInstanceOf isResolveLibraryTypeNamesTask =
120 new isInstanceOf<ResolveLibraryTypeNamesTask>(); 129 new isInstanceOf<ResolveLibraryTypeNamesTask>();
121 isInstanceOf isResolveUnitTask = new isInstanceOf<ResolveUnitTask>(); 130 isInstanceOf isResolveUnitTask = new isInstanceOf<ResolveUnitTask>();
122 isInstanceOf isResolveUnitTypeNamesTask = 131 isInstanceOf isResolveUnitTypeNamesTask =
123 new isInstanceOf<ResolveUnitTypeNamesTask>(); 132 new isInstanceOf<ResolveUnitTypeNamesTask>();
124 isInstanceOf isResolveVariableReferencesTask = 133 isInstanceOf isResolveVariableReferencesTask =
125 new isInstanceOf<ResolveVariableReferencesTask>(); 134 new isInstanceOf<ResolveVariableReferencesTask>();
126 isInstanceOf isScanDartTask = new isInstanceOf<ScanDartTask>(); 135 isInstanceOf isScanDartTask = new isInstanceOf<ScanDartTask>();
127 isInstanceOf isStrongModeVerifyUnitTask = 136 isInstanceOf isStrongModeVerifyUnitTask =
128 new isInstanceOf<StrongModeVerifyUnitTask>(); 137 new isInstanceOf<StrongModeVerifyUnitTask>();
(...skipping 1139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1268 1277
1269 void _checkCircularities( 1278 void _checkCircularities(
1270 String variableName, List<String> otherVariables, String content) { 1279 String variableName, List<String> otherVariables, String content) {
1271 // Evaluating the first constant should produce an error. 1280 // Evaluating the first constant should produce an error.
1272 CompilationUnit unit = _resolveUnit(content); 1281 CompilationUnit unit = _resolveUnit(content);
1273 _expectCircularityError(_evaluateConstant(unit, variableName)); 1282 _expectCircularityError(_evaluateConstant(unit, variableName));
1274 // And all the other constants involved in the strongly connected component 1283 // And all the other constants involved in the strongly connected component
1275 // should be set to the same error state. 1284 // should be set to the same error state.
1276 for (String otherVariableName in otherVariables) { 1285 for (String otherVariableName in otherVariables) {
1277 PropertyInducingElement otherVariableElement = 1286 PropertyInducingElement otherVariableElement =
1278 _findVariable(unit, otherVariableName); 1287 getTopLevelVariableElement(unit, otherVariableName);
1279 _expectCircularityError((otherVariableElement 1288 _expectCircularityError((otherVariableElement
1280 as TopLevelVariableElementImpl).evaluationResult); 1289 as TopLevelVariableElementImpl).evaluationResult);
1281 } 1290 }
1282 } 1291 }
1283 1292
1284 EvaluationResultImpl _computeTopLevelVariableConstValue( 1293 EvaluationResultImpl _computeTopLevelVariableConstValue(
1285 String variableName, String content) { 1294 String variableName, String content) {
1286 return _evaluateConstant(_resolveUnit(content), variableName); 1295 return _evaluateConstant(_resolveUnit(content), variableName);
1287 } 1296 }
1288 1297
1289 EvaluationResultImpl _evaluateConstant( 1298 EvaluationResultImpl _evaluateConstant(
1290 CompilationUnit unit, String variableName) { 1299 CompilationUnit unit, String variableName) {
1291 // Find the element for the given constant. 1300 // Find the element for the given constant.
1292 PropertyInducingElement variableElement = _findVariable(unit, variableName); 1301 PropertyInducingElement variableElement =
1302 getTopLevelVariableElement(unit, variableName);
1293 // Now compute the value of the constant. 1303 // Now compute the value of the constant.
1294 computeResult(variableElement, CONSTANT_VALUE, 1304 computeResult(variableElement, CONSTANT_VALUE,
1295 matcher: isComputeConstantValueTask); 1305 matcher: isComputeConstantValueTask);
1296 expect(outputs[CONSTANT_VALUE], same(variableElement)); 1306 expect(outputs[CONSTANT_VALUE], same(variableElement));
1297 EvaluationResultImpl evaluationResult = 1307 EvaluationResultImpl evaluationResult =
1298 (variableElement as TopLevelVariableElementImpl).evaluationResult; 1308 (variableElement as TopLevelVariableElementImpl).evaluationResult;
1299 return evaluationResult; 1309 return evaluationResult;
1300 } 1310 }
1301 1311
1302 void _expectCircularityError(EvaluationResultImpl evaluationResult) { 1312 void _expectCircularityError(EvaluationResultImpl evaluationResult) {
1303 expect(evaluationResult, isNotNull); 1313 expect(evaluationResult, isNotNull);
1304 expect(evaluationResult.value, isNull); 1314 expect(evaluationResult.value, isNull);
1305 expect(evaluationResult.errors, hasLength(1)); 1315 expect(evaluationResult.errors, hasLength(1));
1306 expect(evaluationResult.errors[0].errorCode, 1316 expect(evaluationResult.errors[0].errorCode,
1307 CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT); 1317 CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT);
1308 } 1318 }
1309 1319
1310 PropertyInducingElement _findVariable(
1311 CompilationUnit unit, String variableName) {
1312 // Find the element for the given constant.
1313 return unit.element.topLevelVariables.firstWhere(
1314 (TopLevelVariableElement variable) => variable.name == variableName);
1315 }
1316
1317 CompilationUnit _resolveSource(Source source) { 1320 CompilationUnit _resolveSource(Source source) {
1318 LibrarySpecificUnit librarySpecificUnit = 1321 LibrarySpecificUnit librarySpecificUnit =
1319 new LibrarySpecificUnit(source, source); 1322 new LibrarySpecificUnit(source, source);
1320 computeResult(librarySpecificUnit, RESOLVED_UNIT1); 1323 computeResult(librarySpecificUnit, RESOLVED_UNIT1);
1321 CompilationUnit unit = outputs[RESOLVED_UNIT1]; 1324 CompilationUnit unit = outputs[RESOLVED_UNIT1];
1322 return unit; 1325 return unit;
1323 } 1326 }
1324 1327
1325 CompilationUnit _resolveUnit(String content) => 1328 CompilationUnit _resolveUnit(String content) =>
1326 _resolveSource(newSource('/test.dart', content)); 1329 _resolveSource(newSource('/test.dart', content));
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
1754 expect(dep2, hasLength(1)); // dart:core 1757 expect(dep2, hasLength(1)); // dart:core
1755 expect(dep3, hasLength(1)); // dart:core 1758 expect(dep3, hasLength(1)); // dart:core
1756 expect(dep4, hasLength(5)); // dart:core, a.dart, aa.dart, ab.dart, b.dart 1759 expect(dep4, hasLength(5)); // dart:core, a.dart, aa.dart, ab.dart, b.dart
1757 expect(dep5, hasLength(5)); // dart:core, a.dart, aa.dart, ab.dart, b.dart 1760 expect(dep5, hasLength(5)); // dart:core, a.dart, aa.dart, ab.dart, b.dart
1758 expect(dep6, hasLength(5)); // dart:core, a.dart, aa.dart, ab.dart, b.dart 1761 expect(dep6, hasLength(5)); // dart:core, a.dart, aa.dart, ab.dart, b.dart
1759 expect(dep7, hasLength(5)); // dart:core, a.dart, aa.dart, ab.dart, b.dart 1762 expect(dep7, hasLength(5)); // dart:core, a.dart, aa.dart, ab.dart, b.dart
1760 } 1763 }
1761 } 1764 }
1762 1765
1763 @reflectiveTest 1766 @reflectiveTest
1767 class ComputePropagableVariableDependenciesTaskTest
1768 extends _AbstractDartTaskTest {
1769 test_perform_instanceField() {
1770 AnalysisTarget source = newSource(
1771 '/test.dart',
1772 '''
1773 class A {
1774 final a = a1 + a2 + a3 + a4 + B.b1 + B.b2 + B.b3 + B.b4;
1775 static const a1 = 1;
1776 final a2 = 2;
1777 final a3;
1778 var a4 = 4;
1779 }
1780 class B {
1781 static const b1 = 1;
1782 static final b2 = 2;
1783 static final b3;
1784 static var b4 = 4;
1785 }
1786 ''');
1787 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
1788 computeResult(target, RESOLVED_UNIT5);
1789 CompilationUnit unit = outputs[RESOLVED_UNIT5];
1790 FieldElement elementA = getFieldInClassElement(unit, 'A', 'a');
1791 // compute
1792 computeResult(elementA, PROPAGABLE_VARIABLE_DEPENDENCIES,
1793 matcher: isComputePropagableVariableDependenciesTask);
1794 // verify
1795 expect(outputs, hasLength(1));
1796 List<VariableElement> dependencies =
1797 outputs[PROPAGABLE_VARIABLE_DEPENDENCIES];
1798 expect(
1799 dependencies,
1800 unorderedEquals([
1801 getFieldInClassElement(unit, 'A', 'a1'),
1802 getFieldInClassElement(unit, 'A', 'a2'),
1803 getFieldInClassElement(unit, 'B', 'b1'),
1804 getFieldInClassElement(unit, 'B', 'b2')
1805 ]));
1806 }
1807
1808 test_perform_topLevel() {
1809 AnalysisTarget source = newSource(
1810 '/test.dart',
1811 '''
1812 const a = d1 + d2 + d3 + d4;
1813 const d1 = 1;
1814 final d2 = 2;
1815 final d3;
1816 var d4 = 4;
1817 ''');
1818 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
1819 computeResult(target, RESOLVED_UNIT5);
1820 CompilationUnit unit = outputs[RESOLVED_UNIT5];
1821 TopLevelVariableElement elementA = getTopLevelVariableElement(unit, 'a');
1822 // compute
1823 computeResult(elementA, PROPAGABLE_VARIABLE_DEPENDENCIES,
1824 matcher: isComputePropagableVariableDependenciesTask);
1825 // verify
1826 expect(outputs, hasLength(1));
1827 List<VariableElement> dependencies =
1828 outputs[PROPAGABLE_VARIABLE_DEPENDENCIES];
1829 expect(
1830 dependencies,
1831 unorderedEquals([
1832 getTopLevelVariableElement(unit, 'd1'),
1833 getTopLevelVariableElement(unit, 'd2')
1834 ]));
1835 }
1836 }
1837
1838 @reflectiveTest
1764 class ContainingLibrariesTaskTest extends _AbstractDartTaskTest { 1839 class ContainingLibrariesTaskTest extends _AbstractDartTaskTest {
1765 test_perform_definingCompilationUnit() { 1840 test_perform_definingCompilationUnit() {
1766 AnalysisTarget library = newSource('/test.dart', 'library test;'); 1841 AnalysisTarget library = newSource('/test.dart', 'library test;');
1767 computeResult(library, INCLUDED_PARTS); 1842 computeResult(library, INCLUDED_PARTS);
1768 computeResult(library, CONTAINING_LIBRARIES, 1843 computeResult(library, CONTAINING_LIBRARIES,
1769 matcher: isContainingLibrariesTask); 1844 matcher: isContainingLibrariesTask);
1770 expect(outputs, hasLength(1)); 1845 expect(outputs, hasLength(1));
1771 List<Source> containingLibraries = outputs[CONTAINING_LIBRARIES]; 1846 List<Source> containingLibraries = outputs[CONTAINING_LIBRARIES];
1772 expect(containingLibraries, unorderedEquals([library])); 1847 expect(containingLibraries, unorderedEquals([library]));
1773 } 1848 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1824 computeResult(part, DART_ERRORS, matcher: isDartErrorsTask); 1899 computeResult(part, DART_ERRORS, matcher: isDartErrorsTask);
1825 expect(outputs, hasLength(1)); 1900 expect(outputs, hasLength(1));
1826 List<AnalysisError> errors = outputs[DART_ERRORS]; 1901 List<AnalysisError> errors = outputs[DART_ERRORS];
1827 // This should contain only the errors in the part file, not the ones in the 1902 // This should contain only the errors in the part file, not the ones in the
1828 // library. 1903 // library.
1829 expect(errors, hasLength(1)); 1904 expect(errors, hasLength(1));
1830 } 1905 }
1831 } 1906 }
1832 1907
1833 @reflectiveTest 1908 @reflectiveTest
1909 class ErrorFilterTest extends _AbstractDartTaskTest {
1910 @override
1911 setUp() {
1912 super.setUp();
1913 context.setConfigurationData(CONFIGURED_ERROR_FILTERS, [
1914 (AnalysisError error) => error.errorCode.name == 'INVALID_ASSIGNMENT'
1915 ]);
1916 }
1917
1918 test_error_filters() {
1919 AnalysisTarget library = newSource(
1920 '/test.dart',
1921 '''
1922 main() {
1923 int x = ""; // INVALID_ASSIGNMENT (suppressed)
1924 // UNUSED_LOCAL_VARIABLE
1925 }''');
1926 computeResult(library, DART_ERRORS, matcher: isDartErrorsTask);
1927 expect(outputs, hasLength(1));
1928 List<AnalysisError> errors = outputs[DART_ERRORS];
1929 expect(errors, hasLength(1));
1930 expect(errors.first.errorCode, HintCode.UNUSED_LOCAL_VARIABLE);
1931 }
1932 }
1933
1934 @reflectiveTest
1834 class EvaluateUnitConstantsTaskTest extends _AbstractDartTaskTest { 1935 class EvaluateUnitConstantsTaskTest extends _AbstractDartTaskTest {
1835 test_perform() { 1936 test_perform() {
1836 Source source = newSource( 1937 Source source = newSource(
1837 '/test.dart', 1938 '/test.dart',
1838 ''' 1939 '''
1839 class C { 1940 class C {
1840 const C(); 1941 const C();
1841 } 1942 }
1842 1943
1843 @x 1944 @x
1844 f() {} 1945 f() {}
1845 1946
1846 const x = const C(); 1947 const x = const C();
1847 '''); 1948 ''');
1848 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 1949 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
1849 computeResult(target, RESOLVED_UNIT10, 1950 computeResult(target, RESOLVED_UNIT11,
1850 matcher: isEvaluateUnitConstantsTask); 1951 matcher: isEvaluateUnitConstantsTask);
1851 CompilationUnit unit = outputs[RESOLVED_UNIT10]; 1952 CompilationUnit unit = outputs[RESOLVED_UNIT11];
1852 CompilationUnitElement unitElement = unit.element; 1953 CompilationUnitElement unitElement = unit.element;
1853 expect( 1954 expect(
1854 (unitElement.types[0].constructors[0] as ConstructorElementImpl) 1955 (unitElement.types[0].constructors[0] as ConstructorElementImpl)
1855 .isCycleFree, 1956 .isCycleFree,
1856 isTrue); 1957 isTrue);
1857 expect( 1958 expect(
1858 (unitElement.functions[0].metadata[0] as ElementAnnotationImpl) 1959 (unitElement.functions[0].metadata[0] as ElementAnnotationImpl)
1859 .evaluationResult, 1960 .evaluationResult,
1860 isNotNull); 1961 isNotNull);
1861 expect( 1962 expect(
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
2234 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 2335 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2235 computeResult(target, HINTS, matcher: isGenerateHintsTask); 2336 computeResult(target, HINTS, matcher: isGenerateHintsTask);
2236 // validate 2337 // validate
2237 _fillErrorListener(HINTS); 2338 _fillErrorListener(HINTS);
2238 errorListener.assertErrorsWithCodes( 2339 errorListener.assertErrorsWithCodes(
2239 <ErrorCode>[HintCode.UNUSED_ELEMENT, HintCode.UNUSED_ELEMENT]); 2340 <ErrorCode>[HintCode.UNUSED_ELEMENT, HintCode.UNUSED_ELEMENT]);
2240 } 2341 }
2241 } 2342 }
2242 2343
2243 @reflectiveTest 2344 @reflectiveTest
2244 class ErrorFilterTest extends _AbstractDartTaskTest {
2245 @override
2246 setUp() {
2247 super.setUp();
2248 context.setConfigurationData(CONFIGURED_ERROR_FILTERS, [
2249 (AnalysisError error) => error.errorCode.name == 'INVALID_ASSIGNMENT'
2250 ]);
2251 }
2252
2253 test_error_filters() {
2254 AnalysisTarget library = newSource('/test.dart', '''
2255 main() {
2256 int x = ""; // INVALID_ASSIGNMENT (suppressed)
2257 // UNUSED_LOCAL_VARIABLE
2258 }''');
2259 computeResult(library, DART_ERRORS, matcher: isDartErrorsTask);
2260 expect(outputs, hasLength(1));
2261 List<AnalysisError> errors = outputs[DART_ERRORS];
2262 expect(errors, hasLength(1));
2263 expect(errors.first.errorCode, HintCode.UNUSED_LOCAL_VARIABLE);
2264 }
2265 }
2266
2267 @reflectiveTest
2268 class GenerateLintsTaskTest extends _AbstractDartTaskTest { 2345 class GenerateLintsTaskTest extends _AbstractDartTaskTest {
2269 void enableLints() { 2346 void enableLints() {
2270 AnalysisOptionsImpl options = context.analysisOptions; 2347 AnalysisOptionsImpl options = context.analysisOptions;
2271 options.lint = true; 2348 options.lint = true;
2272 context.analysisOptions = options; 2349 context.analysisOptions = options;
2273 } 2350 }
2274 2351
2275 @override 2352 @override
2276 void setUp() { 2353 void setUp() {
2277 super.setUp(); 2354 super.setUp();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
2330 Y m(Z x) {} 2407 Y m(Z x) {}
2331 } 2408 }
2332 class B extends A { 2409 class B extends A {
2333 var f; 2410 var f;
2334 m(x) {} 2411 m(x) {}
2335 } 2412 }
2336 class X {} 2413 class X {}
2337 class Y {} 2414 class Y {}
2338 class Z {} 2415 class Z {}
2339 '''); 2416 ''');
2340 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8, 2417 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9,
2341 matcher: isInferInstanceMembersInUnitTask); 2418 matcher: isInferInstanceMembersInUnitTask);
2342 CompilationUnit unit = outputs[RESOLVED_UNIT8]; 2419 CompilationUnit unit = outputs[RESOLVED_UNIT9];
2343 VariableDeclaration field = getFieldInClass(unit, 'B', 'f'); 2420 VariableDeclaration field = getFieldInClass(unit, 'B', 'f');
2344 MethodDeclaration method = getMethodInClass(unit, 'B', 'm'); 2421 MethodDeclaration method = getMethodInClass(unit, 'B', 'm');
2345 DartType typeX = getClass(unit, 'X').element.type; 2422 DartType typeX = getClass(unit, 'X').element.type;
2346 DartType typeY = getClass(unit, 'Y').element.type; 2423 DartType typeY = getClass(unit, 'Y').element.type;
2347 DartType typeZ = getClass(unit, 'Z').element.type; 2424 DartType typeZ = getClass(unit, 'Z').element.type;
2348 2425
2349 expect(field.element.type, typeX); 2426 expect(field.element.type, typeX);
2350 expect(method.element.returnType, typeY); 2427 expect(method.element.returnType, typeY);
2351 expect(method.element.parameters[0].type, typeZ); 2428 expect(method.element.parameters[0].type, typeZ);
2352 } 2429 }
(...skipping 11 matching lines...) Expand all
2364 '/second.dart', 2441 '/second.dart',
2365 ''' 2442 '''
2366 import 'first.dart'; 2443 import 'first.dart';
2367 2444
2368 const b = a; 2445 const b = a;
2369 class M { 2446 class M {
2370 String c = a; 2447 String c = a;
2371 } 2448 }
2372 '''); 2449 ''');
2373 computeResult( 2450 computeResult(
2374 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT8, 2451 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT9,
2375 matcher: isInferInstanceMembersInUnitTask); 2452 matcher: isInferInstanceMembersInUnitTask);
2376 CompilationUnit firstUnit = outputs[RESOLVED_UNIT8]; 2453 CompilationUnit firstUnit = outputs[RESOLVED_UNIT9];
2377 computeResult( 2454 computeResult(
2378 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT8); 2455 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT9);
2379 CompilationUnit secondUnit = outputs[RESOLVED_UNIT8]; 2456 CompilationUnit secondUnit = outputs[RESOLVED_UNIT9];
2380 2457
2381 VariableDeclaration variableA = getTopLevelVariable(firstUnit, 'a'); 2458 VariableDeclaration variableA = getTopLevelVariable(firstUnit, 'a');
2382 VariableDeclaration variableB = getTopLevelVariable(secondUnit, 'b'); 2459 VariableDeclaration variableB = getTopLevelVariable(secondUnit, 'b');
2383 VariableDeclaration variableC = getFieldInClass(secondUnit, 'M', 'c'); 2460 VariableDeclaration variableC = getFieldInClass(secondUnit, 'M', 'c');
2384 InterfaceType stringType = context.typeProvider.stringType; 2461 InterfaceType stringType = context.typeProvider.stringType;
2385 2462
2386 expect(variableA.element.type, stringType); 2463 expect(variableA.element.type, stringType);
2387 expect(variableB.element.type, stringType); 2464 expect(variableB.element.type, stringType);
2388 expect(variableB.initializer.staticType, stringType); 2465 expect(variableB.initializer.staticType, stringType);
2389 expect(variableC.element.type, stringType); 2466 expect(variableC.element.type, stringType);
2390 expect(variableC.initializer.staticType, stringType); 2467 expect(variableC.initializer.staticType, stringType);
2391 } 2468 }
2392 2469
2393 void test_perform_reresolution() { 2470 void test_perform_reresolution() {
2394 enableStrongMode(); 2471 enableStrongMode();
2395 AnalysisTarget source = newSource( 2472 AnalysisTarget source = newSource(
2396 '/test.dart', 2473 '/test.dart',
2397 ''' 2474 '''
2398 const topLevel = ''; 2475 const topLevel = '';
2399 class C { 2476 class C {
2400 String field = topLevel; 2477 String field = topLevel;
2401 } 2478 }
2402 '''); 2479 ''');
2403 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8); 2480 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9);
2404 CompilationUnit unit = outputs[RESOLVED_UNIT8]; 2481 CompilationUnit unit = outputs[RESOLVED_UNIT9];
2405 VariableDeclaration topLevelDecl = getTopLevelVariable(unit, 'topLevel'); 2482 VariableDeclaration topLevelDecl = getTopLevelVariable(unit, 'topLevel');
2406 VariableDeclaration fieldDecl = getFieldInClass(unit, 'C', 'field'); 2483 VariableDeclaration fieldDecl = getFieldInClass(unit, 'C', 'field');
2407 VariableElement topLevel = topLevelDecl.name.staticElement; 2484 VariableElement topLevel = topLevelDecl.name.staticElement;
2408 VariableElement field = fieldDecl.name.staticElement; 2485 VariableElement field = fieldDecl.name.staticElement;
2409 2486
2410 InterfaceType stringType = context.typeProvider.stringType; 2487 InterfaceType stringType = context.typeProvider.stringType;
2411 expect(topLevel.type, stringType); 2488 expect(topLevel.type, stringType);
2412 expect(field.type, stringType); 2489 expect(field.type, stringType);
2413 expect(fieldDecl.initializer.staticType, stringType); 2490 expect(fieldDecl.initializer.staticType, stringType);
2414 } 2491 }
2415 } 2492 }
2416 2493
2417 @reflectiveTest 2494 @reflectiveTest
2418 class InferStaticVariableTypesInUnitTaskTest extends _AbstractDartTaskTest { 2495 class InferStaticVariableTypesInUnitTaskTest extends _AbstractDartTaskTest {
2419 void test_perform_const_field() { 2496 void test_perform_const_field() {
2420 enableStrongMode(); 2497 enableStrongMode();
2421 AnalysisTarget source = newSource( 2498 AnalysisTarget source = newSource(
2422 '/test.dart', 2499 '/test.dart',
2423 ''' 2500 '''
2424 class M { 2501 class M {
2425 static const X = ""; 2502 static const X = "";
2426 } 2503 }
2427 '''); 2504 ''');
2428 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6, 2505 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT7,
2429 matcher: isInferStaticVariableTypesInUnitTask); 2506 matcher: isInferStaticVariableTypesInUnitTask);
2430 CompilationUnit unit = outputs[RESOLVED_UNIT6]; 2507 CompilationUnit unit = outputs[RESOLVED_UNIT7];
2431 VariableDeclaration declaration = getFieldInClass(unit, 'M', 'X'); 2508 VariableDeclaration declaration = getFieldInClass(unit, 'M', 'X');
2432 InterfaceType stringType = context.typeProvider.stringType; 2509 InterfaceType stringType = context.typeProvider.stringType;
2433 expect(declaration.element.type, stringType); 2510 expect(declaration.element.type, stringType);
2434 } 2511 }
2435 2512
2436 void test_perform_nestedDeclarations() { 2513 void test_perform_nestedDeclarations() {
2437 enableStrongMode(); 2514 enableStrongMode();
2438 AnalysisTarget source = newSource( 2515 AnalysisTarget source = newSource(
2439 '/test.dart', 2516 '/test.dart',
2440 ''' 2517 '''
2441 var f = (int x) { 2518 var f = (int x) {
2442 int squared(int value) => value * value; 2519 int squared(int value) => value * value;
2443 var xSquared = squared(x); 2520 var xSquared = squared(x);
2444 return xSquared; 2521 return xSquared;
2445 }; 2522 };
2446 '''); 2523 ''');
2447 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6, 2524 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT7,
2448 matcher: isInferStaticVariableTypesInUnitTask); 2525 matcher: isInferStaticVariableTypesInUnitTask);
2449 } 2526 }
2450 2527
2451 void test_perform_recursive() { 2528 void test_perform_recursive() {
2452 enableStrongMode(); 2529 enableStrongMode();
2453 AnalysisTarget firstSource = newSource( 2530 AnalysisTarget firstSource = newSource(
2454 '/first.dart', 2531 '/first.dart',
2455 ''' 2532 '''
2456 import 'second.dart'; 2533 import 'second.dart';
2457 2534
2458 var a = new M(); 2535 var a = new M();
2459 var c = b; 2536 var c = b;
2460 '''); 2537 ''');
2461 AnalysisTarget secondSource = newSource( 2538 AnalysisTarget secondSource = newSource(
2462 '/second.dart', 2539 '/second.dart',
2463 ''' 2540 '''
2464 import 'first.dart'; 2541 import 'first.dart';
2465 2542
2466 var b = a; 2543 var b = a;
2467 class M {} 2544 class M {}
2468 '''); 2545 ''');
2469 computeResult( 2546 computeResult(
2470 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT6, 2547 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT7,
2471 matcher: isInferStaticVariableTypesInUnitTask); 2548 matcher: isInferStaticVariableTypesInUnitTask);
2472 CompilationUnit firstUnit = outputs[RESOLVED_UNIT6]; 2549 CompilationUnit firstUnit = outputs[RESOLVED_UNIT7];
2473 computeResult( 2550 computeResult(
2474 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT6); 2551 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT7);
2475 CompilationUnit secondUnit = outputs[RESOLVED_UNIT6]; 2552 CompilationUnit secondUnit = outputs[RESOLVED_UNIT7];
2476 2553
2477 VariableDeclaration variableA = getTopLevelVariable(firstUnit, 'a'); 2554 VariableDeclaration variableA = getTopLevelVariable(firstUnit, 'a');
2478 VariableDeclaration variableB = getTopLevelVariable(secondUnit, 'b'); 2555 VariableDeclaration variableB = getTopLevelVariable(secondUnit, 'b');
2479 VariableDeclaration variableC = getTopLevelVariable(firstUnit, 'c'); 2556 VariableDeclaration variableC = getTopLevelVariable(firstUnit, 'c');
2480 ClassDeclaration classM = getClass(secondUnit, 'M'); 2557 ClassDeclaration classM = getClass(secondUnit, 'M');
2481 DartType typeM = classM.element.type; 2558 DartType typeM = classM.element.type;
2482 2559
2483 expect(variableA.element.type, typeM); 2560 expect(variableA.element.type, typeM);
2484 expect(variableB.element.type, typeM); 2561 expect(variableB.element.type, typeM);
2485 expect(variableB.initializer.staticType, typeM); 2562 expect(variableB.initializer.staticType, typeM);
2486 expect(variableC.element.type, typeM); 2563 expect(variableC.element.type, typeM);
2487 expect(variableC.initializer.staticType, typeM); 2564 expect(variableC.initializer.staticType, typeM);
2488 } 2565 }
2489 2566
2490 void test_perform_simple() { 2567 void test_perform_simple() {
2491 enableStrongMode(); 2568 enableStrongMode();
2492 AnalysisTarget source = newSource( 2569 AnalysisTarget source = newSource(
2493 '/test.dart', 2570 '/test.dart',
2494 ''' 2571 '''
2495 var X = 1; 2572 var X = 1;
2496 2573
2497 var Y = () { 2574 var Y = () {
2498 return 1 + X; 2575 return 1 + X;
2499 }; 2576 };
2500 '''); 2577 ''');
2501 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6, 2578 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT7,
2502 matcher: isInferStaticVariableTypesInUnitTask); 2579 matcher: isInferStaticVariableTypesInUnitTask);
2503 CompilationUnit unit = outputs[RESOLVED_UNIT6]; 2580 CompilationUnit unit = outputs[RESOLVED_UNIT7];
2504 TopLevelVariableDeclaration declaration = unit.declarations[1]; 2581 TopLevelVariableDeclaration declaration = unit.declarations[1];
2505 FunctionExpression function = 2582 FunctionExpression function =
2506 declaration.variables.variables[0].initializer; 2583 declaration.variables.variables[0].initializer;
2507 BlockFunctionBody body = function.body; 2584 BlockFunctionBody body = function.body;
2508 ReturnStatement statement = body.block.statements[0]; 2585 ReturnStatement statement = body.block.statements[0];
2509 Expression expression = statement.expression; 2586 Expression expression = statement.expression;
2510 InterfaceType intType = context.typeProvider.intType; 2587 InterfaceType intType = context.typeProvider.intType;
2511 expect(expression.staticType, intType); 2588 expect(expression.staticType, intType);
2512 } 2589 }
2513 } 2590 }
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
2835 static void _assertHasCore(List<Source> sources, int lenght) { 2912 static void _assertHasCore(List<Source> sources, int lenght) {
2836 expect(sources, hasLength(lenght)); 2913 expect(sources, hasLength(lenght));
2837 expect(sources, contains(predicate((Source s) { 2914 expect(sources, contains(predicate((Source s) {
2838 return s.fullName.endsWith('core.dart'); 2915 return s.fullName.endsWith('core.dart');
2839 }))); 2916 })));
2840 } 2917 }
2841 } 2918 }
2842 2919
2843 @reflectiveTest 2920 @reflectiveTest
2844 class PartiallyResolveUnitReferencesTaskTest extends _AbstractDartTaskTest { 2921 class PartiallyResolveUnitReferencesTaskTest extends _AbstractDartTaskTest {
2845 test_perform() { 2922 test_perform_propagable() {
2846 enableStrongMode(); 2923 enableStrongMode();
2847 Source source = newSource( 2924 Source source = newSource(
2848 '/test.dart', 2925 '/test.dart',
2849 ''' 2926 '''
2850 int a = b; 2927 const t1 = 1;
2851 int b = c; 2928 final t2 = 2;
2852 var d = 0; 2929 var t3 = 3;
2853 class A {} 2930 final t4;
2854 class C { 2931 class C {
2855 static final f = ''; 2932 static const fs1 = 1;
2856 var g = 0; 2933 static final fs2 = 2;
2934 static var fs3 = 3;
2935 static final fs4;
2936 const fi1 = 1;
2937 final fi2 = 2;
2938 var fi3 = 3;
2939 final fi4;
2857 } 2940 }
2858 '''); 2941 ''');
2859 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 2942 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2860 computeResult(target, RESOLVED_UNIT5, 2943 computeResult(target, PROPAGABLE_VARIABLES_IN_UNIT,
2861 matcher: isPartiallyResolveUnitReferencesTask); 2944 matcher: isPartiallyResolveUnitReferencesTask);
2862 // Test the outputs 2945 // PROPAGABLE_VARIABLES_IN_UNIT
2863 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(4)); 2946 List<VariableElement> variables = outputs[PROPAGABLE_VARIABLES_IN_UNIT];
2864 CompilationUnit unit = outputs[RESOLVED_UNIT5]; 2947 expect(variables.map((v) => v.displayName),
2865 expect(unit, same(outputs[RESOLVED_UNIT5])); 2948 unorderedEquals(['t1', 't2', 'fs1', 'fs2', 'fi1', 'fi2']));
2866 // Test the state of the AST
2867 TopLevelVariableDeclaration a = unit.declarations[0];
2868 VariableDeclaration variableA = a.variables.variables[0];
2869 SimpleIdentifier initializer = variableA.initializer;
2870 expect(initializer.staticElement, isNotNull);
2871 } 2949 }
2872 2950
2873 test_perform_importExport() { 2951 test_perform_strong_importExport() {
2874 newSource( 2952 newSource(
2875 '/a.dart', 2953 '/a.dart',
2876 ''' 2954 '''
2877 library a; 2955 library a;
2878 class A<T> { 2956 class A<T> {
2879 T m() {} 2957 T m() {}
2880 } 2958 }
2881 '''); 2959 ''');
2882 newSource( 2960 newSource(
2883 '/b.dart', 2961 '/b.dart',
(...skipping 20 matching lines...) Expand all
2904 FunctionDeclaration mainFunction = unit.declarations[0]; 2982 FunctionDeclaration mainFunction = unit.declarations[0];
2905 expect(mainFunction.element, isNotNull); 2983 expect(mainFunction.element, isNotNull);
2906 BlockFunctionBody body = mainFunction.functionExpression.body; 2984 BlockFunctionBody body = mainFunction.functionExpression.body;
2907 List<Statement> statements = body.block.statements; 2985 List<Statement> statements = body.block.statements;
2908 ExpressionStatement statement = statements[0]; 2986 ExpressionStatement statement = statements[0];
2909 MethodInvocation invocation = statement.expression; 2987 MethodInvocation invocation = statement.expression;
2910 MethodElement methodElement = invocation.methodName.staticElement; 2988 MethodElement methodElement = invocation.methodName.staticElement;
2911 expect(methodElement, isNull); 2989 expect(methodElement, isNull);
2912 } 2990 }
2913 2991
2914 test_perform_notResolved() { 2992 test_perform_strong_inferable() {
2915 enableStrongMode(); 2993 enableStrongMode();
2916 Source source = newSource( 2994 Source source = newSource(
2917 '/test.dart', 2995 '/test.dart',
2996 '''
2997 int a = b;
2998 int b = c;
2999 var d = 0;
3000 class A {}
3001 class C {
3002 static final f = '';
3003 var g = 0;
3004 }
3005 ''');
3006 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
3007 computeResult(target, RESOLVED_UNIT5,
3008 matcher: isPartiallyResolveUnitReferencesTask);
3009 CompilationUnit unit = outputs[RESOLVED_UNIT5];
3010 // INFERABLE_STATIC_VARIABLES_IN_UNIT
3011 {
3012 List<VariableElement> variables =
3013 outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT];
3014 expect(variables, hasLength(4));
3015 expect(variables.map((v) => v.displayName),
3016 unorderedEquals(['a', 'b', 'd', 'f']));
3017 }
3018 // Test the state of the AST
3019 TopLevelVariableDeclaration a = unit.declarations[0];
3020 VariableDeclaration variableA = a.variables.variables[0];
3021 SimpleIdentifier initializer = variableA.initializer;
3022 expect(initializer.staticElement, isNotNull);
3023 }
3024
3025 test_perform_strong_notResolved() {
3026 enableStrongMode();
3027 Source source = newSource(
3028 '/test.dart',
2918 ''' 3029 '''
2919 int A; 3030 int A;
2920 f1() { 3031 f1() {
2921 A; 3032 A;
2922 } 3033 }
2923 var f2 = () { 3034 var f2 = () {
2924 A; 3035 A;
2925 void f3() { 3036 void f3() {
2926 A; 3037 A;
2927 } 3038 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2971 3082
2972 ConstructorDeclaration constructor = members[0]; 3083 ConstructorDeclaration constructor = members[0];
2973 expectReference(constructor.body, false); 3084 expectReference(constructor.body, false);
2974 3085
2975 MethodDeclaration method = members[1]; 3086 MethodDeclaration method = members[1];
2976 expectReference(method.body, false); 3087 expectReference(method.body, false);
2977 } 3088 }
2978 } 3089 }
2979 3090
2980 @reflectiveTest 3091 @reflectiveTest
3092 class PropagateVariableTypesInUnitTaskTest extends _AbstractDartTaskTest {
3093 void test_perform_cycle() {
3094 AnalysisTarget source = newSource(
3095 '/test.dart',
3096 '''
3097 final piFirst = true;
3098 final pi = piFirst ? 3.14 : tau / 2;
3099 final tau = piFirst ? pi * 2 : 6.28;
3100 ''');
3101 // compute
3102 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6,
3103 matcher: isPropagateVariableTypesInUnitTask);
3104 CompilationUnit unit = outputs[RESOLVED_UNIT6];
3105 // verify
3106 TopLevelVariableElement piFirst =
3107 getTopLevelVariableElement(unit, 'piFirst');
3108 TopLevelVariableElement pi = getTopLevelVariableElement(unit, 'pi');
3109 TopLevelVariableElement tau = getTopLevelVariableElement(unit, 'tau');
3110 expect(piFirst.propagatedType, context.typeProvider.boolType);
3111 expect(pi.propagatedType, isNull);
3112 expect(tau.propagatedType, isNull);
3113 }
3114
3115 void test_perform_topLevel() {
3116 AnalysisTarget source = newSource(
3117 '/test.dart',
3118 '''
3119 final a = b + c.length;
3120 final b = 1;
3121 final c = '2';
3122 final d = a / 2.0;
3123 ''');
3124 // compute
3125 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6,
3126 matcher: isPropagateVariableTypesInUnitTask);
3127 CompilationUnit unit = outputs[RESOLVED_UNIT6];
3128 // verify
3129 InterfaceType intType = context.typeProvider.intType;
3130 InterfaceType numType = context.typeProvider.numType;
3131 InterfaceType stringType = context.typeProvider.stringType;
3132 expect(getTopLevelVariableElement(unit, 'a').propagatedType, intType);
3133 expect(getTopLevelVariableElement(unit, 'b').propagatedType, intType);
3134 expect(getTopLevelVariableElement(unit, 'c').propagatedType, stringType);
3135 expect(getTopLevelVariableElement(unit, 'd').propagatedType, numType);
3136 }
3137 }
3138
3139 @reflectiveTest
3140 class PropagateVariableTypeTaskTest extends _AbstractDartTaskTest {
3141 void test_perform_cycle() {
3142 AnalysisTarget source = newSource(
3143 '/test.dart',
3144 '''
3145 final piFirst = true;
3146 final pi = piFirst ? 3.14 : tau / 2;
3147 final tau = piFirst ? pi * 2 : 6.28;
3148 ''');
3149 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
3150 CompilationUnit unit = outputs[RESOLVED_UNIT5];
3151 TopLevelVariableElement piFirst =
3152 getTopLevelVariableElement(unit, 'piFirst');
3153 TopLevelVariableElement pi = getTopLevelVariableElement(unit, 'pi');
3154 TopLevelVariableElement tau = getTopLevelVariableElement(unit, 'tau');
3155 // compute
3156 computeResult(piFirst, PROPAGATED_VARIABLE,
3157 matcher: isPropagateVariableTypeTask);
3158 expect(piFirst.propagatedType, context.typeProvider.boolType);
3159 expect(pi.propagatedType, isNull);
3160 expect(tau.propagatedType, isNull);
3161 }
3162
3163 void test_perform_null() {
3164 enableStrongMode();
3165 AnalysisTarget source = newSource(
3166 '/test.dart',
3167 '''
3168 var a = null;
3169 ''');
3170 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
3171 CompilationUnit unit = outputs[RESOLVED_UNIT5];
3172 TopLevelVariableElement a = getTopLevelVariableElement(unit, 'a');
3173 // compute
3174 computeResult(a, PROPAGATED_VARIABLE, matcher: isPropagateVariableTypeTask);
3175 expect(a.propagatedType, isNull);
3176 }
3177
3178 void test_perform_topLevel() {
3179 AnalysisTarget source = newSource(
3180 '/test.dart',
3181 '''
3182 final a = b + c.length;
3183 final b = 1;
3184 final c = '2';
3185 ''');
3186 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
3187 CompilationUnit unit = outputs[RESOLVED_UNIT5];
3188 TopLevelVariableElement elementA = getTopLevelVariableElement(unit, 'a');
3189 TopLevelVariableElement elementB = getTopLevelVariableElement(unit, 'b');
3190 TopLevelVariableElement elementC = getTopLevelVariableElement(unit, 'c');
3191 // compute
3192 computeResult(elementA, PROPAGATED_VARIABLE,
3193 matcher: isPropagateVariableTypeTask);
3194 InterfaceType intType = context.typeProvider.intType;
3195 InterfaceType stringType = context.typeProvider.stringType;
3196 expect(elementA.propagatedType, intType);
3197 expect(elementB.propagatedType, intType);
3198 expect(elementC.propagatedType, stringType);
3199 }
3200 }
3201
3202 @reflectiveTest
2981 class ResolveInstanceFieldsInUnitTaskTest extends _AbstractDartTaskTest { 3203 class ResolveInstanceFieldsInUnitTaskTest extends _AbstractDartTaskTest {
2982 @override 3204 @override
2983 void setUp() { 3205 void setUp() {
2984 super.setUp(); 3206 super.setUp();
2985 enableStrongMode(); 3207 enableStrongMode();
2986 } 3208 }
2987 3209
2988 // Test inference of instance fields across units 3210 // Test inference of instance fields across units
2989 void test_perform_inference_cross_unit_instance() { 3211 void test_perform_inference_cross_unit_instance() {
2990 List<Source> sources = newSources({ 3212 List<Source> sources = newSources({
(...skipping 14 matching lines...) Expand all
3005 test1() { 3227 test1() {
3006 int x = 0; 3228 int x = 0;
3007 x = new A().a2; 3229 x = new A().a2;
3008 } 3230 }
3009 ''' 3231 '''
3010 }); 3232 });
3011 InterfaceType intType = context.typeProvider.intType; 3233 InterfaceType intType = context.typeProvider.intType;
3012 DartType dynamicType = context.typeProvider.dynamicType; 3234 DartType dynamicType = context.typeProvider.dynamicType;
3013 3235
3014 computeResult( 3236 computeResult(
3015 new LibrarySpecificUnit(sources[1], sources[1]), RESOLVED_UNIT7); 3237 new LibrarySpecificUnit(sources[1], sources[1]), RESOLVED_UNIT8);
3016 CompilationUnit unit1 = outputs[RESOLVED_UNIT7]; 3238 CompilationUnit unit1 = outputs[RESOLVED_UNIT8];
3017 3239
3018 // B.b2 shoud be resolved on the rhs, but not yet inferred. 3240 // B.b2 shoud be resolved on the rhs, but not yet inferred.
3019 assertVariableDeclarationTypes( 3241 assertVariableDeclarationTypes(
3020 getFieldInClass(unit1, "B", "b2"), dynamicType, intType); 3242 getFieldInClass(unit1, "B", "b2"), dynamicType, intType);
3021 3243
3022 computeResult( 3244 computeResult(
3023 new LibrarySpecificUnit(sources[0], sources[0]), RESOLVED_UNIT7); 3245 new LibrarySpecificUnit(sources[0], sources[0]), RESOLVED_UNIT8);
3024 CompilationUnit unit0 = outputs[RESOLVED_UNIT7]; 3246 CompilationUnit unit0 = outputs[RESOLVED_UNIT8];
3025 3247
3026 // B.b2 should now be fully resolved and inferred. 3248 // B.b2 should now be fully resolved and inferred.
3027 assertVariableDeclarationTypes( 3249 assertVariableDeclarationTypes(
3028 getFieldInClass(unit1, "B", "b2"), intType, intType); 3250 getFieldInClass(unit1, "B", "b2"), intType, intType);
3029 3251
3030 // A.a2 should now be resolved on the rhs, but not yet inferred. 3252 // A.a2 should now be resolved on the rhs, but not yet inferred.
3031 assertVariableDeclarationTypes( 3253 assertVariableDeclarationTypes(
3032 getFieldInClass(unit0, "A", "a2"), dynamicType, intType); 3254 getFieldInClass(unit0, "A", "a2"), dynamicType, intType);
3033 3255
3034 computeResult( 3256 computeResult(
3035 new LibrarySpecificUnit(sources[2], sources[2]), RESOLVED_UNIT7); 3257 new LibrarySpecificUnit(sources[2], sources[2]), RESOLVED_UNIT8);
3036 3258
3037 // A.a2 should now be fully resolved and inferred. 3259 // A.a2 should now be fully resolved and inferred.
3038 assertVariableDeclarationTypes( 3260 assertVariableDeclarationTypes(
3039 getFieldInClass(unit0, "A", "a2"), intType, intType); 3261 getFieldInClass(unit0, "A", "a2"), intType, intType);
3040 3262
3041 assertVariableDeclarationTypes( 3263 assertVariableDeclarationTypes(
3042 getFieldInClass(unit1, "B", "b2"), intType, intType); 3264 getFieldInClass(unit1, "B", "b2"), intType, intType);
3043 } 3265 }
3044 3266
3045 // Test inference of instance fields across units 3267 // Test inference of instance fields across units
(...skipping 16 matching lines...) Expand all
3062 3284
3063 test1() { 3285 test1() {
3064 int x = 0; 3286 int x = 0;
3065 x = new A().a2; 3287 x = new A().a2;
3066 } 3288 }
3067 ''' 3289 '''
3068 }); 3290 });
3069 DartType dynamicType = context.typeProvider.dynamicType; 3291 DartType dynamicType = context.typeProvider.dynamicType;
3070 3292
3071 computeResult( 3293 computeResult(
3072 new LibrarySpecificUnit(sources[0], sources[0]), RESOLVED_UNIT7); 3294 new LibrarySpecificUnit(sources[0], sources[0]), RESOLVED_UNIT8);
3073 CompilationUnit unit0 = outputs[RESOLVED_UNIT7]; 3295 CompilationUnit unit0 = outputs[RESOLVED_UNIT8];
3074 3296
3075 // A.a2 should now be resolved on the rhs, but not yet inferred. 3297 // A.a2 should now be resolved on the rhs, but not yet inferred.
3076 assertVariableDeclarationTypes( 3298 assertVariableDeclarationTypes(
3077 getFieldInClass(unit0, "A", "a2"), dynamicType, dynamicType); 3299 getFieldInClass(unit0, "A", "a2"), dynamicType, dynamicType);
3078 3300
3079 computeResult( 3301 computeResult(
3080 new LibrarySpecificUnit(sources[2], sources[2]), RESOLVED_UNIT7); 3302 new LibrarySpecificUnit(sources[2], sources[2]), RESOLVED_UNIT8);
3081 3303
3082 // A.a2 should now be fully resolved and inferred. 3304 // A.a2 should now be fully resolved and inferred.
3083 assertVariableDeclarationTypes( 3305 assertVariableDeclarationTypes(
3084 getFieldInClass(unit0, "A", "a2"), dynamicType, dynamicType); 3306 getFieldInClass(unit0, "A", "a2"), dynamicType, dynamicType);
3085 } 3307 }
3086 3308
3087 // Test inference of instance fields across units with cycles 3309 // Test inference of instance fields across units with cycles
3088 void test_perform_inference_cross_unit_static_instance() { 3310 void test_perform_inference_cross_unit_static_instance() {
3089 List<Source> sources = newSources({ 3311 List<Source> sources = newSources({
3090 '/a.dart': ''' 3312 '/a.dart': '''
(...skipping 17 matching lines...) Expand all
3108 // inference in A now works. 3330 // inference in A now works.
3109 x = A.a1; 3331 x = A.a1;
3110 x = new A().a2; 3332 x = new A().a2;
3111 } 3333 }
3112 ''' 3334 '''
3113 }); 3335 });
3114 InterfaceType intType = context.typeProvider.intType; 3336 InterfaceType intType = context.typeProvider.intType;
3115 DartType dynamicType = context.typeProvider.dynamicType; 3337 DartType dynamicType = context.typeProvider.dynamicType;
3116 3338
3117 computeResult( 3339 computeResult(
3118 new LibrarySpecificUnit(sources[1], sources[1]), RESOLVED_UNIT7); 3340 new LibrarySpecificUnit(sources[1], sources[1]), RESOLVED_UNIT8);
3119 CompilationUnit unit1 = outputs[RESOLVED_UNIT7]; 3341 CompilationUnit unit1 = outputs[RESOLVED_UNIT8];
3120 3342
3121 assertVariableDeclarationTypes( 3343 assertVariableDeclarationTypes(
3122 getFieldInClass(unit1, "B", "b1"), intType, intType); 3344 getFieldInClass(unit1, "B", "b1"), intType, intType);
3123 assertVariableDeclarationTypes( 3345 assertVariableDeclarationTypes(
3124 getFieldInClass(unit1, "B", "b2"), dynamicType, intType); 3346 getFieldInClass(unit1, "B", "b2"), dynamicType, intType);
3125 3347
3126 computeResult( 3348 computeResult(
3127 new LibrarySpecificUnit(sources[0], sources[0]), RESOLVED_UNIT7); 3349 new LibrarySpecificUnit(sources[0], sources[0]), RESOLVED_UNIT8);
3128 CompilationUnit unit0 = outputs[RESOLVED_UNIT7]; 3350 CompilationUnit unit0 = outputs[RESOLVED_UNIT8];
3129 3351
3130 assertVariableDeclarationTypes( 3352 assertVariableDeclarationTypes(
3131 getFieldInClass(unit0, "A", "a1"), intType, intType); 3353 getFieldInClass(unit0, "A", "a1"), intType, intType);
3132 assertVariableDeclarationTypes( 3354 assertVariableDeclarationTypes(
3133 getFieldInClass(unit0, "A", "a2"), dynamicType, intType); 3355 getFieldInClass(unit0, "A", "a2"), dynamicType, intType);
3134 3356
3135 assertVariableDeclarationTypes( 3357 assertVariableDeclarationTypes(
3136 getFieldInClass(unit1, "B", "b1"), intType, intType); 3358 getFieldInClass(unit1, "B", "b1"), intType, intType);
3137 assertVariableDeclarationTypes( 3359 assertVariableDeclarationTypes(
3138 getFieldInClass(unit1, "B", "b2"), intType, intType); 3360 getFieldInClass(unit1, "B", "b2"), intType, intType);
3139 3361
3140 computeResult( 3362 computeResult(
3141 new LibrarySpecificUnit(sources[2], sources[2]), RESOLVED_UNIT7); 3363 new LibrarySpecificUnit(sources[2], sources[2]), RESOLVED_UNIT8);
3142 3364
3143 assertVariableDeclarationTypes( 3365 assertVariableDeclarationTypes(
3144 getFieldInClass(unit0, "A", "a1"), intType, intType); 3366 getFieldInClass(unit0, "A", "a1"), intType, intType);
3145 assertVariableDeclarationTypes( 3367 assertVariableDeclarationTypes(
3146 getFieldInClass(unit0, "A", "a2"), intType, intType); 3368 getFieldInClass(unit0, "A", "a2"), intType, intType);
3147 3369
3148 assertVariableDeclarationTypes( 3370 assertVariableDeclarationTypes(
3149 getFieldInClass(unit1, "B", "b1"), intType, intType); 3371 getFieldInClass(unit1, "B", "b1"), intType, intType);
3150 assertVariableDeclarationTypes( 3372 assertVariableDeclarationTypes(
3151 getFieldInClass(unit1, "B", "b2"), intType, intType); 3373 getFieldInClass(unit1, "B", "b2"), intType, intType);
(...skipping 18 matching lines...) Expand all
3170 test1() { 3392 test1() {
3171 int x = 0; 3393 int x = 0;
3172 x = new A().a2; 3394 x = new A().a2;
3173 } 3395 }
3174 ''' 3396 '''
3175 }); 3397 });
3176 InterfaceType intType = context.typeProvider.intType; 3398 InterfaceType intType = context.typeProvider.intType;
3177 DartType dynamicType = context.typeProvider.dynamicType; 3399 DartType dynamicType = context.typeProvider.dynamicType;
3178 3400
3179 computeResult( 3401 computeResult(
3180 new LibrarySpecificUnit(sources[0], sources[0]), RESOLVED_UNIT7); 3402 new LibrarySpecificUnit(sources[0], sources[0]), RESOLVED_UNIT8);
3181 CompilationUnit unit0 = outputs[RESOLVED_UNIT7]; 3403 CompilationUnit unit0 = outputs[RESOLVED_UNIT8];
3182 3404
3183 // A.a2 should now be resolved on the rhs, but not yet inferred. 3405 // A.a2 should now be resolved on the rhs, but not yet inferred.
3184 assertVariableDeclarationTypes( 3406 assertVariableDeclarationTypes(
3185 getFieldInClass(unit0, "A", "a2"), dynamicType, dynamicType); 3407 getFieldInClass(unit0, "A", "a2"), dynamicType, dynamicType);
3186 3408
3187 // B.b2 shoud be resolved on the rhs, but not yet inferred. 3409 // B.b2 shoud be resolved on the rhs, but not yet inferred.
3188 assertVariableDeclarationTypes( 3410 assertVariableDeclarationTypes(
3189 getFieldInClass(unit0, "B", "b2"), dynamicType, intType); 3411 getFieldInClass(unit0, "B", "b2"), dynamicType, intType);
3190 3412
3191 computeResult( 3413 computeResult(
3192 new LibrarySpecificUnit(sources[1], sources[1]), RESOLVED_UNIT7); 3414 new LibrarySpecificUnit(sources[1], sources[1]), RESOLVED_UNIT8);
3193 3415
3194 // A.a2 should now be fully resolved and inferred. 3416 // A.a2 should now be fully resolved and inferred.
3195 assertVariableDeclarationTypes( 3417 assertVariableDeclarationTypes(
3196 getFieldInClass(unit0, "A", "a2"), dynamicType, dynamicType); 3418 getFieldInClass(unit0, "A", "a2"), dynamicType, dynamicType);
3197 3419
3198 // B.b2 should now be fully resolved and inferred. 3420 // B.b2 should now be fully resolved and inferred.
3199 assertVariableDeclarationTypes( 3421 assertVariableDeclarationTypes(
3200 getFieldInClass(unit0, "B", "b2"), intType, intType); 3422 getFieldInClass(unit0, "B", "b2"), intType, intType);
3201 } 3423 }
3202 } 3424 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
3271 void f() { 3493 void f() {
3272 var c = new C(); 3494 var c = new C();
3273 c.m(); 3495 c.m();
3274 } 3496 }
3275 class C { 3497 class C {
3276 void m() { 3498 void m() {
3277 f(); 3499 f();
3278 } 3500 }
3279 } 3501 }
3280 '''); 3502 ''');
3281 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9, 3503 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT10,
3282 matcher: isResolveUnitTask); 3504 matcher: isResolveUnitTask);
3283 CompilationUnit unit = outputs[RESOLVED_UNIT9]; 3505 CompilationUnit unit = outputs[RESOLVED_UNIT10];
3284 3506
3285 FunctionDeclaration f = unit.declarations[0]; 3507 FunctionDeclaration f = unit.declarations[0];
3286 _assertResolved(f.functionExpression.body); 3508 _assertResolved(f.functionExpression.body);
3287 3509
3288 MethodDeclaration m = (unit.declarations[1] as ClassDeclaration).members[0]; 3510 MethodDeclaration m = (unit.declarations[1] as ClassDeclaration).members[0];
3289 _assertResolved(m.body); 3511 _assertResolved(m.body);
3290 3512
3291 expect(outputs[RESOLVE_UNIT_ERRORS], hasLength(0)); 3513 expect(outputs[RESOLVE_UNIT_ERRORS], hasLength(0));
3292 } 3514 }
3293 3515
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
3527 // Check that even within a static variable cycle, inferred 3749 // Check that even within a static variable cycle, inferred
3528 // types get propagated to the members of the cycle. 3750 // types get propagated to the members of the cycle.
3529 void test_perform_cycle() { 3751 void test_perform_cycle() {
3530 AnalysisTarget source = newSource( 3752 AnalysisTarget source = newSource(
3531 '/test.dart', 3753 '/test.dart',
3532 ''' 3754 '''
3533 var piFirst = true; 3755 var piFirst = true;
3534 var pi = piFirst ? 3.14 : tau / 2; 3756 var pi = piFirst ? 3.14 : tau / 2;
3535 var tau = piFirst ? pi * 2 : 6.28; 3757 var tau = piFirst ? pi * 2 : 6.28;
3536 '''); 3758 ''');
3537 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9); 3759 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT10);
3538 CompilationUnit unit = outputs[RESOLVED_UNIT9]; 3760 CompilationUnit unit = outputs[RESOLVED_UNIT10];
3539 VariableElement piFirst = 3761 VariableElement piFirst =
3540 getTopLevelVariable(unit, 'piFirst').name.staticElement; 3762 getTopLevelVariable(unit, 'piFirst').name.staticElement;
3541 VariableElement pi = getTopLevelVariable(unit, 'pi').name.staticElement; 3763 VariableElement pi = getTopLevelVariable(unit, 'pi').name.staticElement;
3542 VariableElement tau = getTopLevelVariable(unit, 'tau').name.staticElement; 3764 VariableElement tau = getTopLevelVariable(unit, 'tau').name.staticElement;
3543 Expression piFirstUse = (getTopLevelVariable(unit, 'tau').initializer 3765 Expression piFirstUse = (getTopLevelVariable(unit, 'tau').initializer
3544 as ConditionalExpression).condition; 3766 as ConditionalExpression).condition;
3545 3767
3546 expect(piFirstUse.staticType, context.typeProvider.boolType); 3768 expect(piFirstUse.staticType, context.typeProvider.boolType);
3547 expect(piFirst.type, context.typeProvider.boolType); 3769 expect(piFirst.type, context.typeProvider.boolType);
3548 expect(pi.type.isDynamic, isTrue); 3770 expect(pi.type.isDynamic, isTrue);
(...skipping 17 matching lines...) Expand all
3566 3788
3567 test1() { 3789 test1() {
3568 int t = 3; 3790 int t = 3;
3569 t = x; 3791 t = x;
3570 t = y; 3792 t = y;
3571 t = A.x; 3793 t = A.x;
3572 t = B.y; 3794 t = B.y;
3573 } 3795 }
3574 '''); 3796 ''');
3575 computeResult( 3797 computeResult(
3576 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT9); 3798 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT10);
3577 CompilationUnit unit1 = outputs[RESOLVED_UNIT9]; 3799 CompilationUnit unit1 = outputs[RESOLVED_UNIT10];
3578 computeResult( 3800 computeResult(
3579 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT9); 3801 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT10);
3580 CompilationUnit unit2 = outputs[RESOLVED_UNIT9]; 3802 CompilationUnit unit2 = outputs[RESOLVED_UNIT10];
3581 3803
3582 InterfaceType intType = context.typeProvider.intType; 3804 InterfaceType intType = context.typeProvider.intType;
3583 3805
3584 assertVariableDeclarationTypes( 3806 assertVariableDeclarationTypes(
3585 getTopLevelVariable(unit1, "x"), intType, intType); 3807 getTopLevelVariable(unit1, "x"), intType, intType);
3586 assertVariableDeclarationTypes( 3808 assertVariableDeclarationTypes(
3587 getFieldInClass(unit1, "A", "x"), intType, intType); 3809 getFieldInClass(unit1, "A", "x"), intType, intType);
3588 3810
3589 assertVariableDeclarationTypes( 3811 assertVariableDeclarationTypes(
3590 getTopLevelVariable(unit2, "y"), intType, intType); 3812 getTopLevelVariable(unit2, "y"), intType, intType);
(...skipping 26 matching lines...) Expand all
3617 '/main.dart': ''' 3839 '/main.dart': '''
3618 import "a.dart"; 3840 import "a.dart";
3619 3841
3620 test1() { 3842 test1() {
3621 int x = 0; 3843 int x = 0;
3622 x = new A().a2; 3844 x = new A().a2;
3623 } 3845 }
3624 ''' 3846 '''
3625 }); 3847 });
3626 List<dynamic> units = 3848 List<dynamic> units =
3627 computeLibraryResults(sources, RESOLVED_UNIT9).toList(); 3849 computeLibraryResults(sources, RESOLVED_UNIT10).toList();
3628 CompilationUnit unit0 = units[0]; 3850 CompilationUnit unit0 = units[0];
3629 CompilationUnit unit1 = units[1]; 3851 CompilationUnit unit1 = units[1];
3630 CompilationUnit unit2 = units[2]; 3852 CompilationUnit unit2 = units[2];
3631 3853
3632 InterfaceType intType = context.typeProvider.intType; 3854 InterfaceType intType = context.typeProvider.intType;
3633 3855
3634 assertVariableDeclarationTypes( 3856 assertVariableDeclarationTypes(
3635 getFieldInClass(unit0, "A", "a2"), intType, intType); 3857 getFieldInClass(unit0, "A", "a2"), intType, intType);
3636 3858
3637 assertVariableDeclarationTypes( 3859 assertVariableDeclarationTypes(
(...skipping 26 matching lines...) Expand all
3664 ''', 3886 ''',
3665 '/c.dart': ''' 3887 '/c.dart': '''
3666 import 'b.dart'; 3888 import 'b.dart';
3667 var bar = new B(); 3889 var bar = new B();
3668 void foo() { 3890 void foo() {
3669 String x = bar.f.z; 3891 String x = bar.f.z;
3670 } 3892 }
3671 ''' 3893 '''
3672 }); 3894 });
3673 List<dynamic> units = 3895 List<dynamic> units =
3674 computeLibraryResults(sources, RESOLVED_UNIT9).toList(); 3896 computeLibraryResults(sources, RESOLVED_UNIT10).toList();
3675 CompilationUnit unit0 = units[0]; 3897 CompilationUnit unit0 = units[0];
3676 CompilationUnit unit2 = units[2]; 3898 CompilationUnit unit2 = units[2];
3677 3899
3678 InterfaceType intType = context.typeProvider.intType; 3900 InterfaceType intType = context.typeProvider.intType;
3679 InterfaceType stringType = context.typeProvider.stringType; 3901 InterfaceType stringType = context.typeProvider.stringType;
3680 3902
3681 assertVariableDeclarationStatementTypes( 3903 assertVariableDeclarationStatementTypes(
3682 getStatementsInTopLevelFunction(unit0, "foo")[0], stringType, intType); 3904 getStatementsInTopLevelFunction(unit0, "foo")[0], stringType, intType);
3683 assertVariableDeclarationStatementTypes( 3905 assertVariableDeclarationStatementTypes(
3684 getStatementsInTopLevelFunction(unit2, "foo")[0], stringType, intType); 3906 getStatementsInTopLevelFunction(unit2, "foo")[0], stringType, intType);
(...skipping 16 matching lines...) Expand all
3701 class B { static var y = A.x; } 3923 class B { static var y = A.x; }
3702 3924
3703 test1() { 3925 test1() {
3704 x = /*severe:StaticTypeError*/"hi"; 3926 x = /*severe:StaticTypeError*/"hi";
3705 y = /*severe:StaticTypeError*/"hi"; 3927 y = /*severe:StaticTypeError*/"hi";
3706 A.x = /*severe:StaticTypeError*/"hi"; 3928 A.x = /*severe:StaticTypeError*/"hi";
3707 B.y = /*severe:StaticTypeError*/"hi"; 3929 B.y = /*severe:StaticTypeError*/"hi";
3708 } 3930 }
3709 '''); 3931 ''');
3710 computeResult( 3932 computeResult(
3711 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT9); 3933 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT10);
3712 CompilationUnit unit1 = outputs[RESOLVED_UNIT9]; 3934 CompilationUnit unit1 = outputs[RESOLVED_UNIT10];
3713 computeResult( 3935 computeResult(
3714 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT9); 3936 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT10);
3715 CompilationUnit unit2 = outputs[RESOLVED_UNIT9]; 3937 CompilationUnit unit2 = outputs[RESOLVED_UNIT10];
3716 3938
3717 InterfaceType intType = context.typeProvider.intType; 3939 InterfaceType intType = context.typeProvider.intType;
3718 InterfaceType stringType = context.typeProvider.stringType; 3940 InterfaceType stringType = context.typeProvider.stringType;
3719 3941
3720 assertVariableDeclarationTypes( 3942 assertVariableDeclarationTypes(
3721 getTopLevelVariable(unit1, "x"), intType, intType); 3943 getTopLevelVariable(unit1, "x"), intType, intType);
3722 assertVariableDeclarationTypes( 3944 assertVariableDeclarationTypes(
3723 getFieldInClass(unit1, "A", "x"), intType, intType); 3945 getFieldInClass(unit1, "A", "x"), intType, intType);
3724 3946
3725 assertVariableDeclarationTypes( 3947 assertVariableDeclarationTypes(
(...skipping 29 matching lines...) Expand all
3755 3977
3756 test1() { 3978 test1() {
3757 int x = 0; 3979 int x = 0;
3758 // inference in A now works. 3980 // inference in A now works.
3759 x = A.a1; 3981 x = A.a1;
3760 x = new A().a2; 3982 x = new A().a2;
3761 } 3983 }
3762 ''' 3984 '''
3763 }); 3985 });
3764 List<dynamic> units = 3986 List<dynamic> units =
3765 computeLibraryResults(sources, RESOLVED_UNIT9).toList(); 3987 computeLibraryResults(sources, RESOLVED_UNIT10).toList();
3766 CompilationUnit unit0 = units[0]; 3988 CompilationUnit unit0 = units[0];
3767 CompilationUnit unit1 = units[1]; 3989 CompilationUnit unit1 = units[1];
3768 CompilationUnit unit2 = units[2]; 3990 CompilationUnit unit2 = units[2];
3769 3991
3770 InterfaceType intType = context.typeProvider.intType; 3992 InterfaceType intType = context.typeProvider.intType;
3771 3993
3772 assertVariableDeclarationTypes( 3994 assertVariableDeclarationTypes(
3773 getFieldInClass(unit0, "A", "a1"), intType, intType); 3995 getFieldInClass(unit0, "A", "a1"), intType, intType);
3774 assertVariableDeclarationTypes( 3996 assertVariableDeclarationTypes(
3775 getFieldInClass(unit0, "A", "a2"), intType, intType); 3997 getFieldInClass(unit0, "A", "a2"), intType, intType);
(...skipping 15 matching lines...) Expand all
3791 AnalysisTarget source = newSource( 4013 AnalysisTarget source = newSource(
3792 '/test.dart', 4014 '/test.dart',
3793 ''' 4015 '''
3794 test() { 4016 test() {
3795 int x = 3; 4017 int x = 3;
3796 x = "hi"; 4018 x = "hi";
3797 var y = 3; 4019 var y = 3;
3798 y = "hi"; 4020 y = "hi";
3799 } 4021 }
3800 '''); 4022 ''');
3801 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9); 4023 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT10);
3802 CompilationUnit unit = outputs[RESOLVED_UNIT9]; 4024 CompilationUnit unit = outputs[RESOLVED_UNIT10];
3803 4025
3804 InterfaceType intType = context.typeProvider.intType; 4026 InterfaceType intType = context.typeProvider.intType;
3805 InterfaceType stringType = context.typeProvider.stringType; 4027 InterfaceType stringType = context.typeProvider.stringType;
3806 4028
3807 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test"); 4029 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
3808 4030
3809 assertVariableDeclarationStatementTypes(statements[0], intType, intType); 4031 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
3810 assertAssignmentStatementTypes(statements[1], intType, stringType); 4032 assertAssignmentStatementTypes(statements[1], intType, stringType);
3811 assertVariableDeclarationStatementTypes(statements[2], intType, intType); 4033 assertVariableDeclarationStatementTypes(statements[2], intType, intType);
3812 assertAssignmentStatementTypes(statements[3], intType, stringType); 4034 assertAssignmentStatementTypes(statements[3], intType, stringType);
(...skipping 16 matching lines...) Expand all
3829 b = 4; 4051 b = 4;
3830 var c = z; 4052 var c = z;
3831 c = "hi"; 4053 c = "hi";
3832 c = 4; 4054 c = 4;
3833 } 4055 }
3834 4056
3835 int y; // field def after use 4057 int y; // field def after use
3836 final z = 42; // should infer `int` 4058 final z = 42; // should infer `int`
3837 } 4059 }
3838 '''); 4060 ''');
3839 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9); 4061 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT10);
3840 CompilationUnit unit = outputs[RESOLVED_UNIT9]; 4062 CompilationUnit unit = outputs[RESOLVED_UNIT10];
3841 4063
3842 InterfaceType intType = context.typeProvider.intType; 4064 InterfaceType intType = context.typeProvider.intType;
3843 InterfaceType stringType = context.typeProvider.stringType; 4065 InterfaceType stringType = context.typeProvider.stringType;
3844 4066
3845 List<Statement> statements = getStatementsInMethod(unit, "A", "test1"); 4067 List<Statement> statements = getStatementsInMethod(unit, "A", "test1");
3846 4068
3847 assertVariableDeclarationStatementTypes(statements[0], intType, intType); 4069 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
3848 assertAssignmentStatementTypes(statements[1], intType, stringType); 4070 assertAssignmentStatementTypes(statements[1], intType, stringType);
3849 assertAssignmentStatementTypes(statements[2], intType, intType); 4071 assertAssignmentStatementTypes(statements[2], intType, intType);
3850 4072
(...skipping 26 matching lines...) Expand all
3877 b = /*severe:StaticTypeError*/"hi"; 4099 b = /*severe:StaticTypeError*/"hi";
3878 b = 4; 4100 b = 4;
3879 var c = z; 4101 var c = z;
3880 c = /*severe:StaticTypeError*/"hi"; 4102 c = /*severe:StaticTypeError*/"hi";
3881 c = 4; 4103 c = 4;
3882 } 4104 }
3883 4105
3884 int y = 0; // field def after use 4106 int y = 0; // field def after use
3885 final z = 42; // should infer `int` 4107 final z = 42; // should infer `int`
3886 '''); 4108 ''');
3887 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9); 4109 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT10);
3888 CompilationUnit unit = outputs[RESOLVED_UNIT9]; 4110 CompilationUnit unit = outputs[RESOLVED_UNIT10];
3889 4111
3890 InterfaceType intType = context.typeProvider.intType; 4112 InterfaceType intType = context.typeProvider.intType;
3891 InterfaceType stringType = context.typeProvider.stringType; 4113 InterfaceType stringType = context.typeProvider.stringType;
3892 4114
3893 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test1"); 4115 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test1");
3894 4116
3895 assertVariableDeclarationStatementTypes(statements[0], intType, intType); 4117 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
3896 assertAssignmentStatementTypes(statements[1], intType, stringType); 4118 assertAssignmentStatementTypes(statements[1], intType, stringType);
3897 assertAssignmentStatementTypes(statements[2], intType, intType); 4119 assertAssignmentStatementTypes(statements[2], intType, intType);
3898 4120
(...skipping 30 matching lines...) Expand all
3929 4151
3930 test() { 4152 test() {
3931 x = "hi"; 4153 x = "hi";
3932 y = /*severe:StaticTypeError*/"hi"; 4154 y = /*severe:StaticTypeError*/"hi";
3933 A.x = "hi"; 4155 A.x = "hi";
3934 A.y = /*severe:StaticTypeError*/"hi"; 4156 A.y = /*severe:StaticTypeError*/"hi";
3935 new A().x2 = "hi"; 4157 new A().x2 = "hi";
3936 new A().y2 = /*severe:StaticTypeError*/"hi"; 4158 new A().y2 = /*severe:StaticTypeError*/"hi";
3937 } 4159 }
3938 '''); 4160 ''');
3939 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9); 4161 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT10);
3940 CompilationUnit unit = outputs[RESOLVED_UNIT9]; 4162 CompilationUnit unit = outputs[RESOLVED_UNIT10];
3941 4163
3942 InterfaceType intType = context.typeProvider.intType; 4164 InterfaceType intType = context.typeProvider.intType;
3943 InterfaceType stringType = context.typeProvider.stringType; 4165 InterfaceType stringType = context.typeProvider.stringType;
3944 DartType bottomType = context.typeProvider.bottomType; 4166 DartType bottomType = context.typeProvider.bottomType;
3945 DartType dynamicType = context.typeProvider.dynamicType; 4167 DartType dynamicType = context.typeProvider.dynamicType;
3946 4168
3947 assertVariableDeclarationTypes( 4169 assertVariableDeclarationTypes(
3948 getTopLevelVariable(unit, "x"), dynamicType, bottomType); 4170 getTopLevelVariable(unit, "x"), dynamicType, bottomType);
3949 assertVariableDeclarationTypes( 4171 assertVariableDeclarationTypes(
3950 getTopLevelVariable(unit, "y"), intType, intType); 4172 getTopLevelVariable(unit, "y"), intType, intType);
(...skipping 19 matching lines...) Expand all
3970 // Test inference between fields and method bodies 4192 // Test inference between fields and method bodies
3971 void test_perform_local_explicit_disabled() { 4193 void test_perform_local_explicit_disabled() {
3972 AnalysisTarget source = newSource( 4194 AnalysisTarget source = newSource(
3973 '/test.dart', 4195 '/test.dart',
3974 ''' 4196 '''
3975 test() { 4197 test() {
3976 int x = 3; 4198 int x = 3;
3977 x = "hi"; 4199 x = "hi";
3978 } 4200 }
3979 '''); 4201 ''');
3980 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT9); 4202 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT10);
3981 CompilationUnit unit = outputs[RESOLVED_UNIT9]; 4203 CompilationUnit unit = outputs[RESOLVED_UNIT10];
3982 4204
3983 InterfaceType intType = context.typeProvider.intType; 4205 InterfaceType intType = context.typeProvider.intType;
3984 InterfaceType stringType = context.typeProvider.stringType; 4206 InterfaceType stringType = context.typeProvider.stringType;
3985 4207
3986 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test"); 4208 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
3987 VariableDeclaration decl = 4209 VariableDeclaration decl =
3988 (statements[0] as VariableDeclarationStatement).variables.variables[0]; 4210 (statements[0] as VariableDeclarationStatement).variables.variables[0];
3989 expect(decl.element.type, intType); 4211 expect(decl.element.type, intType);
3990 expect(decl.initializer.staticType, intType); 4212 expect(decl.initializer.staticType, intType);
3991 4213
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
4245 return field; 4467 return field;
4246 } 4468 }
4247 } 4469 }
4248 } 4470 }
4249 } 4471 }
4250 fail('No field named $fieldName in $className'); 4472 fail('No field named $fieldName in $className');
4251 return null; 4473 return null;
4252 } 4474 }
4253 4475
4254 /** 4476 /**
4477 * Return the element of the field with the given [fieldName] in the class
4478 * with the given [className] in the given compilation [unit].
4479 */
4480 FieldElement getFieldInClassElement(
4481 CompilationUnit unit, String className, String fieldName) {
4482 return getFieldInClass(unit, className, fieldName)?.name?.staticElement;
4483 }
4484
4485 /**
4255 * Return the declaration of the method with the given [methodName] in the 4486 * Return the declaration of the method with the given [methodName] in the
4256 * class with the given [className] in the given compilation [unit]. 4487 * class with the given [className] in the given compilation [unit].
4257 */ 4488 */
4258 MethodDeclaration getMethodInClass( 4489 MethodDeclaration getMethodInClass(
4259 CompilationUnit unit, String className, String methodName) { 4490 CompilationUnit unit, String className, String methodName) {
4260 ClassDeclaration unitMember = getClass(unit, className); 4491 ClassDeclaration unitMember = getClass(unit, className);
4261 NodeList<ClassMember> classMembers = unitMember.members; 4492 NodeList<ClassMember> classMembers = unitMember.members;
4262 for (ClassMember classMember in classMembers) { 4493 for (ClassMember classMember in classMembers) {
4263 if (classMember is MethodDeclaration) { 4494 if (classMember is MethodDeclaration) {
4264 if (classMember.name.name == methodName) { 4495 if (classMember.name.name == methodName) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
4315 for (VariableDeclaration variable in variables) { 4546 for (VariableDeclaration variable in variables) {
4316 if (variable.name.name == variableName) { 4547 if (variable.name.name == variableName) {
4317 return variable; 4548 return variable;
4318 } 4549 }
4319 } 4550 }
4320 } 4551 }
4321 } 4552 }
4322 return null; 4553 return null;
4323 } 4554 }
4324 4555
4556 /**
4557 * Return the top-level variable element with the given [name].
4558 */
4559 TopLevelVariableElement getTopLevelVariableElement(
4560 CompilationUnit unit, String name) {
4561 return getTopLevelVariable(unit, name)?.name?.staticElement;
4562 }
4563
4325 void setUp() { 4564 void setUp() {
4326 super.setUp(); 4565 super.setUp();
4327 emptySource = newSource('/test.dart'); 4566 emptySource = newSource('/test.dart');
4328 } 4567 }
4329 4568
4330 /** 4569 /**
4331 * Fill [errorListener] with [result] errors in the current [task]. 4570 * Fill [errorListener] with [result] errors in the current [task].
4332 */ 4571 */
4333 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) { 4572 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) {
4334 List<AnalysisError> errors = task.outputs[result]; 4573 List<AnalysisError> errors = task.outputs[result];
4335 expect(errors, isNotNull, reason: result.name); 4574 expect(errors, isNotNull, reason: result.name);
4336 errorListener = new GatheringErrorListener(); 4575 errorListener = new GatheringErrorListener();
4337 errorListener.addAll(errors); 4576 errorListener.addAll(errors);
4338 } 4577 }
4339 } 4578 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/task/dart.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698