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