Bug 22128 – opApply delegate can escape scope without duly invoking GC allocation

Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-07-18T23:33:09Z
Last change time
2022-02-21T09:22:33Z
Keywords
safe
Assigned to
No Owner
Creator
Eyal

Comments

Comment #0 by eyal — 2021-07-18T23:33:09Z
@safe: import std; struct S { int delegate(char x) @safe _dlg; @safe @nogc int opApply(int delegate(char x) @safe dlg) { _dlg = dlg; return 0; } } @safe unittest { S s; @safe void f() { int i = 1; foreach(x; s) { writeln(x, ": ", i); } // BUG disappears if this is used instead: // s.opApply((char x){ writeln(x, ": ", i); return 0; }); // because then the frame of 'f' comes from GC correctly (non-scoped delegate) } f(); s._dlg('x'); writeln("Again:"); s._dlg('y'); }
Comment #1 by bugzilla — 2022-02-21T08:48:20Z
If `scope` is added just before `int delegate`, the `_dlg = dlg;` will be diagnosed as an error.
Comment #2 by bugzilla — 2022-02-21T09:19:07Z
The problem is solved if -preview=dip1000 is used to compile it. But as a general practice, I recommend adding `scope` to the delegate parameter to opApply().
Comment #3 by eyal — 2022-02-21T09:22:33Z
When is dip1000 becoming default? Until then - the default behavior is buggy. Is the bug policy to reject any bug that will be solved by future features in preview mode?