[Issue 12406] Broken delegate closure

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Jan 23 07:09:40 PST 2015


https://issues.dlang.org/show_bug.cgi?id=12406

Vladimir Panteleev <thecybershadow at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
           Priority|P2                          |P1
                 CC|                            |thecybershadow at gmail.com

--- Comment #2 from Vladimir Panteleev <thecybershadow at gmail.com> ---
Reduced:

////////////////////////////// test.d //////////////////////////////
struct Dg
{
    Dg delegate() action;
}

void fn(void delegate()) { }

Dg createDg()
{
    int x; fn({ x++; }); // required

    Dg dg;

    Dg createDg2()
    {
        int x; void unusedFun() { x++; } // required
        //fn({x++;}); // workaround - force closure creation
        return Dg(() => dg); // lambda returns garbage instead of dg
    }

    return dg = Dg(&createDg2);
}

void main()
{
    auto dg = createDg();
    foreach (i; 0..10)
        dg = dg.action();
}
////////////////////////////////////////////////////////////////////

As in the reduced example, you can work around this bug by forcing the compiler
to create a closure for the inner function.

--


More information about the Digitalmars-d-bugs mailing list