[Issue 20006] [REG 2.086.0] DIP1000: return scope ref outlives the scope of the argument

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Mar 13 10:05:49 UTC 2020


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

Walter Bright <bugzilla at digitalmars.com> changed:

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

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

--


More information about the Digitalmars-d-bugs mailing list