From 327280d6ea0d62b0210fc2f681b3f1d211ad2ed0 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Thu, 12 Jun 2025 14:30:41 +0200 Subject: [PATCH] Test: ignore inferred arity in case of `%raw`. The inferred arity of raw JS code is actively used with `%ffi`, but it can produce unexpected results with `%raw`: ``` let foo: int => int = %raw(`function add(x, y=5){ return x + y }`) Console.log(foo(2)) ``` --- CHANGELOG.md | 3 +++ compiler/core/lam_arity_analysis.ml | 2 -- compiler/core/lam_pass_collect.ml | 7 ------- tests/tests/src/raw_arity.mjs | 11 +++++++++++ tests/tests/src/raw_arity.res | 3 +++ 5 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 tests/tests/src/raw_arity.mjs create mode 100644 tests/tests/src/raw_arity.res diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f90555c57..18564c2ed8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ # 12.0.0-alpha.15 (Unreleased) +#### :bug: Bug fix +- ignore inferred arity in functions inside `%raw` functions, leaving to `%ffi` the responsibility to check the arity since it gives an error in case of mismatch. https://github.com/rescript-lang/rescript/pull/7542 + #### :nail_care: Polish - Better error message for when trying to await something that is not a promise. https://github.com/rescript-lang/rescript/pull/7561 diff --git a/compiler/core/lam_arity_analysis.ml b/compiler/core/lam_arity_analysis.ml index 6e0027ca5f..ee3d91f154 100644 --- a/compiler/core/lam_arity_analysis.ml +++ b/compiler/core/lam_arity_analysis.ml @@ -69,8 +69,6 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t = with | Submodule subs -> subs.(m) (* TODO: shall we store it as array?*) | Single _ -> Lam_arity.na) - | Lprim {primitive = Praw_js_code {code_info = Exp (Js_function {arity})}} -> - Lam_arity.info [arity] false | Lprim {primitive = Praise; _} -> Lam_arity.raise_arity_info | Lglobal_module _ (* TODO: fix me never going to happen *) | Lprim _ -> Lam_arity.na (* CHECK*) diff --git a/compiler/core/lam_pass_collect.ml b/compiler/core/lam_pass_collect.ml index 5f4a0d46e5..7e1b2dfcd2 100644 --- a/compiler/core/lam_pass_collect.ml +++ b/compiler/core/lam_pass_collect.ml @@ -64,13 +64,6 @@ let collect_info (meta : Lam_stats.t) (lam : Lam.t) = | Lprim {primitive = Psome | Psome_not_nest; args = [v]} -> Hash_ident.replace meta.ident_tbl ident (Normal_optional v); collect v - | Lprim - { - primitive = Praw_js_code {code_info = Exp (Js_function {arity})}; - args = _; - } -> - Hash_ident.replace meta.ident_tbl ident - (FunctionId {arity = Lam_arity.info [arity] false; lambda = None}) | Lprim {primitive = Pnull_to_opt; args = [(Lvar _ as l)]; _} -> Hash_ident.replace meta.ident_tbl ident (OptionalBlock (l, Null)) | Lprim {primitive = Pundefined_to_opt; args = [(Lvar _ as l)]; _} -> diff --git a/tests/tests/src/raw_arity.mjs b/tests/tests/src/raw_arity.mjs new file mode 100644 index 0000000000..e7ebe48da4 --- /dev/null +++ b/tests/tests/src/raw_arity.mjs @@ -0,0 +1,11 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +let foo = (function add(x, y=5){ return x + y }); + +console.log(foo(2)); + +export { + foo, +} +/* Not a pure module */ diff --git a/tests/tests/src/raw_arity.res b/tests/tests/src/raw_arity.res new file mode 100644 index 0000000000..39d7c62d5e --- /dev/null +++ b/tests/tests/src/raw_arity.res @@ -0,0 +1,3 @@ +let foo: int => int = %raw(`function add(x, y=5){ return x + y }`) + +Console.log(foo(2))