Bug 15952 – [REG 2.053] struct in closure is destroyed when parent function returns

Status
NEW
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-04-23T18:25:12Z
Last change time
2024-12-13T18:47:37Z
Keywords
wrong-code
Assigned to
No Owner
Creator
ag0aep6g
Moved to GitHub: dmd#19119 →

Comments

Comment #0 by ag0aep6g — 2016-04-23T18:25:12Z
Found by Ramon who posted to D.learn: http://forum.dlang.org/post/[email protected] ---- struct S { int x; ~this() {x = 1;} } void delegate() makeDelegate() { auto s = S(2); return {assert(s.x == 2); /* fails; should pass */}; } void main() { auto dg = makeDelegate(); dg(); } ---- Apparently, the destructor is called when makeDelegate returns. But s is in a closure and should be destroyed only when that closure is collected.
Comment #1 by snarwin+bugzilla — 2021-04-05T23:23:27Z
*** Issue 21669 has been marked as a duplicate of this issue. ***
Comment #2 by johan_forsberg_86 — 2021-04-06T05:22:08Z
Five year anniversary?
Comment #3 by schveiguy — 2021-04-06T17:07:18Z
As my issue got marked as a duplicate of this, I'd say the title and expectation is different from mine. It would be a noble goal for the compiler to try and make this work, but I think the correct path is to disallow this delegate. Prior to 2.053, this was an error because the destructor would render the instance invalid. I think we should revert to that expectation -- a struct reference shnould not outlive its lifetime.
Comment #4 by ibuclaw — 2022-12-29T19:27:47Z
(In reply to Steven Schveighoffer from comment #3) > As my issue got marked as a duplicate of this, I'd say the title and > expectation is different from mine. > In both cases, there's a delegate function that references a field of the struct being destructor'd. Just two different ways to exit a scope to trigger the dtor call.
Comment #5 by ibuclaw — 2022-12-29T20:30:08Z
Comment #6 by robert.schadek — 2024-12-13T18:47:37Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19119 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB