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 @@ -575,15 +575,18 @@ nothrow @safe @nogc unittest
575
575
// / ditto
576
576
void destroy (T)(T obj) if (is (T == class ))
577
577
{
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; } ();
578
581
static if (__traits(getLinkage, T) == " C++" )
579
582
{
580
583
obj.__xdtor();
581
584
582
585
enum classSize = __traits(classInstanceSize, T);
583
- ( cast ( void * )obj) [0 .. classSize] = typeid (T).initializer[];
586
+ ptr [0 .. classSize] = typeid (T).initializer[];
584
587
}
585
588
else
586
- rt_finalize(cast ( void * )obj );
589
+ rt_finalize(ptr );
587
590
}
588
591
589
592
// / ditto
@@ -670,6 +673,40 @@ nothrow @safe @nogc unittest
670
673
assert (i == 0 ); // `i` is back to its initial state `0`
671
674
}
672
675
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
+
673
710
unittest
674
711
{
675
712
interface I { }
You can’t perform that action at this time.
0 commit comments