Skip to content

[12.x] Fold compare against 0 only where JS operators agree - #9401

Open
illusionalsagacity wants to merge 4 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-compare-fold-12.x
Open

illusionalsagacity wants to merge 4 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-compare-fold-12.x

Conversation

@illusionalsagacity

@illusionalsagacity illusionalsagacity commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Part of #9398 (item 1). Backport of the master fix to maintenance/12.x; the bug is present in 12.3.1.

Problem

Js_exp_make.int_comp rewrites a runtime compare call that is compared with 0 into a direct JS comparison: compare(a, b) == 0 becomes a === b, < 0 becomes a < b, and so on. The pattern matched any runtime function named compare without looking at its module, so the structural Primitive_object.compare and Primitive_float.compare were folded as well:

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

Before:

function equal(a, b) {
  return a === b;
}

function less(a, b) {
  return a < b;
}

function nanEqual(a, b) {
  return a === b;
}

equal({x: 1}, {x: 1}) returns false, less([10], [9]) returns true because JS compares the arrays as strings, and nanEqual(nan, nan) returns false although compare(nan, nan) is 0.

After:

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;
}

Fix

The fold now applies only when the call is the compare of Primitive_int, Primitive_bool, Primitive_string or Primitive_bigint. Each of those is a total order that agrees with the JS <, === and > operators, so their output is unchanged. Primitive_float.compare (NaN) and Primitive_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_comp folds a comparison of two int constants, reading the left one as unsigned and the right one as signed. When the compare fold recursed into it with a negative left constant, the result was wrong:

let a = compare(-1, -1) == 0 // folded to false
let b = compare(-5, 3) > 0 // folded to true

On 12.x the unsigned read is still needed: is_out compares (e >>> 0) > range through int_comp and relies on it when e is a constant (gpr_3877_test fails without it). So here the compare fold compares two int constants as signed itself and the general case is unchanged. On master, where is_out is gone, the unsigned read is removed instead.

Tests

tests/tests/src/compare_fold_test.res compares 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 polymorphic mem, assoc, mem_assoc and remove_assoc now call Primitive_object.compare(...) === 0 instead of ===. They were affected by the same bug.
  • caml_compare_bigint_test.mjs: that test types its values as float, so its compare is the float one and is no longer folded.

The fix was written by Claude and reviewed by me.

`compare(a, b) == 0`, `< 0` and `> 0` are folded to `a === b`, `a < b`
and `a > b` for any type, not only ints, so structural comparison of
records, tuples, arrays, options and NaN gives the wrong answer.

Cause: compiler/backend/js_exp_make.ml:1539 (int_comp matches any runtime
"compare")

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
int_comp rewrote any runtime call named "compare" compared with 0 into a
direct JS comparison (compare(a, b) == 0 -> a === b, < 0 -> a < b, ...).
The pattern ignored the runtime module, so Primitive_object.compare
(structural) and Primitive_float.compare (NaN ordering) were folded too,
giving wrong results for records, tuples, arrays, options and NaN.

Only fold the compare of Primitive_int, Primitive_bool, Primitive_string
and Primitive_bigint, whose results always agree with the JS operators.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
int_comp folds a comparison of two int constants at compile time. It
reads the left constant as unsigned and the right one as signed, so when
the fold of compare(a, b) <op> 0 recursed into it with a negative left
constant, that constant became a large positive number: compare(-1, -1)
== 0 folded to false and compare(-5, 3) > 0 to true. Plain comparisons
such as -1 == -1 are folded earlier and were not affected.

The unsigned read cannot simply be dropped here as on master: is_out
compares (e >>> 0) > range through int_comp and relies on it for a
constant e (gpr_3877_test). So the compare fold now compares two int
constants as signed itself, and the general case is left alone.

Also correct two comments: the one on compare_agrees_with_js_operators
now says where the folded and the runtime result can differ (values JS
cannot order), and the one in j.ml no longer says that the module of a
runtime function can be ignored when matching on its name.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant