Bug 9989 – destructor triggers creation of opAssign for structs
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-25T03:51:00Z
Last change time
2013-04-26T01:36:20Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
monarchdodra
Comments
Comment #0 by monarchdodra — 2013-04-25T03:51:08Z
As discussed here: https://github.com/D-Programming-Language/phobos/pull/1260
Declaring a destructor in a struct triggers the creation of an opAssign. It shouldn't.
Note that it doesn't create a postblit (which is correct):
//--------
struct S
{
~this(){}
}
void main()
{
S s;
s.opAssign(s); //OK, should fail.
s.__postblit(); //Correctly fails.
}
//--------
The reason an opAssign is generated if a destructor is present is because assignment can no longer just copy over the data. The destructor must be run, and the generated opAssign ensures that. The generated opAssign looks like:
S tmp = this; // bit copy
this = s; // bit copy
tmp.dtor();
Comment #3 by k.hara.pg — 2013-04-26T01:36:20Z
(In reply to comment #2)
> The reason an opAssign is generated if a destructor is present is because
> assignment can no longer just copy over the data. The destructor must be run,
> and the generated opAssign ensures that. The generated opAssign looks like:
>
> S tmp = this; // bit copy
> this = s; // bit copy
> tmp.dtor();
OK. it is sane behavior, and my fix was not correct.
But, if so, current dmd generates wrong code for struct S.
I filed new issue for the problem.
Issue 9994 - Built-in generated opAssign should call dtor on assignment