[Issue 20149] [DIP1000] Local data escapes `opSlice` if not annotated with `return`

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Sep 8 20:37:04 UTC 2019


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

--- Comment #10 from Walter Bright <bugzilla at digitalmars.com> ---
Here's what's actually happening. It has nothing to do with inout. A reduced
example:
-----------------
@safe:

struct S {
  this(int) { }
  char[] opSlice() return { return buf[]; }
  char[4] buf;
}

S bar();

char[] fun() {
  return S()[]; // Error: escaping ref to stack value returned by S('\xff')
  return S(1)[]; // should generate same error here
  return bar()[]; // Error: escaping ref to stack value returned by bar()
}
----------------

The trouble is that the temporary returned by the constructor call is being
treated differently than the temporary returned by the struct literal and the
temporary returned by the function call, when they should be treated the same.

--


More information about the Digitalmars-d-bugs mailing list