DIP 1016--ref T accepts r-values--Community Review Round 1

Manu turkeyman at gmail.com
Sat Nov 10 07:11:25 UTC 2018


On Fri, Nov 9, 2018 at 11:00 PM Neia Neutuladh via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
>
> 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.

This is the proper lowering, as detailed extensively in the DIP.
This operation is invalid, it's clearly an escaping local, and the
normal compiler semantics apply; there are no special rules prescribed
to this case:

void fun() @safe
{
    int* p;
    {
        int __tmp = 10;
        p = &__tmp;
    }
}

error : cannot take address of local `__tmp` in `@safe` function `fun`


More information about the Digitalmars-d mailing list