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