Skip to content

Commit 5dd89ef

Browse files
danrubelcommit-bot@chromium.org
authored andcommitted
fix invalid var init parser recovery
Fix Dart-Code/Dart-Code#1548 Change-Id: Ib593c17fa8dfcee3c8ad139b72ceff56497d43a1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/99093 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Dan Rubel <danrubel@google.com>
1 parent b9cbb67 commit 5dd89ef

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

pkg/analyzer/test/generated/parser_test.dart

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3933,13 +3933,26 @@ class Wrong<T> {
39333933
}
39343934

39353935
void test_invalidInterpolation_missingClosingBrace_issue35900() {
3936-
parseCompilationUnit(r"main () { print('${x' '); }", errors: [
3937-
expectedError(ScannerErrorCode.EXPECTED_TOKEN, 23, 1),
3938-
expectedError(ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 26, 1),
3939-
expectedError(ParserErrorCode.EXPECTED_TOKEN, 20, 3),
3940-
expectedError(ParserErrorCode.EXPECTED_STRING_LITERAL, 23, 1),
3941-
expectedError(ParserErrorCode.EXPECTED_EXECUTABLE, 27, 0),
3942-
]);
3936+
parseCompilationUnit(r"main () { print('${x' '); }",
3937+
errors: usingFastaParser
3938+
? [
3939+
expectedError(ScannerErrorCode.EXPECTED_TOKEN, 23, 1),
3940+
expectedError(
3941+
ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 26, 1),
3942+
expectedError(ParserErrorCode.EXPECTED_TOKEN, 20, 3),
3943+
expectedError(ParserErrorCode.EXPECTED_STRING_LITERAL, 23, 1),
3944+
expectedError(ParserErrorCode.EXPECTED_EXECUTABLE, 27, 0),
3945+
]
3946+
: [
3947+
expectedError(ScannerErrorCode.EXPECTED_TOKEN, 23, 1),
3948+
expectedError(
3949+
ScannerErrorCode.UNTERMINATED_STRING_LITERAL, 26, 1),
3950+
expectedError(ParserErrorCode.EXPECTED_TOKEN, 20, 3),
3951+
expectedError(ParserErrorCode.EXPECTED_TOKEN, 23, 1),
3952+
expectedError(ParserErrorCode.EXPECTED_TOKEN, 23, 1),
3953+
expectedError(ParserErrorCode.EXPECTED_EXECUTABLE, 23, 1),
3954+
expectedError(ParserErrorCode.UNEXPECTED_TOKEN, 23, 1),
3955+
]);
39433956
}
39443957

39453958
void test_invalidInterpolationIdentifier_startWithDigit() {
@@ -5364,6 +5377,20 @@ main() {
53645377
errors: [expectedError(ParserErrorCode.TYPEDEF_IN_CLASS, 10, 7)]);
53655378
}
53665379

5380+
void test_unexpectedCommaThenInterpolation() {
5381+
// https://github.com/Dart-Code/Dart-Code/issues/1548
5382+
parseCompilationUnit(r"main() { String s = 'a' 'b', 'c$foo'; return s; }",
5383+
errors: usingFastaParser
5384+
? [
5385+
expectedError(ParserErrorCode.MISSING_IDENTIFIER, 29, 2),
5386+
expectedError(ParserErrorCode.EXPECTED_TOKEN, 29, 2),
5387+
]
5388+
: [
5389+
expectedError(ParserErrorCode.MISSING_IDENTIFIER, 29, 2),
5390+
expectedError(ParserErrorCode.EXPECTED_TOKEN, 29, 1),
5391+
]);
5392+
}
5393+
53675394
void test_unexpectedTerminatorForParameterGroup_named() {
53685395
createParser('(a, b})');
53695396
FormalParameterList list = parser.parseFormalParameterList();

pkg/front_end/lib/src/fasta/parser/identifier_context_impl.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import '../../scanner/token.dart' show Token;
66

77
import '../fasta_codes.dart' as fasta;
88

9-
import '../scanner/token_constants.dart' show IDENTIFIER_TOKEN;
9+
import '../scanner/token_constants.dart' show IDENTIFIER_TOKEN, STRING_TOKEN;
1010

1111
import 'identifier_context.dart';
1212

@@ -624,7 +624,8 @@ class LocalVariableDeclarationIdentifierContext extends IdentifierContext {
624624

625625
// Recovery
626626
if (isOneOfOrEof(identifier, const [';', '=', ',', '{', '}']) ||
627-
looksLikeStatementStart(identifier)) {
627+
looksLikeStatementStart(identifier) ||
628+
identifier.kind == STRING_TOKEN) {
628629
identifier = parser.insertSyntheticIdentifier(token, this,
629630
message: fasta.templateExpectedIdentifier.withArguments(identifier));
630631
} else {

0 commit comments

Comments
 (0)