Thread overview
[Issue 5862] case statement allows runtime values
Aug 18, 2015
Sobirari Muhomori
Aug 27, 2016
Andrej Mitrovic
Apr 27, 2023
Nick Treleaven
August 18, 2015
https://issues.dlang.org/show_bug.cgi?id=5862

Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

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

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |INVALID

--- Comment #7 from Andrej Mitrovic <andrej.mitrovich@gmail.com> ---
As mentioned in the pull request the proposal to disallow this was rejected.

I would like to remind about this:

-----
import std.stdio;

void main()
{
    int first = 1;
    int second = 1;

    switch (1)
    {
        case first:
        {
            writeln("first");
            break;
        }

        case second:
        {
            writeln("second");
            break;
        }

        default:
    }
}
-----

The lexically first matching case will match. That's ok, but it should probably be documented if it's not (this style of code should be rare in the wild in any case).

--
April 27, 2023
https://issues.dlang.org/show_bug.cgi?id=5862

Nick Treleaven <nick@geany.org> changed:

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

--- Comment #8 from Nick Treleaven <nick@geany.org> ---
(In reply to Andrej Mitrovic from comment #0)
>        case y:  // <- should not be allowed

comment #4:
> Strangely, 1.068 (Win32) correctly rejects the code.

Also rejected on Linux now:

switchmut.d(14): Error: `case` variables have to be `const` or `immutable`

--