copying a struct containing a struct

eskimo jfanatiker at gmx.at
Fri Nov 16 13:03:02 PST 2012


I hit a strange problem, which seems to be a compiler bug, but I kind of
doubt it, because it is really serious and I did not find anything in
bugzilla. So here is the example code, that behaves really not as
expected and in my understanding just plain and seriously wrong:

---
#!/usr/bin/rdmd
import std.stdio;

struct Test {
    this(this) {
        writeln("postblit called for: ", &this);
    }
    ref Test opAssign(ref Test other) {
        writeln("opAssign called for: ", &this);
        return this;
    }

    ~this() {
        writeln("Destructed: ", &this);
    }
    int buf;

}
struct TTest {
    Test inner;
}
void main() {
    TTest t1;
    TTest t2;
    writeln("t1: ", &t1.inner);
    writeln("t2: ", &t2.inner);
    t1=t2;
}
---

I would have expected, that opAssign is called for t1 and the
destructors of the two structs. The output:
---
./test1.d
t1: BFF57114
t2: BFF57118
postblit called for: BFF5711C
Destructed: BFF570F4
Destructed: BFF57118
Destructed: BFF57114
---

So instead of opAssign postblit gets called and not even for one of the
two created structs but for some temporary (BFF5711C)? This alone would
be already wrong behaviour, but to make matters worse, for this
temporary not even the destructor is called, instead the destructor of
some other unknown object gets called: BFF570F4.

Does someone have a clue what is going on? Is there a bug report I
missed? 

Btw. if you replace t1=t2; with t1.inner=t2.inner; everything works as
expected.



More information about the Digitalmars-d mailing list