Skip to content

Fix emitting unary minus for floats in case of negative constants. #5737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Fix issue where formatter erases tail comments inside JSX tag https://github.com/rescript-lang/syntax/issues/663
- Fix issue where the JSX prop has type annotation of the first class module https://github.com/rescript-lang/syntax/pull/666
- Fix issue where an empty record literal {} expected to have a non-record type would type check https://github.com/rescript-lang/rescript-compiler/pull/5729
- Fix emitting unary minus for floats in case of negative constants https://github.com/rescript-lang/rescript-compiler/pull/5737

#### :eyeglasses: Spec Compliance

Expand Down
13 changes: 9 additions & 4 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
expression ~level:13 cxt f e
| Bin
( Minus,
{ expression_desc = Number (Int { i = 0l; _ } | Float { f = "0." }) },
{
expression_desc =
Number ((Int { i = 0l; _ } | Float { f = "0." }) as desc);
},
e )
(* TODO:
Handle multiple cases like
Expand All @@ -675,7 +678,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
{[ 0.000 - x ]}
*) ->
P.cond_paren_group f (level > 13) 1 (fun _ ->
P.string f "-";
P.string f (match desc with Float _ -> "- " | _ -> "-");
expression ~level:13 cxt f e)
| Bin (op, e1, e2) ->
let out, lft, rght = Js_op_util.op_prec op in
Expand Down Expand Up @@ -718,8 +721,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
Js_op.Lit (Ext_ident.convert x))))
(*name convention of Record is slight different from modules*)
| Caml_block (el, mutable_flag, _, Blk_record { fields; record_repr }) -> (
if Array.length fields <> 0 && Ext_array.for_alli fields (fun i v -> string_of_int i = v) then
expression_desc cxt ~level f (Array (el, mutable_flag))
if
Array.length fields <> 0
&& Ext_array.for_alli fields (fun i v -> string_of_int i = v)
then expression_desc cxt ~level f (Array (el, mutable_flag))
else
match record_repr with
| Record_regular ->
Expand Down
4 changes: 4 additions & 0 deletions jscomp/test/format_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ List.iter((function (param) {
scan_float("File \"format_test.ml\", line 122, characters 13-20", param[1], param[0]);
}), literals);

var f1 = - -9.9;

eq("File \"format_test.ml\", line 128, characters 5-12", f1, 9.9);

Mt.from_pair_suites("Format_test", suites.contents);

exports.suites = suites;
Expand Down
5 changes: 4 additions & 1 deletion jscomp/test/format_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ let () =
scan_float __LOC__ b a
) literals


let () =
let f = -9.9 in
let f1 = -.f in
eq __LOC__ f1 9.9

let () = Mt.from_pair_suites __MODULE__ !suites