Skip to content

Commit 41ec1b0

Browse files
authored
Fix trailing undefined for optional parameters not omitted with @send (#6716)
* Fix trailing undefined for optional parameters not omitted with @send * release.ninja changed * CHANGELOG * Handle more cases of trailing undefineds * CHANGELOG * Check first if there are any trailing undefined args
1 parent 1ec144b commit 41ec1b0

5 files changed

+39
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#### :bug: Bug Fix
2020

2121
- Improve error when using '@deriving(accessors)' on a variant with record arguments. https://github.com/rescript-lang/rescript-compiler/pull/6712
22-
2322
- Stop escaping JSX prop names with hyphens. https://github.com/rescript-lang/rescript-compiler/pull/6705
23+
- Fix trailing undefined for optional parameters not omitted with `@send` and `@new`. https://github.com/rescript-lang/rescript-compiler/pull/6716
2424

2525
# 11.1.0-rc.7
2626

jscomp/core/lam_compile_external_call.ml

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ type specs = External_arg_spec.params
151151

152152
type exprs = E.t list
153153

154+
let keep_non_undefined_args (arg_types : specs) (args : exprs) =
155+
let rec has_undefined_trailing_args arg_types args =
156+
match (arg_types, args) with
157+
| ( [{External_arg_spec.arg_label = Arg_optional; _}],
158+
[{J.expression_desc = Undefined {isUnit = false}; _}] ) ->
159+
true
160+
| ( _ :: arg_types_rest, _ :: args_rest ) ->
161+
has_undefined_trailing_args arg_types_rest args_rest
162+
| _ -> false
163+
in
164+
let rec aux arg_types args =
165+
match (arg_types, args) with
166+
| ( {External_arg_spec.arg_label = Arg_optional; _} :: arg_types_rest,
167+
{J.expression_desc = Undefined {isUnit = false}; _} :: args_rest ) ->
168+
aux arg_types_rest args_rest
169+
| _ -> args
170+
in
171+
if (has_undefined_trailing_args arg_types args) then
172+
aux (List.rev arg_types) (List.rev args) |> List.rev
173+
else args
174+
154175
(* TODO: fix splice,
155176
we need a static guarantee that it is static array construct
156177
otherwise, we should provide a good error message here,
@@ -176,7 +197,7 @@ let assemble_args_no_splice (arg_types : specs) (args : exprs) :
176197
| _ :: _, [] -> assert false
177198
in
178199
let args, eff = aux arg_types args in
179-
( args,
200+
( keep_non_undefined_args arg_types args,
180201
match eff with
181202
| [] -> None
182203
| x :: xs ->
@@ -271,16 +292,6 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
271292
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
272293
else
273294
let args, eff = assemble_args_no_splice arg_types args in
274-
let rec keepNonUndefinedArgs argsList (argTypes : specs) =
275-
match (argsList, argTypes) with
276-
| ( {J.expression_desc = Undefined {isUnit = false}; _} :: rest,
277-
{External_arg_spec.arg_label = Arg_optional; _} :: argTypes ) ->
278-
keepNonUndefinedArgs rest argTypes
279-
| _ -> argsList
280-
in
281-
let args =
282-
keepNonUndefinedArgs (List.rev args) (List.rev arg_types) |> List.rev
283-
in
284295
add_eff eff
285296
@@ E.call ~info:{ arity = Full; call_info = Call_na } fn args
286297
| Js_module_as_fn { external_module_name; splice } ->

jscomp/runtime/release.ninja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ o runtime/caml_exceptions.cmj : cc_cmi runtime/caml_exceptions.res | runtime/cam
2525
o runtime/caml_exceptions.cmi : cc runtime/caml_exceptions.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2626
o runtime/caml_float.cmj : cc_cmi runtime/caml_float.res | runtime/caml_float.cmi runtime/caml_float_extern.cmj
2727
o runtime/caml_float.cmi : cc runtime/caml_float.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
28-
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
28+
o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml.cmj runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
2929
o runtime/caml_format.cmi : cc runtime/caml_format.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
3030
o runtime/caml_hash.cmj : cc_cmi runtime/caml_hash.res | runtime/caml_hash.cmi runtime/caml_hash_primitive.cmj runtime/caml_nativeint_extern.cmj runtime/js.cmj
3131
o runtime/caml_hash.cmi : cc runtime/caml_hash.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
@@ -41,7 +41,7 @@ o runtime/caml_md5.cmj : cc_cmi runtime/caml_md5.res | runtime/caml_array_extern
4141
o runtime/caml_md5.cmi : cc runtime/caml_md5.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
4242
o runtime/caml_module.cmj : cc_cmi runtime/caml_module.res | runtime/caml_array_extern.cmj runtime/caml_module.cmi runtime/caml_obj.cmj
4343
o runtime/caml_module.cmi : cc runtime/caml_module.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
44-
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj runtime/js.cmj
44+
o runtime/caml_obj.cmj : cc_cmi runtime/caml_obj.res | runtime/caml.cmj runtime/caml_array_extern.cmj runtime/caml_obj.cmi runtime/caml_option.cmj runtime/js.cmj
4545
o runtime/caml_obj.cmi : cc runtime/caml_obj.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
4646
o runtime/caml_option.cmj : cc_cmi runtime/caml_option.res | runtime/caml_option.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
4747
o runtime/caml_option.cmi : cc runtime/caml_option.resi | runtime/bs_stdlib_mini.cmi runtime/caml_undefined_extern.cmj runtime/js.cmi runtime/js.cmj
@@ -58,7 +58,7 @@ o runtime/caml_bigint_extern.cmi runtime/caml_bigint_extern.cmj : cc runtime/cam
5858
o runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj : cc runtime/caml_external_polyfill.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5959
o runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj : cc runtime/caml_float_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6060
o runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj : cc runtime/caml_int64_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
61-
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/js.cmi runtime/js.cmj
61+
o runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj : cc runtime/caml_js_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/caml_exceptions.cmj runtime/caml_option.cmj runtime/js.cmi runtime/js.cmj
6262
o runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj : cc runtime/caml_nativeint_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6363
o runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6464
o runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj

jscomp/test/omit_trailing_undefined_in_external_calls.js

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/omit_trailing_undefined_in_external_calls.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ external formatDate: (Js.Date.t, ~options: dateFormatOptions=?, ~done: bool=?) =
99
let x = formatDate(Js.Date.make())
1010
let x = formatDate(Js.Date.make(), ~options={someOption: true})
1111
let x = formatDate(Js.Date.make(), ~done=true)
12+
13+
@send external floatToString: (float, ~radix: int=?) => string = "toString"
14+
15+
let x = floatToString(42.)
16+
17+
@new external regExpFromString: (string, ~flags: string=?) => Js.Re.t = "RegExp"
18+
19+
let x = regExpFromString("ab+c")

0 commit comments

Comments
 (0)