[Issue 669] (a == b) misuses opCmp and opEquals

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Dec 10 11:43:31 PST 2006


http://d.puremagic.com/issues/show_bug.cgi?id=669





------- Comment #2 from burton-radons at smocky.com  2006-12-10 13:43 -------
Grah I'm a stupid. Here's code which actually exhibits the problem:

        class C
        {
                override int opEquals (Object other) { printf ("opEquals\n");
return 0; }
                override int opCmp (Object other) { printf ("opCmp\n"); return
0; }
        }

        void main ()
        {
                auto type = typeid (typeof (C));
                void *data = new C;

                assert (type.equals (&data, &data) == 0);
        }

The fault is TypeInfo_Class in internal.object. It's implemented like this:

        int equals(void *p1, void *p2)
        {
                Object o1 = *cast(Object*)p1;
                Object o2 = *cast(Object*)p2;

                return o1 == o2 || (o1 && o1.opCmp(o2) == 0);
        }       

When it should be implemented like this:

        int equals(void *p1, void *p2)
        {
                Object o1 = *cast(Object*)p1;
                Object o2 = *cast(Object*)p2;

                return o1 == o2;
        }       

This problem showed itself because I was comparing boxed objects, so I assume
it's written the way it is now for associative arrays. If so then associative
arrays should have their own slot in a TypeInfo for performing this
(unorderedOrEqual), and std.boxer can have a pure form with the correct
semantics for ==. Otherwise std.boxer's Box.opEqualsInternal can have a clause
for objects.


-- 



More information about the Digitalmars-d-bugs mailing list