Thread overview
[Issue 5281] New: Equality among arrays of Bigints
Nov 28, 2010
Matthias Walter
Dec 02, 2010
Don
Jun 25, 2011
Kenji Hara
Oct 09, 2011
Kenji Hara
November 27, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5281

           Summary: Equality among arrays of Bigints
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-11-27 11:02:42 PST ---
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)]);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 28, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5281


Matthias Walter <xammy@xammy.homelinux.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xammy@xammy.homelinux.net
         OS/Version|Windows                     |All


--- Comment #1 from Matthias Walter <xammy@xammy.homelinux.net> 2010-11-28 08:27:14 PST ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 02, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5281


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch, wrong-code
                 CC|                            |clugdbug@yahoo.com.au
         Depends on|                            |3659
           Severity|normal                      |critical


--- Comment #2 from Don <clugdbug@yahoo.com.au> 2010-12-01 23:56:02 PST ---
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);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 02, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5281



--- Comment #3 from bearophile_hugs@eml.cc 2010-12-02 04:45:44 PST ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5281


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |k.hara.pg@gmail.com


--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2011-06-25 12:12:51 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5281


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2011-10-09 03:57:56 PDT ---
By fixing bug 3659, template opEquals is also captured by TypeInfo.equals, and this problem has been fixed.

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