Skip to content

Commit 15ebdcb

Browse files
committed
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)) ```
1 parent 6fe5c47 commit 15ebdcb

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
1313
# 12.0.0-alpha.15 (Unreleased)
1414

15+
#### :bug: Bug fix
16+
- 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
17+
1518
#### :nail_care: Polish
1619

1720
- Better error message for when trying to await something that is not a promise. https://github.com/rescript-lang/rescript/pull/7561

compiler/core/lam_arity_analysis.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
6969
with
7070
| Submodule subs -> subs.(m) (* TODO: shall we store it as array?*)
7171
| Single _ -> Lam_arity.na)
72-
| Lprim {primitive = Praw_js_code {code_info = Exp (Js_function {arity})}} ->
73-
Lam_arity.info [arity] false
7472
| Lprim {primitive = Praise; _} -> Lam_arity.raise_arity_info
7573
| Lglobal_module _ (* TODO: fix me never going to happen *) | Lprim _ ->
7674
Lam_arity.na (* CHECK*)

compiler/core/lam_pass_collect.ml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ let collect_info (meta : Lam_stats.t) (lam : Lam.t) =
6464
| Lprim {primitive = Psome | Psome_not_nest; args = [v]} ->
6565
Hash_ident.replace meta.ident_tbl ident (Normal_optional v);
6666
collect v
67-
| Lprim
68-
{
69-
primitive = Praw_js_code {code_info = Exp (Js_function {arity})};
70-
args = _;
71-
} ->
72-
Hash_ident.replace meta.ident_tbl ident
73-
(FunctionId {arity = Lam_arity.info [arity] false; lambda = None})
7467
| Lprim {primitive = Pnull_to_opt; args = [(Lvar _ as l)]; _} ->
7568
Hash_ident.replace meta.ident_tbl ident (OptionalBlock (l, Null))
7669
| Lprim {primitive = Pundefined_to_opt; args = [(Lvar _ as l)]; _} ->

tests/tests/src/raw_arity.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
4+
let foo = (function add(x, y=5){ return x + y });
5+
6+
console.log(foo(2));
7+
8+
export {
9+
foo,
10+
}
11+
/* Not a pure module */

tests/tests/src/raw_arity.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let foo: int => int = %raw(`function add(x, y=5){ return x + y }`)
2+
3+
Console.log(foo(2))

0 commit comments

Comments
 (0)