Skip to content

Commit

Permalink
Issue 56355. Fix token cycle via 'previous'.
Browse files Browse the repository at this point in the history
Bug: #56355
Change-Id: I1ee7f837dc8beed9bb2ed1f27c45108345e7cceb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392460
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Oct 29, 2024
1 parent 97dafd7 commit 84e15e9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 49 deletions.
11 changes: 9 additions & 2 deletions pkg/analyzer/lib/src/fasta/ast_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3636,17 +3636,24 @@ class AstBuilder extends StackListener {
var awaitToken = variable.name;
if (awaitToken.type == Keyword.AWAIT ||
awaitToken.type == TokenType.IDENTIFIER) {
// We see `x.foo await;`, where `;` is synthetic.
// It is followed by `y.bar()`.
// Insert a new `;`, and (unfortunately) drop `await;`.
type.name2.setNext(semicolon.next!);
var semicolon2 = parser.rewriter.insertSyntheticToken(
type.name2,
TokenType.SEMICOLON,
);
push(
ExpressionStatementImpl(
expression: PrefixedIdentifierImpl(
prefix: SimpleIdentifierImpl(importPrefix.name),
period: importPrefix.period,
identifier: SimpleIdentifierImpl(type.name2),
),
semicolon: semicolon,
semicolon: semicolon2,
),
);
parser.rewriter.insertToken(semicolon, awaitToken);
return;
}
}
Expand Down
28 changes: 8 additions & 20 deletions pkg/analyzer/test/src/dart/parser/top_level_function_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ FunctionDeclaration
// https://github.com/dart-lang/sdk/issues/56355
var parseResult = parseStringWithErrors(r'''
void get() {
http.Response response = http
http.Response response = http2
}
''');

Expand Down Expand Up @@ -109,25 +109,13 @@ FunctionDeclaration
identifier: SimpleIdentifier
token: T7 Response @20
previous: T6 |.|
next: T8 |response|
semicolon: T9 ; @45 <synthetic>
previousX: T10 http @40
previousX: T11 = @38
previous: T8 |response|
next: T10 |http|
next: T9 |;|
next: T8 |response|
ExpressionStatement
expression: SimpleIdentifier
token: T8 response @29
previous: T9 |;|
next: T12 |;|
semicolon: T12 ; @45 <synthetic>
previous: T8 |response|
next: T13 |}|
rightBracket: T13 } @45
previous: T12 |;|
next: T14 ||
next: T8 |;|
semicolon: T8 ; @46 <synthetic>
previous: T7 |Response|
next: T9 |}|
rightBracket: T9 } @46
previous: T8 |;|
next: T10 ||
''',
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ main() {
class VariableDeclarationStatementParserTest extends ParserDiagnosticsTest {
test_recovery_propertyAccess_beforeAwait_hasIdentifier() {
var parseResult = parseStringWithErrors(r'''
void f(x) async {
void f() async {
x.foo
await x.bar();
await y.bar();
}
''');
parseResult.assertErrors([
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 28, 5),
error(ParserErrorCode.EXPECTED_TOKEN, 28, 5),
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 27, 5),
error(ParserErrorCode.EXPECTED_TOKEN, 27, 5),
]);

var node = parseResult.findNode.singleBlock;
Expand All @@ -41,31 +41,29 @@ Block
token: foo
semicolon: ; <synthetic>
ExpressionStatement
expression: AwaitExpression
awaitKeyword: await
expression: MethodInvocation
target: SimpleIdentifier
token: x
operator: .
methodName: SimpleIdentifier
token: bar
argumentList: ArgumentList
leftParenthesis: (
rightParenthesis: )
expression: MethodInvocation
target: SimpleIdentifier
token: y
operator: .
methodName: SimpleIdentifier
token: bar
argumentList: ArgumentList
leftParenthesis: (
rightParenthesis: )
semicolon: ;
rightBracket: }
''');
}

test_recovery_propertyAccess_beforeAwait_noIdentifier() {
var parseResult = parseStringWithErrors(r'''
void f(x) async {
void f() async {
x.
await x.foo();
await y.foo();
}
''');
parseResult.assertErrors([
error(ParserErrorCode.EXPECTED_TOKEN, 31, 1),
error(ParserErrorCode.EXPECTED_TOKEN, 30, 1),
]);

var node = parseResult.findNode.singleBlock;
Expand All @@ -86,7 +84,7 @@ Block
awaitKeyword: await
expression: MethodInvocation
target: SimpleIdentifier
token: x
token: y
operator: .
methodName: SimpleIdentifier
token: foo
Expand All @@ -100,13 +98,13 @@ Block

test_recovery_propertyAccess_beforeIdentifier_hasIdentifier() {
var parseResult = parseStringWithErrors(r'''
void f(x) {
void f() {
x.foo
bar();
}
''');
parseResult.assertErrors([
error(ParserErrorCode.EXPECTED_TOKEN, 22, 3),
error(ParserErrorCode.EXPECTED_TOKEN, 21, 3),
]);

var node = parseResult.findNode.singleBlock;
Expand All @@ -123,12 +121,9 @@ Block
token: foo
semicolon: ; <synthetic>
ExpressionStatement
expression: MethodInvocation
methodName: SimpleIdentifier
token: bar
argumentList: ArgumentList
leftParenthesis: (
rightParenthesis: )
expression: RecordLiteral
leftParenthesis: (
rightParenthesis: )
semicolon: ;
rightBracket: }
''');
Expand Down

0 comments on commit 84e15e9

Please sign in to comment.