Bug 15544 – Escaping fields to a heap delegate must be disallowed in @safe code
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-01-10T15:00:00Z
Last change time
2017-01-16T23:26:40Z
Keywords
pull, safe
Assigned to
nobody
Creator
sludwig
Comments
Comment #0 by sludwig — 2016-01-10T15:00:08Z
From https://github.com/rejectedsoftware/vibe.d/issues/570
The following code currently compiles fine, but results in a dangling reference to "this.x" when the delegate gets called after the life time of the originating "Test" instance. At least in @safe code, taking the address of a struct field of "this" within a non-scoped delegate must instead result in a compile-time error.
---
void delegate() @safe _del;
struct Test {
int x = 42;
@safe void test() {
_del = { assert(x == 42); };
}
}
---