Skip to content

Commit

Permalink
Merge pull request #6271 from LemonBoy/structfill
Browse files Browse the repository at this point in the history
Fix issue 16146 - Postblit the values in struct literals.
  • Loading branch information
WalterBright committed Dec 22, 2016
2 parents cb7f8fe + 5b2fb91 commit d7e22de
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/init.d
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ extern (C++) final class StructInitializer : Initializer

override Initializer semantic(Scope* sc, Type t, NeedInterpret needInterpret)
{
//printf("StructInitializer::semantic(t = %s) %s\n", t->toChars(), toChars());
//printf("StructInitializer::semantic(t = %s) %s\n", t.toChars(), toChars());
t = t.toBasetype();
if (t.ty == Tsarray && t.nextOf().toBasetype().ty == Tstruct)
t = t.nextOf().toBasetype();
Expand Down Expand Up @@ -329,7 +329,7 @@ extern (C++) final class StructInitializer : Initializer
continue;
}
value[i] = iz;
(*elements)[fieldi] = ex;
(*elements)[fieldi] = doCopyOrMove(sc, ex);
++fieldi;
}
if (errors)
Expand Down
26 changes: 26 additions & 0 deletions test/runnable/bug16146.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
struct X {
int* rc;
this (int n) { auto x = new int[](1); rc = x.ptr; *rc = n; }
this (this) { ++*rc; }
~this () { --*rc; }
@disable void opAssign (X src);
}

struct Y {
X x;
}

void frob(X x)
{
Y y = { x: x };
// The 'rc' counter starts from 1 and gets bumped when:
// - 'f0' is passed to 'frob'
// - 'y' is initialized with 'x'
assert(*y.x.rc == 3);
}

void main ()
{
auto f0 = X(1);
frob(f0);
}

0 comments on commit d7e22de

Please sign in to comment.