Bug 4274 – Better array of inner structs error message

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-06-04T18:20:00Z
Last change time
2015-06-09T05:11:46Z
Keywords
diagnostic
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-06-04T18:20:50Z
This wrong D2 program: void main() { struct Foo { void bar() {} } auto foos = new Foo[1]; // line 5 } At compile time dmd v2.046 prints: temp.d(5): Error: cannot have array of inner structs Foo But for D newbies coming from C or C++ can find that error message cryptic. Using a static struct solves this problem. So a possible error message can be: temp.d(5): Error: cannot have array of inner structs Foo, you can use a 'static struct' instead. This improved error message is not perfect, but it seems better. (I don't understand why D error messages don't have the ending full stop.)
Comment #1 by nfxjfg — 2010-06-05T04:33:03Z
I don't understand why arrays of inner structs shouldn't be possible. Is this just another seemingly random restriction with no real foundation of the dmd compiler?
Comment #2 by bearophile_hugs — 2010-06-05T04:50:08Z
Structs defined inside functions keep a hidden pointer field to the function they are into. If you return the array of nonstatic structs, the scope stops existing and this pointer points to garbage. To avoid this you need 'struct closures', but I prefer to avoid them.
Comment #3 by nfxjfg — 2010-06-05T05:04:20Z
So what? You _can_ have an array of closures. Why not an array of nested structs? The compiler should obviously allocate all variables referenced by the struct on the heap, just like it is done with closures. Otherwise, it's an half-implemented garbage feature that should be removed before it causes any more harm.
Comment #4 by nfxjfg — 2010-06-05T05:10:24Z
I just tested it, and it seems for nested structs, the compiler correctly allocates upvalues on the heap. The error message you're receiving doesn't make any sense. At least the reason for it must different.
Comment #5 by nfxjfg — 2010-06-08T04:28:26Z
Turning this into a "real" bug; maybe I'm wrong though.
Comment #6 by yebblies — 2011-07-01T04:52:59Z
Arrays of nested structs are now allowed (dmd 1.068 & dmd 2.053)
Comment #7 by github-bugzilla — 2014-05-12T20:40:56Z