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

Siarhei Siamashka siarhei.siamashka at gmail.com
Wed Dec 21 14:38:48 UTC 2022


On Wednesday, 21 December 2022 at 12:44:18 UTC, Nick Treleaven 
wrote:
> On Friday, 16 December 2022 at 13:56:19 UTC, bauss wrote:
>> On Friday, 16 December 2022 at 13:25:25 UTC, Nick Treleaven 
>> wrote:
>>> This pull disallows throwing an immutable object:
>>>
>>> https://github.com/dlang/dmd/pull/14706
>>>
>>> You can still throw a const object though, which would work 
>>> for your `enforce`.
>
> Of course that was changed as immutable can convert to const 
> before throwing, and const shouldn't be violated even if not 
> immutable.

Using `const` is not an undefined behavior, but it's still not 
safe and not desirable. Because we have to hope that the catch 
blocks never try to modify the received exception object and 
never let it escape the catch block scope. This makes `immutable` 
a much better fit.

>> Personally I think it should always just be implied const like:
>>
>> catch (Exception e) should imply catch (const e) that way both 
>> mutable and immutable will work.
>
> That solves the throw/catch qualifier mismatch problem, but it 
> still can violate immutable when the runtime sets the stack 
> trace.

If a custom Throwable.TraceInfo is already set (see 
https://forum.dlang.org/post/bvgalazssljjnchqnjso@forum.dlang.org 
as an example), then the runtime does not try to modify it. This 
is valid for a wide range of the older versions of the D compiler 
& druntime up to and including the most recent v2.101.1 (but the 
future versions may of course change).


> Also not sure if the runtime may set another field.

To the best of my knowledge, it doesn't. Tested by valgrind [in 
this 
way](https://forum.dlang.org/post/msjrcymphcdquslfgbrn@forum.dlang.org) and also looked at the druntime code.


> If reference counted exceptions are implemented then that might 
> conflict with const/immutable too (though it can be worked 
> around with a hashtable).

The reference counted exceptions are already implemented and can 
be previewed by using the `-dip1008` command line option for DMD 
or the `-fpreview=dip1008` command line option for GDC (only 
GDC12 or newer). The current implementation [checks the reference 
counter 
here](https://github.com/dlang/dmd/blob/v2.101.1/druntime/src/rt/dwarfeh.d#L295-L299) and only increments it if it's non-zero. The zero value of the reference counter means that it is GC allocated (this works fine for static immutable exception instances too).

I have no clue if there are any plans to make `-dip1008` 
available by default any time soon and its status is "Postponed". 
I'm not sure if anything other than 
https://github.com/dlang/dmd/pull/14710 is preventing this from 
happening. If anyone is aware of any other 
[DIP1008](https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1008.md) blockers, then please let me know.


More information about the Digitalmars-d mailing list