[Issue 20956] [DIP1000] @safe defeated by closure capturing ref parameter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jan 4 11:52:46 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=20956

Dennis <dkorpel at live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel at live.nl

--- Comment #1 from Dennis <dkorpel at live.nl> ---
Reduced a bit more:
```
@safe:
alias DG = void delegate() @safe;

void main()
{
    DG dg = getDg(42);
    stompStack();
    dg();
}

DG getDg(int val)
{
    return forwardDg(val);
}

DG forwardDg(ref int c)
{
    return () {assert(c == 42);};
}

void stompStack()
{
    int[256] oops = int.max;
}
```
Also, it fails both with or without -dip1000. I think closing over a ref
parameter should be an error (at least in `@safe` code), because it's really
hard for `forwardDg` to create a closure including the stack frame of `getDg`:
it has to be done at runtime, since you can't statically know where the `ref`
parameter came from.

--


More information about the Digitalmars-d-bugs mailing list