www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22837] New: [dip1000] checkConstructorEscape quits after

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

          Issue ID: 22837
           Summary: [dip1000] checkConstructorEscape quits after first
                    non-pointer
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: safe
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dkorpel live.nl

dmd checks escaping pointers of a constructor by fake assigning arguments to
`this`, but it stops at the first non pointer argument.
```
bool checkConstructorEscape() {
// ...
    /* Attempt to assign each `return` arg to the `this` reference
     */
    foreach (const i; 0 .. n)
    {
        Expression arg = (*ce.arguments)[i];
        if (!arg.type.hasPointers())
            return false; // < EARLY EXIT
```

This means you can escape scope pointers like this:
```
 safe:
struct S 
{
    int* p;

    this(int dummy, return scope int* p) 
    {
        this.p = p;
    }
}

int* escape()
{
    int x;
    auto s = S(0, &x);
    return s.p;
}
```

--
Mar 02 2022