opEquals @safe is ignored

Simen Kjærås simen.kjaras at gmail.com
Sun May 24 10:43:33 UTC 2020


On Sunday, 24 May 2020 at 08:57:28 UTC, Luis wrote:
> dmd ignores @trusted or @safe on opEquals, throwing this error :
>
> onlineapp.d(27): Error: @safe function 
> onlineapp.__unittest_L24_C7 cannot call @system function 
> object.opEquals
>
> An override @system or @trusted function can't be @safe, or I 
> it a bug ?
>
> Also, how will this be affected by DIP1028 ?

Looking at the output, it's actually druntime's opEquals that 
can't be called. That's the global function that ensure opEquals 
is called on both sides of the comparison, and it's not @safe. 
This makes classes even worse to use in D than, and I'm not sure 
how to properly handle this. If we make it @trusted, suddenly I 
can call @system opEquals from @safe functions without issue.

Root cause is, classes in D all inherit from Object, which has 
some defaults that aren't always what you want. It seems DIP1028 
will make your code automatically compile, along with this:

class C {
     override bool opEquals(Object o) @system {
         *cast(int*)123456 = 3;
         return true;
     }
}

@safe void mySafeFunction() {
     assert(new C() == new C());
}

--
   Simen


More information about the Digitalmars-d-learn mailing list