diff --git a/CHANGELOG.md b/CHANGELOG.md index bb6a20bbe2..39faffae31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Integer patterns that alias the same int32 value (e.g. `0xFFFF_FFFF` and `-1`) may now produce unused-case warnings. https://github.com/rescript-lang/rescript/pull/8717 https://github.com/rescript-lang/rescript/pull/8858 - Fix constant folding of a `switch` picking the wrong case for `@as(int)` constructors and for coerced variants. https://github.com/rescript-lang/rescript/pull/9318 - Fix constant folding of comparisons between `@as(int)` constructors and other constructors, and merging of switch arms returning them. https://github.com/rescript-lang/rescript/pull/9319 +- Fix `compare(a, b)` compared with `0` being folded into a JS comparison for records, tuples, arrays, options and floats, and folding to the wrong result for negative int constants. https://github.com/rescript-lang/rescript/pull/9400 #### :nail_care: Polish diff --git a/compiler/core/j.ml b/compiler/core/j.ml index dc5aa2514d..5fd05c06a7 100644 --- a/compiler/core/j.ml +++ b/compiler/core/j.ml @@ -61,13 +61,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/core/js_exp_make.ml b/compiler/core/js_exp_make.ml index 1babd208a3..7b2c25ac48 100644 --- a/compiler/core/js_exp_make.ml +++ b/compiler/core/js_exp_make.ml @@ -1376,22 +1376,41 @@ let compare_int_aux (cmp : Lam_compat.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). 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] + let rec int_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) = match (cmp, e0.expression_desc, e1.expression_desc) with | _, Number (Int {i = l}), Number (Int {i = r}) -> + (* [is_out] relies on the left constant being read as unsigned *) let l = Ext_int.int32_unsigned_to_int l in let r = Int32.to_int r in bool (compare_int_aux cmp l r) | ( _, Call ( { - expression_desc = Var (Qualified ({kind = Runtime}, Some "compare")); - _; + expression_desc = + Var (Qualified ({kind = Runtime; id}, Some "compare")); }, [l; r], _ ), - Number (Int {i = 0l}) ) -> - int_comp cmp l r (* = 0 > 0 < 0 *) + Number (Int {i = 0l}) ) + when compare_agrees_with_js_operators id -> ( + (* = 0 > 0 < 0 *) + match (l.expression_desc, r.expression_desc) with + | Number (Int {i = l}), Number (Int {i = r}) -> + (* both are signed here, unlike in the case above *) + bool (compare_int_aux cmp (Int32.to_int l) (Int32.to_int r)) + | _ -> int_comp cmp l r) | (Ceq, Optional_block _, Undefined _ | Ceq, Undefined _, Optional_block _) when no_side_effect e0 && no_side_effect e1 -> false_ diff --git a/tests/tests/src/caml_compare_bigint_test.mjs b/tests/tests/src/caml_compare_bigint_test.mjs index 4c00d5c600..d47ac2d84d 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..1542e8b6d7 --- /dev/null +++ b/tests/tests/src/compare_fold_test.mjs @@ -0,0 +1,78 @@ +// 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.js"; +import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js"; + +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 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 29, characters 7-14", Primitive_object.compare(id([ + 1, + 2 + ]), id([ + 1, + 2 + ])) === 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 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 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 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 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); + }); +}); + +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..b0399182bc --- /dev/null +++ b/tests/tests/src/compare_fold_test.res @@ -0,0 +1,74 @@ +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, constant variants); structural comparison of records, +tuples, arrays, options and NaN floats keeps calling the runtime. +*/ + +@inline(never) let id = x => x + +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}) + 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("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) + 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 f83f1e404c..eb974bbe8f 100644 --- a/tests/tests/src/test_list.mjs +++ b/tests/tests/src/test_list.mjs @@ -2,6 +2,7 @@ import * as Belt_List from "@rescript/runtime/lib/es6/Belt_List.js"; import * as Pervasives from "@rescript/runtime/lib/es6/Pervasives.js"; +import * as Primitive_object from "@rescript/runtime/lib/es6/Primitive_object.js"; function length_aux(_len, _x) { while (true) { @@ -361,7 +362,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; @@ -388,7 +389,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; @@ -425,7 +426,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; @@ -453,7 +454,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 {