Bug 21220 – [DIP1000] scope variable may escape through scope dynamic array parameter
Status
RESOLVED
Resolution
DUPLICATE
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Mac OS X
Creation time
2020-09-04T00:09:27Z
Last change time
2021-02-18T00:55:57Z
Keywords
accepts-invalid, pull, safe
Assigned to
No Owner
Creator
Akira Yamaguchi
Comments
Comment #0 by outland.karasu — 2020-09-04T00:09:27Z
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);
}
--------
Comment #1 by dlang-bot — 2021-02-17T14:42:10Z
@RazvanN7 created dlang/dmd pull request #12204 "Fix Issue 21220 - [DIP1000] scope variable may escape through scope dynamic array parameter" fixing this issue:
- Fix Issue 21220 - [DIP1000] scope variable may escape through scope dynamic array parameter
https://github.com/dlang/dmd/pull/12204
Comment #2 by pro.mathias.lang — 2021-02-18T00:55:57Z
Duplicate of 20505, which is itself a subset of 17764.
*** This issue has been marked as a duplicate of issue 20505 ***