www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19752] New: dip1000 isn't safe if struct contains a slice

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

          Issue ID: 19752
           Summary: dip1000 isn't  safe if struct contains a slice
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: atila.neves gmail.com

The code below compiles as expected with dip100. It's  safe because `range`'s
return value has the lifetime extended from the `this` parameter:

--------------------------------
struct Container {

    auto range()  safe return scope {
        static struct Range {
            Container *self;
        }

        return Range(&this);
    }
}

--------------------------------

Trying to escape the result of `range` then fails to compile, as expected.
However, adding a slice member variable to the struct above causes this:

----------------------------------
struct Container {
    // as before
    int[] ints;
----------------------------------

$ dmd -preview=dip1000 bug2.d
bug2.d(10): Error: cannot take address of scope parameter this in  safe
function range


From the original DIP, it's to be expected that `ints` would have the same
lifetime as `this` and things should work. This is particularly bad because if the programmer attempts a workaround the code below compiles and shouldn't: https://github.com/atilaneves/automem/issues/26 --
Mar 19 2019