Comment #0 by snarwin+bugzilla — 2022-10-23T19:57:13Z
As of DMD 2.100.2, the following program fails to compile even with `-preview=dip1000`:
---
struct S
{
int* a, b;
}
int* example(int* p) @safe
{
int n;
scope int* sp = &n;
S s;
s.a = sp; // s inferred as scope
s.b = p;
return s.b;
}
---
The error message is:
---
bug.d(13): Error: scope variable `s` may not be returned
---
This error occurs because the compiler does not distinguish between the lifetimes of `s.a` and `s.b`. While `s.a` is scope, and cannot safely be returned, there would be no danger in allowing the function to return `s.b`.
Currently, this limitation can be worked around by splitting `s` into separate variables, or by using `@trusted`.
Comment #1 by robert.schadek — 2024-12-13T19:25:08Z