Skip to content

__traits(hasMember, T, "__xdtor") is true in some cases where there is no __xdtor member #20547

Open

Description

@dlangBugzillaToGithub

Jonathan M Davis (@jmdavis) reported this on 2024-11-18T11:30:12Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=24865

Description

This code

void main()
{
    import std.stdio;
    import std.traits;

    static struct S2 { ~this() { writeln("destroyed"); } }
    static struct S3 { S2 field; }
    static struct S6 { S3[0] field; }

    S6 s6;

    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);
}

prints out

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

when compiling, and it prints out nothing when it runs.

S6 should not have a destructor, because while the elements of its static array have destructors, the static array has no length, and so, there's nothing to destroy.
The fact that nothing prints out when running the program shows that S6 has no destructor. allMembers also has no __xdtor member, which lines up with the behavior of the running program. However, for some reason, hasMember reports that there is an __xdtor member.

Given that allMembers matches the actual behavior, I'm inclined to think that the bug is in hasMember rather than there being an invisible __xdtor that allMembers isn't reporting, but either way, there is a discrepancy here which needs to be fixed.

This affects the implementation of {core.internal,std}.traits.hasElaborateDestructor.

Activity

  1. thewilsonator commented on Apr 13, 2025

    @thewilsonator
    Contributor
    S2 s2;
    s2.__xdtor(); // compiles
    S3 s3;
    s3.__xdtor(); // compiles
    S6 s6;
    s6.__xdtor(); // Error: `this` for `~this` needs to be type `S3` not type `S6`

    which is... not an error I would expect, but still fails.

  2. thewilsonator commented on Apr 13, 2025

    @thewilsonator
    Contributor

    cc @RazvanN7 you last touched this for #15406

  3. jmdavis commented on Feb 9, 2026

    @jmdavis
    Member

    #22524 is probably related to this in some manner, though if so, the issue manifests differently in each case.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions