DIP 1016--ref T accepts r-values--Community Review Round 1
Neia Neutuladh
neia at ikeran.org
Sat Nov 10 06:58:59 UTC 2018
On Fri, 09 Nov 2018 21:56:09 -0800, Manu wrote:
> That's not @safe, for obvious reasons.
I see three different ways of implementing DIP 1016, and exactly what's
@safe differs in each.
The compiler can add in the temporary variable declaration at the start of
the current scope, and the following would compile:
int* gun(return ref p) { return &p; }
void main()
{
// compiler adds: int __tmp = 10;
int* p;
// compiler converts to: p = gun(__tmp);
p = gun(10);
}
Or the compiler can add a new scope just for the temporary variable,
entirely preventing escaping. It would be lowered to:
int* p;
{
int __tmp = 10;
p = gun(__tmp);
}
And that wouldn't fly, obviously.
Or it could add the temporary variable just before the current statement,
so you could write:
writeln("hi");
int* p = gun(10);
// compiler sees:
writeln("hi");
int __tmp = 10;
int* p;
p = gun(__tmp);
But if you tried declaring the pointer first, you'd get:
writeln("hi");
int* p;
p = gun(10);
// compiler sees:
writeln("hi");
int* p;
int __tmp = 10;
p = gun(__tmp);
And -dip1000 requires the pointed-to value to come into existence before
the pointer, so this doesn't work.
More information about the Digitalmars-d
mailing list