Skip to content

__traits(allMembers, T) sometimes missing __xdtor when it's used lexically prior to T's declaration #22524

Closed

Description

@ntrel
template hasElaborateDestructor(S)
{
    // C has no __xdtor
    pragma(msg, S, " template scope: ", __traits(allMembers, S));

    enum hasElaborateDestructor = hasDtor([__traits(allMembers, S)]);
}

private bool hasDtor(string[] members)
{
    foreach (name; members)
    {
        if (name == "__xdtor")
            return true;
    }
    return false;
}

struct Array(T)
{
    ~this() 
    {
        static if (hasElaborateDestructor!T) {}
    }
}

struct B {
    Array!C c;
}

struct C {
    ~this()  {}
}

struct D {
    ~this()  {}
}

pragma(msg, "C: ", hasElaborateDestructor!C); // wrongly false due to B
pragma(msg, "D: ", hasElaborateDestructor!D); // true

// C has __xdtor
pragma(msg, "C module scope: ", __traits(allMembers, C));

Reduced from dlang/phobos#10950.

Activity

  1. jmdavis commented on Feb 9, 2026

    @jmdavis
    Member

    Strictly speaking, neither the static if nor a template is required to trigger this issue. After messing around with it a bit, this will also reproduce it:

    bool hasDtor(string[] members)
    {
        foreach (name; members)
        {
            if (name == "__xdtor")
                return true;
        }
        return false;
    }
    
    enum e = hasDtor([__traits(allMembers, C)]); // false
    static assert(hasDtor([__traits(allMembers, C)])); // true as it should be
    pragma(msg, hasDtor([__traits(allMembers, C)])); // false, but should be true
    pragma(msg, e); // false, but should be true
    struct C {
        ~this() {}
    }
    
    static assert(hasDtor([__traits(allMembers, C)]));
    pragma(msg, hasDtor([__traits(allMembers, C)])); // true as it should be
    static assert(e); // fails

    The primary thing that seems to matter is whether __traits(allMembers, C) is being used lexically above C being declared. However, note that using hasDtor([__traits(allMembers, C)]) in a static assertion prior to C's succeeds like it's supposed to, whereas storing the result in a manifest constant or printing it out with a pragma gives the wrong answer.

    So, something very wrong is going on here. And it may or may not be related to #20547 (which is why the current implementation of hasElaborateDestructor doesn't just use __traits(hasMember, S, "__xdtor")), though that involves the compiler incorrectly reporting that there is an __xdtor when there isn't, so it's definitely not the same issue even if it's possible that it's the same piece of code causing both problems.

    Also, interestingly, when I look at the disassembly on godbolt, C doesn't have an __xdtor symbol. My understanding was that any struct which has an explicit destructor has __dtor (which is the symbol for the destructor), and any struct which either has a destructor and/or has at least one member with a destructor then has an __xdtor which the correctly calls __dtor if there is one and calls the __xdtor on each of the member variables. For instance,

    struct C
    {
        ~this() {}
    }
    
    struct D
    {
        C c;
    }
    pragma(msg, __traits(allMembers, C));
    pragma(msg, __traits(allMembers, D));

    prints out

    AliasSeq!("__dtor", "__xdtor", "opAssign")
    AliasSeq!("c", "__xdtor", "opAssign")
    

    However, when I look at the disassembly on godbolt, __xdtor does not exist, and instead, D has a __fieldDtor member which calls C's __dtor.

    So, the members that the compiler shows for destructors don't actually match what the compiler generates. I don't know if that's because the representation was changed, but what the type introspection reports wasn't changed, because we didn't want to break code, or what. But either way, there's clearly some funny business going on with __xdtor and how the compiler handles it, and it wouldn't surprise me if the fact that it's not something that matches what ends up in the binary could factor into why we're getting weird bugs with type introspection when __xdtor is involved.

  2. added theissue type on Feb 9, 2026
  3. changed the title [-]`__xdtor` missing when template instantiated in `static if`[/-] [+]`__traits(allMembers, T)` sometimes missing `__xdtor` when it's used lexically prior to `T`'s declaration[/+] on Feb 9, 2026
  4. jmdavis commented on Feb 9, 2026

    @jmdavis
    Member

    And to take the example from #20547, if we have

    static struct S2 { ~this() {} }
    static struct S3 { S2 field; }
    static struct S6 { S3[0] field; }
    
    pragma(msg, "S2: " ~ __traits(allMembers, S2).stringof);
    pragma(msg, "S2: " ~ __traits(hasMember, S2, "__xdtor").stringof);
    
    pragma(msg, "S3: " ~ __traits(allMembers, S3).stringof);
    pragma(msg, "S3: " ~ __traits(hasMember, S3, "__xdtor").stringof);
    
    pragma(msg, "S6: " ~ __traits(allMembers, S6).stringof);
    pragma(msg, "S6: " ~ __traits(hasMember, S6, "__xdtor").stringof);

    it prints out

    S2: AliasSeq!("__dtor", "__xdtor", "opAssign")
    S2: true
    S3: AliasSeq!("field", "__xdtor", "opAssign")
    S3: true
    S6: AliasSeq!("field", "opAssign")
    S6: true
    

    whereas if we switch the order

    pragma(msg, "S2: " ~ __traits(allMembers, S2).stringof);
    pragma(msg, "S2: " ~ __traits(hasMember, S2, "__xdtor").stringof);
    
    pragma(msg, "S3: " ~ __traits(allMembers, S3).stringof);
    pragma(msg, "S3: " ~ __traits(hasMember, S3, "__xdtor").stringof);
    
    pragma(msg, "S6: " ~ __traits(allMembers, S6).stringof);
    pragma(msg, "S6: " ~ __traits(hasMember, S6, "__xdtor").stringof);
    
    static struct S2 { ~this() {} }
    static struct S3 { S2 field; }
    static struct S6 { S3[0] field; }

    we get

    S2: AliasSeq!("__dtor")
    S2: true
    S3: AliasSeq!("field")
    S3: true
    S6: AliasSeq!("field")
    S6: true
    

    So, not only does it affect __xdtor, but it affects the generated opAssign.

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

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions