Bug 24636 – [DIP1000] Closures need to capture the whole frame

Status
NEW
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-06-27T19:31:57Z
Last change time
2024-12-13T19:36:05Z
Keywords
safe
Assigned to
No Owner
Creator
Bolpat
Moved to GitHub: dmd#20477 →

Comments

Comment #0 by qs.il.paperinik — 2024-06-27T19:31:57Z
```d // Use -dip1000 int delegate() @safe f(bool fail)(int x) @safe { int* p = &x; static if (fail) return () => *p; else return () { cast(void) x; return *p; }; } void main() @safe { auto dg = f!false(42); assert(dg() == 42); } ``` If a variable isn’t expressly named in a closure, it is not captured. When `f` returns, `p` points to `x`, but `x` is not part of the capture and therefore gone when it’s not mentioned itself. The compiler, unless it can prove that for some of the local state, it cannot possibly be referenced by a delegate, must put the whole stack frame into the closure. This seems similar to https://issues.dlang.org/show_bug.cgi?id=20956, but it requires nothing but being able to take the address of `x`.
Comment #1 by robert.schadek — 2024-12-13T19:36:05Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20477 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB