dlang-community / libdparse

Library for lexing and parsing D source code
https://libdparse.dlang.io
Boost Software License 1.0
115 stars 57 forks source link

declaration in scope (exit) causes a parse error, but is valid D code #498

Closed brianush1 closed 1 year ago

brianush1 commented 1 year ago
void main() {
    scope (exit)
        int hi;
}

compiles fine with DMD, but doesn't parse with libdparse

WebFreak001 commented 1 year ago

this also is valid, but what does this even mean? (it compiles and runs as if the scope guard wasn't there):

void main() {
    switch (x)
    {
        case 1:
            break;
        scope (exit)
            default:
                foo();
                break;
    }
}
brianush1 commented 1 year ago
void main() {
    switch (2)
    {
        case 1:
            break;

        if (true) {
            writeln("huh");
            default:
                writeln("ran");
                foo();
        }

        break;
    }
}

so, this code prints "ran"... I guess jumping to a switch label acts the same as jumping to a label using goto, which means switch labels can be inside blocks..? and scope (exit) gets lowered down to a try/finally, so that works out

also, interestingly, if you dump the AST you can see that the break; statement in your code example is not actually inside the scope (exit) statement...