This repository was archived by the owner on Oct 12, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +39
-2
lines changed Expand file tree Collapse file tree 1 file changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -576,15 +576,18 @@ nothrow @safe @nogc unittest
576
576
// / ditto
577
577
void destroy (T)(T obj) if (is (T == class ))
578
578
{
579
+ // go through void** to avoid `alias this` preferring alias
580
+ // this should not be necessary when @@@BUG6777@@@ is fixed
581
+ auto ptr = () @trusted { return * cast (void ** ) &obj; } ();
579
582
static if (__traits(getLinkage, T) == " C++" )
580
583
{
581
584
obj.__xdtor();
582
585
583
586
enum classSize = __traits(classInstanceSize, T);
584
- ( cast ( void * )obj) [0 .. classSize] = typeid (T).initializer[];
587
+ ptr [0 .. classSize] = typeid (T).initializer[];
585
588
}
586
589
else
587
- rt_finalize(cast ( void * )obj );
590
+ rt_finalize(ptr );
588
591
}
589
592
590
593
// / ditto
@@ -671,6 +674,40 @@ nothrow @safe @nogc unittest
671
674
assert (i == 0 ); // `i` is back to its initial state `0`
672
675
}
673
676
677
+ unittest
678
+ {
679
+ // class with an `alias this`
680
+ class A
681
+ {
682
+ static int dtorCount;
683
+ ~this ()
684
+ {
685
+ dtorCount++ ;
686
+ }
687
+ }
688
+
689
+ class B
690
+ {
691
+ A a;
692
+ alias a this ;
693
+ this ()
694
+ {
695
+ a = new A;
696
+ }
697
+ static int dtorCount;
698
+ ~this ()
699
+ {
700
+ dtorCount++ ;
701
+ }
702
+ }
703
+ auto b = new B;
704
+ assert (A.dtorCount == 0 );
705
+ assert (B.dtorCount == 0 );
706
+ destroy (b);
707
+ assert (A.dtorCount == 0 );
708
+ assert (B.dtorCount == 1 );
709
+ }
710
+
674
711
unittest
675
712
{
676
713
interface I { }
You can’t perform that action at this time.
0 commit comments