You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
__traits(allMembers, T) sometimes missing __xdtor when it's used lexically prior to T's declaration #22524
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:
boolhasDtor(string[] members)
{
foreach (name; members)
{
if (name =="__xdtor")
returntrue;
}
returnfalse;
}
enum e = hasDtor([__traits(allMembers, C)]); // falsestaticassert(hasDtor([__traits(allMembers, C)])); // true as it should bepragma(msg, hasDtor([__traits(allMembers, C)])); // false, but should be truepragma(msg, e); // false, but should be truestructC {
~this() {}
}
staticassert(hasDtor([__traits(allMembers, C)]));
pragma(msg, hasDtor([__traits(allMembers, C)])); // true as it should bestaticassert(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,
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.
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
Activity
jmdavis commentedon Feb 9, 2026
Strictly speaking, neither the
static ifnor a template is required to trigger this issue. After messing around with it a bit, this will also reproduce it:The primary thing that seems to matter is whether
__traits(allMembers, C)is being used lexically aboveCbeing declared. However, note that usinghasDtor([__traits(allMembers, C)])in a static assertion prior toC's succeeds like it's supposed to, whereas storing the result in a manifest constant or printing it out with apragmagives 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
hasElaborateDestructordoesn't just use__traits(hasMember, S, "__xdtor")), though that involves the compiler incorrectly reporting that there is an__xdtorwhen 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,
Cdoesn't have an__xdtorsymbol. 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__xdtorwhich the correctly calls__dtorif there is one and calls the__xdtoron each of the member variables. For instance,prints out
However, when I look at the disassembly on godbolt,
__xdtordoes not exist, and instead,Dhas a__fieldDtormember which callsC'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
__xdtorand 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__xdtoris involved.[-]`__xdtor` missing when template instantiated in `static if`[/-][+]`__traits(allMembers, T)` sometimes missing `__xdtor` when it's used lexically prior to `T`'s declaration[/+]jmdavis commentedon Feb 9, 2026
And to take the example from #20547, if we have
it prints out
whereas if we switch the order
we get
So, not only does it affect
__xdtor, but it affects the generatedopAssign.`__traits(allMembers, Struct)` should call `dsymbolSemantic`
`__traits(allMembers, Struct)` should call `dsymbolSemantic` (#22657)
`__traits(allMembers, Struct)` should call `dsymbolSemantic` (dlang/d…