Thread overview
[Issue 18637] [scope][DIP1000] "copying & i into allocated memory escapes a reference to local variable i" where it's inappropriate
[Issue 18637] [scope][DIP1000] "Error: returning & i escapes a reference to local variable i" where it's inappropriate
Mar 20, 2018
Carsten Blüggel
Mar 21, 2018
Carsten Blüggel
Mar 21, 2018
Walter Bright
Mar 22, 2018
Carsten Blüggel
March 20, 2018
https://issues.dlang.org/show_bug.cgi?id=18637

--- Comment #1 from Carsten Blüggel <chilli@posteo.net> ---
For reference: https://github.com/dlang/phobos/blob/master/dip1000.mak

--
March 21, 2018
https://issues.dlang.org/show_bug.cgi?id=18637

Carsten Blüggel <chilli@posteo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[scope][DIP1000] "Error:    |[scope][DIP1000] "copying &
                   |returning & i escapes a     |i into allocated memory
                   |reference to local variable |escapes a reference to
                   |i" where it's inappropriate |local variable i" where
                   |                            |it's inappropriate

--- Comment #2 from Carsten Blüggel <chilli@posteo.net> ---
Since https://github.com/dlang/dmd/commit/ce08cffe71911e86d8adfb241ff4d4e63136db8c the error message is now:

std/exception.d(1238): Error: copying & i into allocated memory escapes a reference to local variable i

Yes, there is "copying & i into allocated memory", but nothing that makes bad use of it.

--
March 21, 2018
https://issues.dlang.org/show_bug.cgi?id=18637

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
Simplifying the test case:

  void test() {
    int i;
    int*[] a = [&i]; // Error: copying `& i` into allocated memory escapes a
reference to local variable `i`
  }

This can be worked around with:

  void test() {
    int i;
    int*[] a = [foo(&i)];
  }
  int* foo(int* p) { return p; }

It can be reasonably argued either way whether the original test case should be allowed in @system. I'll argue the workaround makes it obvious what one is doing, and unlikely to do it by accident. It's similar to:

  int* test() { int i; return &i; }

being an error even in @system code.

--
March 22, 2018
https://issues.dlang.org/show_bug.cgi?id=18637

Carsten Blüggel <chilli@posteo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|rejects-valid, spec         |
             Status|NEW                         |RESOLVED
         Resolution|---                         |WONTFIX

--