From 9669dc883d55df5ebaa4fbe06d6d855c87010bcf Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Mon, 13 Jan 2025 22:14:39 -0800 Subject: [PATCH] code generation: remove redundant switch cases branches in generated JS (#1295) * Fix redundant branches in generated switch body, fixes #6671 (#6672) * Fix redundant branches in generated switch body, fixes #6671 Signed-Off-By: Vincenzo Pellegrini * Updated changelog --------- Signed-off-by: Vincenzo Pellegrini * fmt * snapshot runtime tests * chore: add changelog entry --------- Signed-off-by: Vincenzo Pellegrini Co-authored-by: Vincenzo Pellegrini --- Changes.md | 14 ++++++++------ jscomp/core/lam_compile.ml | 9 +++++++++ .../dist/jscomp/test/flow_parser_reg_test.js | 2 -- jscomp/test/dist/jscomp/test/gpr_1438.js | 16 ---------------- jscomp/test/dist/jscomp/test/gpr_1698_test.js | 2 -- jscomp/test/dist/jscomp/test/mario_game.js | 19 ------------------- jscomp/test/dist/jscomp/test/ocaml_re_test.js | 17 ----------------- .../dist/jscomp/test/ocaml_typedtree_test.js | 2 -- jscomp/test/dist/jscomp/test/string_test.js | 5 ----- jscomp/test/dist/jscomp/test/tscanf_test.js | 5 ----- jscomp/test/dist/jscomp/test/typeof_test.js | 3 --- 11 files changed, 17 insertions(+), 77 deletions(-) diff --git a/Changes.md b/Changes.md index f4ea7cb423..e6d964310b 100644 --- a/Changes.md +++ b/Changes.md @@ -84,15 +84,17 @@ Unreleased - BREAKING: ppx: print the `deprecated` alert for `@@deriving abstract` at the declaration site rather than at (all) usages ([#1269](https://github.com/melange-re/melange/pull/1269)) -- core: prettify melange code generation for `for` loops +- JS generation: prettify `for` loops ([#1275](https://github.com/melange-re/melange/pull/1275)) -- core: improve generated code formatting for `throw` and `return` statements, - JS objects ([#1286](https://github.com/melange-re/melange/pull/1286), +- JS generation: improve formatting for `throw` and `return` statements, JS + objects ([#1286](https://github.com/melange-re/melange/pull/1286), [#1289](https://github.com/melange-re/melange/pull/1289)) -- core: improve generated code formatting for empty return and continue - statements ([#1288](https://github.com/melange-re/melange/pull/1288)) -- core: in generated code, remove trailing spaces before commas in `export` +- JS generation: improve formatting for empty return and continue statements + ([#1288](https://github.com/melange-re/melange/pull/1288)) +- JS generation: remove trailing spaces before commas in `export` ([#1287](https://github.com/melange-re/melange/pull/1287)) +- JS generation: remove redundant switch cases branches + ([#1295](https://github.com/melange-re/melange/pull/1295)) 4.0.1 2024-06-07 --------------- diff --git a/jscomp/core/lam_compile.ml b/jscomp/core/lam_compile.ml index ecd08f14e8..4e90c96c65 100644 --- a/jscomp/core/lam_compile.ml +++ b/jscomp/core/lam_compile.ml @@ -598,6 +598,15 @@ and compile_general_cases : break still should not be printed (it will be continuned) TOOD: disabled temporarily since it's not perfect yet *) morph_declare_to_assign cxt (fun cxt declaration -> + (* Exclude cases that are the same as the default if the default is defined *) + let cases = + match default with + | Default lam -> + List.filter + ~f:(fun (_, lam1) -> not (Lam.eq_approx lam lam1)) + cases + | _ -> cases + in let default = match default with | Complete -> None diff --git a/jscomp/test/dist/jscomp/test/flow_parser_reg_test.js b/jscomp/test/dist/jscomp/test/flow_parser_reg_test.js index 29b4af314a..0f1e83227c 100644 --- a/jscomp/test/dist/jscomp/test/flow_parser_reg_test.js +++ b/jscomp/test/dist/jscomp/test/flow_parser_reg_test.js @@ -2167,8 +2167,6 @@ function parse_sign(f) { switch (match.hd) { case 43 : return eat(f); - case 44 : - return f; case 45 : const init = eat(f); return { diff --git a/jscomp/test/dist/jscomp/test/gpr_1438.js b/jscomp/test/dist/jscomp/test/gpr_1438.js index 7ca2a85ace..3d4a12f6d4 100644 --- a/jscomp/test/dist/jscomp/test/gpr_1438.js +++ b/jscomp/test/dist/jscomp/test/gpr_1438.js @@ -12,22 +12,6 @@ function actionKey(key, a, b, c, d, e) { return e; case 116 : return b; - case 100 : - case 101 : - case 102 : - case 103 : - case 104 : - case 105 : - case 108 : - case 109 : - case 110 : - case 111 : - case 112 : - case 113 : - case 114 : - case 115 : - case 117 : - break; case 99 : case 118 : return a; diff --git a/jscomp/test/dist/jscomp/test/gpr_1698_test.js b/jscomp/test/dist/jscomp/test/gpr_1698_test.js index 0622d95354..157e8ef7e8 100644 --- a/jscomp/test/dist/jscomp/test/gpr_1698_test.js +++ b/jscomp/test/dist/jscomp/test/gpr_1698_test.js @@ -167,8 +167,6 @@ function compare(context, state, _a, _b) { switch (b.TAG) { case /* Pow */ 3 : return 1; - case /* Gcd */ 5 : - return -1; default: return -1; } diff --git a/jscomp/test/dist/jscomp/test/mario_game.js b/jscomp/test/dist/jscomp/test/mario_game.js index 0e052ac2ef..c165a0d098 100644 --- a/jscomp/test/dist/jscomp/test/mario_game.js +++ b/jscomp/test/dist/jscomp/test/mario_game.js @@ -2363,25 +2363,6 @@ function keydown(evt) { case 83 : pressed_keys.down = true; break; - case 67 : - case 69 : - case 70 : - case 71 : - case 72 : - case 73 : - case 74 : - case 75 : - case 76 : - case 77 : - case 78 : - case 79 : - case 80 : - case 81 : - case 82 : - case 84 : - case 85 : - case 86 : - break; case 87 : pressed_keys.up = true; break; diff --git a/jscomp/test/dist/jscomp/test/ocaml_re_test.js b/jscomp/test/dist/jscomp/test/ocaml_re_test.js index 8bddb9d38e..e6af06aae9 100644 --- a/jscomp/test/dist/jscomp/test/ocaml_re_test.js +++ b/jscomp/test/dist/jscomp/test/ocaml_re_test.js @@ -3508,23 +3508,6 @@ function parse(multiline, dollar_endonly, dotall, ungreedy, s) { }); case 90 : return /* Last_end_of_line */ 7; - case 58 : - case 59 : - case 60 : - case 61 : - case 62 : - case 63 : - case 64 : - case 91 : - case 92 : - case 93 : - case 94 : - case 95 : - case 96 : - return { - TAG: /* Set */ 0, - _0: single(c) - }; case 98 : return alt$1({ hd: /* Beg_of_word */ 2, diff --git a/jscomp/test/dist/jscomp/test/ocaml_typedtree_test.js b/jscomp/test/dist/jscomp/test/ocaml_typedtree_test.js index 9199718320..4225b84980 100644 --- a/jscomp/test/dist/jscomp/test/ocaml_typedtree_test.js +++ b/jscomp/test/dist/jscomp/test/ocaml_typedtree_test.js @@ -66087,8 +66087,6 @@ function is_nonexpansive_mod(_mexp) { return false; case /* Tstr_include */ 12 : return is_nonexpansive_mod(id_mod_list._0.incl_mod); - case /* Tstr_attribute */ 13 : - return true; default: return true; } diff --git a/jscomp/test/dist/jscomp/test/string_test.js b/jscomp/test/dist/jscomp/test/string_test.js index 1ebf0338f8..c770f13d1b 100644 --- a/jscomp/test/dist/jscomp/test/string_test.js +++ b/jscomp/test/dist/jscomp/test/string_test.js @@ -47,11 +47,6 @@ function gg(x) { case 4 : a = 6; break; - case 5 : - case 6 : - case 7 : - a = 8; - break; case 8 : a = 7; break; diff --git a/jscomp/test/dist/jscomp/test/tscanf_test.js b/jscomp/test/dist/jscomp/test/tscanf_test.js index 2a707dd3b3..35d0a56471 100644 --- a/jscomp/test/dist/jscomp/test/tscanf_test.js +++ b/jscomp/test/dist/jscomp/test/tscanf_test.js @@ -2018,11 +2018,6 @@ function scan_elems$3(ib, accu) { hd: i, tl: accu }); - case "]" : - return Stdlib__List.rev({ - hd: i, - tl: accu - }); default: return Stdlib__List.rev({ hd: i, diff --git a/jscomp/test/dist/jscomp/test/typeof_test.js b/jscomp/test/dist/jscomp/test/typeof_test.js index a7b935f372..4c24718adc 100644 --- a/jscomp/test/dist/jscomp/test/typeof_test.js +++ b/jscomp/test/dist/jscomp/test/typeof_test.js @@ -8,9 +8,6 @@ function string_or_number(x) { const ty = Js__Js_types.classify(x); if (/* tag */ typeof ty === "number" || typeof ty === "string") { switch (ty) { - case /* JSFalse */ 0 : - case /* JSTrue */ 1 : - return false; default: return false; }