[Issue 21109] Wrong result when using sort() on enum arrays

2022-12-17 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=21109

Iain Buclaw  changed:

   What|Removed |Added

   Priority|P1  |P2

--


[Issue 21109] Wrong result when using sort() on enum arrays

2020-08-07 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=21109

Simen Kjaeraas  changed:

   What|Removed |Added

 CC||simen.kja...@gmail.com

--- Comment #7 from Simen Kjaeraas  ---
We can reduce this issue down to this code:

struct S {
int[] arr;
}

unittest {
import std.stdio;
foreach (idx; 0 .. 10) {
writeln([S([0])] < [S([0])]);
}
}

I'm not convinced that being able to compare two arrays like this should be
possible at all - the rule that for any T where T.init < T.init does not
compile, [T.init] < [T.init] should not compile seems sensible to me.

If we need it to work, it should definitely do a member-by-member comparison.
Not least, this should be documented.

--


[Issue 21109] Wrong result when using sort() on enum arrays

2020-08-07 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=21109

--- Comment #6 from Andrej Mitrovic  ---
(In reply to Boris Carvajal from comment #5)
> There is no 'opCmp' defined for the struct so the runtime just use a memcmp
> for the comparison.
> The code itself calls '__cmp' from core/internal/array/comparison.d (because
> the element are an array '[S(2, [0, 0])]') and there the 3 'static if'
> conditions fail but if you define 'opCmp' the second path is taken.
> 
> So the following works:
> 
> struct S
> {
> int[] arr;
> int opCmp(ref const S s) const { return arr < s.arr; }
> }
> 
> There is a comment on clone.d file that says:
> 
> "Essentially, a struct which does not define opCmp is not comparable."
> 
> This concise phrase should be on the spec.

Hmm.

In the past the compiler would also do memcmp when a struct didn't define
`opEquals`, but we changed it so it does member-by-member `==` comparison.

I wonder if we can do something similar for opCmp, or if that wouldn't make
sense..

--


[Issue 21109] Wrong result when using sort() on enum arrays

2020-08-04 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=21109

Boris Carvajal  changed:

   What|Removed |Added

 CC||boris...@gmail.com

--- Comment #5 from Boris Carvajal  ---
There is no 'opCmp' defined for the struct so the runtime just use a memcmp for
the comparison.
The code itself calls '__cmp' from core/internal/array/comparison.d (because
the element are an array '[S(2, [0, 0])]') and there the 3 'static if'
conditions fail but if you define 'opCmp' the second path is taken.

So the following works:

struct S
{
int[] arr;
int opCmp(ref const S s) const { return arr < s.arr; }
}

There is a comment on clone.d file that says:

"Essentially, a struct which does not define opCmp is not comparable."

This concise phrase should be on the spec.

--


[Issue 21109] Wrong result when using sort() on enum arrays

2020-08-03 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=21109

Andrej Mitrovic  changed:

   What|Removed |Added

Summary|Possibly wrong codegen when |Wrong result when using
   |using enum arrays   |sort() on enum arrays

--