Bug 327 – Compiler accepts ';' by itself as a statement
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-09-06T14:53:00Z
Last change time
2014-02-15T13:19:59Z
Keywords
accepts-invalid
Assigned to
bugzilla
Creator
smjg
Comments
Comment #0 by smjg — 2006-09-06T14:53:26Z
The following code compiles without error:
----------
void main() {
;
}
----------
and produces a program that does nothing.
Nothing in the D spec allows a semicolon on its own to be used as a statement. Nonetheless, the current compiler allows it. It has, however, made exceptions for certain control structures, e.g. it rejects
if (...) ;
for (...) ;
while allowing some other similar forms.
The language would gain nothing by allowing ';' as a statement in any context. In C, it may have had its uses in conjunction with preprocessor macros. But in D, which has no macro preprocessor, no such use cases exist.
Moreover, it is much simpler to disallow such a useless statement form altogether than to let it through and then catch a handful of specific cases.
There was once a specific exception, to the effect of
LabelledStatement:
Identifier ':' Statement
Identifier ':' ';'
but this has since disappeared from the spec. But if for any reason this is ever reinstated, then implementing it explicitly would still be a lot simpler than the mess things are in at the moment.
Existing DStress testcases:
http://dstress.kuehne.cn/nocompile/e/ExpressionStatement_06_A.d
et seq.
Comment #1 by lio+bugzilla — 2006-11-15T02:16:51Z
Fixing this would break goto labels at the end of a function. These labels should now be followed by a single colon, since they label a statement, even an empty one. I'm setting severity to minor.
Comment #2 by smjg — 2006-11-15T06:31:33Z
Please clarify. Why do you feel that the explicit solution I've clearly given for the LabelledStatement issue won't work?
Comment #3 by bugzilla — 2006-11-18T16:47:55Z
Updated the spec to allow this.
Comment #4 by smjg — 2006-11-19T08:01:43Z
Why won't you tell us why?
And even if one were to accept this change, I don't consider it fixed at all, because the compiler is still accepting it even in some cases where the design of D is to forbid it. See DStress testcases ExpressionStatement_07_* and ExpressionStatement_08_*.
Comment #5 by smjg — 2006-11-19T08:38:04Z
(In reply to comment #4)
> Why won't you tell us why?
I should've replied to your mention on the newsgroup of case and default statements before I wrote that. Well, I have now.
Comment #6 by bugzilla — 2006-11-19T14:32:35Z
Both DebugStatement and VersionStatement allow a ';' as a statement, as both have Statement in the grammar, and Statement includes ';'. In other words, this is not a bug as the spec matches the implementation. If you want to change it, it should be marked as an enhancement request, not a bug.
As to why allow empty statements, 1) such are common in C like languages and are expected to be there and 2) they are 'boundary' or 'degenerate' cases and can be handy to have as markers, placeholders, or to just make it easier for programs that generate source code as output. They don't hurt anything.
Comment #7 by smjg — 2009-07-26T16:01:26Z
The INVALID resolution was due to a mistake in the spec that has since been fixed. And the original issue was fixed by changing the spec. Reverting resolution.