www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16146] New: postblit is not called on struct creation with

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

          Issue ID: 16146
           Summary: postblit is not called on struct creation with
                    "{field:value}" syntax
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ketmar ketmar.no-ip.org

here is sample code:

import std.stdio;


struct X {
  int* rc;
  this (int n) { auto x = new int[](1); rc = x.ptr; *rc = n; }
  this (this) { writeln("postblit; rc=", *rc); ++*rc; }
  ~this () { writeln("dtor; rc=", *rc, "; will be ", *rc-1); --*rc; }
  void opAssign (X src) { assert(0); } // just in case
}

struct Boo {
  X st;
}

void boo (ref Boo boo) {
  writeln("boo");
}

void foo (X fl) {
  writeln("foo");
  version(bug)
    Boo b = { st: fl };
  else
    auto b = Boo(fl);
  writeln("foo 001");
  boo(b);
  writeln("foo exit");
}

void main () {
  {
    auto fl = X(1);
    writeln("000");
    foo(fl);
  }
  writeln("001");
}


with normal code path the output is:
000
postblit; rc=1
foo
postblit; rc=2
foo 001
boo
foo exit
dtor; rc=3; will be 2
dtor; rc=2; will be 1
dtor; rc=1; will be 0
001

with -version=bug the output is:
000
postblit; rc=1
foo
foo 001
boo
foo exit
dtor; rc=2; will be 1
dtor; rc=1; will be 0
dtor; rc=0; will be -1
001


as we can see, postblit is not called when Boo is created with `{ st: fl }`
syntax. this makes writing refcounted data types with this syntax impossible
without ugly hacks.

--
Jun 09 2016