Repository navigation
Fold float addition of int literals only when the sum is exact - #9415
Conversation
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>
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Fold float addition of int literals only when the sum is exacta4f6450 · illusionalsagacity · Nov 23, 2026, 20:27 UTC
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
-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")
-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
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
left a comment
There was a problem hiding this comment.
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.
|
@illusionalsagacity Good to merge? As it is still marked as draft. |
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>
Part of #9398 (item 8).
Problem
Float.fromIntof an int literal stays an int literal in the JS IR, and+.and-.of two such literals were folded with int32 arithmetic, soFloat.fromInt(2147483647) +. Float.fromInt(1)became-2147483648. A chainx + k + jwas also turned intox + (k + j), which isn't exact for floats. Separately,0 - ewas printed as-eeven wheneitself starts with-, giving--a(a decrement) or--5(a syntax error). That includes an operand inside an identitySomeand a raw literal such as%raw("-5").Before:
After:
Before:
After:
Fix
0 - eprints as- ewhenestarts with-. This is in the same change because no longer folding0 - min_intwould otherwise print--2147483648.Int arithmetic still wraps:
int32_addandint32_minusfold 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, and0. -. xgives-0for0.0. The two branches conflict in that case ofjs_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 againstmaintenance/12.xonce 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.