[Issue 22174] New: destroy should be @nogc when class destructor is @nogc

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 3 18:12:19 UTC 2021


          Issue ID: 22174
           Summary: destroy should be @nogc when class destructor is @nogc
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody at puremagic.com
          Reporter: sbuhesap at gmail.com

Pretty much same as the issue summary: destroy should be @nogc when class
destructor is @nogc.

A piece of code that should work but doesn't:

import core.stdc.stdlib;
import core.lifetime;

class Example {
    int x;

    ~this() @nogc {}

void main() @nogc {
    enum size = __traits(classInstanceSize, Example);
    auto pointer = malloc(size)[0..size];
    scope(exit) free(pointer.ptr);

    Example ex = emplace!(Example)(pointer); // Error: `@nogc` function `D
main` cannot call non- at nogc function `object.destroy!(true, Example).destroy`

There should be a @nogc overload for destroy that allows people to use class
destructors. Currently there is no way to call destroy in a @nogc function
without breaking the type system. 

Or just un-deprecating class allocators / dealloctors would work too.

A recent discussion I found about this:


More information about the Digitalmars-d-bugs mailing list