3198 – wrong initializer for structs arrays

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3198 - wrong initializer for structs arrays
Summary: wrong initializer for structs arrays
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 All
: P2 major
Assignee: No Owner
URL:
Keywords: patch, wrong-code
Depends on:
Blocks:
Reported: 2009年07月21日 10:20 UTC by nfxjfg
Modified: 2014年04月18日 09:12 UTC (History)
2 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description nfxjfg 2009年07月21日 10:20:14 UTC
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 Don 2009年12月30日 14:35:46 UTC
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 Don 2009年12月30日 14:56:32 UTC
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 Don 2011年01月21日 13:42:39 UTC
The patch for bug 1914 fixes this.


AltStyle によって変換されたページ (->オリジナル) /