digitalmars.D.bugs - [Issue 22718] New: Grammar for case/default statement is
- d-bugmail puremagic.com (70/70) Jan 31 2022 https://issues.dlang.org/show_bug.cgi?id=22718
https://issues.dlang.org/show_bug.cgi?id=22718 Issue ID: 22718 Summary: Grammar for case/default statement is Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: major Priority: P1 Component: dlang.org Assignee: nobody puremagic.com Reporter: deadalnix gmail.com 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. --
Jan 31 2022