Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/compare-fold-only-primitives.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- 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`.
7 changes: 0 additions & 7 deletions compiler/backend/ext_int.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions compiler/backend/ext_int.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 4 additions & 7 deletions compiler/backend/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions compiler/backend/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <op> 0] is [a <op> 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 : 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; id}, Some "compare"));
},
[l; r],
_ ),
Number (Int {i = 0l}) ) ->
Number (Int {i = 0l}) )
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 ->
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler_tests/src/caml_compare_bigint_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
78 changes: 78 additions & 0 deletions tests/compiler_tests/src/compare_fold_test.mjs
Original file line number Diff line number Diff line change
@@ -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.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 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 */
74 changes: 74 additions & 0 deletions tests/compiler_tests/src/compare_fold_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
open Mocha
open Test_utils

/***
`compare(a, b) <op> 0` is folded to `a <op> 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)
})
})
9 changes: 5 additions & 4 deletions tests/compiler_tests/src/test_list.mjs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)};
Expand Down
Loading