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