Skip to content

Fix issue 15829 - hasElaborateDestructor doesn't work for classes #4119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -3208,24 +3208,23 @@ unittest
}

/**
True if $(D S) or any type directly embedded in the representation
of $(D S) defines an elaborate destructor. Elaborate destructors
are introduced by defining $(D ~this()) for a $(D
struct).
True if `T` or any type directly embedded in the representation
of `T` defines an elaborate destructor. Elaborate destructors
are introduced by defining `~this()`.

Classes and unions never have elaborate destructors, even
though classes may define $(D ~this()).
Note: Previously hasElaborateDestructor worked only for strusts, but
starting with DMD 2.072 it was extended to support classes.
*/
template hasElaborateDestructor(S)
template hasElaborateDestructor(T)
{
static if (isStaticArray!S && S.length)
static if (isStaticArray!T && T.length)
{
enum bool hasElaborateDestructor = hasElaborateDestructor!(typeof(S.init[0]));
enum bool hasElaborateDestructor = hasElaborateDestructor!(typeof(T.init[0]));
}
else static if (is(S == struct))
else static if (is(T == struct) || is(T == class))
{
enum hasElaborateDestructor = hasMember!(S, "__dtor")
|| anySatisfy!(.hasElaborateDestructor, FieldTypeTuple!S);
enum hasElaborateDestructor = hasMember!(T, "__dtor")
|| anySatisfy!(.hasElaborateDestructor, FieldTypeTuple!T);
}
else
{
Expand All @@ -3245,6 +3244,8 @@ unittest
static struct S5 { S3[] field; }
static struct S6 { S3[0] field; }
static struct S7 { @disable this(); S3 field; }
static class C1 { }
static class C2 { ~this() {} }
static assert(!hasElaborateDestructor!S1);
static assert( hasElaborateDestructor!S2);
static assert( hasElaborateDestructor!(immutable S2));
Expand All @@ -3255,6 +3256,8 @@ unittest
static assert(!hasElaborateDestructor!S5);
static assert(!hasElaborateDestructor!S6);
static assert( hasElaborateDestructor!S7);
static assert(!hasElaborateDestructor!C1);
static assert( hasElaborateDestructor!C2);
}

alias Identity(alias A) = A;
Expand Down