Skip to content

Commit 7739459

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Move switch expression resolution into ResolverVisitor.
The analyzer uses two different mechanisms to visit expressions during resolution: - `Expression.accept` (which defers to `ResolverVisitor.visit...` methods using the standard visitor infrastructure), and - The abstract method `ExpressionImpl.resolveExpression` (which is implemented in every expression type). The `Expression.accept` approach doesn't support contexts, so it is only used when the context is the unknown type schema (`_`). (The reason there are two approaches is largely a historical accident, and I hope one day to unify them into a single approach. But that's not the subject of this change.) Prior to this change, the resolution logic for most expression types was in the `visit...` method, and the corresponding `resolveExpression` method did nothing but call `visit...`. But there was one exception: for switch expressions, the resolution logic was in `resolveExpression`, and the corresponding `visit...` method called `analyzeExpression` (which then called `resolveExpression`). This change moves the resolution logic for switch expressions into the `visit...` method, and changes the corresponding `resolveExpression` method to call `visit...`. This makes switch expression resolution consistent with all other expression types, which should make the resolution logic easier to reason about. There should be no functional change. Change-Id: I209cdb5cd721608f60e6668a4be76fd20a7452de Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398585 Auto-Submit: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent abb17bc commit 7739459

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17111,17 +17111,7 @@ final class SwitchExpressionImpl extends ExpressionImpl
1711117111

1711217112
@override
1711317113
void resolveExpression(ResolverVisitor resolver, DartType contextType) {
17114-
inferenceLogWriter?.enterExpression(this, contextType);
17115-
var previousExhaustiveness = resolver.legacySwitchExhaustiveness;
17116-
var staticType = resolver
17117-
.analyzeSwitchExpression(
17118-
this, expression, cases.length, SharedTypeSchemaView(contextType))
17119-
.type
17120-
.unwrapTypeView();
17121-
recordStaticType(staticType, resolver: resolver);
17122-
resolver.popRewrite();
17123-
resolver.legacySwitchExhaustiveness = previousExhaustiveness;
17124-
inferenceLogWriter?.exitExpression(this);
17114+
resolver.visitSwitchExpression(this, contextType: contextType);
1712517115
}
1712617116

1712717117
@override

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3770,8 +3770,16 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
37703770
covariant SwitchExpressionImpl node, {
37713771
DartType contextType = UnknownInferredType.instance,
37723772
}) {
3773-
analyzeExpression(node, SharedTypeSchemaView(contextType));
3773+
inferenceLogWriter?.enterExpression(node, contextType);
3774+
var previousExhaustiveness = legacySwitchExhaustiveness;
3775+
var staticType = analyzeSwitchExpression(node, node.expression,
3776+
node.cases.length, SharedTypeSchemaView(contextType))
3777+
.type
3778+
.unwrapTypeView();
3779+
node.recordStaticType(staticType, resolver: this);
37743780
popRewrite();
3781+
legacySwitchExhaustiveness = previousExhaustiveness;
3782+
inferenceLogWriter?.exitExpression(node);
37753783
}
37763784

37773785
@override

0 commit comments

Comments
 (0)