Bug 11824 – A stack variable escaping problem in CTFE Phobos code
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-12-26T09:50:00Z
Last change time
2014-02-27T04:25:33Z
Keywords
CTFE, pull, rejects-valid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2013-12-26T09:50:35Z
import std.array: array;
import std.range: repeat;
auto r = [1].repeat(1).array;
void main() {}
DMD 2.065alpha gives me:
...\dmd2\src\phobos\std\array.d(45): Error: returning a pointer to a local stack variable
...\dmd2\src\phobos\std\array.d(49): called from here: trustedGetAddr(result[i])
...\dmd2\src\phobos\std\array.d(49): called from here: emplace(trustedGetAddr(result[i]), e)
test.d(3): called from here: array(repeat([1], 1u))
Comment #1 by dlang-bugzilla — 2014-02-03T07:47:33Z
I have encountered the same problem:
import std.algorithm;
import std.array;
auto f(int arr) { return [5]; }
immutable x = [1].map!f.array();
This is a regression from 2.064.2.
Comment #2 by dlang-bugzilla — 2014-02-03T07:57:23Z
I can work *around* the issue, but there is a CTFE bug in there to begin with.
Here is a reduced test case:
//----
int foo(T)()
{
T[] arr = new T[](1);
T* getAddr(ref T a)
{
return &a;
}
getAddr(arr[0]);
return 5;
}
void main()
{
enum a = foo!int(); //OK!
enum b = foo!(int[])(); //FAILS!
}
//----
main.d(6): Error: returning a pointer to a local stack variable
main.d(8): called from here: getAddr(arr[0])
main.d(15): called from here: foo()
//----
First of all: "foo()" ? What are the template parameters?
Second, I *think* there is a rejects valid: getAddr rejects "return &a" if a is of type "T[]", but not "T".
To be perfectly pedantic: a happens to be a stack variable, yes, but *not* inside "getAddr" 's scope. So this should not be rejected during CTFE I think.
Comment #5 by k.hara.pg — 2014-02-03T21:52:04Z
(In reply to comment #4)
> I can work *around* the issue, but there is a CTFE bug in there to begin with.
>
[snip]
> To be perfectly pedantic: a happens to be a stack variable, yes, but *not*
> inside "getAddr" 's scope. So this should not be rejected during CTFE I think.
Yes, this is a compiler bug.
https://github.com/D-Programming-Language/dmd/pull/3204
Comment #6 by github-bugzilla — 2014-02-06T16:26:12Z