Issue 24865 - __traits(hasMember, T, "__xdtor") is true in some cases where there is no __xdtor member
Summary: __traits(hasMember, T, "__xdtor") is true in some cases where there is no __x...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-11-18 11:30 UTC by Jonathan M Davis
Modified: 2024-11-18 11:30 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonathan M Davis 2024-11-18 11:30:12 UTC
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.