Skip to content

Commit

Permalink
Merge pull request #3826 from yebblies/issue12503
Browse files Browse the repository at this point in the history
Issue 12503 - Bad optimization with scope(success) and return statement
  • Loading branch information
WalterBright authored and 9rnsr committed Aug 27, 2014
1 parent 77d60e1 commit 31d0aa4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/irstate.c
Expand Up @@ -26,6 +26,7 @@ IRState::IRState(IRState *irs, Statement *s)
contBlock = NULL;
switchBlock = NULL;
defaultBlock = NULL;
finallyBlock = NULL;
ident = NULL;
ehidden = NULL;
startaddress = NULL;
Expand Down Expand Up @@ -60,6 +61,7 @@ IRState::IRState(IRState *irs, Dsymbol *s)
contBlock = NULL;
switchBlock = NULL;
defaultBlock = NULL;
finallyBlock = NULL;
ident = NULL;
ehidden = NULL;
startaddress = NULL;
Expand Down Expand Up @@ -95,6 +97,7 @@ IRState::IRState(Module *m, Dsymbol *s)
contBlock = NULL;
switchBlock = NULL;
defaultBlock = NULL;
finallyBlock = NULL;
ident = NULL;
ehidden = NULL;
shidden = NULL;
Expand Down Expand Up @@ -195,6 +198,18 @@ block *IRState::getDefaultBlock()
return NULL;
}

block *IRState::getFinallyBlock()
{
IRState *bc;

for (bc = this; bc; bc = bc->prev)
{
if (bc->finallyBlock)
return bc->finallyBlock;
}
return NULL;
}

FuncDeclaration *IRState::getFunc()
{
IRState *bc;
Expand Down
2 changes: 2 additions & 0 deletions src/irstate.h
Expand Up @@ -47,6 +47,7 @@ struct IRState
block *contBlock;
block *switchBlock;
block *defaultBlock;
block *finallyBlock;

IRState(IRState *irs, Statement *s);
IRState(IRState *irs, Dsymbol *s);
Expand All @@ -56,6 +57,7 @@ struct IRState
block *getContBlock(Identifier *ident);
block *getSwitchBlock();
block *getDefaultBlock();
block *getFinallyBlock();
FuncDeclaration *getFunc();
int arrayBoundsCheck();
};
Expand Down
14 changes: 5 additions & 9 deletions src/s2ir.c
Expand Up @@ -812,17 +812,12 @@ class S2irVisitor : public Visitor
else
bc = BCret;

block *btry = blx->curblock->Btry;
if (btry)
if (block *finallyBlock = irs->getFinallyBlock())
{
// A finally block is a successor to a return block inside a try-finally
if (btry->numSucc() == 2) // try-finally
{
block *bfinally = btry->nthSucc(1);
assert(bfinally->BC == BC_finally);
blx->curblock->appendSucc(bfinally);
}
assert(finallyBlock->BC == BC_finally);
blx->curblock->appendSucc(finallyBlock);
}

block_next(blx, bc, NULL);
}

Expand Down Expand Up @@ -1099,6 +1094,7 @@ class S2irVisitor : public Visitor
block *contblock = block_calloc(blx);
tryblock->appendSucc(contblock);
contblock->BC = BC_finally;
bodyirs.finallyBlock = contblock;

if (s->body)
Statement_toIR(s->body, &bodyirs);
Expand Down
28 changes: 28 additions & 0 deletions test/runnable/xtest46.d
Expand Up @@ -5117,6 +5117,33 @@ void test6910()
alias Test6910!(i, Bag!(A)).fn func;
}

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

void fun12503()
{
string b = "abc";
try
{
try
{
b = null;
return;
}
catch
{
}
}
finally
{
assert("abc" !is b);
}
}

void test12503()
{
fun12503();
}

/***************************************************/
// 6902

Expand Down Expand Up @@ -7212,6 +7239,7 @@ int main()
test7871();
test7906();
test7907();
test12503();
test8004();
test8064();
test8105();
Expand Down

0 comments on commit 31d0aa4

Please sign in to comment.