July 18, 2021
https://issues.dlang.org/show_bug.cgi?id=22128

          Issue ID: 22128
           Summary: opApply delegate can escape scope without duly
                    invoking GC allocation
           Product: D
           Version: D2
          Hardware: All
               URL: http://dlang.org/
                OS: All
            Status: NEW
          Severity: major
          Priority: P3
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: eyal@weka.io

@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');
}

--