The D standard library is built on GC, is that a negative or positive?

Siarhei Siamashka siarhei.siamashka at gmail.com
Fri Dec 16 04:54:21 UTC 2022


On Thursday, 15 December 2022 at 13:45:03 UTC, Adam D Ruppe wrote:
> 1) `immutable` is ignored on exceptions. The implementation 
> casts it away internally as it is thrown, so when it is caught, 
> it is mutable again. If someone tried to actually modify it at 
> this point, it is undefined behavior. (in this case i think the 
> implementation would just retain the edits for another call to 
> the same enforce msg, T arguments)

So I tried to mark this immutable data with 
VALGRIND_MAKE_MEM_NOACCESS and valgrind reports just a single 
write access to it from here: 
https://github.com/dlang/dmd/blob/v2.101.1/druntime/src/rt/deh.d#L46

Is it possible to construct the immutable exception data in a way 
that the `cast(byte*) t !is typeid(t).initializer.ptr` check 
would fail? What's the purpose of this check?

Another problem is illustrated by the example below:

```D
T enforce(string msg, T)(T cond) {
     if (!cond) {
         static immutable e = new Exception(msg);
         throw e;
     }
     return cond;
}

void main() {
     try {
         enforce!"trigger an exception"(1 == 2);
     } catch (Exception e) {
         assert(0, "if we reach here, then it's probably a 
compiler bug");
     } catch (immutable Exception e) {
         // the proper place to handle it is here
         assert(e.msg == "trigger an exception");
     }
}
```

If it's an immutable exception, then the compiler should probably 
only catch it as immutable?

> static exceptions work for simple cases but there's a number of 
> pitfalls using them beyond the basics, especially when threads 
> come into play.

Right now it doesn't look like there are way too many problems 
preventing immutable exceptions from becoming usable.


More information about the Digitalmars-d mailing list