Jump to page: 1 2
Thread overview
[Issue 1841] New: Closure detection doesn't work when variable is used in a nested function
Feb 15, 2008
d-bugmail
Mar 31, 2008
d-bugmail
Jul 27, 2010
Don
Oct 20, 2010
Don
Mar 28, 2012
Don
Nov 12, 2012
Denis Shelomovskij
Nov 12, 2012
Denis Shelomovskij
Jan 24, 2013
Don
Jan 26, 2013
Don
Jan 27, 2013
Walter Bright
Feb 01, 2013
Don
February 15, 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@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.


-- 

March 31, 2008
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?


-- 

July 27, 2010
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: -------
October 20, 2010
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: -------
March 28, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=1841


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xinok@live.com


--- Comment #4 from Don <clugdbug@yahoo.com.au> 2012-03-27 23:23:03 PDT ---
*** 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: -------
November 12, 2012
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


--- Comment #5 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2012-11-12 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: -------
November 12, 2012
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: -------
January 24, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=1841



--- Comment #6 from Don <clugdbug@yahoo.com.au> 2013-01-24 01:23:08 PST ---
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: -------
January 26, 2013
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


--- Comment #7 from Don <clugdbug@yahoo.com.au> 2013-01-26 02:22:04 PST ---
*** 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: -------
January 27, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=1841



--- Comment #8 from github-bugzilla@puremagic.com 2013-01-27 11:34:12 PST ---
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: -------
« First   ‹ Prev
1 2