Bug 18578 – First enum value assigned 0 instead of EnumBaseType.init
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-03-08T05:01:08Z
Last change time
2023-09-20T14:15:42Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Jonathan Marler
Comments
Comment #0 by johnnymarler — 2018-03-08T05:01:08Z
The following example does not compile:
enum Foo { foo1 }
enum Bar : Foo { bar1 }
It fails with the message:
Error: cannot implicitly convert expression 0 of type int to Foo
We can fix it by changing the definition of Bar to this:
enum Bar : Foo { bar1 = Foo.init }
However, the spec (https://dlang.org/spec/enum.html) indicates that the value of `bar` should already have been `Foo.init`.
> 6. The value of an EnumMember is given by its AssignExpression. If there is no AssignExpression and it is the first EnumMember, its value is EnumBaseType.init.
It looks like the implementation is assigning the value `0` to the first enum value instead of EnumBaseType.init. Either the spec is wrong or this is a bug.
Comment #1 by razvan.nitu1305 — 2018-03-26T15:45:17Z
new TC:
---
enum E { e1 = 1 }
enum F: E { f1 }
static assert(F.init == 1);
---
> /tmp/temp_7F28124F66B0.d(4,1): Error: static assert: `cast(F)cast(E)0 == 1` is false
Comment #5 by nick — 2023-01-30T17:02:28Z
*** Issue 23492 has been marked as a duplicate of this issue. ***
Comment #6 by dlang-bot — 2023-09-09T14:23:13Z
@ntrel created dlang/dlang.org pull request #3694 "First enum member is converted from 0 to base type" fixing this issue:
- First enum member is converted from 0 to base type
Fix Issue 18578 - First enum value assigned 0 instead of EnumBaseType.init.
https://github.com/dlang/dlang.org/pull/3694
Comment #7 by dlang-bot — 2023-09-20T14:15:42Z
dlang/dlang.org pull request #3694 "[spec] First enum member is converted from 0 to base type" was merged into master:
- e1cc573c6aabe8619aa94a52f7886d4f4072cc8a by Nick Treleaven:
First enum member is converted from 0 to base type
Fix Issue 18578 - First enum value assigned 0 instead of EnumBaseType.init.
https://github.com/dlang/dlang.org/pull/3694