[Issue 9084] New: Structs assignment and associative arrays
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Nov 27 10:52:36 PST 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9084
Summary: Structs assignment and associative arrays
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: maxim at maxim-fomin.ru
--- Comment #0 from Maxim Fomin <maxim at maxim-fomin.ru> 2012-11-27 10:52:35 PST ---
Assigning structs to AA arrays causes bugs due to incomplete object
construction:
import std.stdio;
struct S
{
int i = 42;
struct SS
{
int ii = 41;
this(this) { writeln("SS postblit"); }
void opAssign(SS rhs) { writeln("SS opAssign"); }
~this() { writeln(ii);}
}
SS ss;
this(this) { writeln("S postblit"); }
void opAssign(S rhs)
{
writeln("S opAssign");
}
~this()
{
writefln("i=%d, ii=%d", i, ss.ii);
}
}
S[int] map ;
// S[2] map;
// S[] map = [S(), S()];
void main()
{
map[1] = S();
}
Here program will produce not 41 and 42, but some arbitraly values because when
druntime allocates space for a new struct object in array, it does not
initialize it. Switching to fixed/dynamic array fixes program.
This causes segfault in phobos because of false assumptions about valid state
of the object:
import std.typecons;
import std.stdio;
alias RefCounted!(int) Foo;
Foo[int] map;
unittest {
map[1] = Foo();
}
void main() { }
In this code a Foo() instance is assigned into allocated but not constructed
object and because struct assignment is rewritten to opAssign call, on entering
Foo.opAssign "this" points to non-constructed object which breaks code which
assumes that object is acrually valid.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list