Bug 6812 – Failed equality of structs with string field
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2011-10-13T11:55:00Z
Last change time
2011-10-13T23:22:29Z
Keywords
wrong-code
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2011-10-13T11:55:44Z
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 by k.hara.pg — 2011-10-13T17:53:17Z
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 by clugdbug — 2011-10-13T23:10:37Z
Looks like a dup of bug 3789
Comment #3 by k.hara.pg — 2011-10-13T23:22:29Z
*** This issue has been marked as a duplicate of issue 3789 ***