Bug 16146 – postblit is not called on struct creation with "{field:value}" syntax
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-06-09T11:17:00Z
Last change time
2017-01-16T23:25:17Z
Assigned to
nobody
Creator
ketmar
Comments
Comment #0 by ketmar — 2016-06-09T11:17:11Z
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.