digitalmars.D.bugs - [Issue 9084] New: Structs assignment and associative arrays
- d-bugmail puremagic.com (64/64) Nov 27 2012 http://d.puremagic.com/issues/show_bug.cgi?id=9084
- d-bugmail puremagic.com (11/11) Dec 13 2012 http://d.puremagic.com/issues/show_bug.cgi?id=9084
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 puremagic.com
ReportedBy: maxim maxim-fomin.ru
---
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: -------
Nov 27 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9084
Maxim Fomin <maxim maxim-fomin.ru> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |DUPLICATE
---
*** This issue has been marked as a duplicate of issue 6178 ***
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 13 2012








d-bugmail puremagic.com