Skip to content

Fold float addition of int literals only when the sum is exact - #9415

Merged
cknitt merged 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-float-add-fold
Nov 27, 2026
Merged

cknitt merged 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-float-add-fold

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Part of #9398 (item 8).

Problem

Float.fromInt of an int literal stays an int literal in the JS IR, and +. and -. of two such literals were folded with int32 arithmetic, so Float.fromInt(2147483647) +. Float.fromInt(1) became -2147483648. A chain x + k + j was also turned into x + (k + j), which isn't exact for floats. Separately, 0 - e was printed as -e even when e itself starts with -, giving --a (a decrement) or --5 (a syntax error). That includes an operand inside an identity Some and a raw literal such as %raw("-5").

let big = Float.fromInt(2147483647) +. Float.fromInt(1)
let chain = x => x +. Float.fromInt(1) +. Float.fromInt(1)
let wrap = Float.toInt(Float.fromInt(2147483647)) + 1
let min = x => x + -2147483648

Before:

let big = -2147483648;

function chain(x) {
  return x + 2;
}

let wrap = -2147483648;

function min(x) {
  return x - -2147483648 | 0;
}

After:

let big = 2147483647 + 1;

function chain(x) {
  return x + 1 + 1;
}

let wrap = -2147483648;

function min(x) {
  return x - -2147483648 | 0;
}
let neg = Float.fromInt(0) -. Float.fromInt(-2147483648)
let twice = a => Float.fromInt(0) -. (Float.fromInt(0) -. a)
let some = a => Float.fromInt(0) -. Option.getUnsafe(Some(Float.fromInt(0) -. a))
let raw = Float.fromInt(0) -. %raw("-5")

Before:

let neg = -2147483648;

function twice(a) {
  return --a;
}

function some(a) {
  return --a;
}

let raw = --5;

After:

let neg = - -2147483648;

function twice(a) {
  return - -a;
}

function some(a) {
  return - -a;
}

let raw = - -5;

Fix

  • Two literals fold only when the exact result fits an int32. Chains are left as written.
  • 0 - e prints as - e when e starts with -. This is in the same change because no longer folding 0 - min_int would otherwise print --2147483648.

Int arithmetic still wraps: int32_add and int32_minus fold int literals themselves, and an unfolded int sum is still wrapped in | 0. Int output is unchanged.

Not fixed here

Two more bugs in the same printer case are fixed on another branch, fix-unary-minus-printing: -(a ** b) prints as -a ** b, a syntax error, and 0. -. x gives -0 for 0.0. The two branches conflict in that case of js_dump.ml; I'll rebase the second one.

Tests

float_add_fold_test.res. No other checked-in output changes.

12.x

12.x has the same bug, and I have a backport ready on fix-float-add-fold-12.x. I'll open it against maintenance/12.x once this one is reviewed.

The fix was written by Claude. A second Claude agent reviewed the first version; this version addresses that review and has not been reviewed again.

Why:
    `Float.fromInt` of an int literal stays an int literal, and `+.` and
    `-.` on two such literals were folded with int32 arithmetic, so
    `Float.fromInt(2147483647) +. Float.fromInt(1)` became `-2147483648`.
    A float chain `x + k + j` was also reassociated to `x + (k + j)`,
    which is inexact in floating point. Unfolding `0 - min_int` then
    exposed that `0 - e` printed as `-e` even when `e` itself starts
    with `-`, giving `--a` (a decrement) or `--5` (a syntax error).
    Float literals now fold only when the exact result fits an int32,
    chains are left as written, and `0 - e` prints `- e` when `e`
    starts with `-`, also when `e` is the payload of an identity
    `Some` or a raw negative literal. Int addition and subtraction fold
    literals with wraparound themselves, so their output stays the same.

JS modified (float addition and subtraction of int literals):
Example:
    let big = Float.fromInt(2147483647) +. Float.fromInt(1)
    let chain = x => x +. Float.fromInt(1) +. Float.fromInt(1)
    let wrap = Float.toInt(Float.fromInt(2147483647)) + 1
    let min = x => x + -2147483648
Before (js):
    let big = -2147483648;

    function chain(x) {
      return x + 2;
    }

    let wrap = -2147483648;

    function min(x) {
      return x - -2147483648 | 0;
    }
After (js):
    let big = 2147483647 + 1;

    function chain(x) {
      return x + 1 + 1;
    }

    let wrap = -2147483648;

    function min(x) {
      return x - -2147483648 | 0;
    }

JS modified (subtraction from zero of an expression starting with a minus):
Example:
    let neg = Float.fromInt(0) -. Float.fromInt(-2147483648)
    let twice = a => Float.fromInt(0) -. (Float.fromInt(0) -. a)
    let some = a => Float.fromInt(0) -. Option.getUnsafe(Some(Float.fromInt(0) -. a))
    let raw = Float.fromInt(0) -. %raw("-5")
Before (js):
    let neg = -2147483648;

    function twice(a) {
      return --a;
    }

    function some(a) {
      return --a;
    }

    let raw = --5;
After (js):
    let neg = - -2147483648;

    function twice(a) {
      return - -a;
    }

    function some(a) {
      return - -a;
    }

    let raw = - -5;

Review: Does int arithmetic still wrap? Yes: `+` and `-` on int
    literals are folded with wraparound, also when a literal only becomes
    visible in the JS layer, and an unfolded int sum is still wrapped in
    `| 0`.
Review: Which checked-in outputs change? Only float_add_fold_test.mjs.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Float addition and subtraction of constants that came from int literals, such as Float.fromInt(2147483647) +. Float.fromInt(1), are no longer folded with int wraparound, and chains such as x +. Float.fromInt(1) +. Float.fromInt(2) are no longer reassociated.
  • Subtracting an expression that starts with a minus sign from zero no longer prints --, which was a decrement or a syntax error.

Semantic Diff

1 commit · Full page

Fold float addition of int literals only when the sum is exact

a4f6450 · illusionalsagacity · Nov 23, 2026, 20:27 UTC

Why

Float.fromInt of an int literal stays an int literal, and +. and -. on two such literals were folded with int32 arithmetic, so Float.fromInt(2147483647) +. Float.fromInt(1) became -2147483648. A float chain x + k + j was also reassociated to x + (k + j), which is inexact in floating point. Unfolding 0 - min_int then exposed that 0 - e printed as -e even when e itself starts with -, giving --a (a decrement) or --5 (a syntax error). Float literals now fold only when the exact result fits an int32, chains are left as written, and 0 - e prints - e when e starts with -, also when e is the payload of an identity Some or a raw negative literal. Int addition and subtraction fold literals with wraparound themselves, so their output stays the same.

JS modified (float addition and subtraction of int literals)

let big = Float.fromInt(2147483647) +. Float.fromInt(1)
let chain = x => x +. Float.fromInt(1) +. Float.fromInt(1)
let wrap = Float.toInt(Float.fromInt(2147483647)) + 1
let min = x => x + -2147483648

js

-let big = -2147483648;
+let big = 2147483647 + 1;
 
 function chain(x) {
-  return x + 2;
+  return x + 1 + 1;
 }
 
 let wrap = -2147483648;
 
 function min(x) {
   return x - -2147483648 | 0;
 }

JS modified (subtraction from zero of an expression starting with a minus)

let neg = Float.fromInt(0) -. Float.fromInt(-2147483648)
let twice = a => Float.fromInt(0) -. (Float.fromInt(0) -. a)
let some = a => Float.fromInt(0) -. Option.getUnsafe(Some(Float.fromInt(0) -. a))
let raw = Float.fromInt(0) -. %raw("-5")

js

-let neg = -2147483648;
+let neg = - -2147483648;
 
 function twice(a) {
-  return --a;
+  return - -a;
 }
 
 function some(a) {
-  return --a;
+  return - -a;
 }
 
-let raw = --5;
+let raw = - -5;

Review

  • Does int arithmetic still wrap? Yes: + and - on int literals are folded with wraparound, also when a literal only becomes visible in the JS layer, and an unfolded int sum is still wrapped in | 0.
  • Which checked-in outputs change? Only float_add_fold_test.mjs.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: a4f6450

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 68.57143% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.22%. Comparing base (45dba2c) to head (a4f6450).
⚠️ Report is 11 commits behind head on master.

Files with missing lines Patch % Lines
compiler/backend/js_dump.ml 38.88% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9415      +/-   ##
==========================================
- Coverage   82.22%   82.22%   -0.01%     
==========================================
  Files         461      462       +1     
  Lines       65573    65606      +33     
==========================================
+ Hits        53916    53943      +27     
- Misses      11657    11663       +6     
Files with missing lines Coverage Δ
compiler/backend/js_exp_make.ml 67.48% <100.00%> (+1.01%) ⬆️
compiler/backend/js_dump.ml 92.96% <38.88%> (-1.12%) ⬇️

... and 4 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.

@cknitt

cknitt commented Nov 26, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T20:23:39.065903Z a4f6450 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: a4f6450057

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I checked overflow at both ends of the int32 range, the min_int guard, and the -- cases (raw JS, bigint and hex float operands). Existing output is unchanged. #9422 should rebase onto this one (see my comment there).

This review was done by Claude.

@cknitt

cknitt commented Nov 27, 2026

Copy link
Copy Markdown
Member

@illusionalsagacity Good to merge? As it is still marked as draft.

@cknitt
cknitt marked this pull request as ready for review November 27, 2026 19:49
@cknitt
cknitt merged commit 1396ee9 into rescript-lang:master Nov 27, 2026
23 checks passed
illusionalsagacity added a commit to illusionalsagacity/rescript-compiler that referenced this pull request Nov 27, 2026
Bring the backport in line with the version of rescript-lang#9415 merged on master
(1396ee9). Its compiler changes and tests are otherwise the same here.

The master version of prints_with_leading_minus also handles
Json_literal, which J has no case for on 12.x. This version keeps a case
master doesn't have: on 12.x the block of an untagged constructor
reaches js_dump as a Caml_block and is printed as its payload, so
`Float.fromInt(0) -. (Obj.magic(Float(-5.0)): float)` printed `--5.0`
without it. On master the block is gone before js_dump. The "negating a
negative operand printed without its wrapper" test covers it.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@illusionalsagacity
illusionalsagacity deleted the fix-float-add-fold branch November 27, 2026 20:22
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.

2 participants