www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23434] New: [DIP1000] Allow storing scope and non-scope data

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

          Issue ID: 23434
           Summary: [DIP1000] Allow storing scope and non-scope data in
                    same aggregate
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

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`.

--
Oct 23 2022