[Issue 22539] New: [dip1000] slicing of returned ref scope static array should not be allowed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 23 19:38:16 UTC 2021


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

          Issue ID: 22539
           Summary: [dip1000] slicing of returned ref scope static array
                    should not be allowed
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: dkorpel at live.nl

Because there is no transitive scope, taking the address of a scope variable is
not allowed. There are 4 cases:

- ref param &pointer: rejected with `checkAddresVar`
- ref param slice[]: rejected with `checkAddresVar`
- ref return &pointer: too restrictive (issue 22519)
- ref return slice[]: too lenient (this issue)

This allows you to escape scope pointers:
```
// REQUIRED_ARGS -preview=dip1000
@safe:
ref int*[1] identity(ref return scope int*[1] x)
{
        return x;
}

int* escape()
{
        int stackVar = 0xFF;
        scope int*[1] x = [&stackVar];
        int*[] y = identity(x)[];
        return y[0];
}

void main()
{
        int* dangling = escape();
}
```

--


More information about the Digitalmars-d-bugs mailing list