Skip to content

Commit

Permalink
Merge pull request #8200 from WalterBright/fix15869
Browse files Browse the repository at this point in the history
fix Issue 15869 - RVO can overwrite argument
merged-on-behalf-of: Razvan Nitu <RazvanN7@users.noreply.github.com>
  • Loading branch information
dlang-bot authored May 14, 2018
2 parents 21f25f2 + 0ba1f25 commit 68eb9d3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dmd/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ extern (C++) bool checkFrameAccess(Loc loc, Scope* sc, AggregateDeclaration ad,
* Mark variable v as modified if it is inside a constructor that var
* is a field in.
*/
private int modifyFieldVar(Loc loc, Scope* sc, VarDeclaration var, Expression e1)
bool modifyFieldVar(Loc loc, Scope* sc, VarDeclaration var, Expression e1)
{
//printf("modifyFieldVar(var = %s)\n", var.toChars());
Dsymbol s = sc.func;
Expand Down
25 changes: 25 additions & 0 deletions src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import dmd.canthrow;
import dmd.complex;
import dmd.constfold;
import dmd.ctfeexpr;
import dmd.ctorflow;
import dmd.dcast;
import dmd.dclass;
import dmd.declaration;
Expand Down Expand Up @@ -5326,6 +5327,30 @@ extern (C++) final class DotVarExp : UnaExp
override Expression toLvalue(Scope* sc, Expression e)
{
//printf("DotVarExp::toLvalue(%s)\n", toChars());
if (e1.op == TOK.this_ && sc.ctorflow.fieldinit.length && !(sc.ctorflow.callSuper & CSX.any_ctor))
{
if (VarDeclaration vd = var.isVarDeclaration())
{
auto ad = vd.isMember2();
if (ad && ad.fields.dim == sc.ctorflow.fieldinit.length)
{
foreach (i, f; ad.fields)
{
if (f == vd)
{
if (!(sc.ctorflow.fieldinit[i] & CSX.this_ctor))
{
/* If the address of vd is taken, assume it is thereby initialized
* https://issues.dlang.org/show_bug.cgi?id=15869
*/
modifyFieldVar(loc, sc, vd, e1);
}
break;
}
}
}
}
}
return this;
}

Expand Down
31 changes: 31 additions & 0 deletions test/runnable/sctor.d
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,36 @@ void test15665()
scope foo = new C15665!int;
}

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

struct Set {
@disable this(this);
int value = 0;
}

Set clobber(ref Set a) {
Set ret; // <- This overwrites *a, i.e. &ret is the same as a
ret.value = a.value; // <- Now a.value is 0
return ret;
}

struct XX {
Set a = Set(1);
this(int n) {
a = clobber(a); // fix is to make this an assignment, not a construction
}
}
void test15869()
{
Set a = Set(1);
a = clobber(a);
assert(a.value == 1);

XX xx = XX(0);
assert(xx.a.value == 1);
}

/***************************************************/

int main()
Expand All @@ -426,6 +456,7 @@ int main()
test14450();
test14944();
test15665();
test15869();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 68eb9d3

Please sign in to comment.