www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5447] New: Should be illegal to throw a non-Throwable

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5447

           Summary: Should be illegal to throw a non-Throwable
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: clugdbug yahoo.com.au
            Blocks: 5402



Exception chaining cannot work as long as it's legal to throw a non-Throwable.
(There is nothing to chain to!)
Even without exception chaining, this also creates a great deal of needless
complexity in the runtime.
This should be rejected at compile time:

void main()
{
  throw new Object;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 13 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5447


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|rejects-valid               |accepts-invalid, patch
         OS/Version|Windows                     |All



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: -------
Jan 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5447






 Index: statement.c
 ===================================================================
 --- statement.c    (revision 871)
 +++ statement.c    (working copy)
    -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;
  }
Sorry, the fd->error(...) should be error(...) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5447


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED



21:46:15 PST ---
http://www.dsource.org/projects/dmd/changeset/873

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 13 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5447


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla kyllingen.net



*** Issue 5402 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 07 2011