Skip to content

Commit d719e70

Browse files
authored
Merge pull request #4475 from kinke/fix_float_opcmp
Cherry-pick druntime fix for array comparisons with floating-point opCmp
2 parents aea290d + cc8b4c5 commit d719e70

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

runtime/druntime/src/core/internal/array/comparison.d

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int __cmp(T)(scope const T[] lhs, scope const T[] rhs) @trusted
8383
// This function is called by the compiler when dealing with array
8484
// comparisons in the semantic analysis phase of CmpExp. The ordering
8585
// comparison is lowered to a call to this template.
86-
int __cmp(T1, T2)(T1[] s1, T2[] s2)
86+
auto __cmp(T1, T2)(T1[] s1, T2[] s2)
8787
if (!__traits(isScalar, T1) && !__traits(isScalar, T2))
8888
{
8989
import core.internal.traits : Unqual;
@@ -237,3 +237,26 @@ if (!__traits(isScalar, T1) && !__traits(isScalar, T2))
237237
auto vb = [cast(void[])b[0], b[1]];
238238
assert(less2(va, vb));
239239
}
240+
241+
// custom aggregate types
242+
@safe unittest
243+
{
244+
// https://issues.dlang.org/show_bug.cgi?id=24044
245+
// Support float opCmp(...) with array
246+
static struct F
247+
{
248+
float f;
249+
float opCmp(F other) const { return this.f - other.f; }
250+
}
251+
252+
F[2] a = [F(1.0f), F(float.nan)];
253+
F[2] b = [F(1.0f), F(1.0f)];
254+
F[1] c = [F(1.0f)];
255+
256+
bool isNan(float f) { return f != f; }
257+
258+
assert(isNan(__cmp(a, b)));
259+
assert(isNan(__cmp(a, a)));
260+
assert(__cmp(b, b) == 0);
261+
assert(__cmp(a, c) > 0);
262+
}

0 commit comments

Comments
 (0)