Skip to content

Commit 8191171

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Use StatementImpl when interfacing with shared code
Change the analyzer's use of the following generic types so that it supplies the type parameter `StatementImpl` instead of `Statement` as the type of AST node it uses to represent statements: - `FlowAnalysis` - `TypeAnalyzerErrors` This required adjusting some `is` tests to test against concrete statement types instead of abstract public interface types (e.g. `is SwitchStatementImpl` instead of `is SwitchStatement`). In one place (in `FlowAnalysisHelper.getLabelTarget`) I changed an if-test to a switch statement so that I could use pattern matching to avoid type casts entirely. In some places, I was able to avoid adding casts by changing method parameters to accept concrete statement types instead of abstract public interface types. This required adding the `covariant` keyword to some methods. This is part of a larger arc of work to change the analyzer's use of the shared code so that the type parameters it supplies are not part of the analyzer public API. See #59763. Change-Id: I8910184ff71ccb97b52e5d44fbf0b6bddd6e273b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403180 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 1c4d7e1 commit 8191171

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class FlowAnalysisHelper {
9696
final bool inferenceUpdate4Enabled;
9797

9898
/// The current flow, when resolving a function body, or `null` otherwise.
99-
FlowAnalysis<AstNode, Statement, Expression, PromotableElementImpl2,
99+
FlowAnalysis<AstNode, StatementImpl, Expression, PromotableElementImpl2,
100100
SharedTypeView<DartType>>? flow;
101101

102102
FlowAnalysisHelper(bool retainDataForTesting, FeatureSet featureSet,
@@ -177,17 +177,17 @@ class FlowAnalysisHelper {
177177
as AssignedVariablesForTesting<AstNode, PromotableElementImpl2>;
178178
}
179179
flow = isNonNullableByDefault
180-
? FlowAnalysis<AstNode, Statement, Expression, PromotableElementImpl2,
181-
SharedTypeView<DartType>>(
180+
? FlowAnalysis<AstNode, StatementImpl, Expression,
181+
PromotableElementImpl2, SharedTypeView<DartType>>(
182182
typeOperations,
183183
assignedVariables!,
184184
respectImplicitlyTypedVarInitializers:
185185
respectImplicitlyTypedVarInitializers,
186186
fieldPromotionEnabled: fieldPromotionEnabled,
187187
inferenceUpdate4Enabled: inferenceUpdate4Enabled,
188188
)
189-
: FlowAnalysis<AstNode, Statement, Expression, PromotableElementImpl2,
190-
SharedTypeView<DartType>>.legacy(
189+
: FlowAnalysis<AstNode, StatementImpl, Expression,
190+
PromotableElementImpl2, SharedTypeView<DartType>>.legacy(
191191
typeOperations, assignedVariables!);
192192
}
193193

@@ -258,7 +258,7 @@ class FlowAnalysisHelper {
258258
}
259259

260260
void for_bodyBegin(AstNode node, Expression? condition) {
261-
flow?.for_bodyBegin(node is Statement ? node : null, condition);
261+
flow?.for_bodyBegin(node is StatementImpl ? node : null, condition);
262262
}
263263

264264
void for_conditionBegin(AstNode node) {
@@ -309,7 +309,7 @@ class FlowAnalysisHelper {
309309
);
310310
}
311311

312-
void labeledStatement_enter(LabeledStatement node) {
312+
void labeledStatement_enter(LabeledStatementImpl node) {
313313
if (flow == null) return;
314314

315315
flow!.labeledStatement_begin(node);
@@ -372,18 +372,22 @@ class FlowAnalysisHelper {
372372
/// not specify a label), so the default enclosing target is returned.
373373
///
374374
/// [isBreak] is `true` for `break`, and `false` for `continue`.
375-
static Statement? getLabelTarget(AstNode? node, Element? element,
375+
static StatementImpl? getLabelTarget(AstNode? node, Element? element,
376376
{required bool isBreak}) {
377377
for (; node != null; node = node.parent) {
378378
if (element == null) {
379-
if (node is DoStatement ||
380-
node is ForStatement ||
381-
(isBreak && node is SwitchStatement) ||
382-
node is WhileStatement) {
383-
return node as Statement;
379+
switch (node) {
380+
case DoStatementImpl():
381+
return node;
382+
case ForStatementImpl():
383+
return node;
384+
case SwitchStatementImpl() when isBreak:
385+
return node;
386+
case WhileStatementImpl():
387+
return node;
384388
}
385389
} else {
386-
if (node is LabeledStatement) {
390+
if (node is LabeledStatementImpl) {
387391
if (_hasLabel(node.labels, element)) {
388392
var statement = node.statement;
389393
// The inner statement is returned for labeled loops and
@@ -399,7 +403,7 @@ class FlowAnalysisHelper {
399403
return statement;
400404
}
401405
}
402-
if (node is SwitchStatement) {
406+
if (node is SwitchStatementImpl) {
403407
for (var member in node.members) {
404408
if (_hasLabel(member.labels, element)) {
405409
return node;

pkg/analyzer/lib/src/dart/resolver/shared_type_analyzer.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SharedTypeAnalyzerErrors
2323
implements
2424
shared.TypeAnalyzerErrors<
2525
AstNode,
26-
Statement,
26+
StatementImpl,
2727
Expression,
2828
PromotableElementImpl2,
2929
SharedTypeView<DartType>,
@@ -238,7 +238,7 @@ class SharedTypeAnalyzerErrors
238238

239239
@override
240240
void switchCaseCompletesNormally(
241-
{required covariant SwitchStatement node, required int caseIndex}) {
241+
{required covariant SwitchStatementImpl node, required int caseIndex}) {
242242
_errorReporter.atToken(
243243
node.members[caseIndex].keyword,
244244
CompileTimeErrorCode.SWITCH_CASE_COMPLETES_NORMALLY,

pkg/analyzer/lib/src/generated/resolver.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
123123
TypeAnalyzer<
124124
DartType,
125125
AstNode,
126-
Statement,
126+
StatementImpl,
127127
Expression,
128128
PromotableElementImpl2,
129129
DartPattern,
@@ -437,7 +437,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
437437
ExecutableElement? get enclosingFunction => _enclosingFunction;
438438

439439
@override
440-
FlowAnalysis<AstNode, Statement, Expression, PromotableElementImpl2,
440+
FlowAnalysis<AstNode, StatementImpl, Expression, PromotableElementImpl2,
441441
SharedTypeView<DartType>> get flow => flowAnalysis.flow!;
442442

443443
bool get isConstructorTearoffsEnabled =>
@@ -995,7 +995,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
995995
}
996996

997997
@override
998-
SwitchStatementMemberInfo<AstNode, Statement, Expression,
998+
SwitchStatementMemberInfo<AstNode, StatementImpl, Expression,
999999
PromotableElementImpl2> getSwitchStatementMemberInfo(
10001000
covariant SwitchStatementImpl node,
10011001
int index,
@@ -2452,7 +2452,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
24522452
}
24532453

24542454
@override
2455-
void visitDoStatement(DoStatement node) {
2455+
void visitDoStatement(covariant DoStatementImpl node) {
24562456
inferenceLogWriter?.enterStatement(node);
24572457
checkUnreachableNode(node);
24582458

@@ -3124,7 +3124,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
31243124
void visitLabel(Label node) {}
31253125

31263126
@override
3127-
void visitLabeledStatement(LabeledStatement node) {
3127+
void visitLabeledStatement(covariant LabeledStatementImpl node) {
31283128
inferenceLogWriter?.enterStatement(node);
31293129
flowAnalysis.labeledStatement_enter(node);
31303130
checkUnreachableNode(node);
@@ -3922,7 +3922,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
39223922
}
39233923

39243924
@override
3925-
void visitWhileStatement(WhileStatement node) {
3925+
void visitWhileStatement(covariant WhileStatementImpl node) {
39263926
inferenceLogWriter?.enterStatement(node);
39273927
checkUnreachableNode(node);
39283928

0 commit comments

Comments
 (0)