Bug 6111 – Escaping reference to local variable not detected
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2011-06-05T19:45:00Z
Last change time
2011-06-06T17:23:56Z
Assigned to
nobody
Creator
bugzilla
Comments
Comment #0 by bugzilla — 2011-06-05T19:45:21Z
import std.algorithm, std.stdio;
auto mymap()
{
int x = 42;
int fun(int a) { return x + a; }
return map!fun([1, 2, 3, 4, 5]);
}
void main()
{
writeln(mymap());
}
This should print [43, 44, 45, 46, 47], but prints garbage because x should be allocated on the heap rather than the stack. The compiler needs to be better at detecting local escapes.
Comment #1 by bugzilla — 2011-06-06T15:57:49Z
Reduced test case:
import std.c.stdio;
auto mymap()
{
int x = 42;
int fun(int a) { return x + a; }
auto map()(int r)
{
struct Result
{
int _input;
int back()
{
return fun(_input);
}
this(int input)
{
_input = input;
}
}
return Result(r);
}
return map(1);
}
void main()
{
printf("%d\n", mymap().back());
}