[Issue 7835] New: Ignored break inside static foreach

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Apr 5 17:39:35 PDT 2012


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 at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2012-04-05 17:40:12 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';
    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: -------


More information about the Digitalmars-d-bugs mailing list