Comment #0 by bearophile_hugs — 2010-11-27T11:02:42Z
Bug found by Ellery Newcomer.
With DMD 2.050 this code asserts at run time:
import std.bigint;
void main() {
assert([BigInt(1)] == [BigInt(1)]);
}
Comment #1 by xammy — 2010-11-28T08:27:14Z
More precisely, the issue is that in the following code, the opEquals method is not called for the array comparison. As it seems, the only opEquals signature working for array comparison is "bool opEquals (ref const BigInt y) const", making it impossible to have more versions to compare to different types.
struct BigInt
{
bool opEquals (Tdummy = void)(ref const BigInt y) const
{
writefln("BigInt.opEquals!void(BigInt) called");
return true;
}
bool opEquals (T: long) (long y) const
{
writefln("BigInt.opEquals!long called");
return true;
}
}
void main()
{
BigInt i,j;
writefln("i == j: %s", i == j);
writefln("[i] == [j]: %s", [i] == [j]);
}
The output is:
BigInt.opEquals!void(BigInt) called
i == j: true
[i] == [j]: true
Comment #2 by clugdbug — 2010-12-01T23:56:02Z
This is difficult. The patch (below) fixes the problem, and I believe it's correct. The problem is, that applying it causes Phobos to break, because it has
been relying on this bug. And I don't know how to modify Phobos to fix it. Specifically, Tuple and Variant are affected.
There's a cluster of bugs related to the problem, eg: bug 3659 ("Too much exegesis on opEquals", bug 3607 "Problems with struct opEquals and const").
But the major problem is that with a const ref signature, opEquals cannot perform comparisons with rvalues. So the code below fails
----
struct Foo
{
bool opEquals(ref const Foo f) const { return true; }
}
Foo foo()
{
Foo result;
return result;
}
void main()
{
Foo f;
// assert(f == foo()); // does not compile
}
----------
To make this patch work with the existing Phobos, while bug 3659 is not fixed,
change
if (!eq)
fdx->error("type signature should be %s not %s"
into
if (!eq && !td)
fdx->error("type signature should be %s not %s"
PATCH:
StructDeclaration::semantic in struct.c
Around line 503,
tfeqptr = new TypeFunction(parameters, Type::tbool, 0, LINKd);
tfeqptr->mod = MODconst;
tfeqptr = (TypeFunction *)tfeqptr->semantic(0, sc2);
Dsymbol *s = search_function(this, Id::eq);
FuncDeclaration *fdx = s ? s->isFuncDeclaration() : NULL;
+ TemplateDeclaration *td = s ? s->isTemplateDeclaration() : NULL;
+ if (td)
+ {
+ Expressions arguments;
+ arguments.setDim(1);
+ arguments.data[0] = (void*) param;
+ fdx = td->deduceFunctionTemplate(sc, loc, NULL, NULL, &arguments, 1);
+ }
if (fdx)
{
eq = fdx->overloadExactMatch(tfeqptr);
if (!eq)
fdx->error("type signature should be %s not %s", tfeqptr->toChars(), fdx->type->toChars());
}
- TemplateDeclaration *td = s ? s->isTemplateDeclaration() : NULL;
- // BUG: should also check that td is a function template, not just a template
- if (!eq && !td)
+ if (!eq)
eq = buildOpEquals(sc2);
Comment #3 by bearophile_hugs — 2010-12-02T04:45:44Z
(In reply to comment #2)
> The problem is, that applying it causes Phobos to break, because it
> has been relying on this bug.
If this this true then this bug gains higher priority.
Comment #4 by k.hara.pg — 2011-06-25T12:12:51Z
(In reply to comment #2)
> But the major problem is that with a const ref signature, opEquals cannot
> perform comparisons with rvalues. So the code below fails
My pull request https://github.com/D-Programming-Language/dmd/pull/41 can fix bug4843, so opEquals overloading with ref and non-ref will be correct.
Comment #5 by k.hara.pg — 2011-10-09T03:57:56Z
By fixing bug 3659, template opEquals is also captured by TypeInfo.equals, and this problem has been fixed.