You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
deadalnix (@deadalnix) reported this on 2022-01-31T13:51:48Z
Transferred from https://issues.dlang.org/show_bug.cgi?id=22718
Description
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.The text was updated successfully, but these errors were encountered: