www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22013] New: Making RefCounted dtor safe breaks DIP1000


          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();
    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;

Jun 10 2021