Bug 1841 – Closure detection doesn't work when variable is used in a nested function
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-02-15T11:58:00Z
Last change time
2013-02-01T00:51:02Z
Keywords
wrong-code
Assigned to
nobody
Creator
webmaster
Comments
Comment #0 by webmaster — 2008-02-15T11:58:19Z
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.
Comment #1 by webmaster — 2008-03-31T12:38:30Z
This seems to work on dmd 2.012. Did dmd change?
Comment #2 by clugdbug — 2010-07-27T00:03:36Z
Here's a clearer test case. The second assert fails.
Closure detection works correctly if you change the delegate literal to:
return delegate int() { int x=heap; return nested_func(); };
---------------------
//import std.stdio;
int delegate() Foo()
{
int stack;
int heap=3;
// writeln("&stack=",&stack," &heap=",&heap);
int nested_func()
{
++heap;
return heap;
}
return delegate int() { return nested_func(); };
}
void main()
{
auto z = Foo();
auto p = Foo();
assert(z()==4); // OK
p();
assert(z()==5); // fails
}
Comment #3 by clugdbug — 2010-10-19T23:56:53Z
See also bug 1908, test case 5w, for another example.
Comment #4 by clugdbug — 2012-03-27T23:23:03Z
*** Issue 7766 has been marked as a duplicate of this issue. ***
Comment #5 by verylonglogin.reg — 2012-11-12T02:42:26Z
*** Issue 7303 has been marked as a duplicate of this issue. ***
Comment #6 by clugdbug — 2013-01-24T01:23:08Z
The basic problem is that the existing closure detection only checks parents of nested functions. It should also check sibling nested functions.
Ie, if one nested function f calls another nested function g, then if g needs a closure, then so does f.
More complex cases are possible, such as where f calls g which calls h, f escapes, h uses closure variables but f and g don't.
I have a fix for the original test case, still working on the more complex cases.
Comment #7 by clugdbug — 2013-01-26T02:22:04Z
*** Issue 2148 has been marked as a duplicate of this issue. ***
Comment #8 by github-bugzilla — 2013-01-27T11:34:12Z