On 11/2/17 6:10 PM, Jonathan M Davis wrote:
Except that IIRC, DIP 25 only applies to @safe code.

I think the original implementation only applied to @safe code, but it seems to have an effect on @system code as well:

Stevens-MacBook-Pro:testd steves$ cat testdip25.d
ref int foo(ref int s)
{
    return s;
}

void main()
{
    int i;
    int *p = &foo(i);
}
Stevens-MacBook-Pro:testd steves$ dmd testdip25.d
Stevens-MacBook-Pro:testd steves$ dmd -dip25 testdip25.d
testdip25.d(3): Error: returning s escapes a reference to parameter s, perhaps annotate with return

I haven't read DIP 25
recently, but I assume that the return on id is equivalent to marking the
this parameter with return, in which case, the compiler knows where the
returned reference came from. So, returning by ref shouldn't be a problem.
The problem is simply taking the address, since the S is a temporary, and
that's already caught by @safe even without compiling with -dip25.
Right, the whole point of 'return' is to tell the compiler that the 'this' parameter is getting returned. IMO, this shouldn't even work on rvalues. It's not even the taking of the address, it's the fact that the address of the result outlives the rvalue. The compiler should be able to tell that if I call id with an rvalue, the resulting reference can't escape the expression.

So it's somewhat of a cross between dip1000 and dip25, but obviously neither flags it.

-Steve

Reply via email to