Bug 24442 – [DIP1000] struct member slice cannot point to other struct member

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-03-19T20:14:36Z
Last change time
2024-03-20T21:54:19Z
Keywords
industry
Assigned to
No Owner
Creator
johanengelen

Comments

Comment #0 by johanengelen — 2024-03-19T20:14:36Z
Testcase with `-dip1000`: ``` struct S { int[10] array; int[] slice; void foo() @safe { slice = array[]; } } ``` results in error: ``` <source>(6): Error: address of variable `this` assigned to `this` with longer lifetime ```
Comment #1 by dkorpel — 2024-03-19T20:37:45Z
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 } ```
Comment #2 by johanengelen — 2024-03-20T21:54:19Z
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.