Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6dbc4d1

Browse files
committedFeb 4, 2023
Make callbacks uncurried in automatic curried application.
When functions such as `Array.map` are called via automatic curried application, if one of the arguments is a callback, turn that into an uncurried type. This allows passing an uncurried callback even though the original `Array.map` expects a curried callback.
1 parent b8522b3 commit 6dbc4d1

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed
 

‎jscomp/ml/typecore.ml

+25-7
Original file line numberDiff line numberDiff line change
@@ -2016,9 +2016,12 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
20162016
end_def ();
20172017
wrap_trace_gadt_instances env (lower_args env []) ty;
20182018
begin_def ();
2019-
let uncurried =
2020-
Ext_list.exists sexp.pexp_attributes (fun ({txt },_) -> txt = "res.uapp")
2021-
&& not (is_automatic_curried_application env funct) in
2019+
let uncurried, funct =
2020+
if Ext_list.exists sexp.pexp_attributes (fun ({txt },_) -> txt = "res.uapp")
2021+
then match is_automatic_curried_application env funct with
2022+
| Some funct -> false, funct
2023+
| None -> true, funct
2024+
else false, funct in
20222025
let (args, ty_res, fully_applied) = type_application uncurried env funct sargs in
20232026
end_def ();
20242027
unify_var env (newvar()) funct.exp_type;
@@ -2979,10 +2982,25 @@ and type_argument ?recarg env sarg ty_expected' ty_expected =
29792982
texp
29802983
and is_automatic_curried_application env funct =
29812984
(* When a curried function is used with uncurried application, treat it as a curried application *)
2982-
!Config.use_automatic_curried_application &&
2983-
match (expand_head env funct.exp_type).desc with
2984-
| Tarrow _ -> true
2985-
| _ -> false
2985+
2986+
let rec fun_arity texp = match texp.desc with
2987+
| Tarrow(_, _, t2, _) -> 1 + fun_arity t2
2988+
| _ -> 0 in
2989+
2990+
let rec make_callbacks_uncurried texp = match texp.desc with
2991+
| Tarrow (lbl, t1, t2, comm) ->
2992+
let a1 = fun_arity t1 in
2993+
let t1 = if a1 = 0 then t1 else Ast_uncurried.make_uncurried_type ~env ~arity:a1 t1 in
2994+
let t2 = make_callbacks_uncurried t2 in
2995+
{texp with desc = Tarrow (lbl, t1, t2, comm)}
2996+
| _ -> texp in
2997+
2998+
let expanded = expand_head env funct.exp_type in
2999+
match expanded.desc with
3000+
| Tarrow _ when !Config.use_automatic_curried_application ->
3001+
let texp = make_callbacks_uncurried expanded in
3002+
Some {funct with exp_type = texp}
3003+
| _ -> None
29863004
and type_application uncurried env funct (sargs : sargs) : targs * Types.type_expr * bool =
29873005
(* funct.exp_type may be generic *)
29883006
let result_type omitted ty_fun =

0 commit comments

Comments
 (0)
Please sign in to comment.