[Issue 16301] CTFE execution of opApply keeps wrong "this" context in foreach's body

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri May 12 15:07:33 PDT 2017


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

--- Comment #4 from Walter Bright <bugzilla at digitalmars.com> ---
The following simpler code reproduces the problem:

------------------------
struct OpApply {
    void opApply(void delegate() dlg) {
        dlg();
    }
}
struct Foo {
    int i;

    int abc() {
    auto o = OpApply();
    void dg() { i = 0; } // couldn't find field i of type int in OpApply()
    o.opApply(&dg);
    return 0;
    }
}

void bar() {
    enum x = Foo().abc();
}
---------------------------

This code fails in a similar manner, and also works outside of CTFE (replace
enum with auto and it compiles):
---------------------------
void opApply(void delegate() dlg) { dlg(); }

struct Foo {
    int i;

    int abc() {
        void dg() { i = 0; } // value of 'this' is not known at compile time
        opApply(&dg);
        return 0;
    }
}

void bar() {
    enum x = Foo().abc();
}
-------------------------

This stuff is pretty deep in the bowels of CTFE, which I don't really
understand :-(

--


More information about the Digitalmars-d-bugs mailing list