Jump to page: 1 2
Thread overview
[Issue 18712] [Reg 2.072] bogus "switch skips declaration" error with case in mixin
Apr 11, 2018
Martin Nowak
Apr 11, 2018
Martin Nowak
May 14, 2018
Walter Bright
May 14, 2018
Walter Bright
May 15, 2018
Rainer Schuetze
May 15, 2018
Walter Bright
May 16, 2018
Rainer Schuetze
May 16, 2018
Rainer Schuetze
Dec 24, 2019
berni44
Dec 17, 2022
Iain Buclaw
Dec 19, 2022
Iain Buclaw
April 11, 2018
https://issues.dlang.org/show_bug.cgi?id=18712

Martin Nowak <code@dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |code@dawg.eu
            Summary|bogus "switch skips         |[Reg 2.072] bogus "switch
                   |declaration" error with     |skips declaration" error
                   |case in mixin               |with case in mixin
                 OS|Windows                     |All

--- Comment #1 from Martin Nowak <code@dawg.eu> ---
Digger says https://github.com/dlang/dmd/pull/5869.

And btw yes, deprecation warnings should be deduplicated before printing, maybe simply by hashing the error and the source code location.

--
April 11, 2018
https://issues.dlang.org/show_bug.cgi?id=18712

--- Comment #2 from Martin Nowak <code@dawg.eu> ---
Also see issue 16254

--
May 14, 2018
https://issues.dlang.org/show_bug.cgi?id=18712

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
If

    mixin("case 0:");

is replaced with:

    { case 0: }

it also fails in the same way.

--
May 14, 2018
https://issues.dlang.org/show_bug.cgi?id=18712

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |WONTFIX

--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> ---
Here's what's happening. After a case/default statement, the parser makes the code between one case/default and the next into a scope. So it looks like:

    int test(int n)
    {
        switch(n)
        {
            mixin("case 0:");
            int x = 1;
            return x;
            case 1:
            {
                int y = 2;
                return y;
            }
            default:
            {
                return -1;
            }
        }
    }

and, of course, now the error message makes sense. `x` is visible to the following two scopes, whether or not the error message is generated. Any time the case/default is not directly in the switch body (not nested via { } or a mixin) the implicit { } scope is not generated. Oops.

Try putting { } in various combinations, and you'll see how it all comes unglued.

I can't think of any solution that 1) works in all cases and 2) doesn't break a lot of existing code. So we're just stuck with it. Fortunately, there is a workaround. Recode the switch like this:

    int test(int n)
    {
        switch(n)
        {
        mixin("case 0:");
        {
                int x = 1;
                return x;
        }
        case 1:
                int y = 2;
                return y;
        default:
                return -1;
        }
    }

I'm going to close this as WONTFIX. If anyone has a brainwave on how to make it work in all cases without breaking code, reopen with proof.

Note that the fundamental problem is a combination of:

1. allowing case/default statements to appear inside nested scopes

2. implicit generation of scopes between case/default pairs

--
May 15, 2018
https://issues.dlang.org/show_bug.cgi?id=18712

--- Comment #5 from Rainer Schuetze <r.sagitario@gmx.de> ---
Thanks for analyzing. I wouldn't be too concerned about possible breakage with a fix, because it's a regression (and a very recent if you compiled the code with -d).

It seems adding the implicit scope block in the parser is too early, as it doesn't know about mixins. OTOH it could be clarified in the spec that 'case' in mixins do not share the scope with trailing code.

--
May 15, 2018
https://issues.dlang.org/show_bug.cgi?id=18712

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
          Component|dmd                         |dlang.org
         Resolution|WONTFIX                     |---
           Severity|regression                  |normal

--- Comment #6 from Walter Bright <bugzilla@digitalmars.com> ---
(In reply to Rainer Schuetze from comment #5)
> Thanks for analyzing. I wouldn't be too concerned about possible breakage with a fix, because it's a regression (and a very recent if you compiled the code with -d).

It's not really a regression. The weirdness with the scope was always there, it's just that nobody noticed it. When it was implemented, I'm sure nobody thought about this behavior.


> It seems adding the implicit scope block in the parser is too early, as it doesn't know about mixins. OTOH it could be clarified in the spec that 'case' in mixins do not share the scope with trailing code.

A spec clarification is indeed in order:

  https://github.com/dlang/dlang.org/pull/2368

Reopening as a spec issue. It is more than just mixins, as the other example I posted here shows.

I investigated not adding the scope in the parser, but in the semantic() phase. That just introduces even more subtle issues.

--
May 16, 2018
https://issues.dlang.org/show_bug.cgi?id=18712

--- Comment #7 from Rainer Schuetze <r.sagitario@gmx.de> ---
I just noticed that in this code:

int test(int n)
{
        switch(n)
        {
        case -1:
                int b = -1;
        mixin("case 0:");
                int x = 1;
                return x;
        case 1:
                int y = 2;
                return y;
        default:
                return -1;
        }
}

The scope of b extends until the end of `case 0`, too.

--
May 16, 2018
https://issues.dlang.org/show_bug.cgi?id=18712

--- Comment #8 from Rainer Schuetze <r.sagitario@gmx.de> ---
I think that the report should not be closed if it is actually undesired and unexpected behavior, just because it cannot easily be fixed.

--
December 24, 2019
https://issues.dlang.org/show_bug.cgi?id=18712

berni44 <bugzilla@d-ecke.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@d-ecke.de

--- Comment #9 from berni44 <bugzilla@d-ecke.de> ---
See also https://github.com/dlang/dlang.org/pull/2512

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=18712

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |regression

--
« First   ‹ Prev
1 2