www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22539] New: [dip1000] slicing of returned ref scope static

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 puremagic.com
          Reporter: dkorpel 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();
}
```

--
Nov 23 2021