[Issue 18792] Incorrect scope analysis with -dip1000 for small-sized-optimized string

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 4 11:06:48 UTC 2020


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

--- Comment #2 from Per Nordlöw <per.nordlow at gmail.com> ---
I managed to reduce the code snippet to


struct SSOString
{
pure nothrow @nogc:

    inout(char)[] opSlice() inout return scope @trusted // TODO @safe for
-dip1000?
    {
        return small.data[0 .. small.length]; // scoped. TODO use .ptr when
proved stable
    }

    struct Small
    {
        ubyte length; // TODO only first 4 bits are needed to represent a
length between 0-15, use other 4 bits
        char[15] data;
    }

    struct Raw                  // same memory layout as `char[]`
    {
        size_t length;          // can be bit-fiddled without GC allocation
        char* ptr;
    }

    union
    {
        Raw raw; // PROBLEM this declaration prevents DIP-1000 scope analysis
from kicking in in `opSlice`
        Small small;
    }
}

@safe pure nothrow @nogc unittest
{
    char[] shouldFail1() @safe pure nothrow @nogc
    {
        SSOString x;
        return x[];             // TODO should fail with -dip1000
    }
}


When compiled with -dip100 the function `shouldFail1` should fail to compile
but it does still compile. When I remove the line

    Raw raw;

scope analysis suddenly starts working which correctly triggers the error

foo.d(33,17): Error: returning `x.opSlice()` escapes a reference to local
variable `x`
        return x[];             // TODO should fail with -dip1000


Small enough, Walter?

--


More information about the Digitalmars-d-bugs mailing list