www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14686] New: Postblit isn't sometimes called on concatenation

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

          Issue ID: 14686
           Summary: Postblit isn't sometimes called on concatenation
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: CTFE, wrong-code
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: k.hara.pg gmail.com

Test case:

extern(C) int printf(const char*, ...);

int test()
{
    string r;

    struct S
    {
        int n;
        this(this)
        {
            if (!__ctfe) printf("copy n = %d\n", n);
            r ~= cast(char)('0' + n);
        }
    }

    S s1 = S(1);
    S s2 = S(2);
    S[] a1 = [S(1)];
    if (!__ctfe) printf("0--\n");

    S[2] sa1 = [s1, s2];
    if (!__ctfe) printf("1-- %.*s\n", r.length, r.ptr);
    assert(r == "12", r);   // OK

    r = "";
    S[] a2 = a1 ~ s2;       // runtime concatenation
    if (!__ctfe) printf("2-- %.*s\n", r.length, r.ptr);
  //assert(r == "12", r);   // NG only in CTFE

    r = "";
    S[2] sa2a = [s1] ~ s2;
    if (!__ctfe) printf("3a-- %.*s\n", r.length, r.ptr);
  //assert(r == "12", r);   // NG, s2 is not copied

    r = "";
    S[2] sa2b = s2 ~ [s1];
    if (!__ctfe) printf("3b-- %.*s\n", r.length, r.ptr);
  //assert(r == "21", r);   // NG, s2 is not copied

    r = "";
    S[3] sa3a = ([s1] ~ [s1]) ~ s2;
    if (!__ctfe) printf("4a-- %.*s\n", r.length, r.ptr);
  //assert(r == "112", r);  // NG, s2 is not copied

    r = "";
    S[3] sa3b = s2 ~ ([s1] ~ [s1]);
    if (!__ctfe) printf("4b-- %.*s\n", r.length, r.ptr);
  //assert(r == "211", r);  // NG, s2 is not copied

    return 1;
}
static assert(test());  // CTFE test

void main() { test(); } // runtime test


Output:

$ dmd -run test
0--
copy n = 1
copy n = 2
1-- 12
copy n = 1
copy n = 2
2-- 12
copy n = 1
3a-- 1
copy n = 1
3b-- 1
copy n = 1
copy n = 1
4a-- 11
copy n = 1
copy n = 1
4b-- 11

--
Jun 11 2015