Skip to content

Commit edd14f3

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
[analyzer] Introduce a new ForLoop class.
This class is part of the public API, and contains the functionality that's common to ForElement and ForStatement. Adding this class allows us to change the type of `ForLoopParts.parent` from `AstNode?` to `ForLoop`. I intend to make use of this in a follow-up CL that makes improvements to the "remove dead code" fix, as part of my work on sound flow analysis. Change-Id: I93ca15d2eeea495064b70690584b7a91058510f7 Bug: #60438 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/423560 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 669bcef commit edd14f3

File tree

5 files changed

+55
-60
lines changed

5 files changed

+55
-60
lines changed

pkg/analysis_server/lib/src/services/completion/dart/in_scope_completion_pass.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
13811381
@override
13821382
void visitForPartsWithExpression(ForPartsWithExpression node) {
13831383
if (node.isFullySynthetic) {
1384-
node.parent?.accept(this);
1384+
node.parent.accept(this);
13851385
}
13861386
}
13871387

pkg/analysis_server/lib/src/services/refactoring/legacy/visible_ranges_computer.dart

+3-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ class VisibleRangesComputer extends GeneralizingAstVisitor<void> {
3535
@override
3636
void visitForPartsWithDeclarations(ForPartsWithDeclarations node) {
3737
var loop = node.parent;
38-
if (loop != null) {
39-
for (var variable in node.variables.variables) {
40-
_addLocalVariable(loop, variable.declaredElement2);
41-
variable.initializer?.accept(this);
42-
}
38+
for (var variable in node.variables.variables) {
39+
_addLocalVariable(loop, variable.declaredElement2);
40+
variable.initializer?.accept(this);
4341
}
4442
}
4543

pkg/analyzer/api.txt

+6-10
Original file line numberDiff line numberDiff line change
@@ -842,14 +842,16 @@ package:analyzer/dart/ast/ast.dart:
842842
keyword (getter: Token)
843843
metadata (getter: NodeList<Annotation>)
844844
pattern (getter: DartPattern)
845-
ForElement (class extends Object implements CollectionElement):
845+
ForElement (class extends Object implements CollectionElement, ForLoop<CollectionElement>)
846+
ForLoop (class<Body extends AstNode> extends Object implements AstNode, sealed (immediate subtypes: ForElement, ForLoopImpl, ForStatement)):
846847
awaitKeyword (getter: Token?)
847-
body (getter: CollectionElement)
848+
body (getter: Body)
848849
forKeyword (getter: Token)
849850
forLoopParts (getter: ForLoopParts)
850851
leftParenthesis (getter: Token)
851852
rightParenthesis (getter: Token)
852-
ForLoopParts (class extends Object implements AstNode, sealed (immediate subtypes: ForEachParts, ForLoopPartsImpl, ForParts))
853+
ForLoopParts (class extends Object implements AstNode, sealed (immediate subtypes: ForEachParts, ForLoopPartsImpl, ForParts)):
854+
parent (getter: ForLoop<AstNode>)
853855
ForParts (class extends Object implements ForLoopParts, sealed (immediate subtypes: ForPartsImpl, ForPartsWithDeclarations, ForPartsWithExpression, ForPartsWithPattern)):
854856
condition (getter: Expression?)
855857
leftSeparator (getter: Token)
@@ -861,13 +863,7 @@ package:analyzer/dart/ast/ast.dart:
861863
initialization (getter: Expression?)
862864
ForPartsWithPattern (class extends Object implements ForParts):
863865
variables (getter: PatternVariableDeclaration)
864-
ForStatement (class extends Object implements Statement):
865-
awaitKeyword (getter: Token?)
866-
body (getter: Statement)
867-
forKeyword (getter: Token)
868-
forLoopParts (getter: ForLoopParts)
869-
leftParenthesis (getter: Token)
870-
rightParenthesis (getter: Token)
866+
ForStatement (class extends Object implements Statement, ForLoop<Statement>)
871867
FormalParameter (class extends Object implements AstNode, sealed (immediate subtypes: DefaultFormalParameter, FormalParameterImpl, NormalFormalParameter)):
872868
covariantKeyword (getter: Token?)
873869
declaredFragment (getter: FormalParameterFragment?, experimental)

pkg/analyzer/lib/dart/ast/ast.dart

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export 'package:analyzer/src/dart/ast/ast.dart'
118118
ForEachPartsWithIdentifier,
119119
ForEachPartsWithPattern,
120120
ForElement,
121+
ForLoop,
121122
ForLoopParts,
122123
FormalParameter,
123124
FormalParameterList,

pkg/analyzer/lib/src/dart/ast/ast.dart

+44-44
Original file line numberDiff line numberDiff line change
@@ -7481,30 +7481,14 @@ final class ForEachPartsWithPatternImpl extends ForEachPartsImpl
74817481

74827482
/// The basic structure of a for element.
74837483
@AnalyzerPublicApi(message: 'exported by lib/dart/ast/ast.dart')
7484-
abstract final class ForElement implements CollectionElement {
7485-
/// The token representing the `await` keyword, or `null` if there was no
7486-
/// `await` keyword.
7487-
Token? get awaitKeyword;
7488-
7489-
/// The body of the loop.
7490-
CollectionElement get body;
7491-
7492-
/// The token representing the `for` keyword.
7493-
Token get forKeyword;
7494-
7495-
/// The parts of the for element that control the iteration.
7496-
ForLoopParts get forLoopParts;
7497-
7498-
/// The left parenthesis.
7499-
Token get leftParenthesis;
7500-
7501-
/// The right parenthesis.
7502-
Token get rightParenthesis;
7503-
}
7484+
abstract final class ForElement
7485+
implements CollectionElement, ForLoop<CollectionElement> {}
75047486

75057487
final class ForElementImpl extends CollectionElementImpl
75067488
with AstNodeWithNameScopeMixin
7507-
implements ForElement {
7489+
implements
7490+
ForElement,
7491+
ForLoopImpl<CollectionElement, CollectionElementImpl> {
75087492
@override
75097493
final Token? awaitKeyword;
75107494

@@ -7584,6 +7568,35 @@ final class ForElementImpl extends CollectionElementImpl
75847568
}
75857569
}
75867570

7571+
/// A for or for-each statement or collection element.
7572+
@AnalyzerPublicApi(message: 'exported by lib/dart/ast/ast.dart')
7573+
sealed class ForLoop<Body extends AstNode> implements AstNode {
7574+
/// The token representing the `await` keyword, or `null` if there's no
7575+
/// `await` keyword.
7576+
Token? get awaitKeyword;
7577+
7578+
/// The body of the loop.
7579+
Body get body;
7580+
7581+
/// The token representing the `for` keyword.
7582+
Token get forKeyword;
7583+
7584+
/// The parts of the for element that control the iteration.
7585+
ForLoopParts get forLoopParts;
7586+
7587+
/// The left parenthesis.
7588+
Token get leftParenthesis;
7589+
7590+
/// The right parenthesis.
7591+
Token get rightParenthesis;
7592+
}
7593+
7594+
sealed class ForLoopImpl<Body extends AstNode, BodyImpl extends Body>
7595+
implements AstNodeImpl, ForLoop<Body> {
7596+
@override
7597+
BodyImpl get body;
7598+
}
7599+
75877600
/// The parts of a for or for-each loop that control the iteration.
75887601
///
75897602
/// forLoopParts ::=
@@ -7595,9 +7608,15 @@ final class ForElementImpl extends CollectionElementImpl
75957608
/// expressionList ::=
75967609
/// [Expression] (',' [Expression])*
75977610
@AnalyzerPublicApi(message: 'exported by lib/dart/ast/ast.dart')
7598-
sealed class ForLoopParts implements AstNode {}
7611+
sealed class ForLoopParts implements AstNode {
7612+
@override
7613+
ForLoop get parent;
7614+
}
75997615

7600-
sealed class ForLoopPartsImpl extends AstNodeImpl implements ForLoopParts {}
7616+
sealed class ForLoopPartsImpl extends AstNodeImpl implements ForLoopParts {
7617+
@override
7618+
ForLoopImpl get parent => super.parent as ForLoopImpl;
7619+
}
76017620

76027621
/// A node representing a parameter to a function.
76037622
///
@@ -8094,30 +8113,11 @@ final class ForPartsWithPatternImpl extends ForPartsImpl
80948113
/// | [DeclaredIdentifier] 'in' [Expression]
80958114
/// | [SimpleIdentifier] 'in' [Expression]
80968115
@AnalyzerPublicApi(message: 'exported by lib/dart/ast/ast.dart')
8097-
abstract final class ForStatement implements Statement {
8098-
/// The token representing the `await` keyword, or `null` if there's no
8099-
/// `await` keyword.
8100-
Token? get awaitKeyword;
8101-
8102-
/// The body of the loop.
8103-
Statement get body;
8104-
8105-
/// The token representing the `for` keyword.
8106-
Token get forKeyword;
8107-
8108-
/// The parts of the for element that control the iteration.
8109-
ForLoopParts get forLoopParts;
8110-
8111-
/// The left parenthesis.
8112-
Token get leftParenthesis;
8113-
8114-
/// The right parenthesis.
8115-
Token get rightParenthesis;
8116-
}
8116+
abstract final class ForStatement implements Statement, ForLoop<Statement> {}
81178117

81188118
final class ForStatementImpl extends StatementImpl
81198119
with AstNodeWithNameScopeMixin
8120-
implements ForStatement {
8120+
implements ForStatement, ForLoopImpl<Statement, StatementImpl> {
81218121
@override
81228122
final Token? awaitKeyword;
81238123

0 commit comments

Comments
 (0)