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

Unified Diff: pkg/analyzer/lib/src/dart/element/builder.dart

Issue 1798223002: Fix visible range of local variables in 'for' loops. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/dart/element/builder.dart
diff --git a/pkg/analyzer/lib/src/dart/element/builder.dart b/pkg/analyzer/lib/src/dart/element/builder.dart
index 8dfc781ddc143e4e2de2524f1f63d315f495a47b..2754786b2d386b1ae0e686fbaf073bab180abad1 100644
--- a/pkg/analyzer/lib/src/dart/element/builder.dart
+++ b/pkg/analyzer/lib/src/dart/element/builder.dart
@@ -543,9 +543,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
_setCodeRange(element, node);
element.metadata = _createElementAnnotations(node.metadata);
ForEachStatement statement = node.parent as ForEachStatement;
- int declarationEnd = node.offset + node.length;
- int statementEnd = statement.offset + statement.length;
- element.setVisibleRange(declarationEnd, statementEnd - declarationEnd - 1);
+ element.setVisibleRange(statement.offset, statement.length);
element.const3 = node.isConst;
element.final2 = node.isFinal;
if (node.type == null) {
@@ -1201,10 +1199,7 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
}
element = variable;
_setCodeRange(element, node);
- Block enclosingBlock = node.getAncestor((node) => node is Block);
- // TODO(brianwilkerson) This isn't right for variables declared in a for
- // loop.
- variable.setVisibleRange(enclosingBlock.offset, enclosingBlock.length);
+ _setVariableVisibleRange(variable, node);
variable.hasImplicitType = varList.type == null;
_currentHolder.addLocalVariable(variable);
variableName.staticElement = element;
@@ -1401,6 +1396,18 @@ class ElementBuilder extends RecursiveAstVisitor<Object> {
}
}
+ void _setVariableVisibleRange(
+ LocalVariableElementImpl element, VariableDeclaration node) {
+ AstNode scopeNode;
+ AstNode parent2 = node.parent.parent;
+ if (parent2 is ForStatement) {
+ scopeNode = parent2;
+ } else {
+ scopeNode = node.getAncestor((node) => node is Block);
+ }
+ element.setVisibleRange(scopeNode.offset, scopeNode.length);
+ }
+
/**
* Make the given holder be the current holder while visiting the given node.
*

Powered by Google App Engine
This is Rietveld 408576698