Skip to content

Commit 105f2ff

Browse files
Fix hasElabroateDestructor and hasAnyDestructor template logic
1 parent 968ea4e commit 105f2ff

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

source/numem/core/memory.d

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ T[] nu_malloca(T)(size_t count) {
161161
/**
162162
Frees a slice.
163163
164+
Notes:
165+
This function will call destructors for types that have destructors
166+
(implicit or otherwise). you may use $(D nogc_zeroinit) to zero fill the
167+
slice before freeing if need be.
168+
164169
Params:
165170
slice = The slice to free.
166171
*/
@@ -169,7 +174,7 @@ void nu_freea(T)(ref T[] slice) {
169174
foreach(i; 0..slice.length) {
170175
nu_release(slice[i]);
171176
}
172-
} else static if (hasElaborateDestructor!T) {
177+
} else static if (hasAnyDestructor!T) {
173178
nogc_delete(slice[0..$]);
174179
}
175180

source/numem/core/traits.d

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,10 @@ template hasElaborateCopyConstructor(T) {
497497
Gets whether type $(D T) has elaborate destructor semantics (is ~this() declared).
498498
*/
499499
template hasElaborateDestructor(T) {
500-
static if (isObjectiveC!T)
501-
enum bool hasElaborateDestructor = false;
502-
else static if (__traits(isStaticArray, T))
500+
static if (__traits(isStaticArray, T))
503501
enum bool hasElaborateDestructor = T.sizeof && hasElaborateDestructor!(BaseElemOf!T);
504-
else static if (isAggregateType!T)
505-
enum bool hasElaborateDestructor = __traits(hasMember, T, "__dtor") ||
506-
anySatisfy!(.hasElaborateDestructor, Fields!T);
502+
else static if (is(T == struct))
503+
enum bool hasElaborateDestructor = is(typeof(() { T.init.__xdtor(); }));
507504
else
508505
enum bool hasElaborateDestructor = false;
509506
}
@@ -517,14 +514,19 @@ template hasAnyDestructor(T) {
517514
enum bool hasAnyDestructor = false;
518515
else static if (__traits(isStaticArray, T))
519516
enum bool hasAnyDestructor = T.sizeof && hasAnyDestructor!(BaseElemOf!T);
520-
else static if (isAggregateType!T)
521-
enum bool hasAnyDestructor =
522-
is(T == class) || // Classes have implicit destructors
523-
is(T == interface) || // Interfaces have implicit destructors
524-
__traits(hasMember, T, "__xdtor") ||
525-
__traits(hasMember, T, "__dtor") ||
526-
anySatisfy!(.hasAnyDestructor, Fields!T);
527-
else
517+
else static if (is(T == class)) // Classes have implicit destructors
518+
enum bool hasAnyDestructor = true;
519+
else static if (is(T == interface)) // Interfaces have implicit destructors
520+
enum bool hasAnyDestructor = true;
521+
else static if (is(T == struct)) {
522+
523+
// NOTE: Some types should always report as true, especially if said types
524+
// can be self-referential.
525+
static if (is(typeof(() { T.init.__xdtor(); })))
526+
enum bool hasAnyDestructor = true;
527+
else
528+
enum bool hasAnyDestructor = anySatisfy!(.hasAnyDestructor, Fields!T);
529+
} else
528530
enum bool hasAnyDestructor = false;
529531
}
530532

source/numem/optional.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public:
9494
*/
9595
void reset() @trusted nothrow {
9696
static if (isHeapAllocated!T) {
97-
static if (hasElaborateDestructor!T)
97+
static if (hasAnyDestructor!T)
9898
nogc_trydelete(this.value_);
9999
else {
100100
nu_free(this.value_);

0 commit comments

Comments
 (0)