Skip to content

Commit

Permalink
[cfe] Use variable name in error message for missing initializer
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
johnniwinther authored and commit-bot@chromium.org committed Jul 8, 2020
1 parent 2b5eaff commit 9ab7a5e
Show file tree
Hide file tree
Showing 52 changed files with 201 additions and 98 deletions.
2 changes: 1 addition & 1 deletion pkg/_fe_analyzer_shared/lib/src/parser/parser_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2697,7 +2697,7 @@ class Parser {
token = parseExpression(assignment);
listener.endVariableInitializer(assignment);
} else {
listener.handleNoVariableInitializer(token.next);
listener.handleNoVariableInitializer(token);
}
return token;
}
Expand Down
29 changes: 16 additions & 13 deletions pkg/front_end/lib/src/fasta/kernel/body_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2394,22 +2394,25 @@ class BodyBuilder extends ScopeListener<JumpTarget>
bool isFinal = (currentLocalVariableModifiers & finalMask) != 0;
bool isLate = (currentLocalVariableModifiers & lateMask) != 0;
Expression initializer;
if (!optional("in", token)) {
if (!optional("in", token.next)) {
// A for-in loop-variable can't have an initializer. So let's remain
// silent if the next token is `in`. Since a for-in loop can only have
// one variable it must be followed by `in`.
if (isConst) {
initializer = buildProblem(
fasta.templateConstFieldWithoutInitializer
.withArguments(token.lexeme),
token.charOffset,
token.length);
} else if (isFinal && !isLate) {
initializer = buildProblem(
fasta.templateFinalFieldWithoutInitializer
.withArguments(token.lexeme),
token.charOffset,
token.length);
if (!token.isSynthetic) {
// If [token] is synthetic it is created from error recovery.
if (isConst) {
initializer = buildProblem(
fasta.templateConstFieldWithoutInitializer
.withArguments(token.lexeme),
token.charOffset,
token.length);
} else if (isFinal && !isLate) {
initializer = buildProblem(
fasta.templateFinalFieldWithoutInitializer
.withArguments(token.lexeme),
token.charOffset,
token.length);
}
}
}
pushNewLocalVariable(initializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ beginCompilationUnit(main)
beginVariablesDeclaration(A, null, null)
handleIdentifier(A, localVariableDeclaration)
beginInitializedIdentifier(A)
handleNoVariableInitializer(<)
handleNoVariableInitializer(A)
endInitializedIdentifier(A)
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], A, A)
endVariablesDeclaration(1, ;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ parseUnit(main)
listener: handleIdentifier(A, localVariableDeclaration)
listener: beginInitializedIdentifier(A)
parseVariableInitializerOpt(A)
listener: handleNoVariableInitializer(<)
listener: handleNoVariableInitializer(A)
listener: endInitializedIdentifier(A)
ensureSemicolon(A)
reportRecoverableError(A, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1992,7 +1992,7 @@ beginCompilationUnit(class)
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
handleIdentifier(, localVariableDeclaration)
beginInitializedIdentifier()
handleNoVariableInitializer(x)
handleNoVariableInitializer()
endInitializedIdentifier()
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
endVariablesDeclaration(1, ;)
Expand Down Expand Up @@ -4433,7 +4433,7 @@ beginCompilationUnit(class)
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
handleIdentifier(, localVariableDeclaration)
beginInitializedIdentifier()
handleNoVariableInitializer(x)
handleNoVariableInitializer()
endInitializedIdentifier()
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
endVariablesDeclaration(1, ;)
Expand Down Expand Up @@ -4520,7 +4520,7 @@ beginCompilationUnit(class)
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
handleIdentifier(, localVariableDeclaration)
beginInitializedIdentifier()
handleNoVariableInitializer(x)
handleNoVariableInitializer()
endInitializedIdentifier()
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
endVariablesDeclaration(1, ;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3748,7 +3748,7 @@ parseUnit(class)
listener: handleIdentifier(, localVariableDeclaration)
listener: beginInitializedIdentifier()
parseVariableInitializerOpt()
listener: handleNoVariableInitializer(x)
listener: handleNoVariableInitializer()
listener: endInitializedIdentifier()
ensureSemicolon()
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand Down Expand Up @@ -9751,7 +9751,7 @@ parseUnit(class)
listener: handleIdentifier(, localVariableDeclaration)
listener: beginInitializedIdentifier()
parseVariableInitializerOpt()
listener: handleNoVariableInitializer(x)
listener: handleNoVariableInitializer()
listener: endInitializedIdentifier()
ensureSemicolon()
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand Down Expand Up @@ -9986,7 +9986,7 @@ parseUnit(class)
listener: handleIdentifier(, localVariableDeclaration)
listener: beginInitializedIdentifier()
parseVariableInitializerOpt()
listener: handleNoVariableInitializer(x)
listener: handleNoVariableInitializer()
listener: endInitializedIdentifier()
ensureSemicolon()
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1947,7 +1947,7 @@ beginCompilationUnit(int)
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
handleIdentifier(, localVariableDeclaration)
beginInitializedIdentifier()
handleNoVariableInitializer(x)
handleNoVariableInitializer()
endInitializedIdentifier()
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
endVariablesDeclaration(1, ;)
Expand Down Expand Up @@ -4338,7 +4338,7 @@ beginCompilationUnit(int)
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
handleIdentifier(, localVariableDeclaration)
beginInitializedIdentifier()
handleNoVariableInitializer(x)
handleNoVariableInitializer()
endInitializedIdentifier()
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
endVariablesDeclaration(1, ;)
Expand Down Expand Up @@ -4424,7 +4424,7 @@ beginCompilationUnit(int)
handleRecoverableError(Message[ExpectedIdentifier, Expected an identifier, but got '('., null, {token: (}], (, ()
handleIdentifier(, localVariableDeclaration)
beginInitializedIdentifier()
handleNoVariableInitializer(x)
handleNoVariableInitializer()
endInitializedIdentifier()
handleRecoverableError(Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}], x, x)
endVariablesDeclaration(1, ;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3624,7 +3624,7 @@ parseUnit(int)
listener: handleIdentifier(, localVariableDeclaration)
listener: beginInitializedIdentifier()
parseVariableInitializerOpt()
listener: handleNoVariableInitializer(x)
listener: handleNoVariableInitializer()
listener: endInitializedIdentifier()
ensureSemicolon()
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand Down Expand Up @@ -9439,7 +9439,7 @@ parseUnit(int)
listener: handleIdentifier(, localVariableDeclaration)
listener: beginInitializedIdentifier()
parseVariableInitializerOpt()
listener: handleNoVariableInitializer(x)
listener: handleNoVariableInitializer()
listener: endInitializedIdentifier()
ensureSemicolon()
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand Down Expand Up @@ -9670,7 +9670,7 @@ parseUnit(int)
listener: handleIdentifier(, localVariableDeclaration)
listener: beginInitializedIdentifier()
parseVariableInitializerOpt()
listener: handleNoVariableInitializer(x)
listener: handleNoVariableInitializer()
listener: endInitializedIdentifier()
ensureSemicolon()
reportRecoverableError(, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,7 @@ parseUnit(EncodingErrorToken())
listener: handleIdentifier(r, localVariableDeclaration)
listener: beginInitializedIdentifier(r)
parseVariableInitializerOpt(r)
listener: handleNoVariableInitializer(i)
listener: handleNoVariableInitializer(r)
listener: endInitializedIdentifier(r)
ensureSemicolon(r)
reportRecoverableError(r, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand All @@ -2210,7 +2210,7 @@ parseUnit(EncodingErrorToken())
listener: handleIdentifier(n, localVariableDeclaration)
listener: beginInitializedIdentifier(n)
parseVariableInitializerOpt(n)
listener: handleNoVariableInitializer(t)
listener: handleNoVariableInitializer(n)
listener: endInitializedIdentifier(n)
ensureSemicolon(n)
reportRecoverableError(n, Message[ExpectedAfterButGot, Expected ';' after this., null, {string: ;}])
Expand Down
2 changes: 1 addition & 1 deletion pkg/front_end/parser_testcases/general/for_in.dart.expect
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ beginCompilationUnit(main)
beginVariablesDeclaration(i, null, var)
handleIdentifier(i, localVariableDeclaration)
beginInitializedIdentifier(i)
handleNoVariableInitializer(in)
handleNoVariableInitializer(i)
endInitializedIdentifier(i)
endVariablesDeclaration(1, null)
handleForInitializerLocalVariableDeclaration(i, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ parseUnit(main)
listener: handleIdentifier(i, localVariableDeclaration)
listener: beginInitializedIdentifier(i)
parseVariableInitializerOpt(i)
listener: handleNoVariableInitializer(in)
listener: handleNoVariableInitializer(i)
listener: endInitializedIdentifier(i)
listener: endVariablesDeclaration(1, null)
listener: handleForInitializerLocalVariableDeclaration(i, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ beginCompilationUnit(main)
beginVariablesDeclaration(i, null, var)
handleIdentifier(i, localVariableDeclaration)
beginInitializedIdentifier(i)
handleNoVariableInitializer(;)
handleNoVariableInitializer(i)
endInitializedIdentifier(i)
endVariablesDeclaration(1, ;)
beginForStatement(for)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ parseUnit(main)
listener: handleIdentifier(i, localVariableDeclaration)
listener: beginInitializedIdentifier(i)
parseVariableInitializerOpt(i)
listener: handleNoVariableInitializer(;)
listener: handleNoVariableInitializer(i)
listener: endInitializedIdentifier(i)
ensureSemicolon(i)
listener: endVariablesDeclaration(1, ;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ beginCompilationUnit(main)
beginVariablesDeclaration(i, null, null)
handleIdentifier(i, localVariableDeclaration)
beginInitializedIdentifier(i)
handleNoVariableInitializer(;)
handleNoVariableInitializer(i)
endInitializedIdentifier(i)
endVariablesDeclaration(1, ;)
beginForStatement(for)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ parseUnit(main)
listener: handleIdentifier(i, localVariableDeclaration)
listener: beginInitializedIdentifier(i)
parseVariableInitializerOpt(i)
listener: handleNoVariableInitializer(;)
listener: handleNoVariableInitializer(i)
listener: endInitializedIdentifier(i)
ensureSemicolon(i)
listener: endVariablesDeclaration(1, ;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ beginCompilationUnit(main)
beginVariablesDeclaration(c, null, var)
handleIdentifier(c, localVariableDeclaration)
beginInitializedIdentifier(c)
handleNoVariableInitializer(;)
handleNoVariableInitializer(c)
endInitializedIdentifier(c)
endVariablesDeclaration(1, ;)
handleIdentifier(c, expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ parseUnit(main)
listener: handleIdentifier(c, localVariableDeclaration)
listener: beginInitializedIdentifier(c)
parseVariableInitializerOpt(c)
listener: handleNoVariableInitializer(;)
listener: handleNoVariableInitializer(c)
listener: endInitializedIdentifier(c)
ensureSemicolon(c)
listener: endVariablesDeclaration(1, ;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ beginCompilationUnit(f)
beginVariablesDeclaration(a, null, var)
handleIdentifier(a, localVariableDeclaration)
beginInitializedIdentifier(a)
handleNoVariableInitializer(,)
handleNoVariableInitializer(a)
endInitializedIdentifier(a)
handleIdentifier(b, localVariableDeclaration)
beginInitializedIdentifier(b)
handleNoVariableInitializer(,)
handleNoVariableInitializer(b)
endInitializedIdentifier(b)
handleIdentifier(c, localVariableDeclaration)
beginInitializedIdentifier(c)
handleNoVariableInitializer(;)
handleNoVariableInitializer(c)
endInitializedIdentifier(c)
endVariablesDeclaration(3, ;)
handleIdentifier(a, expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ parseUnit(f)
listener: handleIdentifier(a, localVariableDeclaration)
listener: beginInitializedIdentifier(a)
parseVariableInitializerOpt(a)
listener: handleNoVariableInitializer(,)
listener: handleNoVariableInitializer(a)
listener: endInitializedIdentifier(a)
parseOptionallyInitializedIdentifier(,)
ensureIdentifier(,, localVariableDeclaration)
listener: handleIdentifier(b, localVariableDeclaration)
listener: beginInitializedIdentifier(b)
parseVariableInitializerOpt(b)
listener: handleNoVariableInitializer(,)
listener: handleNoVariableInitializer(b)
listener: endInitializedIdentifier(b)
parseOptionallyInitializedIdentifier(,)
ensureIdentifier(,, localVariableDeclaration)
listener: handleIdentifier(c, localVariableDeclaration)
listener: beginInitializedIdentifier(c)
parseVariableInitializerOpt(c)
listener: handleNoVariableInitializer(;)
listener: handleNoVariableInitializer(c)
listener: endInitializedIdentifier(c)
ensureSemicolon(c)
listener: endVariablesDeclaration(3, ;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ beginCompilationUnit(f)
beginVariablesDeclaration(a, null, var)
handleIdentifier(a, localVariableDeclaration)
beginInitializedIdentifier(a)
handleNoVariableInitializer(,)
handleNoVariableInitializer(a)
endInitializedIdentifier(a)
handleIdentifier(b, localVariableDeclaration)
beginInitializedIdentifier(b)
handleNoVariableInitializer(;)
handleNoVariableInitializer(b)
endInitializedIdentifier(b)
endVariablesDeclaration(2, ;)
beginReturnStatement(return)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ parseUnit(f)
listener: handleIdentifier(a, localVariableDeclaration)
listener: beginInitializedIdentifier(a)
parseVariableInitializerOpt(a)
listener: handleNoVariableInitializer(,)
listener: handleNoVariableInitializer(a)
listener: endInitializedIdentifier(a)
parseOptionallyInitializedIdentifier(,)
ensureIdentifier(,, localVariableDeclaration)
listener: handleIdentifier(b, localVariableDeclaration)
listener: beginInitializedIdentifier(b)
parseVariableInitializerOpt(b)
listener: handleNoVariableInitializer(;)
listener: handleNoVariableInitializer(b)
listener: endInitializedIdentifier(b)
ensureSemicolon(b)
listener: endVariablesDeclaration(2, ;)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ beginCompilationUnit(f)
beginVariablesDeclaration(a, null, var)
handleIdentifier(a, localVariableDeclaration)
beginInitializedIdentifier(a)
handleNoVariableInitializer(,)
handleNoVariableInitializer(a)
endInitializedIdentifier(a)
handleIdentifier(b, localVariableDeclaration)
beginInitializedIdentifier(b)
handleNoVariableInitializer(;)
handleNoVariableInitializer(b)
endInitializedIdentifier(b)
endVariablesDeclaration(2, ;)
handleIdentifier(a, expression)
Expand Down
Loading

0 comments on commit 9ab7a5e

Please sign in to comment.