Bug 17246 – [REG2.053] Extra destructor call.

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-03-08T08:16:15Z
Last change time
2017-12-18T22:55:43Z
Assigned to
No Owner
Creator
Jack Applegame

Comments

Comment #0 by japplegame — 2017-03-08T08:16:15Z
import std.stdio; struct Foo { int val; this(int val) { writefln("%s.this(int)", val); this.val = val; } this(this) { writefln("%s.this(this)", val); this.val = val; } ~this() { writefln("%s.~this()", val); this.val = val; } } struct Bar { Foo foo; this(Foo foo, bool) { this.foo = foo; } } bool fun(bool val) { return !val; } auto genBar(bool flag) { return flag ? Bar() : Bar(Foo(10), fun(!flag)); } void main(string[] args) { auto bar = genBar(args.length == 0); } Compiler generates extra destructor call: 10.this(int) 10.this(this) 10.~this() 10.~this() 10.~this() If we slightly change function genBar: auto genBar(bool flag) { auto a = flag ? Bar() : Bar(Foo(10), fun(!flag)); return a; } or auto genBar(bool flag) { return flag ? Bar() : Bar(Foo(10), false); } then output looks as expected: 10.this(int) 10.this(this) 10.~this() 10.~this()
Comment #1 by dlang-bugzilla — 2017-07-02T14:36:29Z
Same example with asserts replacing writelns, and checks based on simple reference counting: struct Foo { int* rc; this(int val) { rc = new int; (*rc) = 1; } this(this) { (*rc)++; } ~this() { if (rc) { assert(*rc > 0); (*rc)--; } } } struct Bar { Foo foo; this(Foo foo, bool) { this.foo = foo; } } bool fun(bool val) { return !val; } auto genBar(bool flag) { return flag ? Bar() : Bar(Foo(10), fun(!flag)); } void main(string[] args) { auto bar = genBar(args.length == 0); } This appears to be a regression, as it worked before 2.052. Introduced in https://github.com/dlang/dmd/commit/e764b3949ae0f95f8fc4d7d2e9114e29fee12493
Comment #2 by bugzilla — 2017-10-24T10:09:01Z
Comment #3 by github-bugzilla — 2017-10-26T12:04:32Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/6eccdeee1d4c41abb0a6e8733e13a85b63765266 fix Issue 17246 - [REG2.053] Extra destructor call. https://github.com/dlang/dmd/commit/614da7260f173ae5dc0ec87e8fa85d3edfb031ce Merge pull request #7238 from WalterBright/fix17246 fix Issue 17246 - [REG2.053] Extra destructor call.
Comment #4 by slavo5150 — 2017-12-09T09:46:52Z
*** Issue 18050 has been marked as a duplicate of this issue. ***
Comment #5 by schveiguy — 2017-12-09T15:05:01Z
*** Issue 17897 has been marked as a duplicate of this issue. ***
Comment #6 by github-bugzilla — 2017-12-18T22:55:43Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/6eccdeee1d4c41abb0a6e8733e13a85b63765266 fix Issue 17246 - [REG2.053] Extra destructor call. https://github.com/dlang/dmd/commit/614da7260f173ae5dc0ec87e8fa85d3edfb031ce Merge pull request #7238 from WalterBright/fix17246