[Issue 4290] New: 'Fragile' opCmp/toHash signature errors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 6 13:08:46 PDT 2010


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

           Summary: 'Fragile' opCmp/toHash signature errors
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: diagnostic
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-06-06 13:08:45 PDT ---
This is a reduced version of a bug in my code. This D2 code compiles, but at
the end there are two instances of Foo inside the AA.


struct Foo {
    int x;
    const bool opEquals(ref const Foo f) {
        return true;
    }
    int opCmp(ref const Foo f) {
        return 0;
    }
    hash_t toHash() {
        return 10;
    }
}
void main() {
    int[Foo] aa;
    aa[Foo(1)]++;
    aa[Foo(2)]++;
    assert(aa.length == 1); // asserts, it's equal to 2
}


The problem is caused just by two missing 'const', the following code is
correct:


struct Foo {
    int x;
    const bool opEquals(ref const Foo f) {
        return true;
    }
    int opCmp(ref const Foo f) const {
        return 0;
    }
    hash_t toHash() const {
        return 10;
    }
}
void main() {
    int[Foo] aa;
    aa[Foo(1)]++;
    aa[Foo(2)]++;
    assert(aa.length == 1); // OK
}


In my opinion the D2 has to catch such programming errors and refuse opCmp and
toHash with a wrong signature, to avoid similar common bugs.

See also bug 3844

-----------------------------

An alternative solution is to invent something similar to the 'override'
keyword for struct member functions:

struct Foo {
    int x;
    const bool opEquals(ref const Foo f) {
        return true;
    }
    static_override int opCmp(ref const Foo f) { // error, doesn't override the
default opCmp
        return 0;
    }
    static_override hash_t toHash() { // error, doesn't override the default
toHash
        return 10;
    }
}
void main() {
    int[Foo] aa;
    aa[Foo(1)]++;
    aa[Foo(2)]++;
    assert(aa.length == 1); // asserts, it's equal to 2
}


But D2 structs don't support inheritance, so static_override is of limited
usefulness, it looks over-engineering.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list