[Issue 5447] Should be illegal to throw a non-Throwable
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Jan 13 07:34:39 PST 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5447
Don <clugdbug at yahoo.com.au> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords|rejects-valid |accepts-invalid, patch
OS/Version|Windows |All
--- Comment #1 from Don <clugdbug at yahoo.com.au> 2011-01-13 07:32:49 PST ---
PATCH: Straightforward. Only non-obvious feature is that NULL as the type in a
catch statement needs to become catch(Throwable) not catch(Object), so that
scope statements will work.
After making this change, a couple of changes (about four?) are needed to
druntime and phobos. All are of the form:
catch(Object o)
{
...
throw o;
}
Just need to change Object to Throwable in each case.
Now probably, it would make sense to also disallow catch(Object).
This would be done with the same code as in ThrowStatement::semantic.
-------------
Index: aggregate.h
===================================================================
--- aggregate.h (revision 871)
+++ aggregate.h (working copy)
@@ -199,6 +199,7 @@
{
static ClassDeclaration *object;
static ClassDeclaration *classinfo;
+ static ClassDeclaration *throwable;
ClassDeclaration *baseClass; // NULL only if this is Object
#if DMDV1
Index: class.c
===================================================================
--- class.c (revision 871)
+++ class.c (working copy)
@@ -31,6 +31,7 @@
ClassDeclaration *ClassDeclaration::classinfo;
ClassDeclaration *ClassDeclaration::object;
+ClassDeclaration *ClassDeclaration::throwable;
ClassDeclaration::ClassDeclaration(Loc loc, Identifier *id, BaseClasses
*baseclasses)
: AggregateDeclaration(loc, id)
@@ -180,6 +181,12 @@
object->error("%s", msg);
object = this;
}
+
+ if (id == Id::Throwable)
+ { if (throwable)
+ throwable->error("%s", msg);
+ throwable = this;
+ }
//if (id == Id::ClassInfo)
if (id == Id::TypeInfo_Class)
Index: statement.c
===================================================================
--- statement.c (revision 871)
+++ statement.c (working copy)
@@ -4210,7 +4210,7 @@
sc = sc->push(sym);
if (!type)
- type = new TypeIdentifier(0, Id::Object);
+ type = new TypeIdentifier(0, Id::Throwable);
type = type->semantic(loc, sc);
if (!type->toBasetype()->isClassHandle())
{
@@ -4435,8 +4435,9 @@
#endif
exp = exp->semantic(sc);
exp = resolveProperties(sc, exp);
- if (!exp->type->toBasetype()->isClassHandle())
- error("can only throw class objects, not type %s",
exp->type->toChars());
+ ClassDeclaration *cd = exp->type->toBasetype()->isClassHandle();
+ if (!cd || ((cd != ClassDeclaration::throwable) &&
!ClassDeclaration::throwable->isBaseOf(cd, NULL)))
+ fd->error("can only throw class objects derived from Throwable, not
type %s", exp->type->toChars());
return this;
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list