Thread overview
[Issue 21460] implicit conversion between two unrelated enum is accepted
Dec 08
Basile-z
Dec 08
Basile-z
Dec 08
Basile-z
Dec 08
Basile-z
December 08
https://issues.dlang.org/show_bug.cgi?id=21460

--- Comment #1 from Basile-z <b2.temp@gmx.com> ---
This is caused by the automatic integer promotion on the condition.
It is done before evaluation of the cases so it "mind-fuck" the type checks
that are correct when no promotion is involved:

---
enum Good  { a = 1 }
enum Bad   { a = 2 }

void main()
{
    Good good;
    switch (good) // no promotion
    {
    case Bad.a : break; // error as expected
    default:
    }
}
---

VS

---
enum Good : ubyte { a = 1 }
enum Bad  : ubyte { a = 2 }

void main()
{
    Good good;
    switch (good) // by promotion replaced by  'cast(int) good'
    {
    case Bad.a : break; // OK because convert to int
    default:
    }
}
---

--
December 08
https://issues.dlang.org/show_bug.cgi?id=21460

Basile-z <b2.temp@gmx.com> changed:

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

--
December 08
https://issues.dlang.org/show_bug.cgi?id=21460

--- Comment #2 from Basile-z <b2.temp@gmx.com> ---
might be fixed by a revival of https://github.com/dlang/dmd/pull/10603.
Not sure, it can be only be related, e.g another side effect of the premature
promotion of the condition.

--
December 08
https://issues.dlang.org/show_bug.cgi?id=21460

--- Comment #3 from Basile-z <b2.temp@gmx.com> ---
The promotion of a switch condition should be done after checking the cases. Then a second pass should be done on the CaseStatement expressions to implicitly cast them to the, eventually promoted, condition. Which should never be a problem.

This is specifically and **definitively** a front-end bug on the SwitchStatement as for example

---
enum Good : ubyte { a = 1 }
enum Bad  : ubyte { a = 2 }

void main(string[] args)
{
    Good good;
    if (good == Bad.a){}
}
---

is rejected. As the cases of a switch should be.

--
July 22
https://issues.dlang.org/show_bug.cgi?id=21460

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|accepts-invalid             |
                 CC|                            |bugzilla@digitalmars.com
           Severity|major                       |enhancement

--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> ---
It's not a bug. It is working as designed. Changing it would be an enhancement request. Changing the Importance to Enhancement.

--