Thread overview
[Issue 11051] Unmatched case in a final switch should throw in both release and non-release mode
Aug 19, 2014
Don
Aug 19, 2014
Orvid King
Aug 28, 2016
Andrej Mitrovic
Aug 28, 2016
Andrej Mitrovic
Jan 21, 2023
Nick Treleaven
Jan 21, 2023
Dlang Bot
Jan 24, 2023
Dlang Bot
Jan 29, 2023
Nick Treleaven
Jan 29, 2023
Nick Treleaven
August 19, 2014
https://issues.dlang.org/show_bug.cgi?id=11051

--- Comment #7 from Don <clugdbug@yahoo.com.au> ---
This is an error which should be caught by the type system at compile time, but the type system is broken for enums.

The problem is that the 'enum' keyword can mean either 'genuine enumeration' or 'collection of named constants with some unspecified relationship between them'.

A 'genuine enumeration' has an implicit contract that it only contains valid values. And 'final switch' relies on that contract. This is broken, because the type system doesn't make that promise. The run-time assert that it's a valid value is really just a hack.

Only a 'genuine enumeration' makes sense in a final switch. And arithmetic and logical operations don't make sense on genuine enumerations.

Interestingly, a whole-program lint tool could identify all enums which appear inside a 'final switch', and disallow all arithmetic on them. <g> Of course that's completely backwards. The semantics of the enum _should_ be deducable from the declaration of the enum.

I see no reason to regard violations of that implicit contract as somehow more important than other contracts. *Any* contract violation will result in incorrect runtime behaviour. The bottom line is that -release is a very dangerous flag. I don't think we should promote a false sense of security about it.

--
August 19, 2014
https://issues.dlang.org/show_bug.cgi?id=11051

--- Comment #8 from bearophile_hugs@eml.cc ---
(In reply to Don from comment #7)

> Only a 'genuine enumeration' makes sense in a final switch.

I'd like "final switch" to support (safely) code like this too:

void main(in string[] args) {
    // Today immutable keeps the value range.
    immutable n = args % 3;

    final switch(n) {
        case 0: break;
        case 1: break;
        case 2: break;
    }
}

--
August 19, 2014
https://issues.dlang.org/show_bug.cgi?id=11051

--- Comment #9 from Orvid King <blah38621@gmail.com> ---
This seems like another case where a check should stay present in release mode, but only if it's in @safe code.

--
August 28, 2016
https://issues.dlang.org/show_bug.cgi?id=11051

--- Comment #10 from Andrej Mitrovic <andrej.mitrovich@gmail.com> ---
(In reply to Orvid King from comment #9)
> This seems like another case where a check should stay present in release
> mode,
> but only if it's in @safe code.

I like this idea!

--
August 28, 2016
https://issues.dlang.org/show_bug.cgi?id=11051

Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|nobody@puremagic.com        |andrej.mitrovich@gmail.com

--- Comment #11 from Andrej Mitrovic <andrej.mitrovich@gmail.com> ---
https://github.com/dlang/dmd/pull/6095

--
January 21, 2023
https://issues.dlang.org/show_bug.cgi?id=11051

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=14643

--
January 21, 2023
https://issues.dlang.org/show_bug.cgi?id=11051

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #12 from Dlang Bot <dlang-bot@dlang.rocks> ---
@ntrel created dlang/dmd pull request #14841 "Fix issue 11051" fixing this issue:

- Fix issue 11051

  Keep a HALT instruction in a final switch statement if
  the function is @safe and -release mode is enabled.

https://github.com/dlang/dmd/pull/14841

--
January 24, 2023
https://issues.dlang.org/show_bug.cgi?id=11051

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #13 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #14841 "Fix issue 11051 - Unmatched case in a final switch should throw in both release and non-release mode" was merged into master:

- c30a823cdecd206070665950fa1bf0f8231b46d5 by Andrej Mitrovic:
  Fix issue 11051

  Keep a HALT instruction in a final switch statement if
  the function is @safe and -release mode is enabled.

https://github.com/dlang/dmd/pull/14841

--
January 29, 2023
https://issues.dlang.org/show_bug.cgi?id=11051

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |public@dicebot.lv

--- Comment #14 from Nick Treleaven <nick@geany.org> ---
*** Issue 14643 has been marked as a duplicate of this issue. ***

--
January 29, 2023
https://issues.dlang.org/show_bug.cgi?id=11051

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |safe
                 CC|                            |nick@geany.org

--- Comment #15 from Nick Treleaven <nick@geany.org> ---
This is fixed, but just to note: Issue 14643 shows without this, @safe can be broken due to flow analysis assuming at least one of the case statements is matched.

--