Repository navigation
Negate floats as -0 - x and parenthesize ** under prefix operators - #9422
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9422 +/- ##
==========================================
- Coverage 82.25% 82.25% -0.01%
==========================================
Files 462 462
Lines 65650 65672 +22
==========================================
+ Hits 53999 54016 +17
- Misses 11651 11656 +5
🚀 New features to boost your workflow:
|
Changelog🐛 Bug fix
Semantic Diff2 commits · Full page Negate floats as -0 - x and parenthesize ** under prefix operatorsc889611 · illusionalsagacity · Nov 25, 2026, 19:30 UTC
JS modified (subtraction from zero) let fromLiteral = x => 0. -. x
let fromInt = x => Float.fromInt(0) -. x
let negate = x => -.x
let negateInt = (x: int) => -x
function fromLiteral(x) {
- return - x;
+ return 0 - x;
}
function fromInt(x) {
- return -x;
+ return 0 - x;
}
function negate(x) {
return - x;
}
function negateInt(x) {
return -x | 0;
}JS modified (subtraction from a negative zero literal) let f = x => -0. -. x
-return -0 - x;
+return - x;JS modified (power under a prefix operator) let negate = (a: float, b: float) => -.(a ** b)
let bitNot = (a: bigint, b: bigint) => ~~~(a ** b)
let typeOf = (a: float, b: float) => typeof(a ** b)
let wait = async (a: float, b: float) => await Obj.magic(a ** b)
function negate(a, b) {
- return - a ** b;
+ return - (a ** b);
}
function bitNot(a, b) {
- return ~a ** b;
+ return ~(a ** b);
}
function typeOf(a, b) {
- return typeof a ** b;
+ return typeof (a ** b);
}
async function wait(a, b) {
- return await a ** b;
+ return await (a ** b);
}Review
Test int negation of operands printed with a leading minusbb6811d · illusionalsagacity · Nov 27, 2026, 16:03 UTC
Review
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
@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. 🎉 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". |
|
The float fixes and the One case of let f = () => -(%raw("-5"): int) // return --5 | 0; SyntaxError
let g = () => -(Obj.magic(-5n): int) // return --5n | 0; SyntaxError
#9415 adds After that rebase, #9415's Small things: This review was done by Claude. |
53b0747 to
458fb9c
Compare
|
Thanks. I rebased this onto #9415, so its commit shows up here until #9415 merges. This should merge after #9415.
Investigated by Claude. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! 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". |
Why:
The JS IR represents a negation as a subtraction from zero, and the
printer printed every `0 - x` as `-x`. That is wrong for a float
subtraction: `0. -. x` is `0` when `x` is `0.`, but `-x` is `-0`.
A float negation is now a subtraction from `-0.`, which equals `-x`
for every `x`, and only that is printed as `- x`. An int negation
stays `0 - x`; it is printed as `-x` only as the operand of a bitwise
operator, such as the `| 0` of every int subtraction, which drops the
sign of a zero.
Prefix operators also printed their operand at the level of `**`, so a
power had no parentheses there, which JavaScript rejects. This concerned
a float negation, `~` on a bigint, `typeof` and `await`; the operands
of all prefix operators now go through `unary_operand`.
Where `0 - x` is printed as `-x`, an `x` that prints with a leading
minus is kept apart by a space, as found by `prints_with_leading_minus`;
of the subtractions, only `-0 - x` prints with a leading minus now.
JS modified (subtraction from zero):
Example:
let fromLiteral = x => 0. -. x
let fromInt = x => Float.fromInt(0) -. x
let negate = x => -.x
let negateInt = (x: int) => -x
Before (js):
function fromLiteral(x) {
return - x;
}
function fromInt(x) {
return -x;
}
function negate(x) {
return - x;
}
function negateInt(x) {
return -x | 0;
}
After (js):
function fromLiteral(x) {
return 0 - x;
}
function fromInt(x) {
return 0 - x;
}
function negate(x) {
return - x;
}
function negateInt(x) {
return -x | 0;
}
JS modified (subtraction from a negative zero literal):
Example:
let f = x => -0. -. x
Before (js): return -0 - x;
After (js): return - x;
JS modified (power under a prefix operator):
Example:
let negate = (a: float, b: float) => -.(a ** b)
let bitNot = (a: bigint, b: bigint) => ~~~(a ** b)
let typeOf = (a: float, b: float) => typeof(a ** b)
let wait = async (a: float, b: float) => await Obj.magic(a ** b)
Before (js): a syntax error
function negate(a, b) {
return - a ** b;
}
function bitNot(a, b) {
return ~a ** b;
}
function typeOf(a, b) {
return typeof a ** b;
}
async function wait(a, b) {
return await a ** b;
}
After (js):
function negate(a, b) {
return - (a ** b);
}
function bitNot(a, b) {
return ~(a ** b);
}
function typeOf(a, b) {
return typeof (a ** b);
}
async function wait(a, b) {
return await (a ** b);
}
Review: Why decide the int case by the enclosing operator? Inlining can put
an int literal into a float subtraction, so a left operand `0` does not
tell an int from a float, but the sign of a zero never survives a bitwise
operator.
Review: Which checked-in outputs change? Only float_add_fold_test.mjs: a
subtraction from a zero int such as `- -5` now prints as `0 - -5`. Its
case for a negative bigint, which this would turn into `0 - -5n` and a
"Cannot mix BigInt" error, negates the bigint with `-.` instead.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Why:
Under a bitwise operator, `0 - x` is printed as `-x`, and an `x`
that prints with a leading minus is kept apart by a space. The tests
only covered int and float literals, so they now also negate raw
JavaScript, a raw literal in an identity `Some`, a negative zero, a
float negation and a negative bigint, whose `- -5n | 0` parses and
then throws a TypeError. `is_pow` lists every expression kind
instead of ending in a wildcard, so a new kind has to be decided
there, and a test case that appeared twice is gone.
Review: Does any output change? No compiler output does; only the
tests of unary_minus_test.res are added or moved.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
458fb9c to
bb6811d
Compare
Why:
unary_minus_test, added on master by rescript-lang#9422, has a self tail call.
With this change its parameters are assigned in argument order, so
the checked-in output changes.
JS modified:
countdown(_start, _x): `_start = 0;` is now assigned before
`_x = x - 1;`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gLsfh9mi4MGnZuqcRb
Signed-off-by: Christoph Knittel <ck@cca.io>
* Evaluate self tail call arguments left to right
Why:
A self tail call is compiled to a loop that reassigns the parameters,
and the assignments were emitted in reverse parameter order, so the
arguments were evaluated right to left, unlike any other call. When a
later argument mutated state an earlier one read, the earlier one saw
the mutated value and the function returned a wrong result. The
assignments are now emitted in argument order.
A self tail call whose argument count differs from the parameter
count, possible through a cast such as `Obj.magic`, made the compiler
crash; it is now compiled as an ordinary call. A self tail call of a
`()` function, which has no JS parameters, dropped its argument together
with the argument's side effects, so a loop could run forever; the side
effects are now kept. Such a function still loops whatever a cast passes
to it.
JS modified (self tail call loops):
Example:
let rec f = (a, b, n) =>
n == 0 ? a + b : f(Console.log("a") == () ? a : 0, Console.log("b") == () ? b : 0, n - 1)
Before (js):
_n = n - 1 | 0;
_b = (console.log("b"), undefined) === undefined ? b : 0;
_a = (console.log("a"), undefined) === undefined ? a : 0;
continue;
After (js):
_a = (console.log("a"), undefined) === undefined ? a : 0;
_b = (console.log("b"), undefined) === undefined ? b : 0;
_n = n - 1 | 0;
continue;
Diagnostic modified (self tail call with a different argument count):
Example:
let rec f = (a, b) => a == 0 ? b : (Obj.magic(f): int => int)(a - 1)
Before (diagnostic):
Fatal error: exception Invalid_argument("List.fold_left2")
After (js):
function f(a, b) {
if (a === 0) {
return b;
} else {
return f(a - 1 | 0);
}
}
JS modified (self tail call of a `()` function):
Example:
let left = ref(3)
@inline(never) let tick = () => left := left.contents - 1
let rec drain = () => if left.contents > 0 { drain(tick()) } else { 0 }
Before (js):
function drain() {
while (true) {
if (left.contents <= 0) {
return 0;
}
continue;
};
}
After (js):
function drain() {
while (true) {
if (left.contents <= 0) {
return 0;
}
tick();
continue;
};
}
Review: Can reordering the assignments change which values the arguments
read? No: each argument reads the loop's local copies of the
parameters, never the reassigned ones, so only the order of side
effects changes; arguments that compile to statements are held in
temporaries before any assignment.
Review: Does Belt rely on the old order? The `reduce` of `HashMap`,
`HashSet` and `MutableSet` read the rest of the container after its
callback only because of it. They now bind the callback's result first,
so they still skip entries the callback removes, as `forEach` does. The
other reordered loops of the runtime and Belt only read immutable data
or values no callback can change.
Review: Which checked-in outputs change? Stdlib_List, Primitive_list, the
rewatch test repo's Array.mjs and several tests, each only in the
order of those assignments.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
* Update unary_minus_test output for left-to-right tail call arguments
Why:
unary_minus_test, added on master by #9422, has a self tail call.
With this change its parameters are assigned in argument order, so
the checked-in output changes.
JS modified:
countdown(_start, _x): `_start = 0;` is now assigned before
`_x = x - 1;`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gLsfh9mi4MGnZuqcRb
Signed-off-by: Christoph Knittel <ck@cca.io>
---------
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Part of #9398 (item 38). Found while reviewing the fixes for it.
Problem
The JS IR represents a negation as a subtraction from zero, and the printer printed every
0 - xas-x. For floats that is wrong:0. -. xis0whenxis0., but-xis-0.Before:
After:
A subtraction from a negative zero literal was printed in full:
Before:
return -0 - x;. After:return - x;.Prefix operators (float negation,
~on a bigint,typeof,await) also printed a power without parentheses, which is a syntax error:Before:
After:
Where an int negation is printed as
-x, an operand that itself prints with a leading minus is kept apart by a space, so-(%raw("-5"): int)prints- -5 | 0and not--5 | 0(a syntax error). This usesprints_with_leading_minusfrom #9415, which also knows negative float and bigint literals, raw JavaScript and JSON literals. Of the subtractions, only-0 - xnow prints with a leading minus, so that is the one subtraction it matches.Fix
-0., which equals-xfor everyx, and only that prints as- x.0 - x. It prints as-xonly as the operand of a bitwise operator, such as the| 0of every int subtraction, which drops the sign of a zero.unary_operand.Tests
unary_minus_test.rescovers negation, powers under every prefix operator, negated operands on both sides of every bitwise operator, and int negation of raw JavaScript, a negative zero, a float negation and a negative bigint. The bigint case prints- -5n | 0, which parses and then throws a TypeError for mixing a bigint and an int; the test checks for that TypeError. Three%identityexternals are needed to reach some of these shapes.In
float_add_fold_test.mjsfrom #9415, subtractions from a zero int such as- -5now print as0 - -5, which is still correct. Its caseFloat.fromInt(0) -. (Obj.magic(-5n): float)would print0 - -5nand throw "Cannot mix BigInt and other types", so it now negates the bigint with-.(Obj.magic(-5n): float), which prints- -5n.Merging
Based on #9415 (
fix-float-add-fold), so its commit shows up here until it merges. This should merge after #9415.12.x
12.x has the same bug: there
unary_minus_testdoesn't even load ("Unary operator used immediately before exponentiation expression"). I'll backport once this one is reviewed.The fix was written by Claude.