Comment #0 by snarwin+bugzilla — 2023-10-30T18:05:37Z
As of DMD 2.105.2, the following invalid program compiles and runs without errors:
---
interface I
{
@safe pure nothrow
void fun(int* p);
}
int* global;
void main() @safe
{
int* escaped;
class Escaper : I
{
@safe pure nothrow
override void fun(int* p)
{
escaped = p;
}
}
int n;
I i = new Escaper;
i.fun(&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 `I.fun(&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 `I.fun` cannot escape. However, these rules do not account for the possibility that a nested derived class 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:25Z
@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:43Z
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