Bug 13983 – RefCounted needs to be @safe

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-01-15T01:37:46Z
Last change time
2023-03-23T19:15:10Z
Keywords
safe
Assigned to
No Owner
Creator
hsteoh
Blocks
18110, 15509

Comments

Comment #0 by hsteoh — 2015-01-15T01:37:46Z
Currently RefCounted fails all three attributes, which greatly limits its usefulness, since the negation of these attributes transitively propagates to its payload and everything underneath. This blocks issue #13936.
Comment #1 by r9shackleford — 2015-03-27T02:47:30Z
ping, this requires a safe wrapper around malloc/free right?
Comment #2 by jack — 2016-05-03T15:59:51Z
Adding @safe is impossible until a scheme is added to the language to detect escaped references. Adding nothrow is also impossible because this throws when the computer is out of memory.
Comment #3 by ag0aep6g — 2016-05-03T16:10:38Z
(In reply to Jack Stouffer from comment #2) > Adding nothrow is also impossible because this throws > when the computer is out of memory. You can still throw an OutOfMemoryError with nothrow. Or does RefCounted throw some an Exception (as opposed to an Error)?
Comment #4 by bugzilla — 2016-06-13T01:34:43Z
(In reply to Jack Stouffer from comment #2) > Adding nothrow is also impossible because this throws > when the computer is out of memory. This is incorrect since the out-of-memory is an Error, not an Exception, and nothrow functions can throw Errors.
Comment #5 by schveiguy — 2016-07-29T18:54:47Z
Comment #6 by bugzilla — 2018-03-12T09:43:25Z
Partial fix (@safe not done) pulled here: https://github.com/dlang/phobos/pull/4832
Comment #7 by chilli — 2018-03-13T11:32:04Z
I' m currently working on a fix for -dip1000 compilable std.algorithm.iteration which will(?) touch "RefCounted needs to be (some more) @safe" as well, sadly all by introducing @trusted :-( ; Am I completely wrong here with @trusted? These are my current typecons.RefCounted related findings in order to fix std/algorithm/iteration.d(4245): Error: @safe function std.algorithm.iteration.__unittest_L4227_C7 cannot call @system function std.algorithm.comparison.equal!().equal!(Result, string[]).equal : ``` diff --git a/std/typecons.d b/std/typecons.d index 3a36bc4..e9f79ba 100644 --- a/std/typecons.d +++ b/std/typecons.d @@ -5540,9 +5540,9 @@ if (!is(T == class) && !(is(T == interface))) { extern(C) private pure nothrow @nogc static // TODO remove pure when https://issues.dlang.org/show_bug.cgi?id=15862 has been fixed { - pragma(mangle, "free") void pureFree( void *ptr ); + pragma(mangle, "free") void pureFree( void *ptr ) @trusted; pragma(mangle, "gc_addRange") void pureGcAddRange( in void* p, size_t sz, const TypeInfo ti = null ); - pragma(mangle, "gc_removeRange") void pureGcRemoveRange( in void* p ); + pragma(mangle, "gc_removeRange") void pureGcRemoveRange( in void* p ) @trusted; } /// $(D RefCounted) storage implementation. @@ -5557,7 +5557,7 @@ if (!is(T == class) && !(is(T == interface))) private Impl* _store; - private void initialize(A...)(auto ref A args) + private void initialize(A...)(auto ref A args) @trusted { import core.exception : onOutOfMemoryError; import std.conv : emplace; @@ -5690,7 +5690,7 @@ to deallocate the corresponding resource. if (--_refCounted._store._count) return; // Done, deallocate - .destroy(_refCounted._store._payload); + () @trusted { .destroy(_refCounted._store._payload); }(); static if (hasIndirections!T) { pureGcRemoveRange(&_refCounted._store._payload); ```
Comment #8 by Ajieskola — 2021-08-25T20:13:20Z
Regarding nothrow and pure, this is fixed as Phobos unittests confirm. Changing the title. And by the way, the struct is also @nogc and -betterC. @safe remains a problem though. It cannot be added in vanilla D, but with -dip1000 flag there may be hope. Atila recently made a PR that attempts to do just that: https://github.com/dlang/phobos/pull/8101
Comment #9 by Ajieskola — 2023-03-03T19:36:36Z
Solved by https://github.com/dlang/phobos/pull/8368. Well, mostly. Caveat 1: It is only `@safe` with the -preview=dip1000 flag. But AFAIK there's no solution to that. Generic safe reference counting without DIP1000 is simply impossible. Caveat 2: To be precise, `RefCounted` is still `@system`. To avoid backward compatibility problems, you have to use `SafeRefCounted` instead if you want to be safe. Feel free to open another issue if you feel these caveats need closing.
Comment #10 by elpenguino+D — 2023-03-23T19:15:10Z
This hasn't been solved at all. RefCounted still isn't @safe. SafeRefCounted is merely a workaround. If the existing RefCounted isn't going to be made @safe, it should be deprecated and removed.