www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5281] New: Equality among arrays of Bigints

reply d-bugmail puremagic.com writes:
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



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: -------
Nov 27 2010
next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Nov 28 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Dec 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5281






 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: -------
Dec 02 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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




 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: -------
Jun 25 2011
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Oct 09 2011