Skip to content

Commit

Permalink
Merge pull request #2176 from ibuclaw/emptytry
Browse files Browse the repository at this point in the history
Don't eagerly remove all catches if try body is empty.
  • Loading branch information
yebblies committed Dec 13, 2013
2 parents 49f4d60 + f06ea2a commit 2530cf4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/statement.c
Expand Up @@ -712,6 +712,13 @@ Statement *CompoundStatement::semantic(Scope *sc)
}
}
}
else
{
/* Remove NULL statements from the list.
*/
statements->remove(i);
continue;
}
}
i++;
}
Expand Down Expand Up @@ -4558,8 +4565,9 @@ Statement *TryCatchStatement::syntaxCopy()
Statement *TryCatchStatement::semantic(Scope *sc)
{
body = body->semanticScope(sc, NULL /*this*/, NULL);
assert(body);

/* Even if body is NULL, still do semantic analysis on catches
/* Even if body is empty, still do semantic analysis on catches
*/
bool catchErrors = false;
for (size_t i = 0; i < catches->dim; i++)
Expand All @@ -4586,17 +4594,9 @@ Statement *TryCatchStatement::semantic(Scope *sc)
if (catchErrors)
return new ErrorStatement();

if (!body)
return NULL;

if (body->isErrorStatement())
return body;

if (!body->hasCode())
{
return NULL;
}

/* If the try body never throws, we can eliminate any catches
* of recoverable exceptions.
*/
Expand All @@ -4618,7 +4618,7 @@ Statement *TryCatchStatement::semantic(Scope *sc)
}

if (catches->dim == 0)
return body;
return body->hasCode() ? body : NULL;

return this;
}
Expand Down
39 changes: 38 additions & 1 deletion test/runnable/eh.d
Expand Up @@ -580,7 +580,7 @@ void test9568()

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

void test8()
void test8a()
{
int a;
goto L2; // L2 is not addressable.
Expand All @@ -596,6 +596,43 @@ L2: ;
assert(a == 100);
}

void test8b()
{
int a;
goto L2; // L2 is not addressable.

try {
}
catch (Exception) {
a += 3;
L2: ;
a += 100;
}
assert(a == 100);
}

void test8c()
{
int a;
goto L2; // L2 is not addressable.

try
static assert(true);
catch (Exception) {
a += 3;
L2: ;
a += 100;
}
assert(a == 100);
}

void test8()
{
test8a();
test8b();
test8c();
}

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

uint foo9(uint i)
Expand Down

0 comments on commit 2530cf4

Please sign in to comment.