[Issue 22539] [dip1000] slicing of returned ref scope static array should not be allowed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 27 21:29:00 UTC 2021


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

--- Comment #10 from Walter Bright <bugzilla at digitalmars.com> ---
Moving on from my previous mistakes:

    @safe:
    ref int* identity(ref return scope int* x) { return x; }

    int* escape() {
        int stackVar;
        scope int* x = &stackVar;
        int* y = identity(x);
        return y; // error: scope variable `y` may not be returned
    }

Good. Next,

    @safe:
    ref int*[1] identity(ref return scope int*[1] x) { return x; }

    int*[1] escape() {
        int stackVar;
        scope int*[1] x = [&stackVar];
        int*[1] y = identity(x);
        return y; // error: scope variable `y` may not be returned
    }

Good.

    int* escape() {
        int stackVar;
        scope int*[1] x = [&stackVar];
        int*[1] y = identity(x);
        return y[0]; // error: scope variable `y` may not be returned
    }

Good.

    int* escape() {
        int stackVar;
        scope int*[1] x = [&stackVar];
        int*[1] y = identity(x);
        int*[] z = y[]; // error: cannot take address of `scope` local `y`
        return z[0];
    }

Good. Here, the slicing of `y` is properly detected. But if we put the [] after
the identity call:

    int* escape() {
        int stackVar;
        scope int*[1] x = [&stackVar];
        int*[] y = identity(x)[];
        return y[0];
    }

No error; bad. The slicing of the return value of identity() is not properly
propagating the scope-ness of its argument x. This would be a problem in
escape.d.

--


More information about the Digitalmars-d-bugs mailing list