D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6812 - Failed equality of structs with string field
Summary: Failed equality of structs with string field
Status: RESOLVED DUPLICATE of issue 3789
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2011-10-13 11:55 UTC by bearophile_hugs
Modified: 2011-10-13 23:22 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-10-13 11:55:44 UTC
This program produces a runtime assert error, it's a bug:


struct Foo {
    string s;
}
void main() {
    Foo b1 = Foo("hello".idup);
    Foo b2 = Foo("hello".idup);
    assert(b1 !is b2); // OK
    assert(b1 == b2);  // line 8, error
}


With DMD 2.056head it gives:

core.exception.AssertError@test(8): Assertion failure


The equality among structs has to call the string equality of their fields. The "is" operator has to compare the structs bitwise.
Comment 1 Kenji Hara 2011-10-13 17:53:17 UTC
Class type has same problem.

struct Foo {
    string s;
}
struct Bar {
    static class X {
        bool opEquals(Object o){ return true; }
    }
    X x;
}
void main() {
    Foo f1 = Foo("hello".idup);
    Foo f2 = Foo("hello".idup);
    assert(f1 !is f2); // OK
    assert(f1 == f2);  // error

    Bar b1 = Bar(new Bar.X());
    Bar b2 = Bar(new Bar.X());
    assert(b1 !is b2);  // OK
    assert(b1 == b2);   // error!
}
Comment 2 Don 2011-10-13 23:10:37 UTC
Looks like a dup of bug 3789
Comment 3 Kenji Hara 2011-10-13 23:22:29 UTC

*** This issue has been marked as a duplicate of issue 3789 ***