From 8c0f94b342dd89ba41220691469ea1ec5b7bc709 Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 23 Nov 2026 15:29:35 -0500 Subject: [PATCH 1/3] 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 +++++-- .../src/caml_compare_bigint_test.mjs | 4 +- .../compiler_tests/src/compare_fold_test.mjs | 69 +++++++++++++++++++ .../compiler_tests/src/compare_fold_test.res | 60 ++++++++++++++++ tests/compiler_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/compiler_tests/src/compare_fold_test.mjs create mode 100644 tests/compiler_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 9b5ba8577b..2a40039191 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 a7ba9b579e..0b6b2d222f 100644 --- a/compiler/backend/js_exp_make.ml +++ b/compiler/backend/js_exp_make.ml @@ -1534,21 +1534,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/compiler_tests/src/caml_compare_bigint_test.mjs b/tests/compiler_tests/src/caml_compare_bigint_test.mjs index 87b8da60d4..555a47d31f 100644 --- a/tests/compiler_tests/src/caml_compare_bigint_test.mjs +++ b/tests/compiler_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/compiler_tests/src/compare_fold_test.mjs b/tests/compiler_tests/src/compare_fold_test.mjs new file mode 100644 index 0000000000..fa8029adcb --- /dev/null +++ b/tests/compiler_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/compiler_tests/src/compare_fold_test.res b/tests/compiler_tests/src/compare_fold_test.res new file mode 100644 index 0000000000..e08cffd84b --- /dev/null +++ b/tests/compiler_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/compiler_tests/src/test_list.mjs b/tests/compiler_tests/src/test_list.mjs index 7938ee8438..6ce17e3929 100644 --- a/tests/compiler_tests/src/test_list.mjs +++ b/tests/compiler_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)}; From a497bde41e2388618d4764424db926581eab16e6 Mon Sep 17 00:00:00 2001 From: Rob Date: Sat, 28 Nov 2026 15:01:14 -0500 Subject: [PATCH 2/3] Simplify the compare fold's module check Why: `compare_agrees_with_js_operators` spelled out its four modules as a chain of `||` comparisons and took the whole `J.module_id` to read its `id`. It now takes the module's ident, bound in the pattern, and checks it against a list of the four names. The output does not change. Signed-off-by: Rob --- compiler/backend/js_exp_make.ml | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/backend/js_exp_make.ml b/compiler/backend/js_exp_make.ml index 0b6b2d222f..9840729097 100644 --- a/compiler/backend/js_exp_make.ml +++ b/compiler/backend/js_exp_make.ml @@ -1539,12 +1539,9 @@ let compare_int_aux (cmp : Lambda.comparison) (l : int) r = [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 compare_agrees_with_js_operators (module_ident : Ident.t) = + List.mem (Ident.name module_ident) + Primitive_modules.[int; bool; string; bigint] let rec int_comp (cmp : Lambda.comparison) (e0 : t) (e1 : t) = match (cmp, e0.expression_desc, e1.expression_desc) with @@ -1554,12 +1551,12 @@ let rec int_comp (cmp : Lambda.comparison) (e0 : t) (e1 : t) = Call ( { expression_desc = - Var (Qualified (({kind = Runtime} as module_id), Some "compare")); + Var (Qualified ({kind = Runtime; id}, Some "compare")); }, [l; r], _ ), Number (Int {i = 0l}) ) - when compare_agrees_with_js_operators module_id -> + when compare_agrees_with_js_operators 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 -> From dac41aa6e4d9a463fca1bb6adb5a49b460b26c0a Mon Sep 17 00:00:00 2001 From: Rob Date: Sat, 28 Nov 2026 16:15:15 -0500 Subject: [PATCH 3/3] Name the values a folded compare of constant variants gets wrong Why: The comment on `compare_agrees_with_js_operators` named `null` constructors among the values whose folded compare differs from the runtime's, but JS orders `null` as `0`, so a `null` tag against the other numbers folds correctly. The values that do differ are an `@as(undefined)` constructor, a NaN typed as an int, and `@as` tags of different JS types, such as a string against a number or `null` against `0`; the comment now names those. The test now checks that constant variants with plain, `@as(int)` and `@as(null)` tags still fold and agree with the runtime, and the changelog entry lists constant variants among the types whose compare folds. Review: Does this fix the variants that fold wrongly? No: they were folded the same way before this pull request, through the int `compare` the type checker picks for constant variants. Fixing them belongs in the choice of that `compare`, not in the fold. Review: Which checked-in outputs change? Only compare_fold_test's, for the new cases. Signed-off-by: Rob --- changes/compare-fold-only-primitives.fix.md | 2 +- compiler/backend/js_exp_make.ml | 9 ++- .../compiler_tests/src/compare_fold_test.mjs | 55 +++++++++++-------- .../compiler_tests/src/compare_fold_test.res | 16 +++++- 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/changes/compare-fold-only-primitives.fix.md b/changes/compare-fold-only-primitives.fix.md index 7f1c402b56..bc5d56ebc9 100644 --- a/changes/compare-fold-only-primitives.fix.md +++ b/changes/compare-fold-only-primitives.fix.md @@ -1,2 +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. +- Fold `compare(a, b)` against `0` into a JS comparison only for ints, bools, strings, bigints and constant variants, 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/js_exp_make.ml b/compiler/backend/js_exp_make.ml index 9840729097..5e734c9d54 100644 --- a/compiler/backend/js_exp_make.ml +++ b/compiler/backend/js_exp_make.ml @@ -1536,9 +1536,12 @@ let compare_int_aux (cmp : Lambda.comparison) (l : int) 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. *) + [Primitive_float] (NaN) nor [Primitive_object] (structural). Constant + variants use the int [compare] too, and fold wrongly where a value is + neither [<] nor [===] the other, for which the runtime [compare] returns 1: + an [@as(undefined)] constructor, a NaN typed as an int, or [@as] tags of + different JS types, such as a string against a number or [null] against + [0]. [null] against other numbers is safe, as JS orders it as [0]. *) let compare_agrees_with_js_operators (module_ident : Ident.t) = List.mem (Ident.name module_ident) Primitive_modules.[int; bool; string; bigint] diff --git a/tests/compiler_tests/src/compare_fold_test.mjs b/tests/compiler_tests/src/compare_fold_test.mjs index fa8029adcb..f0e4b1264e 100644 --- a/tests/compiler_tests/src/compare_fold_test.mjs +++ b/tests/compiler_tests/src/compare_fold_test.mjs @@ -20,46 +20,55 @@ Mocha.describe("Compare_fold_test", () => { 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); + Test_utils.eq("File \"compare_fold_test.res\", line 23, characters 7-14", Primitive_object.compare(a, b) === 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 24, characters 7-14", Primitive_object.compare(a, c) < 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 25, 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([ + Test_utils.eq("File \"compare_fold_test.res\", line 29, 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); + Test_utils.eq("File \"compare_fold_test.res\", line 30, characters 7-14", Primitive_object.compare(id([10]), id([9])) < 0, false); + Test_utils.eq("File \"compare_fold_test.res\", line 31, 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); + Test_utils.eq("File \"compare_fold_test.res\", line 36, characters 7-14", Primitive_float.compare(nan, id(1.0)) < 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 37, 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); + Test_utils.eq("File \"compare_fold_test.res\", line 41, characters 7-14", id(1) < id(2), true); + Test_utils.eq("File \"compare_fold_test.res\", line 42, characters 7-14", id("b") > id("a"), true); + Test_utils.eq("File \"compare_fold_test.res\", line 43, characters 7-14", id(true) === id(true), true); + Test_utils.eq("File \"compare_fold_test.res\", line 44, characters 7-14", id(2n) >= id(1n), true); + Test_utils.eq("File \"compare_fold_test.res\", line 45, characters 7-14", id(1) !== id(1), false); + Test_utils.eq("File \"compare_fold_test.res\", line 46, characters 7-14", id(1) <= id(2), true); + }); + Mocha.test("constant variants, which fold", () => { + Test_utils.eq("File \"compare_fold_test.res\", line 50, characters 7-14", id("A") < id("C"), true); + Test_utils.eq("File \"compare_fold_test.res\", line 51, characters 7-14", id("B") === id("B"), true); + Test_utils.eq("File \"compare_fold_test.res\", line 52, characters 7-14", id(2) > id(1), true); + Test_utils.eq("File \"compare_fold_test.res\", line 53, characters 7-14", id(1) >= id(2), false); + Test_utils.eq("File \"compare_fold_test.res\", line 54, characters 7-14", id(null) < id(1), true); + Test_utils.eq("File \"compare_fold_test.res\", line 55, characters 7-14", id(2) > id(null), true); + Test_utils.eq("File \"compare_fold_test.res\", line 56, characters 7-14", id(null) === id(null), 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); + Test_utils.eq("File \"compare_fold_test.res\", line 60, characters 7-14", true, true); + Test_utils.eq("File \"compare_fold_test.res\", line 61, characters 7-14", false, false); + Test_utils.eq("File \"compare_fold_test.res\", line 62, characters 7-14", true, true); + Test_utils.eq("File \"compare_fold_test.res\", line 63, characters 7-14", true, true); + Test_utils.eq("File \"compare_fold_test.res\", line 65, characters 7-14", true, true); + Test_utils.eq("File \"compare_fold_test.res\", line 66, 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); + Test_utils.eq("File \"compare_fold_test.res\", line 70, characters 7-14", Primitive_float.compare(id(1.5), id(2.5)) < 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 71, characters 7-14", Primitive_float.compare(id(2.5), id(2.5)) === 0, true); + Test_utils.eq("File \"compare_fold_test.res\", line 72, characters 7-14", Primitive_float.compare(id(-0.5), id(-1.5)) > 0, true); }); }); diff --git a/tests/compiler_tests/src/compare_fold_test.res b/tests/compiler_tests/src/compare_fold_test.res index e08cffd84b..b0399182bc 100644 --- a/tests/compiler_tests/src/compare_fold_test.res +++ b/tests/compiler_tests/src/compare_fold_test.res @@ -3,7 +3,7 @@ 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, +with `compare` (int, bool, string, bigint, constant variants); structural comparison of records, tuples, arrays, options and NaN floats keeps calling the runtime. */ @@ -11,6 +11,10 @@ tuples, arrays, options and NaN floats keeps calling the runtime. type point = {x: int} +type letter = A | B | C +type rank = | @as(1) Low | @as(2) High +type level = | @as(null) Off | @as(1) Dim | @as(2) Bright + describe(__MODULE__, () => { test("records", () => { let a = id({x: 1}) @@ -42,6 +46,16 @@ describe(__MODULE__, () => { eq(__LOC__, compare(id(1), id(2)) <= 0, true) }) + test("constant variants, which fold", () => { + eq(__LOC__, compare(id(A), id(C)) < 0, true) + eq(__LOC__, compare(id(B), id(B)) == 0, true) + eq(__LOC__, compare(id(High), id(Low)) > 0, true) + eq(__LOC__, compare(id(Low), id(High)) >= 0, false) + eq(__LOC__, compare(id(Off), id(Dim)) < 0, true) + eq(__LOC__, compare(id(Bright), id(Off)) > 0, true) + eq(__LOC__, compare(id(Off), id(Off)) == 0, true) + }) + test("negative int constants", () => { eq(__LOC__, compare(-1, -1) == 0, true) eq(__LOC__, compare(-5, 3) > 0, false)