[Issue 21762] object.destroy may silently fail depending on whether a member function is a template

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jul 1 01:37:34 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=21762

--- Comment #7 from thomas.bockman at gmail.com ---
I ran into this again in a different context. One workaround for the compiler
bug is to change the definition of object.destroy to do the work of __xdtor
itself when the type has a destructor, but no __xdtor is found:

//////////////////////////////////////////////////////////////////
import core.internal.lifetime : emplaceInitializer;
import core.internal.traits : hasElaborateDestructor;

void destroy(bool initialize = true, T)(ref T obj)
    if(is(T == struct))
{
    static if(initialize) {
        destroy!false(obj);
        emplaceInitializer(obj);
    } else
    static if(hasElaborateDestructor!T) {
        static if(__traits(hasMember, T, `__xdtor`)
        && __traits(isSame, T, __traits(parent, obj.__xdtor)))
            obj.__xdtor();
        else {
            static if(__traits(hasMember, T, `__dtor`)
            && __traits(isSame, T, __traits(parent, obj.__dtor)))
                obj.__dtor();
            foreach_reverse(ref objField; obj.tupleof)
                destroy!false(objField);
        }
    }
}
//////////////////////////////////////////////////////////////////

Should I submit a druntime pull request for this?

--


More information about the Digitalmars-d-bugs mailing list