Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar for case/default statement is wrong #4122

Open
dlangBugzillaToGithub opened this issue Jan 31, 2022 · 0 comments
Open

Grammar for case/default statement is wrong #4122

dlangBugzillaToGithub opened this issue Jan 31, 2022 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant