Bug 3198 – wrong initializer for structs arrays

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2009-07-21T10:20:00Z
Last change time
2014-04-18T09:12:03Z
Keywords
patch, wrong-code
Assigned to
nobody
Creator
nfxjfg

Comments

Comment #0 by nfxjfg — 2009-07-21T10:20:14Z
The following example compiles, but the output is wrong. It seems the initializer for a struct array is ignored; instead, the default initializer of the struct is used for the array. import std.stdio; import std.string; struct T { int g = 1; string toString() { return format("%s", g); } } class Foo { int[5] x = 4; //this doesn't work //basically, the "=T(4)" part is ignored T[5] y = T(4); } void main() { auto f = new Foo(); //both lines should output the same //but it shows f.y is [1,1,1,1,1] writefln("f.x = %s", f.x); writefln("f.y = %s", f.y); }
Comment #1 by clugdbug — 2009-12-30T14:35:46Z
The bug clearly lies in todt.c, inside dt_t **TypeSArray::toDtElem(dt_t **pdt, Expression *e). If it's an array of structs, (ie, tbn->ty == Tstruct), then the 'e' value is completely ignored!! This is certainly wrong. Changing the two places where the check is: if (tbn->ty == Tstruct) into: if (tbn != e->type && tbn->ty == Tstruct) allows the test case to pass. But that may not be the correct criterion to use.
Comment #2 by clugdbug — 2009-12-30T14:56:32Z
Or else, create a new version of TypeStruct::toDtElem, which doesn't have the special-case for structs members, and call it from StructDeclaration::toDt() and ClassDeclaration::toDt2()
Comment #3 by clugdbug — 2011-01-21T13:42:39Z
The patch for bug 1914 fixes this.
Comment #4 by clugdbug — 2011-02-06T13:48:37Z