`return ref`, DIP25, and struct/class lifetimes

Dicebot via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 16 13:49:45 PDT 2016


There is also another counter-obvious bit regarding current 
implementation - it only tracks lifetime of actual references. 
Check this example:

ref int wrap ( return ref int input )
{
     return input;
}

int badWrapper()
{
     int z;
     {
         int x = 42;
         z = wrap(x);
     }
     return z;
}


it looks obvious that this compiles OK with dip25 check because 
once value assignment happens, there is no more reference to 
track. However it is very common to expect different semantics if 
return type contains reference types - probably because it would 
be very useful and because Rust has changed expectations of what 
lifetime control can do :) And yet it will still work exactly the 
same with no warnings from compiler, creating false sense of 
correctness.


More information about the Digitalmars-d-learn mailing list