D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11130 - Enum members are now all in scope when evaluating enum member initializers
Summary: Enum members are now all in scope when evaluating enum member initializers
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2013-09-27 06:34 UTC by Andrej Mitrovic
Modified: 2013-10-26 08:01 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2013-09-27 06:34:40 UTC
This may or may not have been an intended change:

-----
enum A = 1;

enum E
{
    A = A
}

void main()
{
}
-----

$ dmd test.d
test.d(7): Error: enum member test.E.A circular reference to enum member

The workaround is simple for module-scoped enums, use the [dot] expression to look up module-scoped symbols:

-----
enum A = 1;

enum E
{
    A = .A  // works
}

void main()
{
}
-----

However the following code does not have an easy workaround:

-----
void main()
{
    enum A = 1;

    enum E
    {
        // circular reference, cannot use ".A" here
        // because "A" is not in module scope
        A = A  
    }
}
-----

Above you would likely have to introduce an alias such as:

-----
void main()
{
    enum A = 1;
    alias thisA = A;
    enum E
    {
        A = thisA  // ok
    }
}
-----

If this regression was intended we're going to have to properly document it, both in the docs and in the upcoming changelog.

The regression was found in DGui, where some enum members have the same name as WinAPI constants, but were purposefully wrapped in the enum to make the constants typed:

enum EdgeType : uint
{
    RAISED_OUTER = BDR_RAISEDOUTER,
    RAISED_INNER = BDR_RAISEDINNER,

    SUNKEN_OUTER = BDR_SUNKENOUTER,
    SUNKEN_INNER = BDR_SUNKENINNER,

    BUMP        = EDGE_BUMP,
    ETCHED      = EDGE_ETCHED,
    EDGE_RAISED = EDGE_RAISED,  // issue here
    SUNKEN      = EDGE_SUNKEN,
}
Comment 1 Walter Bright 2013-09-27 11:56:20 UTC
This was an intended change.

Before, an enum member was not inserted into the symbol table until *after* its initialization expression was evaluated. But in order for forward references to work, this is no longer possible, now all the enum member symbols are visible inside the { }.

I am surprised this broke any code, I don't consider it a reasonable practice, but you're right that we need to document this behavior.

I'm going to reset this as a "spec" issue.
Comment 3 github-bugzilla 2013-10-18 14:24:56 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dlang.org

https://github.com/D-Programming-Language/dlang.org/commit/9d451c948483ea1425a835841e70bcc20e76c7f6
fix Issue 11130 - Enum members are now all in scope when evaluating enum member initializers

https://github.com/D-Programming-Language/dlang.org/commit/9ad3961dee2c6ea1710e3a1ba3391b993ab11985
Merge pull request #387 from WalterBright/fix11130

fix Issue 11130 - Enum members are now all in scope when evaluating enum...
Comment 4 github-bugzilla 2013-10-26 08:01:19 UTC
Commit pushed to 2.064 at https://github.com/D-Programming-Language/dlang.org

https://github.com/D-Programming-Language/dlang.org/commit/5572a0c432418a36fe3216fd076b84751628c97b
Merge pull request #387 from WalterBright/fix11130

fix Issue 11130 - Enum members are now all in scope when evaluating enum...