Repository navigation
[12.x] Fold float addition of int literals only when the sum is exact - #9450
Open
illusionalsagacity wants to merge 2 commits into
Open
illusionalsagacity wants to merge 2 commits into
illusionalsagacity wants to merge 2 commits into
Conversation
`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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #9415 to 12.x.
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 identitySome, a raw literal such as%raw("-5")and, on 12.x, the payload of an@unboxedconstructor.Before:
After:
Before:
After:
Fix
As on master:
Js_exp_make.float_addandfloat_minusfold 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 formin_int, whose negation doesn't fit.int32_addandint32_minusfold literals with wraparound themselves, so int output stays the same.Js_dumpprints0 - eas- ewheneprints with a leading-(prints_with_leading_minus).Differences from master:
@unboxedconstructor reachesJs_dumpas aCaml_blockand is printed as its payload, soprints_with_leading_minusalso looks through it. On master that block is gone beforeJs_dump. Without this case,untaggedabove still prints--5.0.Json_literal, so the case master has for it doesn't apply.Tests
tests/tests/src/float_add_fold_test.resis the same as on master. It covers folding of float literals, chains,min_int, negating negative operands (literals,0 - x, hex floats, bigints, an identitySome, an@unboxedpayload and raw JS), and int wraparound. Only its.mjsis new; no other module intests/testschanges output.Investigated by Claude.