From 1437815af4f09184deac8c86f048f509e2a2dc6f Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Thu, 10 Oct 2024 20:32:16 +0800 Subject: [PATCH 1/5] Fix variant coercion with as attribute (#7058) * Use value from as attribute if available * Add tests * Update CHANGELOG * Fix formatting --- CHANGELOG.md | 1 + compiler/core/lam_constant_convert.ml | 5 +++++ tests/tests/src/VariantCoercion.js | 28 ++++++++++++++++++++++++- tests/tests/src/VariantCoercion.res | 30 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d71a3d889..9f163672b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - Fix bigint max, min https://github.com/rescript-lang/rescript-compiler/pull/7088 - Fix parsing issue with nested variant pattern type spreads. https://github.com/rescript-lang/rescript-compiler/pull/7080 - Fix JSX settings inheritance: only 'version' propagates to dependencies, preserving their 'mode' and 'module'. https://github.com/rescript-lang/rescript-compiler/pull/7094 +- Fix variant cast to int. https://github.com/rescript-lang/rescript-compiler/pull/7058 #### :nail_care: Polish diff --git a/compiler/core/lam_constant_convert.ml b/compiler/core/lam_constant_convert.ml index 6f132e9453..ad0e7de05a 100644 --- a/compiler/core/lam_constant_convert.ml +++ b/compiler/core/lam_constant_convert.ml @@ -48,6 +48,11 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t = | Pt_assertfalse -> Const_int {i = Int32.of_int i; comment = Pt_assertfalse} | Pt_constructor {name; const; non_const; attrs} -> let tag_type = Ast_untagged_variants.process_tag_type attrs in + let i = + match tag_type with + | Some (Ast_untagged_variants.Int v) -> v + | _ -> i + in Const_int { i = Int32.of_int i; diff --git a/tests/tests/src/VariantCoercion.js b/tests/tests/src/VariantCoercion.js index 1cea8b731b..5913561e11 100644 --- a/tests/tests/src/VariantCoercion.js +++ b/tests/tests/src/VariantCoercion.js @@ -82,6 +82,31 @@ let CoerceFromPolyvariantToVariant = { withUnboxedCatchAllP: "One" }; +function f1() { + return "b"; +} + +function f2() { + return "b"; +} + +for (let x$1 = 1; x$1 <= 2; ++x$1) { + console.log(x$1); +} + +let y = "one".length; + +let z = 1.5 + 2.0 + 1.5; + +let CoerceVariantBinaryOp = { + x: 2, + v: 2, + f1: f1, + f2: f2, + y: y, + z: z +}; + let a$2 = "Three"; let b = "Three"; @@ -107,4 +132,5 @@ exports.CoerceFromIntToVariant = CoerceFromIntToVariant; exports.CoerceFromFloatToVariant = CoerceFromFloatToVariant; exports.CoerceFromBigintToVariant = CoerceFromBigintToVariant; exports.CoerceFromPolyvariantToVariant = CoerceFromPolyvariantToVariant; -/* No side effect */ +exports.CoerceVariantBinaryOp = CoerceVariantBinaryOp; +/* Not a pure module */ diff --git a/tests/tests/src/VariantCoercion.res b/tests/tests/src/VariantCoercion.res index f8f0826f67..c1e197be55 100644 --- a/tests/tests/src/VariantCoercion.res +++ b/tests/tests/src/VariantCoercion.res @@ -120,3 +120,33 @@ module CoerceFromPolyvariantToVariant = { let withUnboxedCatchAll: withUnboxedCatchAll = #One let withUnboxedCatchAllP = (withUnboxedCatchAll :> withUnboxedCatchAllP) } + +module CoerceVariantBinaryOp = { + type flag = | @as(0) A | @as(2) B + + let x = 0->lor((B :> int)) + + let v = B + let f1 = () => + switch v { + | A => "a" + | B => "b" + } + let f2 = () => + switch (v :> int) { + | 2 => "b" + | _ => "a" + } + + for x in 1 to (B :> int) { + Js.log(x) + } + + type flagStr = | @as("one") One | @as("two") Two + + let y = (One :> string)->String.length + + type flagFloat = | @as(1.5) X | @as(2.0) Y + + let z = (X :> float) +. (Y :> float) +. 1.5 +} From 4bfe542d575825036cbde5819cf3812adf3edcf8 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 10 Oct 2024 18:47:45 +0200 Subject: [PATCH 2/5] Move ounit_tests to tests folder (#7096) * Move ounit_tests to tests folder * CHANGELOG --- CHANGELOG.md | 1 + compiler/dune | 2 +- dune | 2 +- tests/dune | 1 + {compiler => tests}/ounit_tests/dune | 0 {compiler => tests}/ounit_tests/ounit_array_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_bal_tree_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_bsb_pkg_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_bsb_regex_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_data_random.ml | 0 {compiler => tests}/ounit_tests/ounit_hash_set_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_hash_stubs_test.ml | 0 {compiler => tests}/ounit_tests/ounit_hashtbl_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_ident_mask_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_int_vec_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_js_regex_checker_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_json_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_list_test.ml | 0 {compiler => tests}/ounit_tests/ounit_map_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_path_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_scc_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_string_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_tests_main.ml | 0 {compiler => tests}/ounit_tests/ounit_tests_main.mli | 0 {compiler => tests}/ounit_tests/ounit_tests_util.ml | 0 {compiler => tests}/ounit_tests/ounit_topsort_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_unicode_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_union_find_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_utf8_test.ml | 0 {compiler => tests}/ounit_tests/ounit_util_tests.ml | 0 {compiler => tests}/ounit_tests/ounit_vec_test.ml | 0 31 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 tests/dune rename {compiler => tests}/ounit_tests/dune (100%) rename {compiler => tests}/ounit_tests/ounit_array_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_bal_tree_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_bsb_pkg_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_bsb_regex_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_data_random.ml (100%) rename {compiler => tests}/ounit_tests/ounit_hash_set_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_hash_stubs_test.ml (100%) rename {compiler => tests}/ounit_tests/ounit_hashtbl_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_ident_mask_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_int_vec_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_js_regex_checker_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_json_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_list_test.ml (100%) rename {compiler => tests}/ounit_tests/ounit_map_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_path_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_scc_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_string_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_tests_main.ml (100%) rename {compiler => tests}/ounit_tests/ounit_tests_main.mli (100%) rename {compiler => tests}/ounit_tests/ounit_tests_util.ml (100%) rename {compiler => tests}/ounit_tests/ounit_topsort_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_unicode_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_union_find_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_utf8_test.ml (100%) rename {compiler => tests}/ounit_tests/ounit_util_tests.ml (100%) rename {compiler => tests}/ounit_tests/ounit_vec_test.ml (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f163672b6..ef10dbd75e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ - Run `build_tests` on Windows. https://github.com/rescript-lang/rescript-compiler/pull/7065 - Rename folder "jscomp" to "compiler". https://github.com/rescript-lang/rescript-compiler/pull/7086 - Disable -bs-cross-module-opt for tests. https://github.com/rescript-lang/rescript-compiler/pull/7071 +- Move `ounit_tests` into the `tests` folder. https://github.com/rescript-lang/rescript-compiler/pull/7096 # 12.0.0-alpha.3 diff --git a/compiler/dune b/compiler/dune index 97b3d99fdd..101b4b8acf 100644 --- a/compiler/dune +++ b/compiler/dune @@ -1,5 +1,5 @@ (dirs bsb bsb_exe bsb_helper bsb_helper_exe bsc cmij common core depends ext - frontend gentype jsoo js_parser ml ounit_tests syntax) + frontend gentype jsoo js_parser ml syntax) (env (dev diff --git a/dune b/dune index a2dde03ccd..f4b1eadb7b 100644 --- a/dune +++ b/dune @@ -1 +1 @@ -(dirs compiler) +(dirs compiler tests) diff --git a/tests/dune b/tests/dune new file mode 100644 index 0000000000..2b0951af4a --- /dev/null +++ b/tests/dune @@ -0,0 +1 @@ +(dirs ounit_tests) diff --git a/compiler/ounit_tests/dune b/tests/ounit_tests/dune similarity index 100% rename from compiler/ounit_tests/dune rename to tests/ounit_tests/dune diff --git a/compiler/ounit_tests/ounit_array_tests.ml b/tests/ounit_tests/ounit_array_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_array_tests.ml rename to tests/ounit_tests/ounit_array_tests.ml diff --git a/compiler/ounit_tests/ounit_bal_tree_tests.ml b/tests/ounit_tests/ounit_bal_tree_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_bal_tree_tests.ml rename to tests/ounit_tests/ounit_bal_tree_tests.ml diff --git a/compiler/ounit_tests/ounit_bsb_pkg_tests.ml b/tests/ounit_tests/ounit_bsb_pkg_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_bsb_pkg_tests.ml rename to tests/ounit_tests/ounit_bsb_pkg_tests.ml diff --git a/compiler/ounit_tests/ounit_bsb_regex_tests.ml b/tests/ounit_tests/ounit_bsb_regex_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_bsb_regex_tests.ml rename to tests/ounit_tests/ounit_bsb_regex_tests.ml diff --git a/compiler/ounit_tests/ounit_data_random.ml b/tests/ounit_tests/ounit_data_random.ml similarity index 100% rename from compiler/ounit_tests/ounit_data_random.ml rename to tests/ounit_tests/ounit_data_random.ml diff --git a/compiler/ounit_tests/ounit_hash_set_tests.ml b/tests/ounit_tests/ounit_hash_set_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_hash_set_tests.ml rename to tests/ounit_tests/ounit_hash_set_tests.ml diff --git a/compiler/ounit_tests/ounit_hash_stubs_test.ml b/tests/ounit_tests/ounit_hash_stubs_test.ml similarity index 100% rename from compiler/ounit_tests/ounit_hash_stubs_test.ml rename to tests/ounit_tests/ounit_hash_stubs_test.ml diff --git a/compiler/ounit_tests/ounit_hashtbl_tests.ml b/tests/ounit_tests/ounit_hashtbl_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_hashtbl_tests.ml rename to tests/ounit_tests/ounit_hashtbl_tests.ml diff --git a/compiler/ounit_tests/ounit_ident_mask_tests.ml b/tests/ounit_tests/ounit_ident_mask_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_ident_mask_tests.ml rename to tests/ounit_tests/ounit_ident_mask_tests.ml diff --git a/compiler/ounit_tests/ounit_int_vec_tests.ml b/tests/ounit_tests/ounit_int_vec_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_int_vec_tests.ml rename to tests/ounit_tests/ounit_int_vec_tests.ml diff --git a/compiler/ounit_tests/ounit_js_regex_checker_tests.ml b/tests/ounit_tests/ounit_js_regex_checker_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_js_regex_checker_tests.ml rename to tests/ounit_tests/ounit_js_regex_checker_tests.ml diff --git a/compiler/ounit_tests/ounit_json_tests.ml b/tests/ounit_tests/ounit_json_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_json_tests.ml rename to tests/ounit_tests/ounit_json_tests.ml diff --git a/compiler/ounit_tests/ounit_list_test.ml b/tests/ounit_tests/ounit_list_test.ml similarity index 100% rename from compiler/ounit_tests/ounit_list_test.ml rename to tests/ounit_tests/ounit_list_test.ml diff --git a/compiler/ounit_tests/ounit_map_tests.ml b/tests/ounit_tests/ounit_map_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_map_tests.ml rename to tests/ounit_tests/ounit_map_tests.ml diff --git a/compiler/ounit_tests/ounit_path_tests.ml b/tests/ounit_tests/ounit_path_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_path_tests.ml rename to tests/ounit_tests/ounit_path_tests.ml diff --git a/compiler/ounit_tests/ounit_scc_tests.ml b/tests/ounit_tests/ounit_scc_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_scc_tests.ml rename to tests/ounit_tests/ounit_scc_tests.ml diff --git a/compiler/ounit_tests/ounit_string_tests.ml b/tests/ounit_tests/ounit_string_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_string_tests.ml rename to tests/ounit_tests/ounit_string_tests.ml diff --git a/compiler/ounit_tests/ounit_tests_main.ml b/tests/ounit_tests/ounit_tests_main.ml similarity index 100% rename from compiler/ounit_tests/ounit_tests_main.ml rename to tests/ounit_tests/ounit_tests_main.ml diff --git a/compiler/ounit_tests/ounit_tests_main.mli b/tests/ounit_tests/ounit_tests_main.mli similarity index 100% rename from compiler/ounit_tests/ounit_tests_main.mli rename to tests/ounit_tests/ounit_tests_main.mli diff --git a/compiler/ounit_tests/ounit_tests_util.ml b/tests/ounit_tests/ounit_tests_util.ml similarity index 100% rename from compiler/ounit_tests/ounit_tests_util.ml rename to tests/ounit_tests/ounit_tests_util.ml diff --git a/compiler/ounit_tests/ounit_topsort_tests.ml b/tests/ounit_tests/ounit_topsort_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_topsort_tests.ml rename to tests/ounit_tests/ounit_topsort_tests.ml diff --git a/compiler/ounit_tests/ounit_unicode_tests.ml b/tests/ounit_tests/ounit_unicode_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_unicode_tests.ml rename to tests/ounit_tests/ounit_unicode_tests.ml diff --git a/compiler/ounit_tests/ounit_union_find_tests.ml b/tests/ounit_tests/ounit_union_find_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_union_find_tests.ml rename to tests/ounit_tests/ounit_union_find_tests.ml diff --git a/compiler/ounit_tests/ounit_utf8_test.ml b/tests/ounit_tests/ounit_utf8_test.ml similarity index 100% rename from compiler/ounit_tests/ounit_utf8_test.ml rename to tests/ounit_tests/ounit_utf8_test.ml diff --git a/compiler/ounit_tests/ounit_util_tests.ml b/tests/ounit_tests/ounit_util_tests.ml similarity index 100% rename from compiler/ounit_tests/ounit_util_tests.ml rename to tests/ounit_tests/ounit_util_tests.ml diff --git a/compiler/ounit_tests/ounit_vec_test.ml b/tests/ounit_tests/ounit_vec_test.ml similarity index 100% rename from compiler/ounit_tests/ounit_vec_test.ml rename to tests/ounit_tests/ounit_vec_test.ml From e4ccfeccfd8240b01726026bfbbf9c8a4e5c9367 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Wed, 9 Oct 2024 07:14:25 +0800 Subject: [PATCH 3/5] Print comments inside fn with no arg --- compiler/syntax/src/res_printer.ml | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index 0c53e9cc00..da02a6c907 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -4842,9 +4842,22 @@ and print_arguments_with_callback_in_last_position ~state args cmt_tbl = and print_arguments ~state ?(partial = false) (args : (Asttypes.arg_label * Parsetree.expression) list) cmt_tbl = match args with - | [(Nolabel, {pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _)})] - -> - Doc.text "()" + | [ + ( Nolabel, + { + pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _); + pexp_loc = loc; + } ); + ] -> + if has_leading_line_comment cmt_tbl loc then + let cmt = print_comments Doc.nil cmt_tbl loc in + Doc.concat + [ + Doc.lparen; + Doc.indent (Doc.group (Doc.concat [Doc.soft_line; cmt])); + Doc.rparen; + ] + else Doc.text "()" | [(Nolabel, arg)] when ParsetreeViewer.is_huggable_expression arg -> let arg_doc = let doc = print_expression_with_comments ~state arg cmt_tbl in From ddafa4488a885e5de75dc84e6b53afdd87b1063e Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Thu, 10 Oct 2024 20:05:50 +0800 Subject: [PATCH 4/5] Add tests --- .../printer/comments/expected/expr.res.txt | 26 +++++++++++++++++ tests/syntax_tests/printer/comments/expr.res | 29 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/tests/syntax_tests/printer/comments/expected/expr.res.txt b/tests/syntax_tests/printer/comments/expected/expr.res.txt index 8fc8f09702..1085861c82 100644 --- a/tests/syntax_tests/printer/comments/expected/expr.res.txt +++ b/tests/syntax_tests/printer/comments/expected/expr.res.txt @@ -234,6 +234,32 @@ let multiply = ( /* c2 */ m2 /* c3 */, ) => () +f( + // a + // b + // c + ) + +f( + // a + // b + // c + ) + +let x = { + f( + // a + // b + // c + ) + + f( + // a + // b + // c + ) +} + f(() => 1) // c1 f(_ => 1) diff --git a/tests/syntax_tests/printer/comments/expr.res b/tests/syntax_tests/printer/comments/expr.res index 25723e34d5..4130510984 100644 --- a/tests/syntax_tests/printer/comments/expr.res +++ b/tests/syntax_tests/printer/comments/expr.res @@ -223,6 +223,35 @@ let f = (/* c0 */ ~greeting /* c1 */, /* c2 */ ~from /* c3 */ as /* c4 */ hometo let multiply = (/* c-2 */ type t /* c-1 */,/* c0 */ m1 /* c1 */, /* c2 */ m2 /* c3 */) => () let multiply = (/* c-4 */ type t /* c-3 */,/* c0 */ m1 /* c1 */, /* c-2 */ type s /* c-1 */, /* c2 */ m2 /* c3 */) => () +f( + // a + // b +// c +) + +f( + // a + // b + // c + () +) + +let x = { + f( + // a + // b + // c + ) + + + f( + // a + // b + // c + () + ) +} + f(() // c1 => 1); From 9825f957c399d06f7bfcf7672525cb0f45a2e1ad Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Thu, 10 Oct 2024 20:08:26 +0800 Subject: [PATCH 5/5] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef10dbd75e..f4f0a957c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ - Fix parsing issue with nested variant pattern type spreads. https://github.com/rescript-lang/rescript-compiler/pull/7080 - Fix JSX settings inheritance: only 'version' propagates to dependencies, preserving their 'mode' and 'module'. https://github.com/rescript-lang/rescript-compiler/pull/7094 - Fix variant cast to int. https://github.com/rescript-lang/rescript-compiler/pull/7058 +- Fix comments formatted away in function without arguments. https://github.com/rescript-lang/rescript-compiler/pull/7095 #### :nail_care: Polish