digitalmars.D.bugs - [Issue 1841] New: Closure detection doesn't work when variable is used in a nested function

http://d.puremagic.com/issues/show_bug.cgi?id=1841
Summary: Closure detection doesn't work when variable is used in
a nested function
Product: D
Version: 2.010
Platform: PC
OS/Version: Linux
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: bugzilla digitalmars.com
ReportedBy: webmaster villagersonline.com
The following function should allocate the variable "heap" on the heap since it
is referenced in a delegate. However, as you can tell from the output of the
program, it gets allocated on the stack instead.
CODE
import std.stdio;
void Foo(void delegate() ignored)
{
int stack;
int heap;
writefln("&stack=",&stack," &heap=",&heap);
writefln();
void nested_func()
{
heap = 1;
}
Foo(delegate void() { nested_func(); });
}
void main()
{
Foo(null);
}
END CODE
For comparison, if you modify the variable "heap" directly in the deletage
literal, instead of having the delegate literal call the nested function, then
"heap" is correctly allocated on the heap.
Interestingly, if you have *two* heap variables, one of which is modified
directly in the delegate literal, and the other of which is only modified
inside the nested function, then BOTH of the heap variables are on the heap.
This seems to indicate that the compiler is doing a "do I need any closures
here" pass, which has a bug, and then has a "build closures" pass, which works
correctly.
--