[Issue 1861] New: .sort fails if opCmp takes ref param

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Feb 22 00:15:38 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1861

           Summary: .sort fails if opCmp takes ref param
           Product: D
           Version: 1.027
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: wbaxter at gmail.com


------
module opcmpref;
import std.stdio;
struct Pair
{
    int a, b;
    // Sort by b first then a
    //  Sorting fails if rhs is a 'ref' param.  Ok if ref removed.
    int opCmp(ref Pair rhs) {
        if (b!=rhs.b) return b-rhs.b;
        return a-rhs.a;
    }
    string toString()
    {
        return std.string.format("(%s,%s)",a,b);
    }
}
void main()
{
    // Single comparisons are all fine
    assert( !(Pair(0,0)<Pair(0,0)) );
    assert( !(Pair(1,0)<Pair(0,0)) );
    assert( !(Pair(0,1)<Pair(0,0)) );
    assert( !(Pair(1,1)<Pair(0,0)) );
    assert(  (Pair(0,0)<Pair(1,0)) );
    assert( !(Pair(1,0)<Pair(1,0)) );
    assert( !(Pair(0,1)<Pair(1,0)) );
    assert( !(Pair(1,1)<Pair(1,0)) );

    assert(  (Pair(0,0)<Pair(0,1)) );
    assert(  (Pair(1,0)<Pair(0,1)) );
    assert( !(Pair(0,1)<Pair(0,1)) );
    assert( !(Pair(1,1)<Pair(0,1)) );
    assert(  (Pair(0,0)<Pair(1,1)) );
    assert(  (Pair(1,0)<Pair(1,1)) );
    assert(  (Pair(0,1)<Pair(1,1)) );
    assert( !(Pair(1,1)<Pair(1,1)) );

    // But sorting fails when opCmp takes 'ref' param
    auto p = [Pair(0,0), Pair(3,0), Pair(2,1), Pair(1,4)];
    p.sort;
    writefln("p=",p);
    assert(p[0] == Pair(0,0));
    assert(p[1] == Pair(3,0));
    assert(p[2] == Pair(2,1));
    assert(p[3] == Pair(1,4));
}
------------

This may be something in the low-level lib rather than the compiler per-se. 
I'm not sure about that.  I have tested and found that the error happens with
both Phobos and Tango.


-- 



More information about the Digitalmars-d-bugs mailing list