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

Vladimir Panteleev <thecybersha...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
           Priority|P2                          |P1
                 CC|                            |thecybersha...@gmail.com

--- Comment #2 from Vladimir Panteleev <thecybersha...@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.

--

Reply via email to