Comment #0 by snarwin+bugzilla — 2023-10-30T18:39:31Z
As of DMD 2.105.2, the following invalid program compiles and runs without errors:
---
alias Dg = void delegate(int* p) @safe pure nothrow;
void main() @safe
{
int* escaped;
int n;
Dg dg = delegate void (int* p) { escaped = p; };
dg(&n);
}
---
This program is invalid because, in @safe code, it assigns the address of the variable `n` to the variable `escaped`, which has a longer lifetime than `n`.
The expression `dg(&n)` should cause a compile-time error, because it assigns the scope pointer value `&n` to the non-scope parameter `p`.
The compiler allows this because, due to the rules laid out in "Inferred scope parameters in pure functions" [1], it believes that the parameter of `dg` cannot escape. However, these rules do not account for the possibility that a pure delegate may escape a non-scope parameter via its nested context.
[1]: https://dlang.org/spec/function.html#pure-scope-inference
Comment #1 by dlang-bot — 2023-10-30T20:04:26Z
@pbackus created dlang/dmd pull request #15756 "Fix 24208, 24212, 24213 - `scope` escape via `pure` + nested context" fixing this issue:
- Fix issues 24212 and 24213
* Issue 24212 - Scope pointer can escape via non-scope parameter of pure
virtual function
* Issue 24213 - Scope pointer can escape via non-scope parameter of pure
delegate
https://github.com/dlang/dmd/pull/15756
Comment #2 by dlang-bot — 2023-10-30T23:04:44Z
dlang/dmd pull request #15756 "Fix 24208, 24212, 24213 - `scope` escape via `pure` + nested context" was merged into master:
- a0705285d78430954f0762a078423c13faf874d7 by Paul Backus:
Fix issues 24212 and 24213
* Issue 24212 - Scope pointer can escape via non-scope parameter of pure
virtual function
* Issue 24213 - Scope pointer can escape via non-scope parameter of pure
delegate
https://github.com/dlang/dmd/pull/15756