D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8888 - enums with initializers inside functions cause linker error
Summary: enums with initializers inside functions cause linker error
Status: RESOLVED DUPLICATE of issue 6057
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks: 8143
  Show dependency treegraph
 
Reported: 2012-10-24 18:49 UTC by bearophile_hugs
Modified: 2013-01-10 07:37 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-10-24 18:49:12 UTC
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 hsteoh 2012-10-27 12:34:50 UTC
Confirmed on Linux 64-bit, git HEAD. Moving the enum outside main() works correctly.
Comment 2 hsteoh 2012-10-27 12:41:02 UTC
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 hsteoh 2012-10-27 17:02:45 UTC
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 hsteoh 2012-10-27 17:05:06 UTC
Another data point: changing 'auto' to 'char[]' makes it work. Specifying 'Code[]' makes it fail.
Comment 5 Andrej Mitrovic 2012-11-19 02:05:59 UTC
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 Andrej Mitrovic 2013-01-10 07:37:02 UTC
The pull for Issue 6057 fixes this.

*** This issue has been marked as a duplicate of issue 6057 ***