Bug 22718 – Grammar for case/default statement is wrong

Status
NEW
Severity
major
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-01-31T13:51:48Z
Last change time
2024-12-15T15:27:22Z
Assigned to
No Owner
Creator
deadalnix
Moved to GitHub: dlang.org#4122 →

Comments

Comment #0 by deadalnix — 2022-01-31T13:51:48Z
The grammar for case/default statement incorrectly specify DMD's behavior. I do believe, based on C and C++'s behavior, and D's principle that valid C code should either compile in D or be an error, but not have different semantic, that DMD is right, and the grammar is wrong. See sample code: import std.stdio; void foo() { writeln(__FUNCTION__); } void bar() { writeln(__FUNCTION__); } void fun(int i) { int n = 3; switch (i) { while (n --> 0) case 0: case 1: foo(); bar(); default: writeln("default"); } } void main() { fun(1); } This prints: example.foo example.foo example.foo example.foo example.bar default As you'd expect using C's grammar. However, current D grammar, as specified here: https://dlang.org/spec/statement.html#ScopeStatementList tells us that all statements following the case/default should be parsed as being part of the case/default statement, unless they are themselves case/default statement. In the above exemple, this mean that the case 1 ought to be considered to be outside the loop,as in: void fun(int i) { int n = 3; switch (i) { while (n --> 0) { case 0: } case 1: foo(); bar(); default: writeln("default"); } } Not that document D's grammar would cause a deluge of bizarre side effects, and would differs from C. Instead, case/default should be defined in a manner similar to LabeledStatement. Thankfully, the compiler seems to already do that.
Comment #1 by robert.schadek — 2024-12-15T15:27:22Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dlang.org/issues/4122 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB