Bug 20582 – destroy should be @nogc if class destructor is @nogc

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-02-15T13:27:26Z
Last change time
2020-02-15T14:27:41Z
Assigned to
No Owner
Creator
Harry Vennik

Comments

Comment #0 by htvennik — 2020-02-15T13:27:26Z
----- class C { ~this() @nogc { } } void foo() { auto c = new C(); () @nogc { destroy(c); } (); } ----- Above code snippet should compile, but doesn't: destroy_nogc.d(9): Error: @nogc delegate destroy_nogc.foo.__lambda1 cannot call non-@nogc function object.destroy!(true, C).destroy This also causes the following to fail: ----- import std.experimental.allocator : make, dispose; import std.experimental.allocator.mallocator : Mallocator; class C { ~this() @nogc { } } void foo() @nogc { auto c = Mallocator.instance.make!C(); Mallocator.instance.dispose(c); } ----- Here, dispose is inferred to be non-@nogc because it calls destroy, which is not @nogc, and thus the following error is produced: make_dispose_nogc.d(12): Error: @nogc function make_dispose_nogc.foo cannot call non-@nogc function std.experimental.allocator.dispose!(shared(Mallocator), C).dispose This effectively makes it impossible to write 100% @nogc code.
Comment #1 by moonlightsentinel — 2020-02-15T14:27:41Z
This is effectively caused by the way destructor chaining is implemented using rt_finalize (which iterates all destructors using RTTI). This makes static type checking for chained destructors impossible right now. So even if the destructor of the current class is @nogc those found in the parent classes might not be - without checking. *** This issue has been marked as a duplicate of issue 15246 ***