Skip to content

Commit 84e15e9

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Issue 56355. Fix token cycle via 'previous'.
Bug: #56355 Change-Id: I1ee7f837dc8beed9bb2ed1f27c45108345e7cceb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392460 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 97dafd7 commit 84e15e9

File tree

3 files changed

+39
-49
lines changed

3 files changed

+39
-49
lines changed

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3636,17 +3636,24 @@ class AstBuilder extends StackListener {
36363636
var awaitToken = variable.name;
36373637
if (awaitToken.type == Keyword.AWAIT ||
36383638
awaitToken.type == TokenType.IDENTIFIER) {
3639+
// We see `x.foo await;`, where `;` is synthetic.
3640+
// It is followed by `y.bar()`.
3641+
// Insert a new `;`, and (unfortunately) drop `await;`.
3642+
type.name2.setNext(semicolon.next!);
3643+
var semicolon2 = parser.rewriter.insertSyntheticToken(
3644+
type.name2,
3645+
TokenType.SEMICOLON,
3646+
);
36393647
push(
36403648
ExpressionStatementImpl(
36413649
expression: PrefixedIdentifierImpl(
36423650
prefix: SimpleIdentifierImpl(importPrefix.name),
36433651
period: importPrefix.period,
36443652
identifier: SimpleIdentifierImpl(type.name2),
36453653
),
3646-
semicolon: semicolon,
3654+
semicolon: semicolon2,
36473655
),
36483656
);
3649-
parser.rewriter.insertToken(semicolon, awaitToken);
36503657
return;
36513658
}
36523659
}

pkg/analyzer/test/src/dart/parser/top_level_function_test.dart

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ FunctionDeclaration
6565
// https://github.com/dart-lang/sdk/issues/56355
6666
var parseResult = parseStringWithErrors(r'''
6767
void get() {
68-
http.Response response = http
68+
http.Response response = http2
6969
}
7070
''');
7171

@@ -109,25 +109,13 @@ FunctionDeclaration
109109
identifier: SimpleIdentifier
110110
token: T7 Response @20
111111
previous: T6 |.|
112-
next: T8 |response|
113-
semicolon: T9 ; @45 <synthetic>
114-
previousX: T10 http @40
115-
previousX: T11 = @38
116-
previous: T8 |response|
117-
next: T10 |http|
118-
next: T9 |;|
119-
next: T8 |response|
120-
ExpressionStatement
121-
expression: SimpleIdentifier
122-
token: T8 response @29
123-
previous: T9 |;|
124-
next: T12 |;|
125-
semicolon: T12 ; @45 <synthetic>
126-
previous: T8 |response|
127-
next: T13 |}|
128-
rightBracket: T13 } @45
129-
previous: T12 |;|
130-
next: T14 ||
112+
next: T8 |;|
113+
semicolon: T8 ; @46 <synthetic>
114+
previous: T7 |Response|
115+
next: T9 |}|
116+
rightBracket: T9 } @46
117+
previous: T8 |;|
118+
next: T10 ||
131119
''',
132120
);
133121
}

pkg/analyzer/test/src/dart/parser/variable_declaration_statement_test.dart

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ main() {
1717
class VariableDeclarationStatementParserTest extends ParserDiagnosticsTest {
1818
test_recovery_propertyAccess_beforeAwait_hasIdentifier() {
1919
var parseResult = parseStringWithErrors(r'''
20-
void f(x) async {
20+
void f() async {
2121
x.foo
22-
await x.bar();
22+
await y.bar();
2323
}
2424
''');
2525
parseResult.assertErrors([
26-
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 28, 5),
27-
error(ParserErrorCode.EXPECTED_TOKEN, 28, 5),
26+
error(ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER, 27, 5),
27+
error(ParserErrorCode.EXPECTED_TOKEN, 27, 5),
2828
]);
2929

3030
var node = parseResult.findNode.singleBlock;
@@ -41,31 +41,29 @@ Block
4141
token: foo
4242
semicolon: ; <synthetic>
4343
ExpressionStatement
44-
expression: AwaitExpression
45-
awaitKeyword: await
46-
expression: MethodInvocation
47-
target: SimpleIdentifier
48-
token: x
49-
operator: .
50-
methodName: SimpleIdentifier
51-
token: bar
52-
argumentList: ArgumentList
53-
leftParenthesis: (
54-
rightParenthesis: )
44+
expression: MethodInvocation
45+
target: SimpleIdentifier
46+
token: y
47+
operator: .
48+
methodName: SimpleIdentifier
49+
token: bar
50+
argumentList: ArgumentList
51+
leftParenthesis: (
52+
rightParenthesis: )
5553
semicolon: ;
5654
rightBracket: }
5755
''');
5856
}
5957

6058
test_recovery_propertyAccess_beforeAwait_noIdentifier() {
6159
var parseResult = parseStringWithErrors(r'''
62-
void f(x) async {
60+
void f() async {
6361
x.
64-
await x.foo();
62+
await y.foo();
6563
}
6664
''');
6765
parseResult.assertErrors([
68-
error(ParserErrorCode.EXPECTED_TOKEN, 31, 1),
66+
error(ParserErrorCode.EXPECTED_TOKEN, 30, 1),
6967
]);
7068

7169
var node = parseResult.findNode.singleBlock;
@@ -86,7 +84,7 @@ Block
8684
awaitKeyword: await
8785
expression: MethodInvocation
8886
target: SimpleIdentifier
89-
token: x
87+
token: y
9088
operator: .
9189
methodName: SimpleIdentifier
9290
token: foo
@@ -100,13 +98,13 @@ Block
10098

10199
test_recovery_propertyAccess_beforeIdentifier_hasIdentifier() {
102100
var parseResult = parseStringWithErrors(r'''
103-
void f(x) {
101+
void f() {
104102
x.foo
105103
bar();
106104
}
107105
''');
108106
parseResult.assertErrors([
109-
error(ParserErrorCode.EXPECTED_TOKEN, 22, 3),
107+
error(ParserErrorCode.EXPECTED_TOKEN, 21, 3),
110108
]);
111109

112110
var node = parseResult.findNode.singleBlock;
@@ -123,12 +121,9 @@ Block
123121
token: foo
124122
semicolon: ; <synthetic>
125123
ExpressionStatement
126-
expression: MethodInvocation
127-
methodName: SimpleIdentifier
128-
token: bar
129-
argumentList: ArgumentList
130-
leftParenthesis: (
131-
rightParenthesis: )
124+
expression: RecordLiteral
125+
leftParenthesis: (
126+
rightParenthesis: )
132127
semicolon: ;
133128
rightBracket: }
134129
''');

0 commit comments

Comments
 (0)