DIP 1021--Argument Ownership and Function Calls--Community Review Round 1

Nick Treleaven nick at geany.org
Thu Aug 1 06:50:32 UTC 2019


On Wednesday, 31 July 2019 at 09:25:26 UTC, Walter Bright wrote:
> Copying c to d will increase the ref count and so it will be 
> safe.

As Olivier said, c and d are just class references to the same 
class instance, so there is only one instance of Array, a ref 
count would still be 1.

This problem can also happen with slices, here's a simplified 
struct that shows the problem and disables the struct postblit:

struct S
{
     private byte* data;
     import core.stdc.stdlib;
     @nogc:

     this(byte b) @trusted
     {
         data = cast(byte*)malloc(1);
         *data = b;
     }
     @disable this(this);

     ref byte get() @trusted return
     {
         return *data;
     }

     ~this() @trusted
     {
         if (data) free(data);
     }
}

@safe:

void maybe_bad(ref S s, ref byte i)
{
     s = S(); // frees s.data
     i++;
}

void main()
{
     auto s = S(5);
     // DIP error: Can't pass multiple mutable references owned by 
`s`
     maybe_bad(s, s.get());
     //auto t = s; // S is not copyable

     auto c = new C;
     c.s = S(4);
     auto d = c;
     // not detected by DIP
     maybe_bad(c.s, d.s.get());

     auto a = [S(3)];
     auto b = a;
     // not detected by DIP
     maybe_bad(a[0], b[0].get());
}

class C
{
     S s;
}



More information about the Digitalmars-d mailing list