[Issue 22916] [dip1000] copy of ref return still treated as scope variable

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Aug 29 03:36:13 UTC 2022


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

--- Comment #8 from Walter Bright <bugzilla at digitalmars.com> ---
(The cheat sheet was out of date. My bad. Remember when we changed `return
scope` to always mean ReturnScope, and `ref scope return` never means
`Ref-ReturnScope`?)

Let's change x to p for clarity:

  ref int f(ref return scope int* p) @safe
  { 
    return *p;
  }

This compiles as `ref` `return scope`. This protects the value of p. Not the
address of p. This compiles successfully, as the value of `p` is what is
returned.

Now let's try not having `return scope` next to each other:

  ref int f(ref5 scope return int* p) @safe
  { 
    return *p;
  }

This compiles as `return ref` `scope`. It fails to compile with:

    Error: scope variable `p` may not be returned.

Both are correct behavior. Note that `return scope` is *always* interpreted as
returnScope.

Also, the behavior is *always* determined by the function signature, not
whatever the return expression is.

--


More information about the Digitalmars-d-bugs mailing list