www.digitalmars.com         C & C++   DMDScript  

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

reply d-bugmail puremagic.com writes:
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.


-- 
Feb 15 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1841





------- Comment #1 from webmaster villagersonline.com  2008-03-31 12:38 -------
This seems to work on dmd 2.012.  Did dmd change?


-- 
Mar 31 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1841


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #2 from Don <clugdbug yahoo.com.au> 2010-07-27 00:03:36 PDT ---
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
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 27 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1841



--- Comment #3 from Don <clugdbug yahoo.com.au> 2010-10-19 23:56:53 PDT ---
See also bug 1908, test case 5w, for another example.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 19 2010