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

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

Issue 1370793002: Improve the handling of inference for instance fields in the task model (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments 1 Created 5 years, 2 months 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 runReflectiveTests(InferStaticVariableTypeTaskTest); 53 runReflectiveTests(InferStaticVariableTypeTaskTest);
54 runReflectiveTests(LibraryErrorsReadyTaskTest); 54 runReflectiveTests(LibraryErrorsReadyTaskTest);
55 runReflectiveTests(LibraryUnitErrorsTaskTest); 55 runReflectiveTests(LibraryUnitErrorsTaskTest);
56 runReflectiveTests(ParseDartTaskTest); 56 runReflectiveTests(ParseDartTaskTest);
57 runReflectiveTests(PartiallyResolveUnitReferencesTaskTest); 57 runReflectiveTests(PartiallyResolveUnitReferencesTaskTest);
58 runReflectiveTests(ResolveFunctionBodiesInUnitTaskTest); 58 runReflectiveTests(ResolveFunctionBodiesInUnitTaskTest);
59 runReflectiveTests(ResolveLibraryTypeNamesTaskTest); 59 runReflectiveTests(ResolveLibraryTypeNamesTaskTest);
60 runReflectiveTests(ResolveUnitTypeNamesTaskTest); 60 runReflectiveTests(ResolveUnitTypeNamesTaskTest);
61 runReflectiveTests(ResolveVariableReferencesTaskTest); 61 runReflectiveTests(ResolveVariableReferencesTaskTest);
62 runReflectiveTests(ScanDartTaskTest); 62 runReflectiveTests(ScanDartTaskTest);
63 runReflectiveTests(StrongModeInferenceTest);
63 runReflectiveTests(VerifyUnitTaskTest); 64 runReflectiveTests(VerifyUnitTaskTest);
64 } 65 }
65 66
66 isInstanceOf isBuildCompilationUnitElementTask = 67 isInstanceOf isBuildCompilationUnitElementTask =
67 new isInstanceOf<BuildCompilationUnitElementTask>(); 68 new isInstanceOf<BuildCompilationUnitElementTask>();
68 isInstanceOf isBuildDirectiveElementsTask = 69 isInstanceOf isBuildDirectiveElementsTask =
69 new isInstanceOf<BuildDirectiveElementsTask>(); 70 new isInstanceOf<BuildDirectiveElementsTask>();
70 isInstanceOf isBuildEnumMemberElementsTask = 71 isInstanceOf isBuildEnumMemberElementsTask =
71 new isInstanceOf<BuildEnumMemberElementsTask>(); 72 new isInstanceOf<BuildEnumMemberElementsTask>();
72 isInstanceOf isBuildExportNamespaceTask = 73 isInstanceOf isBuildExportNamespaceTask =
(...skipping 1826 matching lines...) Expand 10 before | Expand all | Expand 10 after
1899 VariableDeclaration field = getFieldInClass(unit, 'B', 'f'); 1900 VariableDeclaration field = getFieldInClass(unit, 'B', 'f');
1900 MethodDeclaration method = getMethodInClass(unit, 'B', 'm'); 1901 MethodDeclaration method = getMethodInClass(unit, 'B', 'm');
1901 DartType typeX = getClass(unit, 'X').element.type; 1902 DartType typeX = getClass(unit, 'X').element.type;
1902 DartType typeY = getClass(unit, 'Y').element.type; 1903 DartType typeY = getClass(unit, 'Y').element.type;
1903 DartType typeZ = getClass(unit, 'Z').element.type; 1904 DartType typeZ = getClass(unit, 'Z').element.type;
1904 1905
1905 expect(field.element.type, typeX); 1906 expect(field.element.type, typeX);
1906 expect(method.element.returnType, typeY); 1907 expect(method.element.returnType, typeY);
1907 expect(method.element.parameters[0].type, typeZ); 1908 expect(method.element.parameters[0].type, typeZ);
1908 } 1909 }
1910
1911 void test_perform_cross_library_const() {
1912 enableStrongMode();
1913 AnalysisTarget firstSource = newSource(
1914 '/first.dart',
1915 '''
1916 library first;
1917
1918 const a = 'hello';
1919 ''');
1920 AnalysisTarget secondSource = newSource(
1921 '/second.dart',
1922 '''
1923 import 'first.dart';
1924
1925 const b = a;
1926 class M {
1927 String c = a;
1928 }
1929 ''');
1930 computeResult(
1931 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT7,
1932 matcher: isInferInstanceMembersInUnitTask);
1933 CompilationUnit firstUnit = outputs[RESOLVED_UNIT7];
1934 computeResult(
1935 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT7);
1936 CompilationUnit secondUnit = outputs[RESOLVED_UNIT7];
1937
1938 VariableDeclaration variableA = getTopLevelVariable(firstUnit, 'a');
1939 VariableDeclaration variableB = getTopLevelVariable(secondUnit, 'b');
1940 VariableDeclaration variableC = getFieldInClass(secondUnit, 'M', 'c');
1941 InterfaceType stringType = context.typeProvider.stringType;
1942
1943 expect(variableA.element.type, stringType);
1944 expect(variableB.element.type, stringType);
1945 expect(variableB.initializer.staticType, stringType);
1946 expect(variableC.element.type, stringType);
1947 expect(variableC.initializer.staticType, stringType);
1948 }
1949
1950 void test_perform_reresolution() {
1951 enableStrongMode();
1952 AnalysisTarget source = newSource(
1953 '/test.dart',
1954 '''
1955 const topLevel = '';
1956 class C {
1957 String field = topLevel;
1958 }
1959 ''');
1960 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT7);
1961 CompilationUnit unit = outputs[RESOLVED_UNIT7];
1962 VariableDeclaration topLevelDecl = getTopLevelVariable(unit, 'topLevel');
1963 VariableDeclaration fieldDecl = getFieldInClass(unit, 'C', 'field');
1964 VariableElement topLevel = topLevelDecl.name.staticElement;
1965 VariableElement field = fieldDecl.name.staticElement;
1966
1967 InterfaceType stringType = context.typeProvider.stringType;
1968 expect(topLevel.type, stringType);
1969 expect(field.type, stringType);
1970 expect(fieldDecl.initializer.staticType, stringType);
1971 }
1909 } 1972 }
1910 1973
1911 @reflectiveTest 1974 @reflectiveTest
1912 class InferStaticVariableTypesInUnitTaskTest extends _AbstractDartTaskTest { 1975 class InferStaticVariableTypesInUnitTaskTest extends _AbstractDartTaskTest {
1913 void test_perform_nestedDeclarations() { 1976 void test_perform_nestedDeclarations() {
1914 enableStrongMode(); 1977 enableStrongMode();
1915 AnalysisTarget source = newSource( 1978 AnalysisTarget source = newSource(
1916 '/test.dart', 1979 '/test.dart',
1917 ''' 1980 '''
1918 var f = (int x) { 1981 var f = (int x) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1980 CompilationUnit unit = outputs[RESOLVED_UNIT6]; 2043 CompilationUnit unit = outputs[RESOLVED_UNIT6];
1981 TopLevelVariableDeclaration declaration = unit.declarations[1]; 2044 TopLevelVariableDeclaration declaration = unit.declarations[1];
1982 FunctionExpression function = 2045 FunctionExpression function =
1983 declaration.variables.variables[0].initializer; 2046 declaration.variables.variables[0].initializer;
1984 BlockFunctionBody body = function.body; 2047 BlockFunctionBody body = function.body;
1985 ReturnStatement statement = body.block.statements[0]; 2048 ReturnStatement statement = body.block.statements[0];
1986 Expression expression = statement.expression; 2049 Expression expression = statement.expression;
1987 InterfaceType intType = context.typeProvider.intType; 2050 InterfaceType intType = context.typeProvider.intType;
1988 expect(expression.staticType, intType); 2051 expect(expression.staticType, intType);
1989 } 2052 }
2053
2054 void test_perform_const_field() {
2055 enableStrongMode();
2056 AnalysisTarget source = newSource(
2057 '/test.dart',
2058 '''
2059 class M {
2060 static const X = "";
2061 }
2062 ''');
2063 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6,
2064 matcher: isInferStaticVariableTypesInUnitTask);
2065 CompilationUnit unit = outputs[RESOLVED_UNIT6];
2066 VariableDeclaration declaration = getFieldInClass(unit, 'M', 'X');
2067 InterfaceType stringType = context.typeProvider.stringType;
2068 expect(declaration.element.type, stringType);
2069 }
1990 } 2070 }
1991 2071
1992 @reflectiveTest 2072 @reflectiveTest
1993 class InferStaticVariableTypeTaskTest extends _AbstractDartTaskTest { 2073 class InferStaticVariableTypeTaskTest extends _AbstractDartTaskTest {
1994 void test_getDeclaration_staticField() { 2074 void test_getDeclaration_staticField() {
1995 AnalysisTarget source = newSource( 2075 AnalysisTarget source = newSource(
1996 '/test.dart', 2076 '/test.dart',
1997 ''' 2077 '''
1998 class C { 2078 class C {
1999 var field = ''; 2079 var field = '';
(...skipping 18 matching lines...) Expand all
2018 CompilationUnit unit = outputs[RESOLVED_UNIT5]; 2098 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2019 VariableDeclaration declaration = getTopLevelVariable(unit, 'topLevel'); 2099 VariableDeclaration declaration = getTopLevelVariable(unit, 'topLevel');
2020 VariableElement variable = declaration.name.staticElement; 2100 VariableElement variable = declaration.name.staticElement;
2021 InferStaticVariableTypeTask inferTask = 2101 InferStaticVariableTypeTask inferTask =
2022 new InferStaticVariableTypeTask(task.context, variable); 2102 new InferStaticVariableTypeTask(task.context, variable);
2023 expect(inferTask.getDeclaration(unit), declaration); 2103 expect(inferTask.getDeclaration(unit), declaration);
2024 } 2104 }
2025 2105
2026 void test_perform() { 2106 void test_perform() {
2027 AnalysisTarget source = newSource( 2107 AnalysisTarget source = newSource(
2108 '/test3.dart',
2109 '''
2110 var topLevel3 = '';
2111 class C {
2112 var field3 = topLevel3;
2113 }
2114 ''');
2115 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2116 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2117 VariableDeclaration topLevelDecl = getTopLevelVariable(unit, 'topLevel3');
2118 VariableDeclaration fieldDecl = getFieldInClass(unit, 'C', 'field3');
2119 VariableElement topLevel = topLevelDecl.name.staticElement;
2120 VariableElement field = fieldDecl.name.staticElement;
2121
2122 computeResult(field, INFERRED_STATIC_VARIABLE,
2123 matcher: isInferStaticVariableTypeTask);
2124 InterfaceType stringType = context.typeProvider.stringType;
2125 expect(topLevel.type, stringType);
2126 expect(field.type, stringType);
2127 expect(fieldDecl.initializer.staticType, stringType);
2128 expect(outputs[INFER_STATIC_VARIABLE_ERRORS], hasLength(0));
2129 }
2130
2131 void test_perform_reresolution() {
2132 AnalysisTarget source = newSource(
2028 '/test.dart', 2133 '/test.dart',
2029 ''' 2134 '''
2030 var topLevel = ''; 2135 const topLevel = '';
2136 class C {
2137 String field = topLevel;
2138 }
2139 ''');
2140 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2141 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2142 VariableDeclaration topLevelDecl = getTopLevelVariable(unit, 'topLevel');
2143 VariableDeclaration fieldDecl = getFieldInClass(unit, 'C', 'field');
2144 VariableElement topLevel = topLevelDecl.name.staticElement;
2145 VariableElement field = fieldDecl.name.staticElement;
2146
2147 computeResult(field, INFERRED_STATIC_VARIABLE,
2148 matcher: isInferStaticVariableTypeTask);
2149 InterfaceType stringType = context.typeProvider.stringType;
2150 expect(topLevel.type, stringType);
2151 expect(field.type, stringType);
2152 expect(fieldDecl.initializer.staticType, stringType);
2153 expect(outputs[INFER_STATIC_VARIABLE_ERRORS], hasLength(0));
2154 }
2155
2156 void test_perform_const() {
2157 AnalysisTarget source = newSource(
2158 '/test.dart',
2159 '''
2160 const topLevel = "hello";
2031 class C { 2161 class C {
2032 var field = topLevel; 2162 var field = topLevel;
2033 } 2163 }
2034 '''); 2164 ''');
2035 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5); 2165 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT5);
2036 CompilationUnit unit = outputs[RESOLVED_UNIT5]; 2166 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2037 VariableElement topLevel = 2167 VariableElement topLevel =
2038 getTopLevelVariable(unit, 'topLevel').name.staticElement; 2168 getTopLevelVariable(unit, 'topLevel').name.staticElement;
2039 VariableElement field = 2169 VariableElement field =
2040 getFieldInClass(unit, 'C', 'field').name.staticElement; 2170 getFieldInClass(unit, 'C', 'field').name.staticElement;
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
2300 class A {} 2430 class A {}
2301 class C { 2431 class C {
2302 static final f = ''; 2432 static final f = '';
2303 var g = 0; 2433 var g = 0;
2304 } 2434 }
2305 '''); 2435 ''');
2306 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 2436 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2307 computeResult(target, RESOLVED_UNIT5, 2437 computeResult(target, RESOLVED_UNIT5,
2308 matcher: isPartiallyResolveUnitReferencesTask); 2438 matcher: isPartiallyResolveUnitReferencesTask);
2309 // Test the outputs 2439 // Test the outputs
2310 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(4)); 2440 expect(outputs[INFERABLE_STATIC_VARIABLES_IN_UNIT], hasLength(5));
2311 CompilationUnit unit = outputs[RESOLVED_UNIT5]; 2441 CompilationUnit unit = outputs[RESOLVED_UNIT5];
2312 expect(unit, same(outputs[RESOLVED_UNIT5])); 2442 expect(unit, same(outputs[RESOLVED_UNIT5]));
2313 // Test the state of the AST 2443 // Test the state of the AST
2314 TopLevelVariableDeclaration a = unit.declarations[0]; 2444 TopLevelVariableDeclaration a = unit.declarations[0];
2315 VariableDeclaration variableA = a.variables.variables[0]; 2445 VariableDeclaration variableA = a.variables.variables[0];
2316 SimpleIdentifier initializer = variableA.initializer; 2446 SimpleIdentifier initializer = variableA.initializer;
2317 expect(initializer.staticElement, isNotNull); 2447 expect(initializer.staticElement, isNotNull);
2318 // Test the error generation 2448 // Test the error generation
2319 _fillErrorListener(PARTIALLY_RESOLVE_REFERENCES_ERRORS); 2449 _fillErrorListener(PARTIALLY_RESOLVE_REFERENCES_ERRORS);
2320 errorListener.assertNoErrors(); 2450 errorListener.assertNoErrors();
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
2457 } 2587 }
2458 2588
2459 void _assertResolved(FunctionBody body) { 2589 void _assertResolved(FunctionBody body) {
2460 ResolutionVerifier verifier = new ResolutionVerifier(); 2590 ResolutionVerifier verifier = new ResolutionVerifier();
2461 body.accept(verifier); 2591 body.accept(verifier);
2462 verifier.assertResolved(); 2592 verifier.assertResolved();
2463 } 2593 }
2464 } 2594 }
2465 2595
2466 @reflectiveTest 2596 @reflectiveTest
2597 class StrongModeInferenceTest extends _AbstractDartTaskTest {
2598
2599 @override
2600 void setUp() {
2601 super.setUp();
2602 enableStrongMode();
2603 }
2604
2605 // Check that even within a static variable cycle, inferred
2606 // types get propagated to the members of the cycle.
2607 void test_perform_cycle() {
2608 AnalysisTarget source = newSource(
2609 '/test.dart',
2610 '''
2611 var piFirst = true;
2612 var pi = piFirst ? 3.14 : tau / 2;
2613 var tau = piFirst ? pi * 2 : 6.28;
2614 ''');
2615 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2616 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2617 VariableElement piFirst =
2618 getTopLevelVariable(unit, 'piFirst').name.staticElement;
2619 VariableElement pi = getTopLevelVariable(unit, 'pi').name.staticElement;
2620 VariableElement tau = getTopLevelVariable(unit, 'tau').name.staticElement;
2621 Expression piFirstUse = (getTopLevelVariable(unit, 'tau').initializer
2622 as ConditionalExpression).condition;
2623
2624 expect(piFirstUse.staticType, context.typeProvider.boolType);
2625 expect(piFirst.type, context.typeProvider.boolType);
2626 expect(pi.type.isDynamic, isTrue);
2627 expect(tau.type.isDynamic, isTrue);
2628 }
2629
2630 void test_perform_local_explicit_disabled() {
2631 AnalysisTarget source = newSource(
2632 '/test.dart',
2633 '''
2634 test() {
2635 int x = 3;
2636 x = "hi";
2637 }
2638 ''');
2639 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2640 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2641
2642 InterfaceType intType = context.typeProvider.intType;
2643 InterfaceType stringType = context.typeProvider.stringType;
2644
2645 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
2646 VariableDeclaration decl =
2647 (statements[0] as VariableDeclarationStatement).variables.variables[0];
2648 expect(decl.element.type, intType);
2649 expect(decl.initializer.staticType, intType);
2650
2651 ExpressionStatement statement = statements[1];
2652 AssignmentExpression assgn = statement.expression;
2653 expect(assgn.leftHandSide.staticType, intType);
2654 expect(assgn.rightHandSide.staticType, stringType);
2655 }
2656
2657 void assertVariableDeclarationTypes(
2658 VariableDeclaration decl, DartType varType, DartType initializerType) {
2659 expect(decl.element.type, varType);
2660 expect(decl.initializer.staticType, initializerType);
2661 }
2662
2663 void assertVariableDeclarationStatementTypes(
2664 Statement stmt, DartType varType, DartType initializerType) {
2665 VariableDeclaration decl =
2666 (stmt as VariableDeclarationStatement).variables.variables[0];
2667 assertVariableDeclarationTypes(decl, varType, initializerType);
2668 }
2669
2670 void assertAssignmentStatementTypes(
2671 Statement stmt, DartType leftType, DartType rightType) {
2672 AssignmentExpression assgn = (stmt as ExpressionStatement).expression;
2673 expect(assgn.leftHandSide.staticType, leftType);
2674 expect(assgn.rightHandSide.staticType, rightType);
2675 }
2676
2677 // Test that local variables in method bodies are inferred appropriately
2678 void test_perform_inference_local_variables() {
2679 AnalysisTarget source = newSource(
2680 '/test.dart',
2681 '''
2682 test() {
2683 int x = 3;
2684 x = "hi";
2685 var y = 3;
2686 y = "hi";
2687 }
2688 ''');
2689 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2690 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2691
2692 InterfaceType intType = context.typeProvider.intType;
2693 InterfaceType stringType = context.typeProvider.stringType;
2694
2695 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
2696
2697 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
2698 assertAssignmentStatementTypes(statements[1], intType, stringType);
2699 assertVariableDeclarationStatementTypes(statements[2], intType, intType);
2700 assertAssignmentStatementTypes(statements[3], intType, stringType);
2701 }
2702
2703 // Test inference interactions between local variables and fields
2704 void test_perform_inference_local_variables_fields() {
2705 AnalysisTarget source = newSource(
2706 '/test.dart',
2707 '''
2708 class A {
2709 int x = 0;
2710
2711 test1() {
2712 var a = x;
2713 a = "hi";
2714 a = 3;
2715 var b = y;
2716 b = "hi";
2717 b = 4;
2718 var c = z;
2719 c = "hi";
2720 c = 4;
2721 }
2722
2723 int y; // field def after use
2724 final z = 42; // should infer `int`
2725 }
2726 ''');
2727 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2728 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2729
2730 InterfaceType intType = context.typeProvider.intType;
2731 InterfaceType stringType = context.typeProvider.stringType;
2732
2733 List<Statement> statements = getStatementsInMethod(unit, "A", "test1");
2734
2735 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
2736 assertAssignmentStatementTypes(statements[1], intType, stringType);
2737 assertAssignmentStatementTypes(statements[2], intType, intType);
2738
2739 assertVariableDeclarationStatementTypes(statements[3], intType, intType);
2740 assertAssignmentStatementTypes(statements[4], intType, stringType);
2741 assertAssignmentStatementTypes(statements[5], intType, intType);
2742
2743 assertVariableDeclarationStatementTypes(statements[6], intType, intType);
2744 assertAssignmentStatementTypes(statements[7], intType, stringType);
2745 assertAssignmentStatementTypes(statements[8], intType, intType);
2746
2747 assertVariableDeclarationTypes(
2748 getFieldInClass(unit, "A", "x"), intType, intType);
2749 assertVariableDeclarationTypes(
2750 getFieldInClass(unit, "A", "z"), intType, intType);
2751 }
2752
2753 // Test inference interactions between local variables and top level
2754 // variables
2755 void test_perform_inference_local_variables_topLevel() {
2756 AnalysisTarget source = newSource(
2757 '/test.dart',
2758 '''
2759 int x = 0;
2760
2761 test1() {
2762 var a = x;
2763 a = /*severe:StaticTypeError*/"hi";
2764 a = 3;
2765 var b = y;
2766 b = /*severe:StaticTypeError*/"hi";
2767 b = 4;
2768 var c = z;
2769 c = /*severe:StaticTypeError*/"hi";
2770 c = 4;
2771 }
2772
2773 int y = 0; // field def after use
2774 final z = 42; // should infer `int`
2775 ''');
2776 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2777 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2778
2779 InterfaceType intType = context.typeProvider.intType;
2780 InterfaceType stringType = context.typeProvider.stringType;
2781
2782 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test1");
2783
2784 assertVariableDeclarationStatementTypes(statements[0], intType, intType);
2785 assertAssignmentStatementTypes(statements[1], intType, stringType);
2786 assertAssignmentStatementTypes(statements[2], intType, intType);
2787
2788 assertVariableDeclarationStatementTypes(statements[3], intType, intType);
2789 assertAssignmentStatementTypes(statements[4], intType, stringType);
2790 assertAssignmentStatementTypes(statements[5], intType, intType);
2791
2792 assertVariableDeclarationStatementTypes(statements[6], intType, intType);
2793 assertAssignmentStatementTypes(statements[7], intType, stringType);
2794 assertAssignmentStatementTypes(statements[8], intType, intType);
2795
2796 assertVariableDeclarationTypes(
2797 getTopLevelVariable(unit, "x"), intType, intType);
2798 assertVariableDeclarationTypes(
2799 getTopLevelVariable(unit, "y"), intType, intType);
2800 assertVariableDeclarationTypes(
2801 getTopLevelVariable(unit, "z"), intType, intType);
2802 }
2803
2804 // Test that inference does not propagate from null
2805 void test_perform_inference_null() {
2806 AnalysisTarget source = newSource(
2807 '/test.dart',
2808 '''
2809 var x = null;
2810 var y = 3;
2811 class A {
2812 static var x = null;
2813 static var y = 3;
2814
2815 var x2 = null;
2816 var y2 = 3;
2817 }
2818
2819 test() {
2820 x = "hi";
2821 y = /*severe:StaticTypeError*/"hi";
2822 A.x = "hi";
2823 A.y = /*severe:StaticTypeError*/"hi";
2824 new A().x2 = "hi";
2825 new A().y2 = /*severe:StaticTypeError*/"hi";
2826 }
2827 ''');
2828 computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT8);
2829 CompilationUnit unit = outputs[RESOLVED_UNIT8];
2830
2831 InterfaceType intType = context.typeProvider.intType;
2832 InterfaceType stringType = context.typeProvider.stringType;
2833 DartType bottomType = context.typeProvider.bottomType;
2834 DartType dynamicType = context.typeProvider.dynamicType;
2835
2836 assertVariableDeclarationTypes(
2837 getTopLevelVariable(unit, "x"), dynamicType, bottomType);
2838 assertVariableDeclarationTypes(
2839 getTopLevelVariable(unit, "y"), intType, intType);
2840 assertVariableDeclarationTypes(
2841 getFieldInClass(unit, "A", "x"), dynamicType, bottomType);
2842 assertVariableDeclarationTypes(
2843 getFieldInClass(unit, "A", "y"), intType, intType);
2844 assertVariableDeclarationTypes(
2845 getFieldInClass(unit, "A", "x2"), dynamicType, bottomType);
2846 assertVariableDeclarationTypes(
2847 getFieldInClass(unit, "A", "y2"), intType, intType);
2848
2849 List<Statement> statements = getStatementsInTopLevelFunction(unit, "test");
2850
2851 assertAssignmentStatementTypes(statements[0], dynamicType, stringType);
2852 assertAssignmentStatementTypes(statements[1], intType, stringType);
2853 assertAssignmentStatementTypes(statements[2], dynamicType, stringType);
2854 assertAssignmentStatementTypes(statements[3], intType, stringType);
2855 assertAssignmentStatementTypes(statements[4], dynamicType, stringType);
2856 assertAssignmentStatementTypes(statements[5], intType, stringType);
2857 }
2858
2859 // Test inference across units (non-cyclic)
2860 void test_perform_inference_cross_unit_non_cyclic() {
2861 AnalysisTarget firstSource = newSource(
2862 '/a.dart',
2863 '''
2864 var x = 2;
2865 class A { static var x = 2; }
2866 ''');
2867 AnalysisTarget secondSource = newSource(
2868 '/test.dart',
2869 '''
2870 import 'a.dart';
2871 var y = x;
2872 class B { static var y = A.x; }
2873
2874 test1() {
2875 x = /*severe:StaticTypeError*/"hi";
2876 y = /*severe:StaticTypeError*/"hi";
2877 A.x = /*severe:StaticTypeError*/"hi";
2878 B.y = /*severe:StaticTypeError*/"hi";
2879 }
2880 ''');
2881 computeResult(
2882 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT8);
2883 CompilationUnit unit1 = outputs[RESOLVED_UNIT8];
2884 computeResult(
2885 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT8);
2886 CompilationUnit unit2 = outputs[RESOLVED_UNIT8];
2887
2888 InterfaceType intType = context.typeProvider.intType;
2889 InterfaceType stringType = context.typeProvider.stringType;
2890 DartType dynamicType = context.typeProvider.dynamicType;
2891
2892 assertVariableDeclarationTypes(
2893 getTopLevelVariable(unit1, "x"), intType, intType);
2894 assertVariableDeclarationTypes(
2895 getFieldInClass(unit1, "A", "x"), intType, intType);
2896
2897 assertVariableDeclarationTypes(
2898 getTopLevelVariable(unit2, "y"), intType, intType);
2899 assertVariableDeclarationTypes(
2900 getFieldInClass(unit2, "B", "y"), intType, intType);
2901
2902 List<Statement> statements =
2903 getStatementsInTopLevelFunction(unit2, "test1");
2904
2905 assertAssignmentStatementTypes(statements[0], intType, stringType);
2906 assertAssignmentStatementTypes(statements[1], intType, stringType);
2907 }
2908
2909 // Test inference across units (cyclic)
2910 void test_perform_inference_cross_unit_cyclic() {
2911 AnalysisTarget firstSource = newSource(
2912 '/a.dart',
2913 '''
2914 import 'test.dart';
2915 var x = 2;
2916 class A { static var x = 2; }
2917 ''');
2918 AnalysisTarget secondSource = newSource(
2919 '/test.dart',
2920 '''
2921 import 'a.dart';
2922 var y = x;
2923 class B { static var y = A.x; }
2924
2925 test1() {
2926 int t = 3;
2927 t = x;
2928 t = y;
2929 t = A.x;
2930 t = B.y;
2931 }
2932 ''');
2933 computeResult(
2934 new LibrarySpecificUnit(firstSource, firstSource), RESOLVED_UNIT8);
2935 CompilationUnit unit1 = outputs[RESOLVED_UNIT8];
2936 computeResult(
2937 new LibrarySpecificUnit(secondSource, secondSource), RESOLVED_UNIT8);
2938 CompilationUnit unit2 = outputs[RESOLVED_UNIT8];
2939
2940 InterfaceType intType = context.typeProvider.intType;
2941 InterfaceType stringType = context.typeProvider.stringType;
2942
2943 assertVariableDeclarationTypes(
2944 getTopLevelVariable(unit1, "x"), intType, intType);
2945 assertVariableDeclarationTypes(
2946 getFieldInClass(unit1, "A", "x"), intType, intType);
2947
2948 assertVariableDeclarationTypes(
2949 getTopLevelVariable(unit2, "y"), intType, intType);
2950 assertVariableDeclarationTypes(
2951 getFieldInClass(unit2, "B", "y"), intType, intType);
2952
2953 List<Statement> statements =
2954 getStatementsInTopLevelFunction(unit2, "test1");
2955
2956 assertAssignmentStatementTypes(statements[1], intType, intType);
2957 assertAssignmentStatementTypes(statements[2], intType, intType);
2958 assertAssignmentStatementTypes(statements[3], intType, intType);
2959 assertAssignmentStatementTypes(statements[4], intType, intType);
2960 }
2961
2962 // Test inference of instance fields across units
2963 // TODO(leafp): Fix this
2964 // https://github.com/dart-lang/dev_compiler/issues/354
2965 void fail_perform_inference_cross_unit_instance() {
2966 List<Source> sources = newSources({
2967 '/a7.dart': '''
2968 import 'b7.dart';
2969 class A {
2970 final a2 = new B().b2;
2971 }
2972 ''',
2973 '/b7.dart': '''
2974 class B {
2975 final b2 = 1;
2976 }
2977 ''',
2978 '/main7.dart': '''
2979 import "a7.dart";
2980
2981 test1() {
2982 int x = 0;
2983 x = new A().a2;
2984 }
2985 '''
2986 });
2987 List<dynamic> units =
2988 computeLibraryResults(sources, RESOLVED_UNIT8).toList();
2989 CompilationUnit unit0 = units[0];
2990 CompilationUnit unit1 = units[1];
2991 CompilationUnit unit2 = units[2];
2992
2993 InterfaceType intType = context.typeProvider.intType;
2994
2995 assertVariableDeclarationTypes(
2996 getFieldInClass(unit0, "A", "a2"), intType, intType);
2997
2998 assertVariableDeclarationTypes(
2999 getFieldInClass(unit1, "B", "b2"), intType, intType);
3000
3001 List<Statement> statements =
3002 getStatementsInTopLevelFunction(unit2, "test1");
3003
3004 assertAssignmentStatementTypes(statements[1], intType, intType);
3005 }
3006
3007 // Test inference between static and instance fields
3008 // TODO(leafp): Fix this
3009 // https://github.com/dart-lang/dev_compiler/issues/354
3010 void fail_perform_inference_cross_unit_static_instance() {
3011 List<Source> sources = newSources({
3012 '/a.dart': '''
3013 import 'b.dart';
3014 class A {
3015 static final a1 = B.b1;
3016 final a2 = new B().b2;
3017 }
3018 ''',
3019 '/b.dart': '''
3020 class B {
3021 static final b1 = 1;
3022 final b2 = 1;
3023 }
3024 ''',
3025 '/main.dart': '''
3026 import "a.dart";
3027
3028 test1() {
3029 int x = 0;
3030 // inference in A now works.
3031 x = A.a1;
3032 x = new A().a2;
3033 }
3034 '''
3035 });
3036 List<dynamic> units =
3037 computeLibraryResults(sources, RESOLVED_UNIT8).toList();
3038 CompilationUnit unit0 = units[0];
3039 CompilationUnit unit1 = units[1];
3040 CompilationUnit unit2 = units[2];
3041
3042 InterfaceType intType = context.typeProvider.intType;
3043
3044 assertVariableDeclarationTypes(
3045 getFieldInClass(unit0, "A", "a1"), intType, intType);
3046 assertVariableDeclarationTypes(
3047 getFieldInClass(unit0, "A", "a2"), intType, intType);
3048
3049 assertVariableDeclarationTypes(
3050 getFieldInClass(unit1, "B", "b1"), intType, intType);
3051 assertVariableDeclarationTypes(
3052 getFieldInClass(unit1, "B", "b2"), intType, intType);
3053
3054 List<Statement> statements =
3055 getStatementsInTopLevelFunction(unit2, "test1");
3056
3057 assertAssignmentStatementTypes(statements[1], intType, intType);
3058 assertAssignmentStatementTypes(statements[2], intType, intType);
3059 }
3060 }
3061
3062 @reflectiveTest
2467 class ResolveLibraryTypeNamesTaskTest extends _AbstractDartTaskTest { 3063 class ResolveLibraryTypeNamesTaskTest extends _AbstractDartTaskTest {
2468 test_perform() { 3064 test_perform() {
2469 Source sourceLib = newSource( 3065 Source sourceLib = newSource(
2470 '/my_lib.dart', 3066 '/my_lib.dart',
2471 ''' 3067 '''
2472 library my_lib; 3068 library my_lib;
2473 part 'my_part.dart'; 3069 part 'my_part.dart';
2474 class A {} 3070 class A {}
2475 class B extends A {} 3071 class B extends A {}
2476 '''); 3072 ''');
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
2784 } 3380 }
2785 } 3381 }
2786 '''); 3382 ''');
2787 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source); 3383 LibrarySpecificUnit target = new LibrarySpecificUnit(source, source);
2788 computeResult(target, VERIFY_ERRORS, matcher: isVerifyUnitTask); 3384 computeResult(target, VERIFY_ERRORS, matcher: isVerifyUnitTask);
2789 // validate 3385 // validate
2790 _fillErrorListener(VERIFY_ERRORS); 3386 _fillErrorListener(VERIFY_ERRORS);
2791 errorListener.assertErrorsWithCodes( 3387 errorListener.assertErrorsWithCodes(
2792 <ErrorCode>[StaticTypeWarningCode.NON_BOOL_CONDITION]); 3388 <ErrorCode>[StaticTypeWarningCode.NON_BOOL_CONDITION]);
2793 } 3389 }
3390
3391 void test_perform_reresolution() {
3392 enableStrongMode();
3393 AnalysisTarget source = newSource(
3394 '/test.dart',
3395 '''
3396 const topLevel = 3;
3397 class C {
3398 String field = topLevel;
3399 }
3400 ''');
3401 computeResult(new LibrarySpecificUnit(source, source), VERIFY_ERRORS);
3402 // validate
3403 _fillErrorListener(VERIFY_ERRORS);
3404 errorListener.assertErrorsWithCodes(
3405 <ErrorCode>[StaticTypeWarningCode.INVALID_ASSIGNMENT]);
3406 }
2794 } 3407 }
2795 3408
2796 class _AbstractDartTaskTest extends AbstractContextTest { 3409 class _AbstractDartTaskTest extends AbstractContextTest {
2797 Source emptySource; 3410 Source emptySource;
2798 3411
2799 GatheringErrorListener errorListener = new GatheringErrorListener(); 3412 GatheringErrorListener errorListener = new GatheringErrorListener();
2800 3413
2801 void assertIsInvalid(AnalysisTarget target, ResultDescriptor descriptor) { 3414 void assertIsInvalid(AnalysisTarget target, ResultDescriptor descriptor) {
2802 CacheEntry entry = context.getCacheEntry(target); 3415 CacheEntry entry = context.getCacheEntry(target);
2803 expect(entry.isInvalid(descriptor), isTrue); 3416 expect(entry.isInvalid(descriptor), isTrue);
2804 } 3417 }
2805 3418
2806 void assertIsValid(AnalysisTarget target, ResultDescriptor descriptor) { 3419 void assertIsValid(AnalysisTarget target, ResultDescriptor descriptor) {
2807 CacheEntry entry = context.getCacheEntry(target); 3420 CacheEntry entry = context.getCacheEntry(target);
2808 expect(entry.isValid(descriptor), isTrue); 3421 expect(entry.isValid(descriptor), isTrue);
2809 } 3422 }
2810 3423
2811 void assertSameResults(List<ResultDescriptor> descriptors) { 3424 void assertSameResults(List<ResultDescriptor> descriptors) {
2812 descriptors.forEach((descriptor) { 3425 descriptors.forEach((descriptor) {
2813 var oldResult = oldOutputs[descriptor]; 3426 var oldResult = oldOutputs[descriptor];
2814 var newResult = outputs[descriptor]; 3427 var newResult = outputs[descriptor];
2815 expect(newResult, same(oldResult), reason: descriptor.name); 3428 expect(newResult, same(oldResult), reason: descriptor.name);
2816 }); 3429 });
2817 } 3430 }
2818 3431
3432 List<dynamic> computeLibraryResults(
3433 List<Source> sources, ResultDescriptor result,
3434 {isInstanceOf matcher: null}) {
3435 dynamic compute(Source source) {
3436 computeResult(new LibrarySpecificUnit(source, source), result,
3437 matcher: matcher);
3438 return outputs[result];
3439 }
3440 return sources.map(compute).toList();
3441 }
3442
2819 /** 3443 /**
2820 * Create a script object with a single fragment containing the given 3444 * Create a script object with a single fragment containing the given
2821 * [scriptContent]. 3445 * [scriptContent].
2822 */ 3446 */
2823 DartScript createScript(String scriptContent) { 3447 DartScript createScript(String scriptContent) {
2824 String htmlContent = ''' 3448 String htmlContent = '''
2825 <!DOCTYPE html> 3449 <!DOCTYPE html>
2826 <html> 3450 <html>
2827 <head> 3451 <head>
2828 <title>test page</title> 3452 <title>test page</title>
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
2894 if (classMember is MethodDeclaration) { 3518 if (classMember is MethodDeclaration) {
2895 if (classMember.name.name == methodName) { 3519 if (classMember.name.name == methodName) {
2896 return classMember; 3520 return classMember;
2897 } 3521 }
2898 } 3522 }
2899 } 3523 }
2900 fail('No method named $methodName in $className'); 3524 fail('No method named $methodName in $className');
2901 return null; 3525 return null;
2902 } 3526 }
2903 3527
3528 List<Statement> getStatementsInMethod(
3529 CompilationUnit unit, String className, String methodName) {
3530 MethodDeclaration method = getMethodInClass(unit, className, methodName);
3531 BlockFunctionBody body = method.body;
3532 return body.block.statements;
3533 }
3534
3535 List<Statement> getStatementsInTopLevelFunction(
3536 CompilationUnit unit, String functionName) {
3537 FunctionDeclaration function = getTopLevelFunction(unit, functionName);
3538 BlockFunctionBody body = function.functionExpression.body;
3539 return body.block.statements;
3540 }
3541
2904 /** 3542 /**
2905 * Return the declaration of the top-level variable with the given 3543 * Return the declaration of the top-level variable with the given
2906 * [variableName] in the given compilation [unit]. 3544 * [variableName] in the given compilation [unit].
2907 */ 3545 */
2908 VariableDeclaration getTopLevelVariable( 3546 VariableDeclaration getTopLevelVariable(
2909 CompilationUnit unit, String variableName) { 3547 CompilationUnit unit, String variableName) {
2910 NodeList<CompilationUnitMember> unitMembers = unit.declarations; 3548 NodeList<CompilationUnitMember> unitMembers = unit.declarations;
2911 for (CompilationUnitMember unitMember in unitMembers) { 3549 for (CompilationUnitMember unitMember in unitMembers) {
2912 if (unitMember is TopLevelVariableDeclaration) { 3550 if (unitMember is TopLevelVariableDeclaration) {
2913 NodeList<VariableDeclaration> variables = 3551 NodeList<VariableDeclaration> variables =
2914 unitMember.variables.variables; 3552 unitMember.variables.variables;
2915 for (VariableDeclaration variable in variables) { 3553 for (VariableDeclaration variable in variables) {
2916 if (variable.name.name == variableName) { 3554 if (variable.name.name == variableName) {
2917 return variable; 3555 return variable;
2918 } 3556 }
2919 } 3557 }
2920 } 3558 }
2921 } 3559 }
2922 return null; 3560 return null;
2923 } 3561 }
2924 3562
3563 /**
3564 * Return the declaration of the top-level function with the given
3565 * [functionName] in the given compilation [unit].
3566 */
3567 FunctionDeclaration getTopLevelFunction(
3568 CompilationUnit unit, String functionName) {
3569 NodeList<CompilationUnitMember> unitMembers = unit.declarations;
3570 for (CompilationUnitMember unitMember in unitMembers) {
3571 if (unitMember is FunctionDeclaration) {
3572 if (unitMember.name.name == functionName) {
3573 return unitMember;
3574 }
3575 }
3576 }
3577 return null;
3578 }
3579
2925 void setUp() { 3580 void setUp() {
2926 super.setUp(); 3581 super.setUp();
2927 emptySource = newSource('/test.dart'); 3582 emptySource = newSource('/test.dart');
2928 } 3583 }
2929 3584
2930 /** 3585 /**
2931 * Fill [errorListener] with [result] errors in the current [task]. 3586 * Fill [errorListener] with [result] errors in the current [task].
2932 */ 3587 */
2933 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) { 3588 void _fillErrorListener(ResultDescriptor<List<AnalysisError>> result) {
2934 List<AnalysisError> errors = task.outputs[result]; 3589 List<AnalysisError> errors = task.outputs[result];
2935 expect(errors, isNotNull, reason: result.name); 3590 expect(errors, isNotNull, reason: result.name);
2936 errorListener = new GatheringErrorListener(); 3591 errorListener = new GatheringErrorListener();
2937 errorListener.addAll(errors); 3592 errorListener.addAll(errors);
2938 } 3593 }
2939 } 3594 }
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