If I try to define the char enum inside the function I receive error messages from the compiler and the linker (DMD 2.061alpha): ------------------- void main() { enum Code : char { A='A', B='B', C='C' } auto arr = [Code.A, Code.B]; } OPTLINK (R) for Win32 Release 8.00.12 Copyright (C) Digital Mars 1989-2010 All rights reserved. http://www.digitalmars.com/ctg/optlink.html test.obj(test) Error 42: Symbol Undefined _Dmain4Code6__initZ ------------------- void main() { enum Code : char { A='A', B='B', C='C' } auto arr = [Code.A, Code.B]; } test.d(2): Error: no identifier for declarator Code test.d(2): Error: semicolon expected, not ':' test.d(2): Error: found ':' instead of statement test.d(4): Error: unrecognized declaration -------------------
Confirmed on Linux 64-bit, git HEAD. Moving the enum outside main() works correctly.
This bug seems to happen only if you assign specific values to the enum. This works: void main() { enum Code : char { A, B, C } auto arr = [Code.A, Code.B]; } But this doesn't: void main() { enum Code { A=1, B=2, C=2 } auto arr = [Code.A, Code.B]; } So the bug isn't specific to char enums, it's just the presence of initializers that trigger it.
This code works correctly on GDC (gdc-4.7 git branch, which I believe is 2.059 based), so this seems to be a backend bug.
Another data point: changing 'auto' to 'char[]' makes it work. Specifying 'Code[]' makes it fail.
Related bug: unittest { enum En8143 : int { A = 10, B = 20, C = 30, D = 20 } enum En8143[][] m3 = to!(En8143[][])([[10, 30], [30, 10]]); static assert(m3 == [[En8143.A, En8143.C], [En8143.C, En8143.A]]); } This breaks both the win32 and posix linkers. The following fixes the win32 linker, but it doesn't fix the posix linker: version(unittest) { enum En8143 : int { A = 10, B = 20, C = 30, D = 20 } enum En8143[][] m3 = to!(En8143[][])([[10, 30], [30, 10]]); static assert(m3 == [[En8143.A, En8143.C], [En8143.C, En8143.A]]); } unittest { // ... }
The pull for Issue 6057 fixes this. *** This issue has been marked as a duplicate of issue 6057 ***