Thread overview
[Issue 1517] New: .sort for structs behaves badly with ref opCmp
Sep 19, 2007
d-bugmail
Sep 19, 2007
d-bugmail
September 19, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1517

           Summary: .sort for structs behaves badly with ref opCmp
           Product: D
           Version: 1.021
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: wbaxter@gmail.com


This is incorrect code according to the spec but it doesn't really make sense to me why it doesn't work.

So the spec says:
"""
For the .sort property to work on arrays of structs or unions, the struct or
union definition must define the function: int opCmp(S) or int opCmp(S*). The
type S is the type of the struct or union. This function will determine the
sort ordering.
"""
But I just happened to use opCmp(ref S) and it seemed to work (everything
compiled fine and ran without crashing, anyway).

But it doesn't actually work.  Sorting sorts improperly.

I think making a ref opCmp in a struct should either be made to work (i see no reason why it should not work), or it should be made a compiler error to try to create one.

Attached is a simple test.


-- 

September 19, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1517





------- Comment #1 from wbaxter@gmail.com  2007-09-19 02:45 -------
Created an attachment (id=185)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=185&action=view)
Simple test


-- 

March 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1517


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #2 from bearophile_hugs@eml.cc 2010-03-17 20:10:47 PDT ---
Reduced test case, on dmd v2.041:


struct S {
    int i;

    int opCmp(S other) {
        return this.i - other.i;
    }
}

void main() {
    S[S] aa;
    foreach (int i; [1, -1, 7])
        aa[S(i)] = S(-i);

    // wrong order:
    assert(aa.keys.sort == [S(1), S(7), S(-1)]);
}


If this is a real bug (and it seems so) then I think it's serious bug, it's worse than a compiler crash, because the program runs unreliably.

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