[Issue 13116] Should not be able to return ref to 'this'

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jul 14 10:20:09 PDT 2014


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

--- Comment #5 from hsteoh at quickfur.ath.cx ---
As Kenji points out, this is part of a more general problem where `super` and
`this` in class methods are lvalues, so they are liable to illegal rebindings:
----
class Base {
    int x;
}
void rebind(ref Base b) { b = new Base; }
void rebind(ref Derived d) { d = new Derived; }
class Derived : Base {
    int y;
    void evil() {
        rebind(super);
        x = 123; // this modifies a different copy of Base.x than this one!

        rebind(this);
        y = 123; // this modifies a different copy of this.y !
    }
}
----
This problem can be solved if we make `this` and `super` rvalues in class
methods. Note that in struct methods, `this` is OK to be an lvalue, because
structs are by-value types so no rebinding is involved in the struct analog of
the above code; it modifies the struct in-place.

--


More information about the Digitalmars-d-bugs mailing list