[Issue 5519] New: Saner struct equality

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 2 15:09:23 PST 2011


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

           Summary: Saner struct equality
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          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 2011-02-02 15:07:06 PST ---
Performing a comparison between two structs is a very common operation. Often
structs contain strings and other things. Currently (DMD 2.051) the struct
equality ignores the contents of strings contained inside structs, and this
behaviour is unacceptably bug-prone in a language like D that's otherwise
oriented toward code safety. So in the following four programs I'd like the
assertions to pass.

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

struct Foo { string s; }
void main() {
    string s1 = "he";
    string s2 = "llo";
    string s3 = "hel";
    string s4 = "lo";
    auto f1 = Foo(s1 ~ s2);
    auto f2 = Foo(s3 ~ s4);
    assert((s1 ~ s2) == (s3 ~ s4));
    assert(f1 == f2); // this asserts
}

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

struct Foo { int[] a; }
void main() {
    auto a1 = [1, 2];
    auto a2 = [3, 4, 5];
    auto a3 = [1, 2, 3];
    auto a4 = [4, 5];
    auto f1 = Foo(a1 ~ a2);
    auto f2 = Foo(a3 ~ a4);
    assert((a1 ~ a2) == (a3 ~ a4));
    assert(f1 == f2); // this asserts
}

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

class Bar {
    int x;
    this(int x_) { x = x_; }
    bool opEquals(Object o) {
        return x == (cast(Bar)o).x;
    }
}
struct Foo { Bar a; }
void main() {
    auto f1 = Foo(new Bar(1));
    auto f2 = Foo(new Bar(1));
    assert(f1 == f2); // this asserts
}

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

struct Foo { int[int] aa; }
void main() {
    auto f1 = Foo([1:0, 2:0]);
    auto f2 = Foo([1:0, 2:0]);
    assert(f1.aa == f2.aa);
    assert(f1 == f2); // this asserts
}

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

The title of this enhancement request is "Saner struct equality" instead of
"Sane struct equality" because (it seems) D struct equality isn't
meant/expected to be fully correct. This example shows a struct comparison
problem this enhancement request doesn't cover:


import core.stdc.string: memset;
struct Foo { long l; byte b; }
void main() {
    Foo f1 = Foo(10, 20);
    Foo f2;
    memset(&f1, 'X', Foo.sizeof);
    f2.l = 10;
    f2.b = 20;
    assert(f1 == f2); // this asserts
}

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

Surprisingly this works (DMD 2.051):


struct Bar {
    int x;
    const bool opEquals(ref const(Bar) o) {
        return x == o.x || x == -o.x;
    }
}
struct Foo { Bar a; }
void main() {
    auto f1 = Foo(Bar(1));
    auto f2 = Foo(Bar(-1));
    assert(f1 == f2);  // this doesn't assert
}

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

-- 
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