public enum GTokenType
{
NONE,
}
public enum GtkRcTokenType
{
INVALID = GTokenType.NONE,
INCLUDE,
}
causes:
test.d(9): Error: enum member test.GtkRcTokenType.INCLUDE initialization with (cast(GtkRcTokenType)cast(GTokenType)0 + 1) causes overflow
Comment #1 by maxim — 2013-09-22T10:54:27Z
Citing 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."
Since second enum is not explicitly based on int and has first enum initializer, that's why type of second enum is deduced to be GTokenType and overflow occures as the first enum has single member. So, according to current spec this is RESOLVED-INVALID.
On the other hand, nothing stops from allowing such code for facilitation purposes by fixing spec.
Comment #2 by mike — 2013-09-22T13:11:20Z
That would be a valid explanation only the following compiles successfully:
public enum GTokenType
{
EOF = 0,
LEFT_PAREN = '(',
RIGHT_PAREN = ')',
LEFT_CURLY = '{',
RIGHT_CURLY = '}',
LEFT_BRACE = '[',
RIGHT_BRACE = ']',
EQUAL_SIGN = '=',
COMMA = ',',
NONE = 110,
ERROR,
CHAR,
BINARY,
OCTAL,
INT,
HEX,
FLOAT,
STRING,
SYMBOL,
IDENTIFIER,
IDENTIFIER_NULL,
COMMENT_SINGLE,
COMMENT_MULTI,
LAST
}
public enum GtkRcTokenType
{
INVALID = GTokenType.LAST,
INCLUDE,
}
If the value of NONE is larger than 110 it fails to compile.
Comment #3 by bugzilla — 2013-09-22T17:20:11Z
It should fail to compile with NONE larger than 110.
The algorithm is if GTokenType.LAST==GTokenType.max, then attempting to calculate GTokenType.LAST+1 overflows it. To get what you are looking for,
use:
enum GtkRcTokenType
{
INVALID = GTokenType.LAST + 1,
INCLUDE,
}
which will compile successfully.
Comment #4 by mike — 2013-09-23T10:02:39Z
(In reply to comment #3)
> It should fail to compile with NONE larger than 110.
>
> The algorithm is if GTokenType.LAST==GTokenType.max, then attempting to
> calculate GTokenType.LAST+1 overflows it.
110 succeeds 111 fails in both cases GTokenType.LAST==GTokenType.max, both cases should either fail or succeed.
Also note that when part of gtkD the enum overflows when NONE is 87, as far as i can tell this depends on the size of the GtkRcTokenType enum.
This seems a bit inconsistent.
> To get what you are looking for, use:
>
> enum GtkRcTokenType
> {
> INVALID = GTokenType.LAST + 1,
> INCLUDE,
> }
>
> which will compile successfully.
I'm casting GTokenType.LAST to int because the values of the enum need to correspond with there C counter part.