www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21220] New: [DIP1000] scope variable may escape through scope

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

          Issue ID: 21220
           Summary: [DIP1000] scope variable may escape through scope
                    dynamic array parameter
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Mac OS X
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: outland.karasu gmail.com

Scoped local array cannot go to outside scope, but currently the variable may
escape to outside through scope dynamic array parameter.

--------
import std;

 safe:

struct A
{
    char[] value;
}

A f(scope A[] array)
{
    return array[0]; // lifetime(array) to infinity 
}

A g()
{
    scope char[4] value = "test";
    scope A a;
    a.value = value[];
    return f([a]); // This is not error.
}

void h()
{
    scope char[30] s = "123456789012345678901234567890";
    writefln("%s", s);
}

void main()
{
    auto a = g();

    // 123456789012345678901234567890
    // 1234[4]
    h();
    writefln("%s[%d]", a.value, a.value.length);
}
--------

I think that this code should be compile error at `f([a])` line.
Other variations are reported compile error.

--------
A g()
{
    scope char[4] value = "test";
    scope A a;
    a.value = value[];
    return f([a]);

    // Error: cannot take address of scope local array in  safe function g
    // scope A[1] array;
    // array[0].value = value[];
    // return f(array[]);

    // Error: scope variable a may not be copied into allocated memory
    // scope A[] array = [a];
    // return f(array);
}
--------

--
Sep 03 2020