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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Aug 22 05:52:15 UTC 2019


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

--- Comment #3 from Mike Franklin <slavo5150 at yahoo.com> ---
Removing all templating from the example above cause the compiler to emit what
I think is correct behavior:

---
import std.stdio;

@safe:

struct ScopeBuffer
{
    this(char[4] buf, size_t len = 0)
    {
        this.buf = buf;
                this.len = len;
    }

    char[] opSlice(size_t lower, size_t upper)
    in
    {
        assert(lower <= len, "Lower bound must be less than or equal to the
length");
        assert(upper <= len, "Upper bound must be less than or equal to the
length");
        assert(lower <= upper, "Lower bound must be less than or equal to the
upper bound");
    }
    do
    {
        return buf[lower .. upper]; // GOOD: Compiler error here
    }

    char[4] buf;
    size_t len;
}

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

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

--


More information about the Digitalmars-d-bugs mailing list