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

--- Comment #4 from Kenji Hara <k.hara...@gmail.com> ---
Maybe SDC inserts a hidden field to access enclosing scope in each derived
classes?

void main() {
    uint x = 7;

    class A {
        int fieldA;
        auto foo() { return x; }
    }

    auto makeB(uint y) {
        class B : A {
            int fieldB;
            auto bar() { return foo() + y; }
        }

        return new B(5);
    }

    A a = new A();
    assert(a.foo() == 7);

    auto b = makeB(3);
    alias B = typeof(b);
    assert(b.bar() == 10);

    pragma(msg, A.fieldA.offsetof);
    pragma(msg, __traits(classInstanceSize, A));
    pragma(msg, B.fieldB.offsetof);
    pragma(msg, __traits(classInstanceSize, B));
}

If B.fieldB.offsetof + int.sizeof + (void*).sizeof <=
__traits(classInstanceSize, B), both B and A have their own hidden fields.

Note that it's intentionally disallowed by dmd. With dmd, implicitly insertion
of hidden field is limited at most once. At best, it's an incompatibility
between dmd and SDC.

--

Reply via email to