www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7835] New: Ignored break inside static foreach

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835

           Summary: Ignored break inside static foreach
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This D2 program compiles with no warnings or errors with DMD 2.059beta:


import core.stdc.stdio: printf;
template TypeTuple(TList...) {
    alias TList TypeTuple;
}
void main() {
    char c = 'b';
    switch (c) {
        case 'a': printf("1 a\n"); break;
        foreach (o; TypeTuple!('b', 'c')) {
            case o: printf("2 %c\n", c); break;
        }
        default: printf("default");
    }
}



But it runs in a wrong way, as you see:

...>dmd -run test.d
2 b
default

...>dmd -w -run test.d
test.d(12): Error: switch case fallthrough - use 'goto default;' if intended


So the break inside the static foreach is ignored.

(This idiom of using a static foreach inside a switch is handy to generate
switch cases.)



Note: adding a second break, like this, doesn't improve the situation:

import core.stdc.stdio: printf;
template TypeTuple(TList...) {
    alias TList TypeTuple;
}
void main() {
    char c = 'b';
    switch (c) {
        case 'a': printf("1 a\n"); break;
        foreach (o; TypeTuple!('b', 'c')) {
            case o: printf("2 %c\n", c); break; break;
        }
        default: printf("default");
    }
}


test.d(10): Warning: statement is not reachable
test.d(10): Warning: statement is not reachable
test.d(12): Error: switch case fallthrough - use 'goto default;' if intended

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 05 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835


Dmitry Olshansky <dmitry.olsh gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmitry.olsh gmail.com



07:15:55 PDT ---

 This D2 program compiles with no warnings or errors with DMD 2.059beta:
 
 
 import core.stdc.stdio: printf;
 template TypeTuple(TList...) {
     alias TList TypeTuple;
 }
 void main() {
     char c = 'b';
L_MySwitch:
     switch (c) {
         case 'a': printf("1 a\n"); break;
         foreach (o; TypeTuple!('b', 'c')) {
             case o: printf("2 %c\n", c); break L_MySwitch;
         }
         default: printf("default");
     }
 }
 
 
For these cases I recommend to use labeled breaks so that it's more clear for humans and compiler alike.
 
 But it runs in a wrong way, as you see:
 
 ...>dmd -run test.d
 2 b
 default
 
 ...>dmd -w -run test.d
 test.d(12): Error: switch case fallthrough - use 'goto default;' if intended
 
 
 So the break inside the static foreach is ignored.
 
 (This idiom of using a static foreach inside a switch is handy to generate
 switch cases.)
 
 
 
 Note: adding a second break, like this, doesn't improve the situation:
 
 import core.stdc.stdio: printf;
 template TypeTuple(TList...) {
     alias TList TypeTuple;
 }
 void main() {
     char c = 'b';
     switch (c) {
         case 'a': printf("1 a\n"); break;
         foreach (o; TypeTuple!('b', 'c')) {
             case o: printf("2 %c\n", c); break; break;
         }
         default: printf("default");
     }
 }
It can't help because the second break is by definition unreachable. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |timon.gehr gmx.ch
         Resolution|                            |INVALID



Not a bug. break applies to the innermost statement that can be broken out
from. This includes foreach.

(I use this idiom often. Use labeled break to break from the switch.)

Please reopen as enhancement if you think the code should be illegal.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835





 Not a bug. break applies to the innermost statement that can be broken out
 from. This includes foreach.
 
 (I use this idiom often. Use labeled break to break from the switch.)
