Skip to content

Commit

Permalink
fix Issue 18457 - betterC - struct destructor is always called at fun…
Browse files Browse the repository at this point in the history
…ction return
  • Loading branch information
WalterBright authored and wilzbach committed Oct 26, 2018
1 parent 5e50feb commit b19d37a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/dmd/func.d
Expand Up @@ -98,8 +98,17 @@ public:
{
DtorExpStatement des;
if (fd.nrvo_can && s.finalbody && (des = s.finalbody.isDtorExpStatement()) !is null &&
fd.nrvo_var == des.var && global.params.useExceptions && ClassDeclaration.throwable)
fd.nrvo_var == des.var)
{
if (!(global.params.useExceptions && ClassDeclaration.throwable))
{
/* Don't need to call destructor at all, since it is nrvo
*/
replaceCurrent(s._body);
s._body.accept(this);
return;
}

/* Normally local variable dtors are called regardless exceptions.
* But for nrvo_var, its dtor should be called only when exception is thrown.
*
Expand Down
29 changes: 29 additions & 0 deletions test/runnable/betterc.d
Expand Up @@ -26,6 +26,7 @@ extern (C) void main()
test(1);
test18472();
testRuntimeLowerings();
test18457();
}

/*******************************************/
Expand Down Expand Up @@ -156,3 +157,31 @@ void testRuntimeLowerings()
break;
}
}

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

__gshared int dtor;

struct S18457
{
int a = 3;
~this() { a = 0; ++dtor; }
}

S18457 myFunction()
{
S18457 s = S18457();
return s;
}

void test18457()
{
{
S18457 s = myFunction();
assert(s.a == 3);
assert(dtor == 0);
}
assert(dtor == 1);
}

0 comments on commit b19d37a

Please sign in to comment.