D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22128 - opApply delegate can escape scope without duly invoking GC allocation
Summary: opApply delegate can escape scope without duly invoking GC allocation
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 major
Assignee: No Owner
URL: http://dlang.org/
Keywords: safe
Depends on:
Blocks:
 
Reported: 2021-07-18 23:33 UTC by Eyal
Modified: 2022-02-21 09:22 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Eyal 2021-07-18 23:33:09 UTC
@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 Walter Bright 2022-02-21 08:48:20 UTC
If `scope` is added just before `int delegate`, the `_dlg = dlg;` will be diagnosed as an error.
Comment 2 Walter Bright 2022-02-21 09:19:07 UTC
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 Eyal 2022-02-21 09:22:33 UTC
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?