[Issue 13114] New: old opCmp requirement for AA keys should be detected for classes

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Jul 12 12:51:16 PDT 2014


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

          Issue ID: 13114
           Summary: old opCmp requirement for AA keys should be detected
                    for classes
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Keywords: accepts-invalid
          Severity: regression
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: schveiguy at yahoo.com
                CC: k.hara.pg at gmail.com

Like https://issues.dlang.org/show_bug.cgi?id=13074, the same problem exists
for classes.

For example:

import std.stdio;
import std.conv;

class C
{
    this(int x, int y) pure {this.x = x; this.y = y;}
    int x;
    int y;
    //override bool opEquals(Object other) { if(auto o = cast(C)other) {return
x == o.x;} return false;}
    override int opCmp(Object other) { if(auto o = cast(C)other) {return x <
o.x ? -1 : x > o.x ? 1 : 0;} return -1;}
    override hash_t toHash() const { return x; }
    override string toString() const { return "{" ~ x.to!string ~ ", " ~
y.to!string ~ "}";}
}


void main()
{
    const c1 = new C(1,1);
    const c2 = new C(1,2);
    const c3 = new C(2,1);
    const c4 = new C(2,2);
    bool[const(C)] arr;
    arr[c1] = true;
    arr[c2] = true;
    arr[c3] = true;
    arr[c4] = true;
    writeln(arr);
}

Under 2.065, this prints:

[{1, 1}:true, {2, 1}:true]

Under git head (even after the fix for issue 13074), this compiles and prints:

[{1, 1}:true, {1, 2}:true, {2, 1}:true, {2, 2}:true]

Note, there are going to be cases that we cannot detect. For example,
bool[Object] would compile perfectly fine, but if you used a C instance as a
key, it would have the same problem. For that, we would need a runtime check.

I don't know if it's worth making a fix for that, it would really entail never
allowing overriding opCmp without also overriding opEquals. That may be a
bridge too far. But we should still fix the easy case (I hope it's easy!)

--


More information about the Digitalmars-d-bugs mailing list