Destructors and static array assignment

David Nadlinger via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 20 07:18:31 PDT 2015


Hi all,

I was about to fix an issue with postblits/destructors in LDC 
when I stumbled upon the following vexing behaviour in DMD (both 
2.067.1 and master):

---
uint dtorCount;

struct S {
     uint x;
     void opAssign(const ref S rhs) { assert(false, "Not called"); 
}
     ~this() { ++dtorCount; }
}


void main() {
     S[2] a;
     a[0].x = 42;

     a = a.init;

     assert(a[0].x == 0); // as expected the value has been reset
     assert(dtorCount == 0); // Passes?!?
}
---

I would have expected this to either call opAssign or to destruct 
the instance, blit the init value, and call any postblits. 
However, as you can see neither the dtor nor opAssign are 
executed. If I add a postblit to S, then suddenly the dtor is 
invoked too.

Am I missing something here, or is this a major bug in struct 
lifetime handling?

I understand why this happens from the DMD source, but it almost 
seems like it was deliberately written this way; thus the 
question.

  — David


More information about the Digitalmars-d mailing list