[Issue 20149] [DIP1000] Local data escapes `opSlice` if not annotated with `return`

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Sep 4 13:15:29 UTC 2019


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

--- Comment #5 from Mike Franklin <slavo5150 at yahoo.com> ---
A more reduced test case:

---
import std.stdio;

@safe:

struct ScopeBuffer
{
    this(char[4] init)
    {
        this.buf = init;
    }

    // The bug is that `inout` implies `return` on `this`.
    // See
https://github.com/dlang/dmd/blob/2dc5b15d1949148d460e8c809dd878e6dec8dfa3/src/dmd/func.d#L511-L513
    inout(char)[] opSlice(size_t lower, size_t upper) inout
    do
    {
        return buf[lower .. upper]; //BUG: compiler error should be emitted
here
    }

    char[4] buf;
}

char[] fun()
{
    char[4] buf = "abcd";
    auto sb = ScopeBuffer(buf);
    return sb[0..2];
}

void main()
{
    auto s = fun();
    writeln(s);
}
---
https://run.dlang.io/is/Pu1125

The problem is that the compiler is inferring `return` on `this` when decorated
with `inout`.

This was discussed in issue 17927.

--


More information about the Digitalmars-d-bugs mailing list