Skip to content

Commit 9ab7a5e

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[cfe] Use variable name in error message for missing initializer
Closes #42610 Change-Id: I70a5ac9302bd6545cd5e73f2dab61a29bc6a3000 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153462 Reviewed-by: Jens Johansen <jensj@google.com> Commit-Queue: Johnni Winther <johnniwinther@google.com>
1 parent 2b5eaff commit 9ab7a5e

File tree

52 files changed

+201
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+201
-98
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2697,7 +2697,7 @@ class Parser {
26972697
token = parseExpression(assignment);
26982698
listener.endVariableInitializer(assignment);
26992699
} else {
2700-
listener.handleNoVariableInitializer(token.next);
2700+
listener.handleNoVariableInitializer(token);
27012701
}
27022702
return token;
27032703
}

pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,22 +2394,25 @@ class BodyBuilder extends ScopeListener<JumpTarget>
23942394
bool isFinal = (currentLocalVariableModifiers & finalMask) != 0;
23952395
bool isLate = (currentLocalVariableModifiers & lateMask) != 0;
23962396
Expression initializer;
2397-
if (!optional("in", token)) {
2397+
if (!optional("in", token.next)) {
23982398
// A for-in loop-variable can't have an initializer. So let's remain
23992399
// silent if the next token is `in`. Since a for-in loop can only have
24002400
// one variable it must be followed by `in`.
2401-
if (isConst) {
2402-
initializer = buildProblem(
2403-
fasta.templateConstFieldWithoutInitializer
2404-
.withArguments(token.lexeme),
2405-
token.charOffset,
2406-
token.length);
2407-
} else if (isFinal && !isLate) {
2408-
initializer = buildProblem(
2409-
fasta.templateFinalFieldWithoutInitializer
2410-
.withArguments(token.lexeme),
2411-
token.charOffset,
2412-
token.length);
2401+
if (!token.isSynthetic) {
2402+
// If [token] is synthetic it is created from error recovery.
2403+
if (isConst) {
2404+
initializer = buildProblem(
2405+
fasta.templateConstFieldWithoutInitializer
2406+
.withArguments(token.lexeme),
2407+
token.charOffset,
2408+
token.length);
2409+
} else if (isFinal && !isLate) {
2410+
initializer = buildProblem(
2411+
fasta.templateFinalFieldWithoutInitializer
2412+
.withArguments(token.lexeme),
2413+
token.charOffset,
2414+
token.length);
2415+
}
24132416
}
24142417
}
24152418
pushNewLocalVariable(initializer);

pkg/front_end/parser_testcases/error_recovery/issue_39060.dart.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ beginCompilationUnit(main)
3737
beginVariablesDeclaration(A, null, null)
3838
handleIdentifier(A, localVariableDeclaration)
3939
beginInitializedIdentifier(A)
40-
handleNoVariableInitializer(<)
40+
handleNoVariableInitializer(A)
4141
endInitializedIdentifier(A)
4242
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], A, A)
4343
endVariablesDeclaration(1, ;)

pkg/front_end/parser_testcases/error_recovery/issue_39060.dart.intertwined.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ parseUnit(main)
4949
listener: handleIdentifier(A, localVariableDeclaration)
5050
listener: beginInitializedIdentifier(A)
5151
parseVariableInitializerOpt(A)
52-
listener: handleNoVariableInitializer(<)
52+
listener: handleNoVariableInitializer(A)
5353
listener: endInitializedIdentifier(A)
5454
ensureSemicolon(A)
5555
reportRecoverableError(A, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])

pkg/front_end/parser_testcases/error_recovery/keyword_named_class_methods.dart.expect

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ beginCompilationUnit(class)
19921992
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
19931993
handleIdentifier(, localVariableDeclaration)
19941994
beginInitializedIdentifier()
1995-
handleNoVariableInitializer(x)
1995+
handleNoVariableInitializer()
19961996
endInitializedIdentifier()
19971997
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
19981998
endVariablesDeclaration(1, ;)
@@ -4433,7 +4433,7 @@ beginCompilationUnit(class)
44334433
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
44344434
handleIdentifier(, localVariableDeclaration)
44354435
beginInitializedIdentifier()
4436-
handleNoVariableInitializer(x)
4436+
handleNoVariableInitializer()
44374437
endInitializedIdentifier()
44384438
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
44394439
endVariablesDeclaration(1, ;)
@@ -4520,7 +4520,7 @@ beginCompilationUnit(class)
45204520
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
45214521
handleIdentifier(, localVariableDeclaration)
45224522
beginInitializedIdentifier()
4523-
handleNoVariableInitializer(x)
4523+
handleNoVariableInitializer()
45244524
endInitializedIdentifier()
45254525
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
45264526
endVariablesDeclaration(1, ;)

