Skip to content

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

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

illusionalsagacity wants to merge 7 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 #9400 (0a686a9a9b) 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.

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 (constant variants use the int compare, so they still fold as well). 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 at compile time, 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, bigint and constant variants (plain, @as(int), @as(null)) 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>
@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9401

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9401

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@9401

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9401

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9401

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9401

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9401

commit: 61977ec

compare_agrees_with_js_operators spelled out its four modules as a
chain of || comparisons and took the whole J.module_id only 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, as on master. The output does not
change.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
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. It left out the values that do differ: 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, as on master. The output does not change.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Bring the 12.x backport in line with the version of rescript-lang#9400 merged on master (0a686a9): add the constant-variant cases (plain, @as(int), @as(null)) to compare_fold_test and link the CHANGELOG entry to rescript-lang#9400.

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