Bug 10555 – enumerator can no longer increment beyond maximum of initializer
Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-06T06:03:00Z
Last change time
2013-10-01T23:13:04Z
Assigned to
nobody
Creator
r.sagitario
Comments
Comment #0 by r.sagitario — 2013-07-06T06:03:46Z
I'm not sure if the new behaviour in git HEAD is desired or not, but this code used to compile until one or two weeks ago:
enum A
{
A0
}
enum B
{
B0 = A.A0,
B1
}
Now it errors out with
Error: enum test.B overflow of enum value cast(B)cast(A)0
because dmd does not want to increment A0.
More strange things:
////////////////////////
enum A
{
A0
}
enum B
{
B0 = A.A0,
B1 = A.A0 + 1
}
Error: cannot implicitly convert expression (1) of type int to A
though this works
enum A
{
A0
}
enum B
{
B0 = A.A0 + 0,
B1
}
Comment #1 by andrej.mitrovich — 2013-07-10T08:16:59Z
Well if anything, the recent enum fixes and subsequent regressions exposed that we have a really poor enum test-suite.
Comment #2 by hsteoh — 2013-08-30T11:02:19Z
git bisect shows that the offending commit was 88ebe192d605bd8d4b5768e8a2500f54d73fb5fd - fix issue 3096 - EnumBaseType
Comment #3 by henning — 2013-08-31T04:16:03Z
I think this behaviour is correct:
Spec:
----
If the EnumBaseType is not explicitly set, and the first EnumMember has an initializer, it is set to the type of that initializer. Otherwise, it defaults to type int.
Named enum members may not have individual Types.
A named enum member can be implicitly cast to its EnumBaseType, but EnumBaseType types cannot be implicitly cast to an enum type.
----
The behaviour before issue 3096 always used int as EnumBaseType even though there is a first initializer.
enum A // int
{
A0
}
enum B // A
{
B0 = A.A0,
B1
}
In this case the base type of B is A and A does not have a member whith a value of 1.
enum A // int
{
A0
}
enum B // A
{
B0 = A.A0,
B1 = A.A0 + 1
}
This is basically the same case as above.
enum A // int
{
A0
}
enum B // int
{
B0 = A.A0 + 0,
B1
}
This works because the type of the first initializer is int. So the base type of B becomes int and ints can be incremented easily.
Comment #4 by henning — 2013-08-31T04:23:52Z
Keep in mind that you can always cast to the base type:
enum A // int
{
A0
}
template BaseType(E)
{
static if (is(E e == enum))
{
alias BaseType = e;
}
else
{
static assert("not an enum");
}
}
enum B // int
{
B0 = cast(BaseType!A)A.A0,
B1
}
Comment #5 by k.hara.pg — 2013-10-01T23:13:04Z
I agree with Henning Pohl. The behavior in 2.063/earlier was an accepts-invalid bug, and it has been correctly fixed in git-head.