www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21427] New: switch skips declaration of variable except it

https://issues.dlang.org/show_bug.cgi?id=21427

          Issue ID: 21427
           Summary: switch skips declaration of variable except it
                    actually doesn't
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: zorael gmail.com

Manjaro/Arch x86_64, dmd v2.094.1.

dmd complains about a variable declared in a switch case to have skipped its
declaration and compilation fails.

Original reduction: https://run.dlang.io/is/ah4XWa

void main()
{
    switch (string.init)
    {
    case "LS":
        switch (string.init)
        {
        case "sasl":
            immutable acceptsExternal = true;
            goto case;

        version (all)
        {
            case "twitch.tv/membership":
            goto case;
        }

        case "znc.in/self-message":
            break;

        default:
            break;
        }
        break;

    default:
        break;
    }
}

 feep[work] | zorael: even shorter https://run.dlang.io/is/nrjBkS
void main() { final switch ("1") { case "1": immutable acceptsExternal = true; break; { case "2": break; } } }
 feep[work] | zorael: this is the actual problem https://run.dlang.io/is/IOW91m
void main() { final switch ("1") { case "1": immutable acceptsExternal = true; break; { case "2": return acceptsExternal; break; } } }
 feep[work] | zorael: workaround https://run.dlang.io/is/as7yjf
void main() { switch (string.init) { case "LS": switch (string.init) { case "sasl": immutable acceptsExternal = true; goto case; case "THISNEVERHAPPENS": // workaround for weird D switch scoping issue version (all) { case "twitch.tv/membership": goto case; } case "znc.in/self-message": break; default: break; } break; default: break; } }
 feep[work] | zorael: alternately, explicitly wrap the acceptsExternal block in
a {}
[...]
 feep[work] | for one, version shouldn't create a scope                        
                                                    
 feep[work] | zorael: the actual problem is it thinks that the versioned case
is in a subscope and so the variable "should remain in scope"                  
                                                                               
             
 feep[work] | which doesn't work cause you can case jump into the versioned
case and skip the immutable variable                    
 feep[work] | so the real issue is that switch thinks version opens a scope (it
doesn't even)
--
Nov 26 2020