Skip to content

Commit

Permalink
Merge pull request #5183 from AndrejMitrovic/fix-12558
Browse files Browse the repository at this point in the history
Issue 12558 - Deprecate implicit catch statements
  • Loading branch information
WalterBright committed Jul 7, 2016
2 parents dd717cd + 9ebcbed commit 18e6844
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/func.d
Expand Up @@ -357,7 +357,7 @@ public:
* // equivalent with:
* // s->body; scope(exit) nrvo_var->edtor;
* as:
* try { s->body; } catch(__o) { nrvo_var->edtor; throw __o; }
* try { s->body; } catch(Throwable __o) { nrvo_var->edtor; throw __o; }
* // equivalent with:
* // s->body; scope(failure) nrvo_var->edtor;
*/
Expand All @@ -373,7 +373,7 @@ public:
}

auto catches = new Catches();
auto ctch = new Catch(Loc(), null, id, handler);
auto ctch = new Catch(Loc(), getThrowable(), id, handler);
ctch.internalCatch = true;
ctch.semantic(sc); // Run semantic to resolve identifier '__o'
catches.push(ctch);
Expand Down Expand Up @@ -3799,13 +3799,13 @@ extern (C++) class FuncDeclaration : Declaration
//printf("fdv->frequire: %s\n", fdv->frequire->toChars());
/* Make the call:
* try { __require(); }
* catch { frequire; }
* catch (Throwable) { frequire; }
*/
Expression eresult = null;
Expression e = new CallExp(loc, new VarExp(loc, fdv.fdrequire, false), eresult);
Statement s2 = new ExpStatement(loc, e);

auto c = new Catch(loc, null, null, sf);
auto c = new Catch(loc, getThrowable(), null, sf);
c.internalCatch = true;
auto catches = new Catches();
catches.push(c);
Expand Down
11 changes: 10 additions & 1 deletion src/statement.d
Expand Up @@ -90,6 +90,15 @@ Expression checkAssignmentAsCondition(Expression e)
return e;
}

/// Return a type identifier reference to 'object.Throwable'
TypeIdentifier getThrowable()
{
auto tid = new TypeIdentifier(Loc(), Id.empty);
tid.addIdent(Id.object);
tid.addIdent(Id.Throwable);
return tid;
}

enum BE : int
{
BEnone = 0,
Expand Down Expand Up @@ -2295,7 +2304,7 @@ extern (C++) final class Catch : RootObject

Catch syntaxCopy()
{
auto c = new Catch(loc, type ? type.syntaxCopy() : null, ident, (handler ? handler.syntaxCopy() : null));
auto c = new Catch(loc, type ? type.syntaxCopy() : getThrowable(), ident, (handler ? handler.syntaxCopy() : null));
c.internalCatch = internalCatch;
return c;
}
Expand Down
10 changes: 5 additions & 5 deletions src/statementsem.d
Expand Up @@ -194,7 +194,7 @@ private extern (C++) final class StatementSemanticVisitor : Visitor
}

auto catches = new Catches();
auto ctch = new Catch(Loc(), null, id, handler);
auto ctch = new Catch(Loc(), getThrowable(), id, handler);
ctch.internalCatch = true;
catches.push(ctch);

Expand Down Expand Up @@ -3441,11 +3441,11 @@ void semantic(Catch c, Scope* sc)

if (!c.type)
{
deprecation(c.loc, "catch statement without an exception " ~
"specification is deprecated; use catch(Throwable) for old behavior");

// reference .object.Throwable
auto tid = new TypeIdentifier(Loc(), Id.empty);
tid.addIdent(Id.object);
tid.addIdent(Id.Throwable);
c.type = tid;
c.type = getThrowable();
}
c.type = c.type.semantic(c.loc, sc);
if (c.type == Type.terror)
Expand Down
39 changes: 39 additions & 0 deletions test/compilable/test12558.d
@@ -0,0 +1,39 @@
// REQUIRED_ARGS:
/*
TEST_OUTPUT:
---
compilable/test12558.d(16): Deprecation: catch statement without an exception specification is deprecated; use catch(Throwable) for old behavior
compilable/test12558.d(21): Deprecation: catch statement without an exception specification is deprecated; use catch(Throwable) for old behavior
---
*/

void main()
{
auto handler = () { };

try {
assert(0);
} catch
handler();

try {
assert(0);
} catch {
handler();
}

// ensure diagnostics are not emitted for verioned-out blocks
version (none)
{
try {
assert(0);
} catch // should not emit diagnostics
handler();

try {
assert(0);
} catch { // ditto
handler();
}
}
}
2 changes: 1 addition & 1 deletion test/fail_compilation/fail23.d
Expand Up @@ -13,7 +13,7 @@ void main()
{
break;
}
catch
catch (Throwable)
{
}
}
6 changes: 3 additions & 3 deletions test/fail_compilation/fail2456.d
Expand Up @@ -93,18 +93,18 @@ void test2456b()
scope(success)
{
try {}
catch {} // NG
catch (Throwable) {} // NG
}

scope(failure)
{
try {}
catch {} // OK
catch (Throwable) {} // OK
}

scope(exit)
{
try {}
catch {} // NG
catch (Throwable) {} // NG
}
}

0 comments on commit 18e6844

Please sign in to comment.