Skip to content

Possible regression in hasElaborateDestructor detection since DMD 2.111.0 #10950

Closed
dlang/dmd
#22554

Description

@tchaloupka

I've found what seems to be a regression related to the hasElaborateDestructor trait.
When compiling and running the following code with DMD 2.110.0, I get this output:

~C(0)
a.b.len=1
b.c.len=0
exit
~B(): c.len=0
~A(): b.len=1
~B(): c.len=1
~C(1)

However, when compiled with DMD 2.111.0, I get:

No destructor found for B
No destructor found for C

~C(1)
a.b.len=1
b.c.len=0
exit
~B(): c.len=0
~A(): b.len=1

So it seems that in 2.111.0 the compiler incorrectly determines that B and C have no destructors, even though they clearly do.

Here’s the minimal reproducible example:

import core.lifetime : moveEmplace;
import core.stdc.stdio : printf;
import core.stdc.stdlib : malloc, realloc, free;

import std.algorithm : max;
import std.traits : hasElaborateDestructor, isImplicitlyConvertible;

void main()
{
    A a;
    B b;
    b.c ~= C(1);
    a.b ~= b;

    printf("a.b.len=%ld\n", a.b.len);
    printf("b.c.len=%ld\n", b.c.len);
    printf("exit\n");
}

struct Array(T, size_t step = 64)
{
    @disable this(this);
    this(size_t cap) @trusted nothrow @nogc {
        buf = (cast(T*)malloc(T.sizeof * cap))[0..cap];
    }

    ~this() nothrow @nogc {
        if (buf) {
            static if (hasElaborateDestructor!T) {
                foreach (ref item; buf[0..len]) destroy(item);
            }
            else pragma(msg, "No destructor found for ", T);
            free(buf.ptr);
        }
    }

    void reserve(size_t elements) @trusted nothrow @nogc {
        if (buf.length - len >= elements) return;
        const newLen = buf.length + max(elements, step);
        if (!buf)
            buf = (cast(T*)malloc(T.sizeof * newLen))[0..newLen];
        else
            buf = (cast(T*)realloc(buf.ptr, T.sizeof * newLen))[0..newLen];
    }

    void put(U)(auto ref scope U item) @trusted
        if (isImplicitlyConvertible!(U, T))
    {
        reserve(1);
        () @trusted { item.moveEmplace(buf[len++]); }();
    }

    alias opOpAssign(string op : "~") = put;

private:
    T[] buf;
    size_t len;
}

struct A {
    Array!B b;
    ~this() nothrow @nogc {
        printf("~A(): b.len=%ld\n", b.len);
    }
}

struct B {
    Array!C c;
    ~this() nothrow @nogc {
        printf("~B(): c.len=%ld\n", c.len);
    }
}

struct C {
    int foo;
    ~this() nothrow @nogc {
        printf("~C(%d)\n", foo);
    }
}

Expected behavior:
hasElaborateDestructor!B and hasElaborateDestructor!C should return true (as in 2.110.0), since both types define destructors.

Actual behavior:
Starting with 2.111.0, both are incorrectly reported as having no destructors.

Activity

  1. added
    Severity:Regression
    on Oct 13, 2025
  2. tchaloupka commented on Feb 5, 2026

    @tchaloupka
    ContributorAuthor

    dmd-2.112.0 is still affected and also latest ldc2-1.42.0-beta2 has the same problem.

  3. ntrel commented on Feb 5, 2026

    @ntrel
    Contributor

    I think this is caused by a4f218a (itself a fix to its parent commit).

  4. ntrel commented on Feb 5, 2026

    @ntrel
    Contributor
  5. ntrel commented on Feb 5, 2026

    @ntrel
    Contributor

    Reduced:

    struct Array(T)
    {
        ~this() 
         {
            static if (hasElaborateDestructor!T) {}
         }
    }
    
    struct B {
        Array!C c;
        //Array!D d;
    }
    
    struct C {
        ~this()  {}
    }
    
    struct D {
        ~this()  {}
    }
    
    import std.traits : hasElaborateDestructor;
    pragma(msg, hasElaborateDestructor!C); // prints false
    

    Now comment B.c and uncomment B.d - the pragma then prints true. Compiler bug?

  6. jmdavis commented on Feb 9, 2026

    @jmdavis
    Member

    Basically, this shows another bug with the compiler incorrectly reporting information about __xdtor (or the lack thereof). So, we have at least two of them now - dlang/dmd#20547, which shows how __traits(hasMember, ..) incorrectly reports that __xdtor is present when it isn't, and now https://github.com/dlang/dmd/issues/22524, which shows how __traits(allMembers, ...) is missing __xdtor when it shouldn't be.

    In principle, all that hasElaborateDestructor should need to do is check whether the type has an __xdtor member. The old implementation did a bunch of extra type introspection to check all of the members for __dtor recursively, which instantiated a bunch of templates which shouldn't be necessary. When fixing it to do the right and efficient thing, I found dlang/dmd#20547 and had to work around it by using __traits(allMembers, ...) instead of __traits(hasMember, ..), which was still worse than what the implementation should be, but it avoided a bunch of template instantiations. However, this bug shows that __traits(allMembers, ...) also behaves incorrectly with regards to __xdtor - albeit differently.

    So, it looks like I'll probably have to switch it back to the old, overly complicated implementation to work around both bugs.

  7. jmdavis commented on Feb 9, 2026

    @jmdavis
    Member

    Actually, I can't switch to the old implementation, because I wasn't just simplifying the implementation. I was also fixing another bug. So, I'll have to figure out an implementation which doesn't reintroduce that bug while working around these compiler bugs. But such is life, I guess...

  8. transferred this issue fromdlang/dmdon Feb 11, 2026
  9. thewilsonator commented on Feb 11, 2026

    @thewilsonator
    Contributor

    Oh, wait @ntrel did you actually want this transferred?

  10. added theissue type on Feb 11, 2026
  11. ntrel commented on Feb 11, 2026

    @ntrel
    Contributor

    @thewilsonator I did because it affects phobos, but the implementation is in druntime so it could be either project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Severity:Regression

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions