Skip to content

Fold compare against 0 only where JS operators agree - #9400

Open
illusionalsagacity wants to merge 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-compare-fold
Open

illusionalsagacity wants to merge 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-compare-fold

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Part of #9398 (item 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

Both constants are now read as signed. Ext_int.int32_unsigned_to_int had no other caller and is removed. No other test output changes.

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.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • 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.

Semantic Diff

1 commit · Full page

✨ Fold compare against 0 only where the JS operators agree

5e95fc5 · illusionalsagacity · Nov 23, 2026, 20:29 UTC · new

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)

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
let intLess = (a: int, b: int) => compare(a, b) < 0

js

 function equal(a, b) {
-  return a === b;
+  return Primitive_object.compare(a, b) === 0;
 }
 
 function less(a, b) {
-  return a < b;
+  return Primitive_object.compare(a, b) < 0;
 }
 
 function nanEqual(a, b) {
-  return a === b;
+  return Primitive_float.compare(a, b) === 0;
 }
 
 function intLess(a, b) {
   return a < b;
 }

JS modified (compare of int constants)

let a = compare(-1, -1) == 0
let b = compare(-5, 3) > 0

js

-let a = false;
+let a = true;
 
-let b = 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.
  • 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.
  • 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.
  • 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.

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.22%. Comparing base (45dba2c) to head (5e95fc5).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9400   +/-   ##
=======================================
  Coverage   82.22%   82.22%           
=======================================
  Files         461      461           
  Lines       65573    65577    +4     
=======================================
+ Hits        53916    53922    +6     
+ Misses      11657    11655    -2     
Files with missing lines Coverage Δ
compiler/backend/ext_int.ml 87.50% <ø> (+7.50%) ⬆️
compiler/backend/j.ml 100.00% <ø> (ø)
compiler/backend/js_exp_make.ml 67.05% <100.00%> (+0.58%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9400

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 5e95fc5

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<int>, b: array<int>) => 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 <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