www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17927] New: [scope] scope input return value can be escaped

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

          Issue ID: 17927
           Summary: [scope] scope input return value can be escaped
           Product: D
           Version: D2
          Hardware: All
                OS: Linux
            Status: NEW
          Keywords: safe
          Severity: normal
          Priority: P4
         Component: dmd
          Assignee: bugzilla digitalmars.com
          Reporter: code dawg.eu

cat > bug.d << CODE
struct String
{
pure nothrow  nogc:
    inout(char)[] opSlice() inout scope  trusted
    {
        return ptr[0 .. len];
    }

    char *ptr;
    size_t len;
}

void escape(const char[] s) nothrow  safe  nogc
{
    static const(char)[] cache;
    cache = s;
}

///
nothrow  safe
unittest
{
    auto s = String(&"Hello".dup[0], 5);
    escape(s[]);
}
CODE
dmd -c -unittest -dip1000 bug.d
----

Should error with `scope variable this.ptr may not be returned`.

workaround:
----
    char[] opSlice() scope  trusted
    {
        return ptr[0 .. len];
    }

    const(char)[] opSlice() const scope  trusted
    {
        return ptr[0 .. len];
    }
----

--
Oct 22