Bug 7358 – `final switch` over enum should add throwing default in debug mode at least
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-24T03:52:00Z
Last change time
2012-10-30T08:15:24Z
Keywords
pull, wrong-code
Assigned to
yebblies
Creator
verylonglogin.reg
Comments
Comment #0 by verylonglogin.reg — 2012-01-24T03:52:00Z
Inspired by bug 5713, comment 5
Even if every enum member is present a default statement should be added at least in debug mode to catch such situation if enum members doesn't cover every possible value:
---
enum E { a = 1, b }
void f(E e) {
final switch (e) {
case E.a:
case E.b:
}
}
void g() {
f(E.a | E.b); // like `f(cast(E)3);`, should throw
}
---
As was written in bug 5713 description:
>The purpose of "final switch" is to increase code safety compared to normal switches on enums.
So it's really bad that it works silently incorrect. Personally I have used `final switch` carefully and thought about it's behavior in such situation since it has been introduced because it is undocumented. So the documentation should be fixed too.
Comment #2 by bearophile_hugs — 2012-02-05T06:14:36Z
(In reply to comment #1)
> https://github.com/D-Programming-Language/dmd/pull/673
I don't like it. There are better solutions, like:
is(typeof(E.a | E.b) == int)
This means f(E.a | E.b) becomes a compile-time error, a type mismatch.
Comment #3 by yebblies — 2012-02-05T06:25:34Z
(In reply to comment #2)
> (In reply to comment #1)
> > https://github.com/D-Programming-Language/dmd/pull/673
>
> I don't like it. There are better solutions, like:
> is(typeof(E.a | E.b) == int)
>
> This means f(E.a | E.b) becomes a compile-time error, a type mismatch.
I don't see what that has to do with this bug report. Do you really want a final switch on an enum type to silently do nothing if an value that is not a member of the enum is passed to it?
Comment #4 by github-bugzilla — 2012-10-30T07:42:24Z