Bug 7719 – enum forward reference error when enum is in braces

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-03-16T16:06:00Z
Last change time
2013-04-03T10:30:56Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2012-03-16T16:06:18Z
enum foo = bar; enum { bar = 1 } void main() { } test.d(3): Error: undefined identifier bar Yet this works ok: enum foo = bar; enum bar = 1; void main() { }
Comment #1 by andrej.mitrovich — 2012-03-16T17:13:03Z
There are more issues with these enums inside of brackets. Right now I'm having the weirdest errors where referencing these enums doesn't work from a class defined below it: enum { wxUSER_ATTENTION_INFO = 1, wxUSER_ATTENTION_ERROR = 2, } If they're each defined separately: enum wxUSER_ATTENTION_INFO = 1; enum wxUSER_ATTENTION_ERROR = 2; then it works. I don't have a minimal test-case for this now (working on it), but there's obviously some issues with the implementation.
Comment #2 by andrej.mitrovich — 2012-10-04T08:25:05Z
I've found some lead: in enum.c: void EnumDeclaration::semantic0(Scope *sc) { /* This function is a hack to get around a significant problem. * The members of anonymous enums, like: * enum { A, B, C } * don't get installed into the symbol table until after they are * semantically analyzed, yet they're supposed to go into the enclosing * scope's table. Hence, when forward referenced, they come out as * 'undefined'. The real fix is to add them in at addSymbol() time. * But to get code to compile, we'll just do this quick hack at the moment * to compile it if it doesn't depend on anything else. */ if (isdone || !scope) return; if (!isAnonymous() || memtype) return; for (size_t i = 0; i < members->dim; i++) { EnumMember *em = (*members)[i]->isEnumMember(); if (em && (em->type || em->value)) return; } // Can do it semantic(sc); } If I remove the for loop and let semantic do its work the OP code compiles. But I don't know the extent of this hack that's in place now, whether it's still necessary or not.
Comment #3 by k.hara.pg — 2013-04-01T05:44:52Z
Comment #4 by github-bugzilla — 2013-04-03T10:28:49Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/b7b239cf8d37a9bdcf720c6e359d5937eeaa7ee7 fix Issue 7719 - enum forward reference error when enum is in braces https://github.com/D-Programming-Language/dmd/commit/33ca607a13d799a7fdc96d3be9f10048246863a3 Merge pull request #1821 from 9rnsr/fix9845 Issue 7719,9845,9846 - fix anonymous enum and forward reference problems