Thread overview
[Issue 21460] implicit conversion between two unrelated enum is accepted
Dec 08, 2020
Basile-z
Dec 08, 2020
Basile-z
Dec 08, 2020
Basile-z
Dec 08, 2020
Basile-z
Jul 22, 2021
Walter Bright
Dec 17, 2022
Iain Buclaw
Feb 11, 2023
Basile-z
Feb 11, 2023
Nick Treleaven
December 08, 2020
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, 2020
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, 2020
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, 2020
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, 2021
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.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=21460

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--
February 11, 2023
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=18146

--
February 11, 2023
https://issues.dlang.org/show_bug.cgi?id=21460

Nick Treleaven <nick@geany.org> changed:

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

--- Comment #5 from Nick Treleaven <nick@geany.org> ---
> Good good;
> switch (good) // by promotion replaced by  'cast(int) good'

Why is an enumeration type promoted to int? Just because it can implicitly convert doesn't mean it should. The fact that the example with the enums that don't have a base type do not promote to int shows this is inconsistent.

--