www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6052] New: [CTFE] Structs elements in an array are treated like reference type

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6052

           Summary: [CTFE] Structs elements in an array are treated like
                    reference type
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Mac OS X
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: kennytm gmail.com



Test case:

--------------------------------------------------
struct Bug6052 {
    int a;
}

bool bug6052() {
    Bug6052[2] arr;
    for (int i = 0; i < 2; ++ i) {
        Bug6052 el = {i};
        Bug6052 ek = el;
        arr[i] = el;
        el.a = i + 2;
        assert(ek.a == i);      // ok
        assert(arr[i].a == i);  // fail
    }
    assert(arr[1].a == 1);  // ok
    assert(arr[0].a == 0);  // fail
    return true;
}

static assert(bug6052());
--------------------------------------------------
x.d(16): Error: assert(arr[cast(uint)i].a == i) failed
x.d(23): Error: cannot evaluate bug6052() at compile time
x.d(23): Error: static assert  (bug6052()) is not evaluatable at compile time
--------------------------------------------------

Setting a struct value on a array should perform a bit-copy, so modifying 'el'
should not affect 'arr[i]', but currently CTFE does it wrongly.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 24 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6052




Note: the last 2 asserts refer to the case when the line 'el.a = i + 2;'  is
commented out.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 24 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6052


kennytm gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |regression


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 24 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6052


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |clugdbug yahoo.com.au
         Resolution|                            |FIXED



A related test case which also fails, but involves static arrays rather than
structs:

bool bug6052b() {
    int[][1] arr;
    int[1] z = [7];
    arr[0] = z;
    assert(arr[0][0] == 7);
    arr[0] = z;
    z[0] = 3;
    assert(arr[0][0] == 3);
    return true;
}

static assert(bug6052b());


https://github.com/D-Programming-Language/dmd/commit/bccb02ad1d8578767f99efeab4a230a229e24392

Case b:
https://github.com/D-Programming-Language/dmd/commit/2ee56a0038ccac3b2225b7feda9d69798cc203e3

D1:
https://github.com/D-Programming-Language/dmd/commit/bccb02ad1d8578767f99efeab4a230a229e24392

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6052


kennytm gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |



The bug still exists if the the struct has a constructor and we're appending to
a dynamic array....

---------------------------------------------
struct Bug6052c {
    int x;
    this(int a) { x = a; }
}
static assert({
    Bug6052c[] pieces = [];
    for (int c = 0; c < 2; ++ c)
        pieces ~= Bug6052c(c);
    assert(pieces[1].x == 1);   // ok
    assert(pieces[0].x == 0);   // asserts
    return true;
}());
---------------------------------------------
x.d(10): Error: assert(pieces[0u].x == 0) failed
<snipped>
---------------------------------------------

or filling the uninitialized portion of a dynamic array....

---------------------------------------------
static assert({
    int[1][] pieces = [];
    pieces.length = 2;
    for (int c = 0; c < 2; ++ c)
        pieces[c][0] = c;
    assert(pieces[1][0] == 1);   // ok
    assert(pieces[0][0] == 0);   // asserts
    return true;
}());
---------------------------------------------
x.d(7): Error: assert(pieces[0u][0u] == 0) failed
<snipped>
---------------------------------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6052


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED



case c:
https://github.com/D-Programming-Language/dmd/commit/0ae51ead31246c7db23438d1d13bbfd8a2145f2f

case d:
https://github.com/D-Programming-Language/dmd/commit/084258c32964dc61333e057065339895d9aca6f0

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 01 2011