Why is Throwable.TraceInfo.toString not @safe?

Johannes Loher johannes.loher at fg4f.de
Mon Jul 22 07:29:21 UTC 2019


Am 22.07.19 um 05:16 schrieb Paul Backus:
> On Sunday, 21 July 2019 at 18:03:33 UTC, Johannes Loher wrote:
>> I'd like to log stacktraces of caught exceptions in an @safe manner.
>> However, Throwable.TraceInfo.toString is not @safe (or @trusted), so
>> this is not possible. Why is it not @safe? Can it be @trusted?
>>
>> Thanks for your help!
> 
> Seems like it's because it uses the form of toString that accepts a
> delegate [1], and that delegate parameter is not marked as @safe.
> 
> [1] https://dlang.org/phobos/object.html#.Throwable.toString.2

I'm not talking about Throwable's toString method, but about
Throwable.TraceInfo's. Throwable.TraceInfo is an Interface inside Throwable:

interface TraceInfo
{
    int opApply(scope int delegate(ref const(char[]))) const;
    int opApply(scope int delegate(ref size_t, ref const(char[]))) const;
    string toString() const;
}

Throwable has a member info of type TraceInfo. It is never explicitly
set, so I assume it is automatically set by runtime magic. This is the
constructor of Throwable:

@nogc @safe pure nothrow this(string msg, Throwable nextInChain = null)
{
    this.msg = msg;
    this.nextInChain = nextInChain;
    //this.info = _d_traceContext();
}

In theory people could define their own exception classes which provide
their own implementation of TraceInfo, but I never heard of anybody
doing this. So the real question is if the toString method of of the
DRuntime implementations (I assume there might be different
implementations for different platforms) are actually @safe (or
@trusted) and why we do not mark the interface to be @safe then.


More information about the Digitalmars-d-learn mailing list