Repository navigation
Fold compare against 0 only where JS operators agree - #9400
illusionalsagacity wants to merge 1 commit into
Conversation
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page ✨ Fold compare against 0 only where the JS operators agree5e95fc5 · illusionalsagacity · Nov 23, 2026, 20:29 UTC · new
JS modified (compare against 0) type point = {x: int}
let equal = (a: point, b: point) => compare(a, b) == 0
let less = (a: array<int>, b: array<int>) => compare(a, b) < 0
let nanEqual = (a: float, b: float) => compare(a, b) == 0
let intLess = (a: int, b: int) => compare(a, b) < 0
function equal(a, b) {
- return a === b;
+ return Primitive_object.compare(a, b) === 0;
}
function less(a, b) {
- return a < b;
+ return Primitive_object.compare(a, b) < 0;
}
function nanEqual(a, b) {
- return a === b;
+ return Primitive_float.compare(a, b) === 0;
}
function intLess(a, b) {
return a < b;
}JS modified (compare of int constants) let a = compare(-1, -1) == 0
let b = compare(-5, 3) > 0
-let a = false;
+let a = true;
-let b = true;
+let b = false;Review
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9400 +/- ##
=======================================
Coverage 82.22% 82.22%
=======================================
Files 461 461
Lines 65573 65577 +4
=======================================
+ Hits 53916 53922 +6
+ Misses 11657 11655 -2
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
Why:
`Js_exp_make.int_comp` rewrites a runtime `compare(a, b)` that is
compared with `0` into the JS operator on `a` and `b`. It matched any
runtime function named `compare` without looking at its module, so the
structural `Primitive_object.compare` and `Primitive_float.compare`
were rewritten as well: equal records compared unequal, arrays were
ordered as strings, and `compare(nan, nan) == 0` was `false`. The fold
now applies only to the `compare` of `Primitive_int`, `Primitive_bool`,
`Primitive_string` and `Primitive_bigint`.
The same fold read the left of two int constants as unsigned and the
right one as signed, so a negative left constant became a large
positive number. Both are now read as signed.
JS modified (compare against 0):
Example:
type point = {x: int}
let equal = (a: point, b: point) => compare(a, b) == 0
let less = (a: array<int>, b: array<int>) => compare(a, b) < 0
let nanEqual = (a: float, b: float) => compare(a, b) == 0
let intLess = (a: int, b: int) => compare(a, b) < 0
Before (js):
function equal(a, b) {
return a === b;
}
function less(a, b) {
return a < b;
}
function nanEqual(a, b) {
return a === b;
}
function intLess(a, b) {
return a < b;
}
After (js):
function equal(a, b) {
return Primitive_object.compare(a, b) === 0;
}
function less(a, b) {
return Primitive_object.compare(a, b) < 0;
}
function nanEqual(a, b) {
return Primitive_float.compare(a, b) === 0;
}
function intLess(a, b) {
return a < b;
}
JS modified (compare of int constants):
Example:
let a = compare(-1, -1) == 0
let b = compare(-5, 3) > 0
Before (js):
let a = false;
let b = true;
After (js):
let a = true;
let b = false;
Review: Which compares still fold? Those of ints, bools, strings and
bigints, where the runtime function gives the same answer as the JS
operator for well-typed values. Constant variants use the int
`compare`, so they fold as before.
Review: Where can the folded and the runtime result still differ? For
values JS cannot order: `@as(null)` against `@as(undefined)`
constructors, or a NaN typed as an int. The runtime `compare` returns 1
for those and the JS operators return `false`; this is unchanged.
Review: Was the unsigned read of the left constant needed? Not on master:
`Ext_int.int32_unsigned_to_int` had no other caller and is removed, and
no checked-in output changes because of it.
Review: Which checked-in outputs change? `test_list.mjs`, whose
polymorphic `mem`, `assoc`, `mem_assoc` and `remove_assoc` now call
`Primitive_object.compare`, and `caml_compare_bigint_test.mjs`, whose
values are typed as floats.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
5a24290 to
5e95fc5
Compare
Part of #9398 (item 1).
Problem
Js_exp_make.int_comprewrites a runtimecomparecall that is compared with0into a direct JS comparison:compare(a, b) == 0becomesa === b,< 0becomesa < b, and so on. The pattern matched any runtime function namedcomparewithout looking at its module, so the structuralPrimitive_object.compareandPrimitive_float.comparewere folded as well:Before:
equal({x: 1}, {x: 1})returnsfalse,less([10], [9])returnstruebecause JS compares the arrays as strings, andnanEqual(nan, nan)returnsfalsealthoughcompare(nan, nan)is0.After:
Fix
The fold now applies only when the call is the
compareofPrimitive_int,Primitive_bool,Primitive_stringorPrimitive_bigint. Each of those is a total order that agrees with the JS<,===and>operators, so their output is unchanged.Primitive_float.compare(NaN) andPrimitive_object.compare(structural) are left as runtime calls.Restricting the fold to ints alone would also fix the bug; I kept the other three because the rewrite is exact for them. Happy to narrow it if you prefer.
Negative int constants
A review of this change found a second bug in the same fold.
int_compfolds a comparison of two int constants, reading the left one as unsigned and the right one as signed. When thecomparefold recursed into it with a negative left constant, the result was wrong:Both constants are now read as signed.
Ext_int.int32_unsigned_to_inthad no other caller and is removed. No other test output changes.Tests
tests/tests/src/compare_fold_test.rescompares records, tuples, arrays, options and NaN through an@inline(never) id, and checks that int, string, bool and bigint still fold, that negative int constants fold to the right result, and that ordinary floats compare correctly through the runtime. Without the compiler change its first three tests fail.Two existing outputs change:
test_list.mjs: the polymorphicmem,assoc,mem_assocandremove_assocnow callPrimitive_object.compare(...) === 0instead of===. They were affected by the same bug.caml_compare_bigint_test.mjs: that test types its values asfloat, so itscompareis the float one and is no longer folded.The fix was written by Claude and reviewed by me.