www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Exceptions in safe D

reply Robert Clipsham <robert octarineparrot.com> writes:
Are exceptions in safe D possible? I started trying to make my code 
 safe (there's no reason why it can't be as far as I'm aware), but I hit 
the following issue:

----
 safe

class MyException : Exception
{
     this()
     {
         super("");
     }
}

void main()
{
     throw new MyException("");
}
----
$ rdmd test.d
test.d(7): Error: safe function 'this' cannot call system function 'this'
test.d(13): Error: constructor test.MyException.this () is not callable 
using argument types (string)
test.d(13): Error: expected 0 arguments, not 1 for non-variadic function 
type  safe MyException()
----

Is this just unimplemented, is there a workaround, or will this never work?

Thanks,

-- 
Robert
http://octarineparrot.com/
Jan 09 2012
next sibling parent reply "Juan Campanas" <johnny bells.com> writes:
On Monday, 9 January 2012 at 21:14:05 UTC, Robert Clipsham wrote:
 Are exceptions in safe D possible? I started trying to make my 
 code  safe (there's no reason why it can't be as far as I'm 
 aware), but I hit the following issue:

 ----
  safe

 class MyException : Exception
 {
    this()
    {
        super("");
    }
 }

 void main()
 {
    throw new MyException("");
 }
 ----
 $ rdmd test.d
 test.d(7): Error: safe function 'this' cannot call system 
 function 'this'
 test.d(13): Error: constructor test.MyException.this () is not 
 callable using argument types (string)
 test.d(13): Error: expected 0 arguments, not 1 for non-variadic 
 function type  safe MyException()
 ----

 Is this just unimplemented, is there a workaround, or will this 
 never work?

 Thanks,
Why are you calling the MyException constructor with a string parameter? Anyways. I think that Exception is not marked as safe.
Jan 09 2012
parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Mon, 09 Jan 2012 22:33:28 +0100, Juan Campanas <johnny bells.com> wrote:

 On Monday, 9 January 2012 at 21:14:05 UTC, Robert Clipsham wrote:
 Are exceptions in safe D possible? I started trying to make my code  
  safe (there's no reason why it can't be as far as I'm aware), but I  
 hit the following issue:

 ----
  safe

 class MyException : Exception
 {
    this()
    {
        super("");
    }
 }

 void main()
 {
    throw new MyException("");
 }
 ----
 $ rdmd test.d
 test.d(7): Error: safe function 'this' cannot call system function  
 'this'
 test.d(13): Error: constructor test.MyException.this () is not callable  
 using argument types (string)
 test.d(13): Error: expected 0 arguments, not 1 for non-variadic  
 function type  safe MyException()
 ----

 Is this just unimplemented, is there a workaround, or will this never  
 work?

 Thanks,
Why are you calling the MyException constructor with a string parameter?
Yeah, that explains the error on line 13.
 Anyways. I think that Exception is not marked as  safe.
Indeed. This seems like a bug. Please file it in bugzilla. http://d.puremagic.com/issues/enter_bug.cgi
Jan 09 2012
prev sibling parent reply "Jesse Phillips" <jessekphillips+D gmail.com> writes:
Maybe I missed something, but the last I knew Safe D is not 
completely implemented. I think checking that you don't call 
system functions is the only piece implemented, and Phobos isn't 
annotated with it.
Jan 09 2012
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday, January 10, 2012 03:55:35 Jesse Phillips wrote:
 Maybe I missed something, but the last I knew Safe D is not
 completely implemented. I think checking that you don't call
 system functions is the only piece implemented, and Phobos isn't
 annotated with it.
Yeah. I'm not sure how well safe is implemented in general, but IIRC, it's not fully implemented yet - though it certainly works well enough to use in many cases. However, one of the biggest problems is essentially the same problem that we have with const, and that is that not enough stuff in druntime and Phobos uses it where it should. Case in point: Exception isn't marked with safe, pure, or nothrow, and it probably could (and probably _should_) be marked with all three. Attribute inferrence for templates has helped a lot with the problem, but there's still plenty of non-templated stuff which needs to be fixed up to work properly with safe, pure, and nothrow. - Jonathan M Davis
Jan 09 2012