pkg/front_end/parser_testcases/error_recovery/keyword_named_class_methods.dart.intertwined.expect

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,7 +3748,7 @@ parseUnit(class)
37483748
listener: handleIdentifier(, localVariableDeclaration)
37493749
listener: beginInitializedIdentifier()
37503750
parseVariableInitializerOpt()
3751-
listener: handleNoVariableInitializer(x)
3751+
listener: handleNoVariableInitializer()
37523752
listener: endInitializedIdentifier()
37533753
ensureSemicolon()
37543754
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
@@ -9751,7 +9751,7 @@ parseUnit(class)
97519751
listener: handleIdentifier(, localVariableDeclaration)
97529752
listener: beginInitializedIdentifier()
97539753
parseVariableInitializerOpt()
9754-
listener: handleNoVariableInitializer(x)
9754+
listener: handleNoVariableInitializer()
97559755
listener: endInitializedIdentifier()
97569756
ensureSemicolon()
97579757
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
@@ -9986,7 +9986,7 @@ parseUnit(class)
99869986
listener: handleIdentifier(, localVariableDeclaration)
99879987
listener: beginInitializedIdentifier()
99889988
parseVariableInitializerOpt()
9989-
listener: handleNoVariableInitializer(x)
9989+
listener: handleNoVariableInitializer()
99909990
listener: endInitializedIdentifier()
99919991
ensureSemicolon()
99929992
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])

pkg/front_end/parser_testcases/error_recovery/keyword_named_top_level_methods.dart.expect

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ beginCompilationUnit(int)
19471947
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
19481948
handleIdentifier(, localVariableDeclaration)
19491949
beginInitializedIdentifier()
1950-
handleNoVariableInitializer(x)
1950+
handleNoVariableInitializer()
19511951
endInitializedIdentifier()
19521952
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
19531953
endVariablesDeclaration(1, ;)
@@ -4338,7 +4338,7 @@ beginCompilationUnit(int)
43384338
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
43394339
handleIdentifier(, localVariableDeclaration)
43404340
beginInitializedIdentifier()
4341-
handleNoVariableInitializer(x)
4341+
handleNoVariableInitializer()
43424342
endInitializedIdentifier()
43434343
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
43444344
endVariablesDeclaration(1, ;)
@@ -4424,7 +4424,7 @@ beginCompilationUnit(int)
44244424
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
44254425
handleIdentifier(, localVariableDeclaration)
44264426
beginInitializedIdentifier()
4427-
handleNoVariableInitializer(x)
4427+
handleNoVariableInitializer()
44284428
endInitializedIdentifier()
44294429
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
44304430
endVariablesDeclaration(1, ;)

pkg/front_end/parser_testcases/error_recovery/keyword_named_top_level_methods.dart.intertwined.expect

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,7 +3624,7 @@ parseUnit(int)
36243624
listener: handleIdentifier(, localVariableDeclaration)
36253625
listener: beginInitializedIdentifier()
36263626
parseVariableInitializerOpt()
3627-
listener: handleNoVariableInitializer(x)
3627+
listener: handleNoVariableInitializer()
36283628
listener: endInitializedIdentifier()
36293629
ensureSemicolon()
36303630
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
@@ -9439,7 +9439,7 @@ parseUnit(int)
94399439
listener: handleIdentifier(, localVariableDeclaration)
94409440
listener: beginInitializedIdentifier()
94419441
parseVariableInitializerOpt()
9442-
listener: handleNoVariableInitializer(x)
9442+
listener: handleNoVariableInitializer()
94439443
listener: endInitializedIdentifier()
94449444
ensureSemicolon()
94459445
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
@@ -9670,7 +9670,7 @@ parseUnit(int)
96709670
listener: handleIdentifier(, localVariableDeclaration)
96719671
listener: beginInitializedIdentifier()
96729672
parseVariableInitializerOpt()
9673-
listener: handleNoVariableInitializer(x)
9673+
listener: handleNoVariableInitializer()
96749674
listener: endInitializedIdentifier()
96759675
ensureSemicolon()
96769676
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Binary file not shown.

pkg/front_end/parser_testcases/error_recovery/utf_16_le_content.crash_dart.intertwined.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ parseUnit(EncodingErrorToken())
21862186
listener: handleIdentifier(r, localVariableDeclaration)
21872187
listener: beginInitializedIdentifier(r)
21882188
parseVariableInitializerOpt(r)
2189-
listener: handleNoVariableInitializer(i)
2189+
listener: handleNoVariableInitializer(r)
21902190
listener: endInitializedIdentifier(r)
21912191
ensureSemicolon(r)
21922192
reportRecoverableError(r, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
@@ -2210,7 +2210,7 @@ parseUnit(EncodingErrorToken())
22102210
listener: handleIdentifier(n, localVariableDeclaration)
22112211
listener: beginInitializedIdentifier(n)
22122212
parseVariableInitializerOpt(n)
2213-
listener: handleNoVariableInitializer(t)
2213+
listener: handleNoVariableInitializer(n)
22142214
listener: endInitializedIdentifier(n)
22152215
ensureSemicolon(n)
22162216
reportRecoverableError(n, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])

0 commit comments

Comments
 (0)