www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2148] New: Incorrect local closure detection when closure is used as local alias parameter

http://d.puremagic.com/issues/show_bug.cgi?id=2148

           Summary: Incorrect local closure detection when closure is used
                    as local alias parameter
           Product: D
           Version: 2.013
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: brunodomedeiros+bugz gmail.com


Check the following code:
---   ---
import std.stdio;

template foo(alias magicFn) {
        int foo() {
                return magicFn();
        }
}


int delegate() myfunc()
{
        int num = 2;
        int nestedFunc() { return num; };

        //return &nestedFunc; 
        return &foo!(nestedFunc);
}


void main(string[] args) {
        auto dg = myfunc();
        writeln("Dg result:", dg()); // prints garbage
}
---- ----
Running this will print garbage, because the compiler thinks nestedFunc is a
local closure (a closure that does not escape the enclosing scope), and thus
will not allocate num on the heap.


-- 
Jun 10 2008