Bug 18045 – Temporary created during comparison not destroyed

Status
RESOLVED
Resolution
FIXED
Severity
blocker
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-12-07T17:27:02Z
Last change time
2017-12-18T22:58:13Z
Assigned to
No Owner
Creator
Andrei Alexandrescu

Comments

Comment #0 by andrei — 2017-12-07T17:27:02Z
Consider: import std.stdio; struct A { int state; this(this) { writeln("postblit: A(", state, ")"); } ~this() { writeln("dtor: A(", state, ")"); } } A fun() { static a = A(42); return a; } int main() { static a = A(-42); return fun() == a; } The code outputs: postblit: A(42) What happens is the following. Static objects issue no constructor or destructor calls. The construction is done during compilation, and destructor is not called for data with static lifetime (see https://issues.dlang.org/show_bug.cgi?id=14650). The postblitted object is the temporary copied from within fun() into its return value. As the output shows, that temporary is never destroyed. It should be destroyed at the end of the full expression. This bug seems to manifest itself with '==` and 'is', but not if I write e.g. return fun().state == 0;
Comment #1 by bugzilla — 2017-12-08T07:24:47Z
Comment #2 by github-bugzilla — 2017-12-08T08:29:38Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/0420fa0cdffc3a846216ab3c31c31571660089cd fix Issue 18045 - Temporary created during comparison not destroyed https://github.com/dlang/dmd/commit/68cd273c85dba9946ecd6e4fcfc40461494b284a Merge pull request #7407 from WalterBright/fix18045 fix Issue 18045 - Temporary created during comparison not destroyed merged-on-behalf-of: Mike Franklin <[email protected]>
Comment #3 by github-bugzilla — 2017-12-18T22:58:13Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/0420fa0cdffc3a846216ab3c31c31571660089cd fix Issue 18045 - Temporary created during comparison not destroyed https://github.com/dlang/dmd/commit/68cd273c85dba9946ecd6e4fcfc40461494b284a Merge pull request #7407 from WalterBright/fix18045