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

Commit

Permalink
Merge pull request #2366 from radcapricorn/fix11294
Browse files Browse the repository at this point in the history
Fix Issue 11294 - Object destruction with alias this
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Nov 27, 2018
2 parents 7438796 + 3b9c093 commit 4fd4fa3
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/object.d
Expand Up @@ -576,15 +576,18 @@ nothrow @safe @nogc unittest
/// ditto
void destroy(T)(T obj) if (is(T == class))
{
// go through void** to avoid `alias this` preferring alias
// this should not be necessary when @@@BUG6777@@@ is fixed
auto ptr = () @trusted { return *cast(void**) &obj; } ();
static if (__traits(getLinkage, T) == "C++")
{
obj.__xdtor();

enum classSize = __traits(classInstanceSize, T);
(cast(void*)obj)[0 .. classSize] = typeid(T).initializer[];
ptr[0 .. classSize] = typeid(T).initializer[];
}
else
rt_finalize(cast(void*)obj);
rt_finalize(ptr);
}

/// ditto
Expand Down Expand Up @@ -671,6 +674,40 @@ nothrow @safe @nogc unittest
assert(i == 0); // `i` is back to its initial state `0`
}

unittest
{
// class with an `alias this`
class A
{
static int dtorCount;
~this()
{
dtorCount++;
}
}

class B
{
A a;
alias a this;
this()
{
a = new A;
}
static int dtorCount;
~this()
{
dtorCount++;
}
}
auto b = new B;
assert(A.dtorCount == 0);
assert(B.dtorCount == 0);
destroy(b);
assert(A.dtorCount == 0);
assert(B.dtorCount == 1);
}

unittest
{
interface I { }
Expand Down

0 comments on commit 4fd4fa3

Please sign in to comment.