You are right, thank you. (My error was to think that "static foreach" doesn't support break.) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |diagnostic
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |
            Summary|Ignored break inside static |switch case fallthrough
                   |foreach                     |error despite a break
                   |                            |inside static foreach



Reopened, because you have missed the error message in my bug report.

Using a labeled break:


import core.stdc.stdio: printf;
template TypeTuple(TList...) {
    alias TList TypeTuple;
}
void main() {
    char c = 'b';
    MySwitch: switch (c) {
        case 'a': printf("1 a\n"); break;
        foreach (o; TypeTuple!('b', 'c')) {
            case o: printf("2 %c\n", c); break MySwitch;
        }
        default: printf("default");
    }
}


DMD 2.059 beta gives (compiling with -w):

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

I have also changed the issue title to better reflect the problem, now the
Keywords is 'diagnostic' because it's giving a warning where there is nothing
to warn against.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835




This compiles with no warnings and it seems to work correctly, but I don't
fully understand it:


import core.stdc.stdio: printf;
template TypeTuple(TList...) {
    alias TList TypeTuple;
}
void main() {
    char c = 'b';
    MySwitch: switch (c) {
        case 'a': printf("1 a\n"); break;
        foreach (o; TypeTuple!('b', 'c')) {
            case o: printf("2 %c\n", c); break;
        }
        break;
        default: printf("default");
    }
}


Is it correct? if the break inside here is meant to be the foreach break:
{ case o: printf("2 %c\n", c); break; }

Then why a single break is enough after:

        foreach (o; TypeTuple!('b', 'c')) {
            case o: printf("2 %c\n", c); break;
        }
        break;

despite the foreach synthesizes more than one switch case?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835




11:37:01 PDT ---

 This compiles with no warnings and it seems to work correctly, but I don't
 fully understand it:
 
 
 import core.stdc.stdio: printf;
 template TypeTuple(TList...) {
     alias TList TypeTuple;
 }
 void main() {
     char c = 'b';
     MySwitch: switch (c) {
         case 'a': printf("1 a\n"); break;
         foreach (o; TypeTuple!('b', 'c')) {
             case o: printf("2 %c\n", c); break;
         }
         break;
         default: printf("default");
     }
 }
 
 
 Is it correct? if the break inside here is meant to be the foreach break:
Yes.
 { case o: printf("2 %c\n", c); break; }
No it's {case 0: printf("2 %c\n", c); } the break did his job already, it can't work twice.
 
 Then why a single break is enough after:
 
         foreach (o; TypeTuple!('b', 'c')) {
             case o: printf("2 %c\n", c); break;
         }
Then the code below gives you one break after that statement.
         break;
 
 despite the foreach synthesizes more than one switch case?
foreach synthesizes exactly one statement here. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835





 This compiles with no warnings and it seems to work correctly, but I don't
 fully understand it:
 
 
 import core.stdc.stdio: printf;
 template TypeTuple(TList...) {
     alias TList TypeTuple;
 }
 void main() {
     char c = 'b';
     MySwitch: switch (c) {
         case 'a': printf("1 a\n"); break;
         foreach (o; TypeTuple!('b', 'c')) {
             case o: printf("2 %c\n", c); break;
         }
         break;
         default: printf("default");
     }
 }
 
 
 Is it correct? if the break inside here is meant to be the foreach break:
 { case o: printf("2 %c\n", c); break; }
 
 Then why a single break is enough after:
 
         foreach (o; TypeTuple!('b', 'c')) {
             case o: printf("2 %c\n", c); break;
         }
         break;
 
 despite the foreach synthesizes more than one switch case?
Your code is expanded to: void main() { char c = 'b'; switch (c) { case 'a': printf("1 a\n"); break; {case 'b': printf("2 %c\n", c); goto break_foreach;} {case 'c': printf("2 %c\n", c); goto break_foreach;} break_foreach: break; default: printf("default"); } } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |INVALID





 Your code is expanded to:
 
 void main() {
     char c = 'b';
     switch (c) {
         case 'a': printf("1 a\n"); break;
         {case 'b': printf("2 %c\n", c); goto break_foreach;}
         {case 'c': printf("2 %c\n", c); goto break_foreach;}
         break_foreach: break;
         default: printf("default");
     }
 }
Thank you again Timon :-) So there is no bug here. This was not easy to understand for me. (Maybe D newbies will enjoy to read an example of this in some D tips&trickls somewhere, or maybe it was just a conceptualization problem of mine.) Issue closed again, as invalid. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835


Brad Roberts <braddr puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |braddr puremagic.com
         Resolution|INVALID                     |



---
I think that the expansion of the static foreach is wrong.  It explains the
behavior, but doesn't excuse it.

I think the bug report is valid.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 06 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7835





 I think that the expansion of the static foreach is wrong.  It explains the
 behavior, but doesn't excuse it.
 
 I think the bug report is valid.
What would be your expected behavior? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 06 2012