D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6060 - Refuse wrong final switch
Summary: Refuse wrong final switch
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: bootcamp, diagnostic
Depends on:
Blocks:
 
Reported: 2011-05-26 15:56 UTC by bearophile_hugs
Modified: 2024-12-13 17:55 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 bearophile_hugs 2011-05-26 15:56:47 UTC
This program compiles and runs with no errors:


void main(string[] args) {
    final switch (args.length) {
        case 0: break;
    }
}


But in such cases I suggest the compiler to statically refuse this code, and give an error.

See also bug 5713 and bug 5714

Note this bug is different from 5713 because in 5713 I don't like an error message (and I'd like the compiler to enforce the presence of the cases for 0,1, and 2), while in this case I'd like an error message.
Comment 1 Denis Shelomovskii 2012-01-24 04:22:51 UTC
With
https://github.com/D-Programming-Language/dmd/commit/11738ba260ced4d522d2334c5e99059a2517035d
the program throws appropriate exception. And the documentation doesn't claim the program invalid. So it's an enhancement.
Comment 2 yebblies 2012-01-31 21:30:04 UTC
Could you please clarify what this is asking for that issue 5713 doesn't cover?
Comment 3 bearophile_hugs 2012-02-05 06:50:27 UTC
(In reply to comment #2)
> Could you please clarify what this is asking for that issue 5713 doesn't cover?

For this program:


void main(string[] args) {
    final switch (args.length) {
        case 0: break;
    }
}


I'd like a compile-time error, like:
"Final switch on ints is not allowed."

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

For this program:


void main() {
    uint x = 100;
    final switch (x % 3) {
        case 0: break;
        case 1: break;
    }
}


I'd like a compile-time error like:
"Some cases (2) are not covered by the final switch."

So this program is accepted with no errors:

void main() {
    uint x = 100;
    final switch (x % 3) {
        case 0: break;
        case 1: break;
        case 2: break;
    }
}

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

I am asking for two different error messages, so I have opened two reports.
Comment 4 bearophile_hugs 2012-02-05 06:51:23 UTC
(In reply to comment #1)

> And the documentation doesn't claim the program invalid.

It's a broken design.
Comment 5 dlangBugzillaToGithub 2024-12-13 17:55:19 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

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

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