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

Olivier FAURE couteaubleu at gmail.com
Mon Jul 22 08:45:11 UTC 2019


On Monday, 22 July 2019 at 00:05:12 UTC, Mike Franklin wrote:
> import core.stdc.stdlib;
>
> struct S
> {
>     int a = 1;
>     int b = 2;
>
>     ref int foo() @safe
>     {
>         if ((rand() % 2) == 0)
>             return a;
>         else
>             return b;
>     }
>
>     ref int bar() @safe
>     {
>         if ((rand() % 4) == 0)
>             return a;
>         else
>             return b;
>     }
> }
>
> ...
>
> I still don't see how the compiler can know statically whether 
> or not `foo()` and `bar()` will return a reference to the same 
> data.

Assuming you compile with dip1000 and dip1021, foo and bar in the 
above code would have to be marked `return` (that is, `this` is a 
`return ref`) or trigger a compile error.

The compiler would presumably be conservative and assumes that 
they always return references to overlapping data, which is 
usually what you want (it's what the Rust compiler does, at 
least).


More information about the Digitalmars-d mailing list