Thread overview
[Issue 6518] New: break inside a static foreach inside a switch
Aug 17, 2011
Kenji Hara
Aug 21, 2011
Kenji Hara
Sep 07, 2011
yebblies
Sep 18, 2011
Walter Bright
Mar 18, 2012
dawg@dawgfoto.de
August 17, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6518

           Summary: break inside a static foreach inside a switch
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-08-17 06:05:15 PDT ---
import std.typetuple: TypeTuple;
enum Foo : uint { A, B, C }
alias TypeTuple!(Foo.A, Foo.B, Foo.C) FooNames;
void main() {
    Foo fo;
    switch (fo) {
        /*static*/ foreach (i, op; __traits(allMembers, Foo))
            case FooNames[i]: break;

        default: assert(0);
    }
}


test.d(8): Error: switch case fallthrough - use 'goto case;' if intended
test.d(8): Error: switch case fallthrough - use 'goto case;' if intended
test.d(8): Error: switch case fallthrough - use 'goto case;' if intended
test.d(10): Error: switch case fallthrough - use 'goto default;' if intended


I think DMD 2.042 doesn't have this problem, this code used to work. I think this problem comes from recent changes in switch switch semantics and analysis.


The following code gives the same errors, showing it's not a problem of break breaking the foreach loop (like it probably has to do; so maybe the first example not correct?):

import std.typetuple: TypeTuple;
enum Foo : uint { A, B, C }
alias TypeTuple!(Foo.A, Foo.B, Foo.C) FooNames;
void main() {
    Foo fo;
    LABEL: switch (fo) {
        /*static*/ foreach (i, op; __traits(allMembers, Foo))
            case FooNames[i]: break LABEL;

        default: assert(0);
    }
}

----------------

That code is a reduction of a problem found in a recent version of the modified regex module:

void parseFlags(S)(S flags)
{
    foreach(ch; flags)//flags are ASCII anyway
    {
        switch(ch)
        {

            foreach(i, op; __traits(allMembers, RegexOption))
            {
                case RegexOptionNames[i]:
                        if(re_flags & mixin("RegexOption."~op))
                            throw new RegexException(text("redundant flag
specified: ",ch));
                        re_flags |= mixin("RegexOption."~op);
                        break;
            }
            default:
                if(__ctfe)
                   assert(text("unknown regex flag '",ch,"'"));
                else
                    new RegexException(text("unknown regex flag '",ch,"'"));
        }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 17, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6518



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2011-08-17 16:31:46 PDT ---
(In reply to comment #0)
> import std.typetuple: TypeTuple;
> enum Foo : uint { A, B, C }
> alias TypeTuple!(Foo.A, Foo.B, Foo.C) FooNames;
> void main() {
>     Foo fo;
>     switch (fo) {
>         /*static*/ foreach (i, op; __traits(allMembers, Foo))
>             case FooNames[i]: break;
> 
>         default: assert(0);
>     }
> }
[snip]
> I think DMD 2.042 doesn't have this problem, this code used to work. I think this problem comes from recent changes in switch switch semantics and analysis.

This code could compile with trunk dmd (c98b611), and it asserts in run time. Which version did you try it with?

> The following code gives the same errors, showing it's not a problem of break breaking the foreach loop (like it probably has to do; so maybe the first example not correct?):
> 
> import std.typetuple: TypeTuple;
> enum Foo : uint { A, B, C }
> alias TypeTuple!(Foo.A, Foo.B, Foo.C) FooNames;
> void main() {
>     Foo fo;
>     LABEL: switch (fo) {
>         /*static*/ foreach (i, op; __traits(allMembers, Foo))
>             case FooNames[i]: break LABEL;
> 
>         default: assert(0);
>     }
> }

This code could compile, and it succeeds to run.

I think these behaviors in my PC are correct.
In first sample code, break-statement eacapes from foreach, and always goes
into default case, and asserts.
And in second sample, break-statement escapes from LABEL-ed switch statement.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6518



--- Comment #2 from bearophile_hugs@eml.cc 2011-08-17 19:59:11 PDT ---
(In reply to comment #1)

> This code could compile with trunk dmd (c98b611), and it asserts in run time. Which version did you try it with?

I have just compiled DMD, and both examples give me those errors still, on Windows. I have compiled the code using the -w DMD switch. The error messages don't appear if I don't use -w.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 21, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6518



--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2011-08-21 07:00:55 PDT ---
More simple test case.

enum Foo : uint { A }
void main() {
    Foo foo;

    switch (foo) {
        enum op = "A";
        case Foo.A: break;
        default:    assert(0);
    }

    // Following gives same error
    //final switch (foo) {
    //    enum op = "A";
    //    case Foo.A: break;
    //}
}

The case statement treats the previous enum declaration as fall-through statement and warns.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 07, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6518


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch, rejects-valid
                 CC|                            |yebblies@gmail.com
           Platform|x86                         |All
         OS/Version|Windows                     |All


--- Comment #4 from yebblies <yebblies@gmail.com> 2011-09-07 14:12:22 EST ---
https://github.com/D-Programming-Language/dmd/pull/370

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6518


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2011-09-17 19:26:30 PDT ---
https://github.com/D-Programming-Language/dmd/commit/9c958300bf88d75489bdc542d0a0464d66274a15

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 18, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=6518



--- Comment #6 from dawg@dawgfoto.de 2012-03-18 10:15:00 PDT ---
*** Issue 7390 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------