Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions changelog/dmd.empty-statement-error.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Using `;` as an empty statement has been turned into an error

This has been deprecated since 2.075.0 because it's error prone:

---
void main()
{
foreach (i; 0 .. 8);
{
// Because of the accidental semicolon above,
// this block statement is executed once.
// It's not the loop body
}
}
---

It will now result in an error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It always has resulted in an error.

@dkorpel ??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog's example is wrong, you can see the new error in the diff of the test file.


$(CONSOLE
app.d(3): Error: use `{ }` for an empty statement, not `;`
)
12 changes: 6 additions & 6 deletions compiler/src/dmd/cparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ final class CParser(AST) : Parser!AST
else if (token.value == TOK.leftCurly)
s = cparseStatement(ParseStatementFlags.curly | ParseStatementFlags.scope_);
else
s = cparseStatement(ParseStatementFlags.semiOk);
s = cparseStatement(0);
s = new AST.LabelStatement(loc, ident, s);
break;
}
Expand Down Expand Up @@ -373,7 +373,7 @@ final class CParser(AST) : Parser!AST
auto statements = new AST.Statements();
while (token.value != TOK.rightCurly && token.value != TOK.endOfFile)
{
statements.push(cparseStatement(ParseStatementFlags.semi | ParseStatementFlags.curlyScope));
statements.push(cparseStatement(ParseStatementFlags.curlyScope));
}
if (endPtr)
*endPtr = token.ptr;
Expand Down Expand Up @@ -514,7 +514,7 @@ final class CParser(AST) : Parser!AST
auto statements = new AST.Statements();
while (token.value != TOK.case_ && token.value != TOK.default_ && token.value != TOK.endOfFile && token.value != TOK.rightCurly)
{
auto cur = cparseStatement(ParseStatementFlags.semi | ParseStatementFlags.curlyScope);
auto cur = cparseStatement(ParseStatementFlags.curlyScope);
statements.push(cur);

// https://issues.dlang.org/show_bug.cgi?id=21739
Expand All @@ -529,7 +529,7 @@ final class CParser(AST) : Parser!AST
}
else
{
s = cparseStatement(ParseStatementFlags.semi);
s = cparseStatement(0);
}
s = new AST.ScopeStatement(loc, s, token.loc);
s = new AST.CaseStatement(loc, exp, s);
Expand All @@ -546,12 +546,12 @@ final class CParser(AST) : Parser!AST
auto statements = new AST.Statements();
while (token.value != TOK.case_ && token.value != TOK.default_ && token.value != TOK.endOfFile && token.value != TOK.rightCurly)
{
statements.push(cparseStatement(ParseStatementFlags.semi | ParseStatementFlags.curlyScope));
statements.push(cparseStatement(ParseStatementFlags.curlyScope));
}
s = new AST.CompoundStatement(loc, statements);
}
else
s = cparseStatement(ParseStatementFlags.semi);
s = cparseStatement(0);
s = new AST.ScopeStatement(loc, s, token.loc);
s = new AST.DefaultStatement(loc, s);
break;
Expand Down
20 changes: 8 additions & 12 deletions compiler/src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -5198,7 +5198,7 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
case TOK.leftCurly:
if (requireDo)
error("missing `do { ... }` after `in` or `out`");
f.fbody = parseStatement(ParseStatementFlags.semi);
f.fbody = parseStatement(0);
f.endloc = endloc;
break;

Expand Down Expand Up @@ -6034,7 +6034,7 @@ LagainStc:
auto statements = new AST.Statements();
while (token.value != TOK.rightCurly && token.value != TOK.endOfFile)
{
statements.push(parseStatement(ParseStatementFlags.semi | ParseStatementFlags.curlyScope));
statements.push(parseStatement(ParseStatementFlags.curlyScope));
}
if (endPtr)
*endPtr = token.ptr;
Expand Down Expand Up @@ -6067,10 +6067,7 @@ LagainStc:
case TOK.semicolon:
if (!(flags & ParseStatementFlags.semiOk))
{
if (flags & ParseStatementFlags.semi)
deprecation("use `{ }` for an empty statement, not `;`");
else
error("use `{ }` for an empty statement, not `;`");
error("use `{ }` for an empty statement, not `;`");
}
nextToken();
s = new AST.ExpStatement(loc, cast(AST.Expression)null);
Expand Down Expand Up @@ -6287,7 +6284,7 @@ LagainStc:
_body = null;
}
else
_body = parseStatement(ParseStatementFlags.semi);
_body = parseStatement(0);
s = new AST.PragmaStatement(loc, ident, args, _body);
break;
}
Expand Down Expand Up @@ -6340,7 +6337,7 @@ LagainStc:
auto statements = new AST.Statements();
while (token.value != TOK.case_ && token.value != TOK.default_ && token.value != TOK.endOfFile && token.value != TOK.rightCurly)
{
auto cur = parseStatement(ParseStatementFlags.semi | ParseStatementFlags.curlyScope);
auto cur = parseStatement(ParseStatementFlags.curlyScope);
statements.push(cur);

// https://issues.dlang.org/show_bug.cgi?id=21739
Expand All @@ -6355,7 +6352,7 @@ LagainStc:
}
else
{
s = parseStatement(ParseStatementFlags.semi);
s = parseStatement(0);
}
s = new AST.ScopeStatement(loc, s, token.loc);

Expand Down Expand Up @@ -6384,12 +6381,12 @@ LagainStc:
auto statements = new AST.Statements();
while (token.value != TOK.case_ && token.value != TOK.default_ && token.value != TOK.endOfFile && token.value != TOK.rightCurly)
{
statements.push(parseStatement(ParseStatementFlags.semi | ParseStatementFlags.curlyScope));
statements.push(parseStatement(ParseStatementFlags.curlyScope));
}
s = new AST.CompoundStatement(loc, statements);
}
else
s = parseStatement(ParseStatementFlags.semi);
s = parseStatement(0);
s = new AST.ScopeStatement(loc, s, token.loc);
s = new AST.DefaultStatement(loc, s);
break;
Expand Down Expand Up @@ -9686,7 +9683,6 @@ immutable PREC[EXP.max + 1] precedence =

enum ParseStatementFlags : int
{
semi = 1, // empty ';' statements are allowed, but deprecated
scope_ = 2, // start a new scope
curly = 4, // { } statement is required
curlyScope = 8, // { } starts a new scope
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -4788,7 +4788,7 @@ private Statements* flatten(Statement statement, Scope* sc)
auto a = new Statements();
while (p.token.value != TOK.endOfFile)
{
Statement s = p.parseStatement(ParseStatementFlags.semi | ParseStatementFlags.curlyScope);
Statement s = p.parseStatement(ParseStatementFlags.curlyScope);
if (!s || global.errors != errors)
return errorStatements();
a.push(s);
Expand Down
8 changes: 4 additions & 4 deletions compiler/test/fail_compilation/fail4559.d
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
REQUIRED_ARGS: -o- -de
REQUIRED_ARGS:
TEST_OUTPUT:
---
fail_compilation/fail4559.d(13): Deprecation: use `{ }` for an empty statement, not `;`
fail_compilation/fail4559.d(19): Deprecation: use `{ }` for an empty statement, not `;`
fail_compilation/fail4559.d(21): Deprecation: use `{ }` for an empty statement, not `;`
fail_compilation/fail4559.d(13): Error: use `{ }` for an empty statement, not `;`
fail_compilation/fail4559.d(19): Error: use `{ }` for an empty statement, not `;`
fail_compilation/fail4559.d(21): Error: use `{ }` for an empty statement, not `;`
---
*/

Expand Down