[Issue 2148] New: Incorrect local closure detection when closure is used as local alias parameter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jun 10 10:06:53 PDT 2008


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 at digitalmars.com
        ReportedBy: brunodomedeiros+bugz at 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.


-- 



More information about the Digitalmars-d-bugs mailing list