Skip to content

[12.x] Negate floats as -0 - x and parenthesize ** under prefix operators - #9452

Open
illusionalsagacity wants to merge 3 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-unary-minus-printing-12.x
Open

illusionalsagacity wants to merge 3 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-unary-minus-printing-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #9422 to 12.x. It depends on the backport of #9415 (fix-float-add-fold-12.x), on which it is based, as #9422 builds on #9415 on master.

Problem

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. Prefix operators also printed their operand at the level of **, so a power had no parentheses there, which JavaScript rejects.

let fromLiteral = x => 0. -. x
let fromInt = x => Float.fromInt(0) -. x
let negate = x => -.x
let negateInt = (x: int) => -x
let f = x => -0. -. x

Before:

function fromLiteral(x) {
  return - x;
}

function fromInt(x) {
  return -x;
}

function negate(x) {
  return - x;
}

function negateInt(x) {
  return -x | 0;
}

function f(x) {
  return -0 - x;
}

After:

function fromLiteral(x) {
  return 0 - x;
}

function fromInt(x) {
  return 0 - x;
}

function negate(x) {
  return - x;
}

function negateInt(x) {
  return -x | 0;
}

function f(x) {
  return - x;
}
let negPow = (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)

@unboxed type floatOrString = Float(float) | String(string)
let negUntagged = () => -.(Obj.magic(Float(base ** exponent)): float)

Before (each is a syntax error):

function negPow(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;
}

function negUntagged() {
  return - base ** exponent;
}

After:

function negPow(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);
}

function negUntagged() {
  return - (base ** exponent);
}

The outputs before are the same on maintenance/12.x and with the #9415 backport.

Fix

As on master:

  • A float negation is a subtraction from -0. (Js_exp_make.float_neg), 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 (int32_operand).
  • The operands of all prefix operators go through unary_operand, which parenthesizes a power.

Differences from master:

  • On 12.x the block of an @unboxed constructor reaches Js_dump as a Caml_block and is printed as its payload, so unary_operand looks through it, as prints_with_leading_minus does since the Fold float addition of int literals only when the sum is exact #9415 backport. Without this, negUntagged above still prints - base ** exponent.
  • The matches list 12.x's JS IR, which has no Json_literal, Jsx, template literals or Record_rest, and has FlatCall, String_index and a Bnot binop. 12.x's JS IR has no source locations, so int32_operand marks none.

Tests

  • tests/tests/src/unary_minus_test.res is master's, plus a power and a negative number as the payload of an @unboxed constructor under a float negation, typeof and an int negation. Its .mjs differs from master's where 12.x optimizes differently: countdown tests x <= 0 instead of !(x > 0), and in negateFiveAfter the literal -5 isn't inlined into the negation, so that case prints -x | 0.
  • tests/tests/src/float_add_fold_test.res negates its negative bigint with -., as on master, and its .mjs changes as on master: a subtraction from a zero int such as - -5 now prints as 0 - -5.

No other module in tests/tests changes output.

Investigated by Claude.

`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. `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 an untagged
constructor, or a raw negative literal. Int addition and subtraction still
fold with wraparound, so their output does not change.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
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>
…escript-lang#9422)

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

Differences from master: on 12.x the block of an untagged constructor
reaches js_dump as a Caml_block and is printed as its payload, so
`unary_operand` looks through it as `prints_with_leading_minus` does.
Without that, `-.(Obj.magic(Float(a ** b)): float)` and
`typeof(Float(a ** b))` still printed `- a ** b` and `typeof a ** b`.
unary_minus_test.res tests both, and an int negation of an untagged
negative payload. 12.x's JS IR has no source locations, no Json_literal,
Jsx, template literals or Record_rest, and has FlatCall, String_index
and a Bnot binop, which the matches list instead.

(cherry picked from commit ccfc979)
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@9452

@rescript/darwin-arm64

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

@rescript/darwin-x64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: a2eb923

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