From e84d0694671eebfc54c68bbd9384817c14beda8d Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Fri, 11 Sep 2015 08:47:05 -0700 Subject: [PATCH] Fixed the last unit test that was failing with the new task model R=scheglov@google.com Review URL: https://codereview.chromium.org//1327363002 . --- pkg/analyzer/lib/src/generated/engine.dart | 32 ++++++++--- pkg/analyzer/lib/src/generated/resolver.dart | 60 ++++++++++++++------ pkg/analyzer/lib/src/task/dart.dart | 2 +- pkg/analyzer/lib/src/task/driver.dart | 5 ++ pkg/analyzer/test/src/task/dart_test.dart | 36 +++++++++--- 5 files changed, 100 insertions(+), 35 deletions(-) diff --git a/pkg/analyzer/lib/src/generated/engine.dart b/pkg/analyzer/lib/src/generated/engine.dart index 5eccbf7ee5b..c0e548ccf0b 100644 --- a/pkg/analyzer/lib/src/generated/engine.dart +++ b/pkg/analyzer/lib/src/generated/engine.dart @@ -10309,6 +10309,12 @@ class RecursiveXmlVisitor_ResolveHtmlTask_internalPerform * used to visit that structure. */ class ResolutionEraser extends GeneralizingAstVisitor { + /** + * A flag indicating whether the elements associated with declarations should + * be erased. + */ + bool eraseDeclarations = true; + @override Object visitAssignmentExpression(AssignmentExpression node) { node.staticElement = null; @@ -10331,13 +10337,17 @@ class ResolutionEraser extends GeneralizingAstVisitor { @override Object visitCompilationUnit(CompilationUnit node) { - node.element = null; + if (eraseDeclarations) { + node.element = null; + } return super.visitCompilationUnit(node); } @override Object visitConstructorDeclaration(ConstructorDeclaration node) { - node.element = null; + if (eraseDeclarations) { + node.element = null; + } return super.visitConstructorDeclaration(node); } @@ -10355,7 +10365,9 @@ class ResolutionEraser extends GeneralizingAstVisitor { @override Object visitDirective(Directive node) { - node.element = null; + if (eraseDeclarations) { + node.element = null; + } return super.visitDirective(node); } @@ -10368,7 +10380,9 @@ class ResolutionEraser extends GeneralizingAstVisitor { @override Object visitFunctionExpression(FunctionExpression node) { - node.element = null; + if (eraseDeclarations) { + node.element = null; + } return super.visitFunctionExpression(node); } @@ -10415,7 +10429,9 @@ class ResolutionEraser extends GeneralizingAstVisitor { @override Object visitSimpleIdentifier(SimpleIdentifier node) { - node.staticElement = null; + if (eraseDeclarations || !node.inDeclarationContext()) { + node.staticElement = null; + } node.propagatedElement = null; return super.visitSimpleIdentifier(node); } @@ -10429,8 +10445,10 @@ class ResolutionEraser extends GeneralizingAstVisitor { /** * Remove any resolution information from the given AST structure. */ - static void erase(AstNode node) { - node.accept(new ResolutionEraser()); + static void erase(AstNode node, {bool eraseDeclarations: true}) { + ResolutionEraser eraser = new ResolutionEraser(); + eraser.eraseDeclarations = eraseDeclarations; + node.accept(eraser); } } diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart index eaed29edf9a..af5235c6a4f 100644 --- a/pkg/analyzer/lib/src/generated/resolver.dart +++ b/pkg/analyzer/lib/src/generated/resolver.dart @@ -9565,6 +9565,12 @@ class PartialResolverVisitor extends ResolverVisitor { */ final List staticVariables = []; + /** + * A flag indicating whether we are currently visiting a child of either a + * field or a top-level variable. + */ + bool inFieldOrTopLevelVariable = false; + /** * A flag indicating whether we should discard errors while resolving the * initializer for variable declarations. We do this for top-level variables @@ -9600,27 +9606,39 @@ class PartialResolverVisitor extends ResolverVisitor { @override Object visitBlockFunctionBody(BlockFunctionBody node) { + if (inFieldOrTopLevelVariable) { + return super.visitBlockFunctionBody(node); + } return null; } @override Object visitExpressionFunctionBody(ExpressionFunctionBody node) { + if (inFieldOrTopLevelVariable) { + return super.visitExpressionFunctionBody(node); + } return null; } @override Object visitFieldDeclaration(FieldDeclaration node) { - if (strongMode && node.isStatic) { - _addStaticVariables(node.fields.variables); - bool wasDiscarding = discardErrorsInInitializer; - discardErrorsInInitializer = true; - try { - return super.visitFieldDeclaration(node); - } finally { - discardErrorsInInitializer = wasDiscarding; + bool wasInFieldOrTopLevelVariable = inFieldOrTopLevelVariable; + try { + inFieldOrTopLevelVariable = true; + if (strongMode && node.isStatic) { + _addStaticVariables(node.fields.variables); + bool wasDiscarding = discardErrorsInInitializer; + discardErrorsInInitializer = true; + try { + return super.visitFieldDeclaration(node); + } finally { + discardErrorsInInitializer = wasDiscarding; + } } + return super.visitFieldDeclaration(node); + } finally { + inFieldOrTopLevelVariable = wasInFieldOrTopLevelVariable; } - return super.visitFieldDeclaration(node); } @override @@ -9637,17 +9655,23 @@ class PartialResolverVisitor extends ResolverVisitor { @override Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { - if (strongMode) { - _addStaticVariables(node.variables.variables); - bool wasDiscarding = discardErrorsInInitializer; - discardErrorsInInitializer = true; - try { - return super.visitTopLevelVariableDeclaration(node); - } finally { - discardErrorsInInitializer = wasDiscarding; + bool wasInFieldOrTopLevelVariable = inFieldOrTopLevelVariable; + try { + inFieldOrTopLevelVariable = true; + if (strongMode) { + _addStaticVariables(node.variables.variables); + bool wasDiscarding = discardErrorsInInitializer; + discardErrorsInInitializer = true; + try { + return super.visitTopLevelVariableDeclaration(node); + } finally { + discardErrorsInInitializer = wasDiscarding; + } } + return super.visitTopLevelVariableDeclaration(node); + } finally { + inFieldOrTopLevelVariable = wasInFieldOrTopLevelVariable; } - return super.visitTopLevelVariableDeclaration(node); } /** diff --git a/pkg/analyzer/lib/src/task/dart.dart b/pkg/analyzer/lib/src/task/dart.dart index 03e449dd09c..88a5637b42c 100644 --- a/pkg/analyzer/lib/src/task/dart.dart +++ b/pkg/analyzer/lib/src/task/dart.dart @@ -2678,7 +2678,7 @@ class InferStaticVariableTypeTask extends InferStaticVariableTask { "NodeLocator failed to find a variable's declaration"); } Expression initializer = declaration.initializer; - initializer.accept(new ResolutionEraser()); + ResolutionEraser.erase(initializer, eraseDeclarations: false); ResolutionContext resolutionContext = ResolutionContextBuilder.contextFor(initializer, errorListener); ResolverVisitor visitor = new ResolverVisitor( diff --git a/pkg/analyzer/lib/src/task/driver.dart b/pkg/analyzer/lib/src/task/driver.dart index e4210d50ec5..8daa5cfe08d 100644 --- a/pkg/analyzer/lib/src/task/driver.dart +++ b/pkg/analyzer/lib/src/task/driver.dart @@ -102,7 +102,12 @@ class AnalysisDriver { WorkOrder workOrder = createWorkOrderForResult(target, result); if (workOrder != null) { while (workOrder.moveNext()) { +// AnalysisTask previousTask = task; +// String message = workOrder.current.toString(); task = performWorkItem(workOrder.current); +// if (task == null) { +// throw new AnalysisException(message, previousTask.caughtException); +// } } } return task; diff --git a/pkg/analyzer/test/src/task/dart_test.dart b/pkg/analyzer/test/src/task/dart_test.dart index f14bbfb2e8b..514f126861d 100644 --- a/pkg/analyzer/test/src/task/dart_test.dart +++ b/pkg/analyzer/test/src/task/dart_test.dart @@ -1911,25 +1911,19 @@ class Z {} @reflectiveTest class InferStaticVariableTypesInUnitTaskTest extends _AbstractDartTaskTest { - void fail_perform_nestedDeclarations() { + void test_perform_nestedDeclarations() { enableStrongMode(); AnalysisTarget source = newSource( '/test.dart', ''' -var Y = (int x, int y) { +var f = (int x) { int squared(int value) => value * value; var xSquared = squared(x); - var ySquared = squared(y); - return Math.sqrt(xSquared + ySquared); + return xSquared; }; '''); computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6, matcher: isInferStaticVariableTypesInUnitTask); - CompilationUnit unit = outputs[RESOLVED_UNIT6]; - VariableDeclaration variableY = getTopLevelVariable(unit, 'Y'); - - InterfaceType intType = context.typeProvider.intType; - expect(variableY.initializer.staticType, intType); } void test_perform_recursive() { @@ -1970,6 +1964,30 @@ class M {} expect(variableC.element.type, typeM); expect(variableC.initializer.staticType, typeM); } + + void test_perform_simple() { + enableStrongMode(); + AnalysisTarget source = newSource( + '/test.dart', + ''' +var X = 1; + +var Y = () { + return 1 + X; +}; +'''); + computeResult(new LibrarySpecificUnit(source, source), RESOLVED_UNIT6, + matcher: isInferStaticVariableTypesInUnitTask); + CompilationUnit unit = outputs[RESOLVED_UNIT6]; + TopLevelVariableDeclaration declaration = unit.declarations[1]; + FunctionExpression function = + declaration.variables.variables[0].initializer; + BlockFunctionBody body = function.body; + ReturnStatement statement = body.block.statements[0]; + Expression expression = statement.expression; + InterfaceType intType = context.typeProvider.intType; + expect(expression.staticType, intType); + } } @reflectiveTest