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






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



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 next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1841




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
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|                            |xinok live.com



*** Issue 7766 has been marked as a duplicate of this issue. ***

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


Denis Shelomovskij <verylonglogin.reg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dpx.infinity gmail.com



13:42:26 MSK ---
*** Issue 7303 has been marked as a duplicate of this issue. ***

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


Denis Shelomovskij <verylonglogin.reg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |verylonglogin.reg gmail.com
           Platform|x86                         |All
            Version|2.010                       |D2
         OS/Version|Linux                       |All
           Severity|normal                      |major


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




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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 24 2013
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|                            |bdom.pub+deebugz gmail.com



*** Issue 2148 has been marked as a duplicate of this issue. ***

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




Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/e5de59d30026fa5267541a928d67af1e8c06f922
Fix issue 1841 Closure detection fails in nested functions

If an escaping function doesn't use any closure variables, but
calls another nested function which does, it must be marked as
needing a closure. This 'calling a sibling' case was missing.

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 27 2013
prev 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|                            |edmccard verizon.net



*** Issue 7801 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 01 2013