Thread overview
[Issue 24442] [DIP1000] struct member slice cannot point to other struct member
Mar 19
Dennis
March 19
https://issues.dlang.org/show_bug.cgi?id=24442

johanengelen@weka.io changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |industry

--
March 19
https://issues.dlang.org/show_bug.cgi?id=24442

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |dkorpel@live.nl
         Resolution|---                         |INVALID

--- Comment #1 from Dennis <dkorpel@live.nl> ---
Interior pointers aren't safe, the error is correct. Without it, you can escape stack pointers.

```
@safe:

struct S {
    int[10] array;
    int[] slice;

    void foo() @trusted {
        slice = array[];
    }
}

void main()
{
    int[] f()
    {
        S s;
        s.foo();
        return s.slice;
    }
    auto x = f(); // dangling pointer
    x[0] = 0xAAAA;
    f(); // stomp stack
    assert(x[0] == 0xAAAA); // fails
}
```

Even when foo becomes `scope`:
```
@safe:

struct S {
    int[10] array;
    int[] slice;

    void foo() scope @trusted {
        slice = array[];
    }
}

void main()
{
    S s;
    int[] f(return scope S s)
    {
        s.foo();
        return s.slice;
    }

    auto x = f(s); // dangling pointer
    x[0] = 0xAAAA;
    f(s); // stomp stack
    assert(x[0] == 0xAAAA); // fails
}
```

--
March 20
https://issues.dlang.org/show_bug.cgi?id=24442

--- Comment #2 from johanengelen@weka.io ---
Thanks for the reminder...
Interior pointers are necessary for certain data structures (e.g. short-string
optimized dynamic-length strings), so it'd be nice when the error message would
tell the user that interior pointers are not @safe.
The current error message reads like a bug in the compiler, imo.

--