Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 3b9c093

Browse files
committed
Fix Issue 11294 - Object destruction with alias this
1 parent feb9616 commit 3b9c093

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/object.d

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,18 @@ nothrow @safe @nogc unittest
575575
/// ditto
576576
void destroy(T)(T obj) if (is(T == class))
577577
{
578+
// go through void** to avoid `alias this` preferring alias
579+
// this should not be necessary when @@@BUG6777@@@ is fixed
580+
auto ptr = () @trusted { return *cast(void**) &obj; } ();
578581
static if (__traits(getLinkage, T) == "C++")
579582
{
580583
obj.__xdtor();
581584

582585
enum classSize = __traits(classInstanceSize, T);
583-
(cast(void*)obj)[0 .. classSize] = typeid(T).initializer[];
586+
ptr[0 .. classSize] = typeid(T).initializer[];
584587
}
585588
else
586-
rt_finalize(cast(void*)obj);
589+
rt_finalize(ptr);
587590
}
588591

589592
/// ditto
@@ -670,6 +673,40 @@ nothrow @safe @nogc unittest
670673
assert(i == 0); // `i` is back to its initial state `0`
671674
}
672675

676+
unittest
677+
{
678+
// class with an `alias this`
679+
class A
680+
{
681+
static int dtorCount;
682+
~this()
683+
{
684+
dtorCount++;
685+
}
686+
}
687+
688+
class B
689+
{
690+
A a;
691+
alias a this;
692+
this()
693+
{
694+
a = new A;
695+
}
696+
static int dtorCount;
697+
~this()
698+
{
699+
dtorCount++;
700+
}
701+
}
702+
auto b = new B;
703+
assert(A.dtorCount == 0);
704+
assert(B.dtorCount == 0);
705+
destroy(b);
706+
assert(A.dtorCount == 0);
707+
assert(B.dtorCount == 1);
708+
}
709+
673710
unittest
674711
{
675712
interface I { }

0 commit comments

Comments
 (0)