[Issue 21220] New: [DIP1000] scope variable may escape through scope dynamic array parameter
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Sep 4 00:09:27 UTC 2020
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 at puremagic.com
Reporter: outland.karasu at 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);
}
--------
--
More information about the Digitalmars-d-bugs
mailing list