[Issue 18637] [scope][DIP1000] "copying & i into allocated memory escapes a reference to local variable i" where it's inappropriate

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 21 07:25:37 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18637

Walter Bright <bugzilla at digitalmars.com> changed:

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

--- Comment #3 from Walter Bright <bugzilla at 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.

--


More information about the Digitalmars-d-bugs mailing list