Thread overview
[Issue 24175] DIP1000 fails to determine proper lifetime for struct
Oct 05, 2023
Dennis
Oct 06, 2023
Grim Maple
Oct 06, 2023
Max Samukha
Oct 06, 2023
apham
October 05, 2023
https://issues.dlang.org/show_bug.cgi?id=24175

Dennis <dkorpel@live.nl> changed:

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

--- Comment #1 from Dennis <dkorpel@live.nl> ---
> However, both `a` and `test` should have same lifetime and therefore this code should be allowed

To communicate that, you need to add `return scope` to parameter `data`, or make the constructor a template function so it's inferred. Otherwise the constructor could assign the stack allocated slice to a global variable.

--
October 06, 2023
https://issues.dlang.org/show_bug.cgi?id=24175

--- Comment #2 from Grim Maple <grimmaple95@gmail.com> ---
I don't understand how this is invalid, when the code I wrote is completely valid, doesn't escape references, yet is marked as "escaping references"

--
October 06, 2023
https://issues.dlang.org/show_bug.cgi?id=24175

Max Samukha <maxsamukha@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxsamukha@gmail.com

--- Comment #3 from Max Samukha <maxsamukha@gmail.com> ---
(In reply to Grim Maple from comment #2)
> I don't understand how this is invalid, when the code I wrote is completely valid, doesn't escape references, yet is marked as "escaping references"

The problem is separate compilation/lack of lifetime inference:

struct A
{
    this(int[] data) @safe;
    int[] a;
}

Now, compiler doesn't know whether the constructor stores the argument in a location outliving 'this'. People are busy trying to be compatible with stone-age technologies, so it's unlikely to change.

--
October 06, 2023
https://issues.dlang.org/show_bug.cgi?id=24175

apham <apz28@hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |apz28@hotmail.com

--- Comment #4 from apham <apz28@hotmail.com> ---
SCOPE & dip1000 is not for prime-time yet. Still having bug & not easier to use as below sample

@safe:

class C1
{
@safe:

    int i;
}

struct A1
{
@safe:

    this(C1 c1, return scope int[] a)
    {
      this.a = a;
      this.c1 = c1;
      this.c1s.reserve(10);
    }

    C1 compute()
    {
        return c1;
    }

    int[] a;
    C1[] c1s;
    C1 c1;
}

int main()
{
    int n;
    { // Simulate a scope block
        C1 c1 = new C1();
        int[3] a = [1, 2, 3];
        A1 a1 = A1(c1, a[]);
        n = a1.compute().i;
    }
    return n;
}


onlineapp.d(18): Error: scope variable `this` assigned to non-scope parameter
`arr` calling `reserve`
/dlang/dmd/linux/bin64/../../src/druntime/import/object.d(3931):        which
is not `scope` because of `cast(void[]*)&arr`
onlineapp.d(38): Error: scope variable `a1` calling non-scope member function
`A1.compute()`

--