From 5e95fc524f52de977d4e28ecb79a640a5bbfb4a0 Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 23 Nov 2026 15:29:35 -0500 Subject: [PATCH] Fold compare against 0 only where the JS operators agree 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, b: array) => 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 --- changes/compare-fold-only-primitives.fix.md | 2 + compiler/backend/ext_int.ml | 7 -- compiler/backend/ext_int.mli | 6 -- compiler/backend/j.ml | 11 ++-- compiler/backend/js_exp_make.ml | 23 +++++-- tests/tests/src/caml_compare_bigint_test.mjs | 4 +- tests/tests/src/compare_fold_test.mjs | 69 ++++++++++++++++++++ tests/tests/src/compare_fold_test.res | 60 +++++++++++++++++ tests/tests/src/test_list.mjs | 9 +-- 9 files changed, 159 insertions(+), 32 deletions(-) create mode 100644 changes/compare-fold-only-primitives.fix.md create mode 100644 tests/tests/src/compare_fold_test.mjs create mode 100644 tests/tests/src/compare_fold_test.res diff --git a/changes/compare-fold-only-primitives.fix.md b/changes/compare-fold-only-primitives.fix.md new file mode 100644 index 0000000000..7f1c402b56 --- /dev/null +++ b/changes/compare-fold-only-primitives.fix.md @@ -0,0 +1,2 @@ +- Fold `compare(a, b)` against `0` into a JS comparison only for ints, bools, strings and bigints, so structural comparison of records, tuples, arrays, options and NaN floats is no longer miscompiled. +- Fix `compare` of int constants against `0` folding to the wrong result when the first constant is negative, such as `compare(-1, -1) == 0`. diff --git a/compiler/backend/ext_int.ml b/compiler/backend/ext_int.ml index e40200581c..3b037646ff 100644 --- a/compiler/backend/ext_int.ml +++ b/compiler/backend/ext_int.ml @@ -22,13 +22,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -let move = 0x1_0000_0000 - -(* works only on 64 bit platform *) -let int32_unsigned_to_int (n : int32) : int = - let i = Int32.to_int n in - if i < 0 then i + move else i - let int32_pow (x : int32) (y : int32) = let x_float = Int32.to_float x in let y_float = Int32.to_float y in diff --git a/compiler/backend/ext_int.mli b/compiler/backend/ext_int.mli index 0cfc2f0ba8..18d22c9322 100644 --- a/compiler/backend/ext_int.mli +++ b/compiler/backend/ext_int.mli @@ -22,10 +22,4 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -val int32_unsigned_to_int : int32 -> int -(** - works on 64 bit platform only - given input as an uint32 and convert it io int64 -*) - val int32_pow : int32 -> int32 -> int32 diff --git a/compiler/backend/j.ml b/compiler/backend/j.ml index dc90f245b1..6926c81d2f 100644 --- a/compiler/backend/j.ml +++ b/compiler/backend/j.ml @@ -59,13 +59,10 @@ and vident = Id of ident | Qualified of module_id * string option when the third one is None, it means the whole module TODO: - invariant, when [kind] is [Runtime], then we can ignore [ident], - since all [runtime] functions are unique, when do the - pattern match we can ignore the first one for simplicity - for example - {[ - Qualified (_, Runtime, Some "compare") - ]} + when [kind] is [Runtime], the name alone does not identify a function: + several runtime modules export the same name with different semantics, + such as [compare] of [Primitive_int], [Primitive_float] and + [Primitive_object]. Match on the module's [ident] as well. *) and exception_ident = ident diff --git a/compiler/backend/js_exp_make.ml b/compiler/backend/js_exp_make.ml index 5df30e496f..2ffbcf3e6d 100644 --- a/compiler/backend/js_exp_make.ml +++ b/compiler/backend/js_exp_make.ml @@ -1518,21 +1518,32 @@ let compare_int_aux (cmp : Lambda.comparison) (l : int) r = | Cle -> l <= r | Cge -> l >= r +(* [compare] of these runtime modules agrees with the JS operators on + well-typed values, so [compare a b 0] is [a b]. Not so for + [Primitive_float] (NaN) nor [Primitive_object] (structural). Values that + JS cannot order, such as [null] and [undefined] constructors or a NaN typed + as an int, compare as greater at runtime and as unordered when folded. *) +let compare_agrees_with_js_operators (module_id : J.module_id) = + let name = Ident.name module_id.id in + name = Primitive_modules.int + || name = Primitive_modules.bool + || name = Primitive_modules.string + || name = Primitive_modules.bigint + let rec int_comp (cmp : Lambda.comparison) (e0 : t) (e1 : t) = match (cmp, e0.expression_desc, e1.expression_desc) with | _, Number (Int {i = l}), Number (Int {i = r}) -> - let l = Ext_int.int32_unsigned_to_int l in - let r = Int32.to_int r in - bool (compare_int_aux cmp l r) + bool (compare_int_aux cmp (Int32.to_int l) (Int32.to_int r)) | ( _, Call ( { - expression_desc = Var (Qualified ({kind = Runtime}, Some "compare")); - _; + expression_desc = + Var (Qualified (({kind = Runtime} as module_id), Some "compare")); }, [l; r], _ ), - Number (Int {i = 0l}) ) -> + Number (Int {i = 0l}) ) + when compare_agrees_with_js_operators module_id -> int_comp cmp l r (* = 0 > 0 < 0 *) | (Ceq, Optional_block _, Undefined _ | Ceq, Undefined _, Optional_block _) when no_side_effect e0 && no_side_effect e1 -> diff --git a/tests/tests/src/caml_compare_bigint_test.mjs b/tests/tests/src/caml_compare_bigint_test.mjs index 87b8da60d4..555a47d31f 100644 --- a/tests/tests/src/caml_compare_bigint_test.mjs +++ b/tests/tests/src/caml_compare_bigint_test.mjs @@ -8,8 +8,8 @@ let five = BigInt("5"); Mocha.describe("Caml_compare_bigint_test", () => { Mocha.test("compare: 123 and 555555", () => { - Test_utils.ok("File \"caml_compare_bigint_test.res\", line 10, characters 7-14", BigInt("555555") > BigInt("123")); - Test_utils.ok("File \"caml_compare_bigint_test.res\", line 11, characters 7-14", BigInt("123") < BigInt("555555")); + Test_utils.ok("File \"caml_compare_bigint_test.res\", line 10, characters 7-14", Primitive_float.compare(BigInt("555555"), BigInt("123")) > 0); + Test_utils.ok("File \"caml_compare_bigint_test.res\", line 11, characters 7-14", Primitive_float.compare(BigInt("123"), BigInt("555555")) < 0); Test_utils.ok("File \"caml_compare_bigint_test.res\", line 12, characters 7-14", BigInt("123") < BigInt("555555")); Test_utils.ok("File \"caml_compare_bigint_test.res\", line 13, characters 7-14", BigInt("123") <= BigInt("555555")); Test_utils.ok("File \"caml_compare_bigint_test.res\", line 14, characters 7-14", BigInt("555555") > BigInt("123")); diff --git a/tests/tests/src/compare_fold_test.mjs b/tests/tests/src/compare_fold_test.mjs new file mode 100644 index 0000000000..fa8029adcb --- /dev/null +++ b/tests/tests/src/compare_fold_test.mjs @@ -0,0 +1,69 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; +import * as Primitive_float from "@rescript/runtime/lib/es6/Primitive_float.mjs"; +import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.mjs"; + +function id(x) { + return x; +} + +Mocha.describe("Compare_fold_test", () => { + Mocha.test("records", () => { + let a = id({ + x: 1 + }); + let b = id({ + x: 1 + }); + let c = id({ + x: 2 + }); + Test_utils.eq("File \"compare_fold_test.res\", line 19, characters 7-14", Primitive_object.compare(a, b) === 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 20, characters 7-14", Primitive_object.compare(a, c) < 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 21, characters 7-14", Primitive_object.compare(c, a) > 0, true); + }); + Mocha.test("tuples, arrays and options", () => { + Test_utils.eq("File \"compare_fold_test.res\", line 25, characters 7-14", Primitive_object.compare(id([ + 1, + 2 + ]), id([ + 1, + 2 + ])) === 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 26, characters 7-14", Primitive_object.compare(id([10]), id([9])) < 0, false); + Test_utils.eq("File \"compare_fold_test.res\", line 27, characters 7-14", Primitive_object.compare(id(undefined), id(1)) < 0, true); + }); + Mocha.test("NaN", () => { + let nan = id(NaN); + Test_utils.eq("File \"compare_fold_test.res\", line 32, characters 7-14", Primitive_float.compare(nan, id(1.0)) < 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 33, characters 7-14", Primitive_float.compare(nan, nan) === 0, true); + }); + Mocha.test("primitives that fold", () => { + Test_utils.eq("File \"compare_fold_test.res\", line 37, characters 7-14", id(1) < id(2), true); + Test_utils.eq("File \"compare_fold_test.res\", line 38, characters 7-14", id("b") > id("a"), true); + Test_utils.eq("File \"compare_fold_test.res\", line 39, characters 7-14", id(true) === id(true), true); + Test_utils.eq("File \"compare_fold_test.res\", line 40, characters 7-14", id(2n) >= id(1n), true); + Test_utils.eq("File \"compare_fold_test.res\", line 41, characters 7-14", id(1) !== id(1), false); + Test_utils.eq("File \"compare_fold_test.res\", line 42, characters 7-14", id(1) <= id(2), true); + }); + Mocha.test("negative int constants", () => { + Test_utils.eq("File \"compare_fold_test.res\", line 46, characters 7-14", true, true); + Test_utils.eq("File \"compare_fold_test.res\", line 47, characters 7-14", false, false); + Test_utils.eq("File \"compare_fold_test.res\", line 48, characters 7-14", true, true); + Test_utils.eq("File \"compare_fold_test.res\", line 49, characters 7-14", true, true); + Test_utils.eq("File \"compare_fold_test.res\", line 51, characters 7-14", true, true); + Test_utils.eq("File \"compare_fold_test.res\", line 52, characters 7-14", -1 === id(-1), true); + }); + Mocha.test("floats that are not NaN", () => { + Test_utils.eq("File \"compare_fold_test.res\", line 56, characters 7-14", Primitive_float.compare(id(1.5), id(2.5)) < 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 57, characters 7-14", Primitive_float.compare(id(2.5), id(2.5)) === 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 58, characters 7-14", Primitive_float.compare(id(-0.5), id(-1.5)) > 0, true); + }); +}); + +export { + id, +} +/* Not a pure module */ diff --git a/tests/tests/src/compare_fold_test.res b/tests/tests/src/compare_fold_test.res new file mode 100644 index 0000000000..e08cffd84b --- /dev/null +++ b/tests/tests/src/compare_fold_test.res @@ -0,0 +1,60 @@ +open Mocha +open Test_utils + +/*** +`compare(a, b) 0` is folded to `a b` only where JS operators agree +with `compare` (int, bool, string, bigint); structural comparison of records, +tuples, arrays, options and NaN floats keeps calling the runtime. +*/ + +@inline(never) let id = x => x + +type point = {x: int} + +describe(__MODULE__, () => { + test("records", () => { + let a = id({x: 1}) + let b = id({x: 1}) + let c = id({x: 2}) + eq(__LOC__, compare(a, b) == 0, true) + eq(__LOC__, compare(a, c) < 0, true) + eq(__LOC__, compare(c, a) > 0, true) + }) + + test("tuples, arrays and options", () => { + eq(__LOC__, compare(id((1, 2)), id((1, 2))) == 0, true) + eq(__LOC__, compare(id([10]), id([9])) < 0, false) + eq(__LOC__, compare(id(None), id(Some(1))) < 0, true) + }) + + test("NaN", () => { + let nan = id(Float.Constants.nan) + eq(__LOC__, compare(nan, id(1.0)) < 0, true) + eq(__LOC__, compare(nan, nan) == 0, true) + }) + + test("primitives that fold", () => { + eq(__LOC__, compare(id(1), id(2)) < 0, true) + eq(__LOC__, compare(id("b"), id("a")) > 0, true) + eq(__LOC__, compare(id(true), id(true)) == 0, true) + eq(__LOC__, compare(id(2n), id(1n)) >= 0, true) + eq(__LOC__, compare(id(1), id(1)) != 0, false) + eq(__LOC__, compare(id(1), id(2)) <= 0, true) + }) + + test("negative int constants", () => { + eq(__LOC__, compare(-1, -1) == 0, true) + eq(__LOC__, compare(-5, 3) > 0, false) + eq(__LOC__, compare(-5, 3) < 0, true) + eq(__LOC__, compare(3, -5) > 0, true) + let k = -1 + eq(__LOC__, compare(k, 0) <= 0, true) + eq(__LOC__, compare(-1, id(-1)) == 0, true) + }) + + test("floats that are not NaN", () => { + eq(__LOC__, compare(id(1.5), id(2.5)) < 0, true) + eq(__LOC__, compare(id(2.5), id(2.5)) == 0, true) + eq(__LOC__, compare(id(-0.5), id(-1.5)) > 0, true) + }) +}) diff --git a/tests/tests/src/test_list.mjs b/tests/tests/src/test_list.mjs index 60ea1ef3fe..8443800747 100644 --- a/tests/tests/src/test_list.mjs +++ b/tests/tests/src/test_list.mjs @@ -1,6 +1,7 @@ // Generated by ReScript, PLEASE EDIT WITH CARE import * as Stdlib_List from "@rescript/runtime/lib/es6/Stdlib_List.mjs"; +import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.mjs"; function length_aux(_len, _x) { while (true) { @@ -405,7 +406,7 @@ function mem(x, _x_) { if (x_ === 0) { return false; } - if (x_.hd === x) { + if (Primitive_object.compare(x_.hd, x) === 0) { return true; } _x_ = x_.tl; @@ -432,7 +433,7 @@ function assoc(x, _x_) { let x_ = _x_; if (x_ !== 0) { let match = x_.hd; - if (match[0] === x) { + if (Primitive_object.compare(match[0], x) === 0) { return match[1]; } _x_ = x_.tl; @@ -469,7 +470,7 @@ function mem_assoc(x, _x_) { if (x_ === 0) { return false; } - if (x_.hd[0] === x) { + if (Primitive_object.compare(x_.hd[0], x) === 0) { return true; } _x_ = x_.tl; @@ -497,7 +498,7 @@ function remove_assoc(x, x_) { } let l = x_.tl; let pair = x_.hd; - if (pair[0] === x) { + if (Primitive_object.compare(pair[0], x) === 0) { return l; } else { return {hd: pair, tl: remove_assoc(x, l)};