Repository navigation
[12.x] Negate floats as -0 - x and parenthesize ** under prefix operators - #9452
Open
illusionalsagacity wants to merge 3 commits into
Open
illusionalsagacity wants to merge 3 commits into
illusionalsagacity wants to merge 3 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>
…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>
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
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 #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 - xas-x. That is wrong for a float subtraction:0. -. xis0whenxis0., but-xis-0. Prefix operators also printed their operand at the level of**, so a power had no parentheses there, which JavaScript rejects.Before:
After:
Before (each is a syntax error):
After:
The outputs before are the same on
maintenance/12.xand with the #9415 backport.Fix
As on master:
-0.(Js_exp_make.float_neg), which equals-xfor everyx, and only that is printed as- x.0 - x. It is printed as-xonly as the operand of a bitwise operator, such as the| 0of every int subtraction, which drops the sign of a zero (int32_operand).unary_operand, which parenthesizes a power.Differences from master:
@unboxedconstructor reachesJs_dumpas aCaml_blockand is printed as its payload, sounary_operandlooks through it, asprints_with_leading_minusdoes since the Fold float addition of int literals only when the sum is exact #9415 backport. Without this,negUntaggedabove still prints- base ** exponent.Json_literal,Jsx, template literals orRecord_rest, and hasFlatCall,String_indexand aBnotbinop. 12.x's JS IR has no source locations, soint32_operandmarks none.Tests
tests/tests/src/unary_minus_test.resis master's, plus a power and a negative number as the payload of an@unboxedconstructor under a float negation,typeofand an int negation. Its.mjsdiffers from master's where 12.x optimizes differently:countdowntestsx <= 0instead of!(x > 0), and innegateFiveAfterthe literal-5isn't inlined into the negation, so that case prints-x | 0.tests/tests/src/float_add_fold_test.resnegates its negative bigint with-., as on master, and its.mjschanges as on master: a subtraction from a zero int such as- -5now prints as0 - -5.No other module in
tests/testschanges output.Investigated by Claude.