June 10, 2021
https://issues.dlang.org/show_bug.cgi?id=22013

          Issue ID: 22013
           Summary: Making RefCounted dtor @safe breaks DIP1000
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: atila.neves@gmail.com

If std.conv.RefCounted's dtor is made @safe, the code below compiles with -preview=dip1000 but shouldn't. It correctly fails to compile if `return scope` is used instead of `scope Container local` but the user shouldn't have to do that manually. Or, at least, they should, but the compiler shouldn't compile buggy code that escapes a reference to a local.

See https://github.com/dlang/phobos/pull/8101#issuecomment-843017282. This issue is blocking that PR.


-------------------------
import std.stdio;
import std.typecons;

struct Container
{
    int[] data;
}

void main ()
{
    auto ptr = getPtr();
    writeln(ptr);
    const save = ptr.dup;
    auto other = getPtr();
    assert(save == ptr);
}

int[] getPtr ()
{
    int[42] local;
    return getPtr2(Container(local));
}

int[] getPtr2 (scope Container local)
{
    RefCounted!Container rc = local;
    return rc.refCountedPayload().data;
}
-------------------------

--