From 7aec236642311e31e3bffd130db81f7a68ada06e Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 16 Oct 2022 07:27:44 +0100 Subject: [PATCH 1/2] Fix emitting unary minus for floats in case of negative constants. --- jscomp/core/js_dump.ml | 13 +++++++++---- jscomp/test/format_test.js | 4 ++++ jscomp/test/format_test.ml | 5 ++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/jscomp/core/js_dump.ml b/jscomp/core/js_dump.ml index f686dcfdbf..4063b60bde 100644 --- a/jscomp/core/js_dump.ml +++ b/jscomp/core/js_dump.ml @@ -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 @@ -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 @@ -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 -> diff --git a/jscomp/test/format_test.js b/jscomp/test/format_test.js index 7c7056d32a..76ba7983c2 100644 --- a/jscomp/test/format_test.js +++ b/jscomp/test/format_test.js @@ -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; diff --git a/jscomp/test/format_test.ml b/jscomp/test/format_test.ml index f451af7ae3..fb2fa2e0e3 100644 --- a/jscomp/test/format_test.ml +++ b/jscomp/test/format_test.ml @@ -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 From 49ed247c5f4dc071020e7a096dbdc6cd44a82763 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 16 Oct 2022 07:29:18 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md Fixes https://github.com/rescript-lang/rescript-compiler/issues/5736 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ecd660fb1..4194cd160a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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