www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9084] New: Structs assignment and associative arrays

reply d-bugmail puremagic.com writes:
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
parent d-bugmail puremagic.com writes:
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