Thread overview
Exceptions in safe D
Jan 09, 2012
Robert Clipsham
Jan 09, 2012
Juan Campanas
Jan 09, 2012
Simen Kjærås
Jan 10, 2012
Jesse Phillips
Jan 10, 2012
Jonathan M Davis
January 09, 2012
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/
January 09, 2012
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.
January 09, 2012
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
January 10, 2012
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.
January 10, 2012
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