www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Escaping address of

reply Nick Treleaven <nick geany.org> writes:
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).
Apr 11 2018
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Apr 11 2018
parent reply Uknown <sireeshkodali1 gmail.com> writes:
On Wednesday, 11 April 2018 at 16:25:20 UTC, Jonathan M Davis 
wrote:
 [...]
Adding a destructor makes the compiler return an error about lifetimes, with or without -dip1000 https://run.dlang.io/is/ddXqNu
Apr 11 2018
parent Nick Treleaven <nick geany.org> writes:
On Thursday, 12 April 2018 at 00:32:49 UTC, Uknown wrote:
 Adding a destructor makes the compiler return an error about 
 lifetimes, with or without -dip1000
Thanks. Filed, mentioning this: https://issues.dlang.org/show_bug.cgi?id=18756
Apr 12 2018