www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14024] New: unstable postblit/destructor call order on static

https://issues.dlang.org/show_bug.cgi?id=14024

          Issue ID: 14024
           Summary: unstable postblit/destructor call order on static
                    array assignment
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: k.hara.pg gmail.com

I found this unstable runtime behavior while fixing other CTFE issues.

Test case:

import core.stdc.stdio;
int test14022()
{
    string op;

    struct S
    {
        char x = 'x';
        this(this) { op ~= x - ('a'-'A'); } // upper case
        ~this() { op ~= x; }                // lower case
    }
    struct T
    {
        S[2] member;
    }

    S[2] makeSA() { return typeof(return).init; }

    version(A)
    {
        S[2] sa = [S('a'), S('b')];
        S[2] sb = [S('x'), S('y')];
    }
    else
    {
        S[2] sb = [S('x'), S('y')];
        S[2] sa = [S('a'), S('b')];
    }
    op = null;
    sa = sb;
    printf("op = %.*s\n", op.length, op.ptr);

    return 1;
}
void main() { test14022(); }

The program output will be switched:
    op = XaYb
and:
    op = YbXa
Depending on the compiler switch -version=A.
(To be precise, it depends on the stack address of sa and sb.)

On static array assignment `sa = sb;`, the order of postblit/destructor calls
should be normal if lhs and rhs have distinct memories.

--
Jan 21 2015