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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 15 09:58:20 PST 2008


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 at digitalmars.com
        ReportedBy: webmaster at 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.


-- 



More information about the Digitalmars-d-bugs mailing list