Skip to content

Commit 6fcd15c

Browse files
stereotype441Commit Queue
authored andcommitted
Parser: fix error reporting when pattern assignment declares a variable without a name.
The analyzer/CFE shared error template framework requires name substitutions to be non-empty. However, sometimes, due to parser error recovery, an identifier token can be an empty string. One of the circumstances in which this occurs is when parsing text like the following: final b = (final g, final ) = 55; In this example, there is a parse error due to the fact that the user is trying to declare variables inside a pattern assignment. But one of those variables doesn't have a name yet, so the parser has created a synthetic token for it whose name is an empty string. To avoid a crash, we must supply some name to the error message template, so the name we supply is `(unnamed)`. Fixes #54178. Bug: #54178 Change-Id: Iabf3263ee1f56a89d1a69bcd74a17a296d470b9c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/339661 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent a25eb56 commit 6fcd15c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

pkg/_fe_analyzer_shared/lib/src/parser/parser_impl.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9771,8 +9771,8 @@ class Parser {
97719771
if (!isBareIdentifier) {
97729772
reportRecoverableError(
97739773
token,
9774-
codes.templatePatternAssignmentDeclaresVariable
9775-
.withArguments(variableName));
9774+
codes.templatePatternAssignmentDeclaresVariable.withArguments(
9775+
variableName.isEmpty ? '(unnamed)' : variableName));
97769776
}
97779777
break;
97789778
}

pkg/analyzer/test/generated/patterns_parser_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7524,6 +7524,21 @@ ForStatement
75247524
''');
75257525
}
75267526

7527+
test_patternAssignment_declaresVariableWithMissingName() {
7528+
// Test case from https://github.com/dart-lang/sdk/issues/54178.
7529+
_parse('''
7530+
void main() {
7531+
final b = (final g, final ) = 55;
7532+
}
7533+
''', errors: [
7534+
error(ParserErrorCode.PATTERN_ASSIGNMENT_DECLARES_VARIABLE, 33, 1),
7535+
error(ParserErrorCode.MISSING_IDENTIFIER, 42, 1),
7536+
error(ParserErrorCode.PATTERN_ASSIGNMENT_DECLARES_VARIABLE, 42, 1),
7537+
]);
7538+
// No assertion on the parsed node text; all we are concerned with is that
7539+
// the parser doesn't crash.
7540+
}
7541+
75277542
test_patternVariableDeclaration_inClass() {
75287543
// If a pattern variable declaration appears outside a function or method,
75297544
// the parser recovers by replacing the pattern with a synthetic identifier,

0 commit comments

Comments
 (0)