Escaping address of

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Apr 11 16:25:20 UTC 2018


On Wednesday, April 11, 2018 16:08:06 Nick Treleaven via Digitalmars-d-learn 
wrote:
> Is this a known bug? With v2.079.0, with or without -dip1000:
>
> @safe unittest
> {
>      struct S
>      {
>          int i;
>      }
>      auto p = &S().i;
> }
>
> The address of field `i` should not escape, right? It's also
> incorrectly allowed when using an lvalue of S (when -dip1000 is
> not present).

Without -dip1000, @safe code specifically disallows & on local variables.
Beyond that, I'm not sure. Arguably, it should disallow it entirely, though
if the compiler can guarantee that the address is on the heap, then it's
probably fine. However, IMHO, regardless of @safe or DIP 1000, your example
should not be legal period. It's taking the address of the field of a
temporary, which is _never_ a valid thing to do, @safe or not. I guess that
the compiler isn't smart enough to figure out that that's what's going on,
since it's i itself that it's getting the address for and not the temporary
directly, but even if it can't be smart enough for some reason to figure out
that what's going on here is never okay, that & should still be @system,
since it's not taking the address of something on the heap.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list