Bug 11428 – A simple std.array.array call at compile-time refused
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2013-11-03T03:31:00Z
Last change time
2014-02-27T04:25:32Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2013-11-03T03:31:19Z
I think this should work, I don't know if this is a regression:
import std.array: array;
const r1 = [""].array; // Error
void main() {
const r2 = [""].array; // OK
}
dmd 2.064beta4 gives:
...\dmd2\src\phobos\std\array.d(44): Error: returning a pointer to a local stack variable
...\dmd2\src\phobos\std\array.d(48): called from here: trustedGetAddr(result[i])
...\dmd2\src\phobos\std\array.d(48): called from here: emplace(trustedGetAddr(result[i]), e)
test.d(2): called from here: array([""])
Comment #1 by monarchdodra — 2013-11-03T06:30:19Z
It's not a regression, because array was not CTFE-able prior to 2.064. I say CTFE, because you are declaring a global static. This also recreates the issue:
//----
import std.array: array;
void main()
{
enum r1 = [""].array; // KO
static const r2 = [""].array; // KO
}
//----
The "issue" seems to only happen if the element type is a string. I can't reproduce with other types. Further more, it only happens if the string is a literal. EG:
//----
void main()
{
static const string r = "";
static const r1 = r.array; // OK
static const r2 = [1].array; // OK
static const r2 = [""].array; // ERROR
}
//----
Here is a reduced case.
//----
T[] arr(T)(T[] input)
{
static T* getAddr(ref T t)
{
return &t; //Error: returning a pointer to a local stack variable
}
getAddr(input[0]);
return input;
}
void main()
{
static const string r = "";
static const r1 = r.arr; // OK
static const r2 = [1].arr; // OK
static const r3 = [""].arr; // ERROR
}
//----
I can't really make any sense of this on my end. Maybe Don has an idea?
Comment #2 by monarchdodra — 2014-02-27T04:25:32Z
*** This issue has been marked as a duplicate of issue 11824 ***