D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15731 - Analysis error on explicit case fall-through
Summary: Analysis error on explicit case fall-through
Status: REOPENED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-27 13:10 UTC by Johan Engelen
Modified: 2024-12-13 18:46 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Johan Engelen 2016-02-27 13:10:38 UTC
The following code compiles fine without explicit fall-through, but with -version=bad it errors.

int hexStringConstant(dchar* p)
{
    while (1)
    {
        dchar c = *p++;
        switch (c)
        {
        case ' ':
        case '\t':
        case '\v':
        case '\f':
            continue;
        case '\r':
            if (*p == '\n')
                continue;
            version(bad)      /// <-- Look here
              goto case;
        case '\n':
            continue;
        case 0:
        case 0x1A:
            return 111;
        case '"':
            return 222;
        default:
            break;
        }
    }
}

> dmd -c bug.d
[ no problem ]

> dmd -c bug.d -w
bug.d(18): Warning: switch case fallthrough - use 'goto case;' if intended

> dmd -c bug.d -version=bad
bug.d(1): Error: function bug.hexStringConstant no return exp; or assert(0); at end of function
Comment 1 Johan Engelen 2016-02-27 13:59:11 UTC
DMD version = 2.070.0
Comment 2 coolfool4 2018-10-28 08:09:55 UTC
I can't get errors nor warnings for fallthroughs except with final switches on enums (-w or -wi don't help).

Compiler versions:
- DMD64 D Compiler v2.082.0
- nightly at https://run.dlang.io/


import std.stdio;

void main() {
    int x = 0;
    switch (x) {
        case 0:
        case 1:
            writeln("woot");
            return;
        default:
    }
}
Comment 3 coolfool4 2018-10-28 08:25:35 UTC
Actually I don't get fallthrough warnings for *any* switches.
Comment 4 coolfool4 2018-10-28 08:28:38 UTC
So it seems to accept *empty* cases to fall through. I guess this is intended, then?
Comment 5 RazvanN 2022-11-03 10:09:28 UTC
When compiling the initial example I get:

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


This seems to have been fixed
Comment 6 Dennis 2022-11-03 10:22:18 UTC
(In reply to RazvanN from comment #5)
> When compiling the initial example I get:
> 
> test.d(18): Error: switch case fallthrough - use 'goto case;' if intended
> 
> 
> This seems to have been fixed

The problem is that with -version=bad, it incorrectly raises an error about missing a return statement or assert(0). That's still the case.

Here's a reduced example:
```
// accepts valid
int a()
{
    while (1)
    {
        switch (0)
        {
            case 0: // < implicit fallthrough
            default: continue;
        }
    }
}

// rejects valid
int b()
{
    while (1)
    {
        switch (0)
        {
            case 0: goto default; // < explicit fallthrough
            default: continue;
        }
    }
}
```
Comment 7 dlangBugzillaToGithub 2024-12-13 18:46:59 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19101

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB