[Issue 22159] "==" causeses error for array of classes in safe method

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 15 17:59:44 UTC 2023


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

Adam D. Ruppe <destructionator at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |destructionator at gmail.com

--- Comment #6 from Adam D. Ruppe <destructionator at gmail.com> ---
Consider the following:

---
class C { int a; this(int) @safe {} }
class D : C {
        this(int a) @safe { super(a); }
        override bool opEquals(Object rhs) const {
                // obviously not safe
                *(cast(int*) 0x5afe) = 0xdead5afe;
                return true;
        }
}

@safe void main()
{
    C c = new D(1);
    C[] a = [c, c, c];
    assert(a == [c, c, c]);
}
---


The compiler error message should tell you to make a `@safe` override for
opEquals in your child class, though doing this still issues an error


---
class C {
        int a; this(int) @safe {}

        override bool opEquals(Object rhs) @safe {
                return this is rhs;
        }
}

@safe void main()
{
    C c = new C(1);
    C[] a = [c, c, c];
    assert(a == [c, c, c]);
}
---

So it should really allow this latter thing and the error message should tell
you to write it, while still prohibiting the earlier example.

--


More information about the Digitalmars-d-bugs mailing list