Bug 1111 – enum value referred to by another value of same enum is considered as enum's base type, not enum type
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-04-08T11:43:00Z
Last change time
2014-02-16T15:26:20Z
Keywords
rejects-valid
Assigned to
bugzilla
Creator
matti.niemenmaa+dbugzilla
Comments
Comment #0 by matti.niemenmaa+dbugzilla — 2007-04-08T11:43:37Z
A strange beetle, this, and difficult to summarize. Example code:
enum Enum : byte {
NORMAL_VALUE = 0,
REFERRING_VALUE = NORMAL_VALUE + 1,
OTHER_NORMAL_VALUE = 2
}
void test(Enum e) {
}
void main() {
test(Enum.NORMAL_VALUE);
test(Enum.REFERRING_VALUE);
test(Enum.OTHER_NORMAL_VALUE);
}
The call on line 11, passing Enum.NORMAL_VALUE, fails unless the definition of REFERRING_VALUE is changed to not refer to NORMAL_VALUE. Alternatively, set REFERRING_VALUE just equal to NORMAL_VALUE (remove the "+ 1"). The error:
asdf.d(11): function asdf.test (Enum) does not match parameter types (byte)
asdf.d(11): Error: cannot implicitly convert expression (0) of type byte to Enum
Fortunately, this can be worked around by referring to Enum.NORMAL_VALUE, as opposed to just NORMAL_VALUE, in the definition of REFERRING_VALUE.
This might have something to do with Issue 633: when NORMAL_VALUE is used in an expression, it is cast to Enum's base type, and left that way? This doesn't explain why using the fully qualified name of NORMAL_VALUE works, though.
Comment #1 by matti.niemenmaa+dbugzilla — 2008-01-01T11:22:06Z