Thread overview
[Issue 20006] [REG 2.086.0] DIP1000: return scope ref outlives the scope of the argument
Jun 27, 2019
anonymous4
Aug 28, 2019
Eugene Wissner
Mar 13, 2020
Walter Bright
Mar 13, 2020
Walter Bright
Mar 13, 2020
Walter Bright
June 27, 2019
https://issues.dlang.org/show_bug.cgi?id=20006

--- Comment #1 from anonymous4 <dfj1esp02@sneakemail.com> ---
Doesn't look like your example escapes a reference to local variable.

--
August 28, 2019
https://issues.dlang.org/show_bug.cgi?id=20006

--- Comment #2 from Eugene Wissner <belka@caraus.de> ---
The constructor parameter is marked with "return scope ref" and it means that Inserter isn't allowed to outlive it.

You can also write

this(return scope ref int i) @trusted
{
    this.i = &i;
}

and it worked prior to 2.086 as well.

--
March 13, 2020
https://issues.dlang.org/show_bug.cgi?id=20006

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|---                         |INVALID

--
March 13, 2020
https://issues.dlang.org/show_bug.cgi?id=20006

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |---

--
March 13, 2020
https://issues.dlang.org/show_bug.cgi?id=20006

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
Let's rewrite the example as:
----
struct Inserter {
    int* i;
}
ref Inserter ctor(return scope ref int) @safe;

Inserter func() @safe {
    int i;
    return ctor(i);
}
---
because, as I endlessly point out, in order to understand what is going on, REWRITE IN SIMPLE TERMS. I.e. lower like the compiler does. In this case, get rid of auto, get rid of member functions, etc. Note constructors return by ref.

The reason no error is given is because ctor() is told to attach the address of i to ctor()'s return value, which is a reference to an instance of Inserter. Now, because Inserter is returned by value, not by reference, the referred to value is returned, NOT THE REFERENCE! Only the ref is protected, not the value being referenced.

Not a bug.

I know this is complicated, and the ONLY way to understand it is to peel away the complexity by lowering it to ref's and pointer's and nothing else.

--