[Issue 9148] 'pure' is broken

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Jul 3 20:59:48 PDT 2015


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

--- Comment #17 from Kenji Hara <k.hara.pg at gmail.com> ---
(In reply to timon.gehr from comment #16)
> I think this is not right. Why should impure nested functions not be able to
> access the enclosing context corresponding to frames of pure functions?

Because the nested impure function may modify the context of other pure
functions.

int g;
int impureFuncCall() { return g; }

auto func(out int delegate() pure pureDg, out void delegate() impureDg) pure
{
    int x = 1;      // become a closure variable

    int foo() pure  // weak purity
    {
        return x;
    }

    auto bar()()
    {
        // modify the context of pure funciton 'foo'
        // depending on the global state,
        if (impureFuncCall())
            x = 2;      // !!
    }

    pureDg = &foo;
    impureDg = &bar!();
}

void main()
{
    int delegate() pure pureDg;
    void delegate() impureDg;

    func(pureDg, impureDg);

    assert(pureDg() == 1);

    g = 1;  // modify the global state
    impureDg(); // modify the context of pureDg.

    assert(pureDg() == 1);  // fails~
}

--


More information about the Digitalmars-d-bugs mailing list