Skip to content

[12.x] Fold float addition of int literals only when the sum is exact - #9450

Open
illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-float-add-fold-12.x
Open

illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-float-add-fold-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #9415 to 12.x.

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, a raw literal such as %raw("-5") and, on 12.x, the payload of an @unboxed constructor.

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")

@unboxed type floatOrString = Float(float) | String(string)
let untagged = Float.fromInt(0) -. (Obj.magic(Float(-5.0)): float)

Before:

let neg = -2147483648;

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

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

let raw = --5;

let untagged = --5.0;

After:

let neg = - -2147483648;

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

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

let raw = - -5;

let untagged = - -5.0;

Fix

As on master:

  • Js_exp_make.float_add and float_minus fold two int literals only when the exact result fits in an int32, and no longer reassociate a chain. Adding a negative literal still becomes a subtraction, except for min_int, whose negation doesn't fit.
  • int32_add and int32_minus fold literals with wraparound themselves, so int output stays the same.
  • Js_dump prints 0 - e as - e when e prints with a leading - (prints_with_leading_minus).

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 prints_with_leading_minus also looks through it. On master that block is gone before Js_dump. Without this case, untagged above still prints --5.0.
  • 12.x's JS IR has no Json_literal, so the case master has for it doesn't apply.

Tests

tests/tests/src/float_add_fold_test.res is the same as on master. It covers folding of float literals, chains, min_int, negating negative operands (literals, 0 - x, hex floats, bigints, an identity Some, an @unboxed payload and raw JS), and int wraparound. Only its .mjs is new; 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>

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