xxxxxxxxxx
import std.stdio;
:
struct ScopeBuffer(T, size_t Len)
{
this(T[Len] buf, size_t len = 0)
{
this.buf = buf;
this.len = len;
}
// Decorateing `opSlice` with `return`, causes the compiler to correctly emit
// a compiler error on line 37, but without `return` a compiler error should be
// emitted on line 25
inout(T)[] opSlice(size_t lower, size_t upper) inout /*return*/
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]; //BUG: compiler error shoud be emitted here
// if `opSlice` is not decorated with `return`
}
T[Len] buf;
size_t len;
}
char[] fun()
{
char[4] buf = "abcd";
auto sb = ScopeBuffer!(char, 4)(buf, 4);
return sb[0..2]; // BUG: compiler allows data internal to `ScopeBuffer` to
// escape here unless `ScopeBuffer.opSlize` is decorated with `return`
}
void main()
{
auto s = fun();
writeln(s);
}