Skip to content

Commit

Permalink
Fix parsing switch statement with SwitchPatternCaseImpl when the patt…
Browse files Browse the repository at this point in the history
…erns feature is disabled.

Bug: #50553
Change-Id: Icd87d422ebf899d4a0d56ab4b7e476ab60949ad4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/272387
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Nov 29, 2022
1 parent 0b71247 commit 2b5608d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pkg/analyzer/lib/src/fasta/ast_builder.dart
Expand Up @@ -975,12 +975,16 @@ class AstBuilder extends StackListener {
assert(optional(':', colon));
debugEvent("CaseMatch");

WhenClauseImpl? whenClause;
if (when != null) {
var expression = pop() as ExpressionImpl;
whenClause = WhenClauseImpl(
whenKeyword: when,
expression: expression,
);
}

if (_featureSet.isEnabled(Feature.patterns)) {
WhenClauseImpl? whenClause;
if (when != null) {
var expression = pop() as ExpressionImpl;
whenClause = WhenClauseImpl(whenKeyword: when, expression: expression);
}
var pattern = pop() as DartPatternImpl;
push(
SwitchPatternCaseImpl(
Expand Down
26 changes: 26 additions & 0 deletions pkg/analyzer/test/src/fasta/ast_builder_test.dart
Expand Up @@ -1333,6 +1333,32 @@ SuperFormalParameter
superKeyword: super
period: .
name: a
''');
}

void test_switchStatement_withPatternCase_whenDisabled() {
var parseResult = parseStringWithErrors(r'''
// @dart = 2.18
void f(Object value) {
switch (value) {
case (int a,) when a == 0:
}
}
''');
parseResult.assertErrors([
error(ParserErrorCode.EXPECTED_TOKEN, 72, 1),
]);

var node = parseResult.findNode.switchCase('case');
assertParsedNodeText(node, r'''
SwitchCase
keyword: case
expression: ParenthesizedExpression
leftParenthesis: (
expression: SimpleIdentifier
token: int
rightParenthesis: )
colon: :
''');
}
}

0 comments on commit 2b5608d

Please sign in to comment.