Bug 13972 – Make scoped, Unique, and RefCounted @nogc

Status
RESOLVED
Resolution
MOVED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-12T16:28:23Z
Last change time
2022-12-16T14:00:22Z
Assigned to
No Owner
Creator
Andrei Alexandrescu
Depends on
15246
Blocks
15509

Comments

Comment #0 by andrei — 2015-01-12T16:28:23Z
Per the comments in http://maikklein.github.io/2015/01/11/Evaluating-D-for-games/ - the main use of these artifacts is to avoid the GC in the first place.
Comment #1 by opantm2+dbugs — 2015-01-13T04:32:50Z
I've looked at making some obvious things @nogc (RefCounted, std.container.array, etc), but have always been blocked by the use of exceptions. There was some talk about how to handle @nogc exceptions, but no real conclusion came of it. Virtually all of these structs that are supposed to completely avoid the GC do not actually do so because they allocate exceptions with the GC (whether by enforce or manually throwing). This is also one of the main reasons I never use @nogc in my own code, even though I avoid using the GC. In some cases there's an obvious solution, such as replacing `enforce(foo)` with: ```` if(foo is null) { static ex = cast(immutable)(new Exception("foo is null")); throw ex; } ```` But that's considerably uglier and does not help in situations where the exception text includes additional information from runtime arguments. Also I'm not sure what kind of impact this would have if called and thrown in a catch block. It would be nice to confirm what approach for avoiding GC exceptions should be used in Phobos, as AFAIK nothing was ever decided, and I'm not sure of an approach without drawbacks towards those who do use the GC.
Comment #2 by bearophile_hugs — 2015-02-11T21:12:01Z
(In reply to Kapps from comment #1) > if(foo is null) { > static ex = cast(immutable)(new Exception("foo is null")); > throw ex; I think no cast is needed: static immutable ex = new Exception("foo is null"); > and does not help in situations where the > exception text includes additional information from runtime arguments. The space for the extra information could be allocated statically, and pasted inside the buffer. But this makes the function not pure. > Also > I'm not sure what kind of impact this would have if called and thrown in a > catch block. In presence of exception chaining how are immutable exceptions working?
Comment #3 by r9shackleford — 2015-02-12T08:06:54Z
Hi, Is anyone working on this? It seems like a lot of effort to get RefCounted to @nogc, just wanted to see if anyone else was working on it before I go any further and possibly duplicate work. I think for this to work properly some of the non-allocating GC functions will need to be annotated with @nogc so we can properly hold references to GC allocated memory - I'm not sure how else to work around this but maybe someone more experienced than I am knows. I opened an enhancement request about it, I'm not sure if it will go anywhere. Bye.
Comment #4 by andrei — 2015-02-12T22:19:09Z
I don't think anyone is working on this for the time being. Marking some of the low-level GC functions as @nogc seems like a good way to go.
Comment #5 by r9shackleford — 2015-04-10T04:40:16Z
AFAICT The only thing blocking refcounted from being @nogc is the enforce in the initialize now.
Comment #6 by r9shackleford — 2015-04-21T01:55:42Z
RefCounted almost done. https://github.com/D-Programming-Language/phobos/pull/3171 made it possible to make @nogc refcounted types, but destroy still doesn't infer attributes as per https://github.com/D-Programming-Language/druntime/pull/1181
Comment #7 by nick — 2022-12-15T19:16:56Z
RefCounted and SafeRefCounted are @nogc. Unique is @nogc (but doesn't manage heap memory anyway). scoped is not.
Comment #8 by razvan.nitu1305 — 2022-12-16T14:00:22Z
This is not a dmd issue, so I'm closing. Anyway, most of these are @nogc now.