Bug 8888 – enums with initializers inside functions cause linker error

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2012-10-24T18:49:00Z
Last change time
2013-01-10T07:37:02Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs
Blocks
8143

Comments

Comment #0 by bearophile_hugs — 2012-10-24T18:49:12Z
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 -------------------
Comment #1 by hsteoh — 2012-10-27T12:34:50Z
Confirmed on Linux 64-bit, git HEAD. Moving the enum outside main() works correctly.
Comment #2 by hsteoh — 2012-10-27T12:41:02Z
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.
Comment #3 by hsteoh — 2012-10-27T17:02:45Z
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.
Comment #4 by hsteoh — 2012-10-27T17:05:06Z
Another data point: changing 'auto' to 'char[]' makes it work. Specifying 'Code[]' makes it fail.
Comment #5 by andrej.mitrovich — 2012-11-19T02:05:59Z
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 { // ... }
Comment #6 by andrej.mitrovich — 2013-01-10T07:37:02Z
The pull for Issue 6057 fixes this. *** This issue has been marked as a duplicate of issue 6057 ***