[Issue 24636] New: Closures need to capture the whole frame
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Thu Jun 27 19:31:57 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=24636
Issue ID: 24636
Summary: Closures need to capture the whole frame
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: major
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: qs.il.paperinik at gmail.com
```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`.
--
More information about the Digitalmars-d-bugs
mailing list