Skip to content

Commit

Permalink
Workaround in hasElaborateDestructor for issue# 24865.
Browse files Browse the repository at this point in the history
__traits(hasMember, ...) currently, incorrectly, reports that some types
have __xdtor when they don't. So unfortunately, we have to check with
__traits(allMembers, ...) until that's fixed.
  • Loading branch information
jmdavis authored and thewilsonator committed Nov 18, 2024
1 parent 66fe8bc commit a4f218a
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion druntime/src/core/internal/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,30 @@ template hasElaborateDestructor(S)
}
else static if (is(S == struct))
{
enum hasElaborateDestructor = __traits(hasMember, S, "__xdtor");
// Once https://issues.dlang.org/show_bug.cgi?id=24865 is fixed, then
// this should be the implementation, but until that's fixed, we need the
// uncommented code.
// enum hasElaborateDestructor = __traits(hasMember, S, "__xdtor");

enum hasElaborateDestructor = hasDtor([__traits(allMembers, S)]);
}
else
{
enum bool hasElaborateDestructor = false;
}
}

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

return false;
}

@safe unittest
{
static struct NoDestructor {}
Expand Down Expand Up @@ -310,6 +326,18 @@ template hasElaborateDestructor(S)
static assert(!hasElaborateDestructor!(int[]));
}

// https://issues.dlang.org/show_bug.cgi?id=24865
@safe unittest
{
static struct S2 { ~this() {} }
static struct S3 { S2 field; }
static struct S6 { S3[0] field; }

static assert( hasElaborateDestructor!S2);
static assert( hasElaborateDestructor!S3);
static assert(!hasElaborateDestructor!S6);
}

// std.traits.hasElaborateCopyDestructor
template hasElaborateCopyConstructor(S)
{
Expand Down

0 comments on commit a4f218a

Please sign in to comment.