www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6518] New: break inside a static foreach inside a switch

reply d-bugmail puremagic.com writes:
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



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: -------
Aug 17 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6518





 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: -------
Aug 17 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6518






 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: -------
Aug 17 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6518




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: -------
Aug 21 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Sep 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Sep 17 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6518




*** 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: -------
Mar 18 2012