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

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue May 23 17:04:00 PDT 2017


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

--- Comment #10 from Eyal <eyal at weka.io> ---
Sorry, this is the actual example that showed the dynamic scoping lookup in the
PR is incorrect:

@safe:

unittest {
    enum Caller { OpApply, Other }
    struct OpApply {
        @safe:
        int delegate(Caller) @safe myDlg;
        int opApply(int delegate(Caller) @safe dlg) {
            myDlg = dlg;
            return dlg(Caller.OpApply);
        }
    }
    struct Foo {
        @safe:
        OpApply o;
        int i;
        this(int x) {
            i = x;
            o = OpApply();
            foreach(caller; o) {
                final switch(caller) {
                case Caller.OpApply:
                    if(i == 1) {
                        auto foo2 = Foo(2);
                        assert(2 == foo2.call(o.myDlg));
                        assert(i == 0);
                    }
                    break;
                case Caller.Other:
                    i = 0;
                    break;
                }
            }
        }

        int call(int delegate(Caller) @safe dlg) {
            dlg(Caller.Other);
            return i;
        }
    }
    Foo(1);
}

The distilled one somehow

--


More information about the Digitalmars-d-bugs mailing list