Skip to content

Commit d22b905

Browse files
committed
Introduce subtyping instead of ad-hoc treatment of function arguments.
Instead of using an ad-hoc treatment of function arguments which are callbacks in automatic curried application, use subtyping in uncurried-always mode. So an uncurried function is cast to a curried one during unification. In addition to the existing examples, this also supports uses of `|>` and callbacks to `div` such as `onClick`, which now can be uncurried functions.
1 parent cce4c48 commit d22b905

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

jscomp/ml/ctype.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,9 @@ let rec unify (env:Env.t ref) t1 t2 =
23412341
with Cannot_expand ->
23422342
unify2 env t1 t2
23432343
end
2344+
| (Tconstr (Pident {name="function$"}, [tFun; _], _), Tarrow _) when !Config.use_automatic_curried_application ->
2345+
(* subtype: an uncurried function is cast to a curried one *)
2346+
unify2 env tFun t2
23442347
| _ ->
23452348
unify2 env t1 t2
23462349
end;

jscomp/ml/typecore.ml

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,12 +2016,9 @@ 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, 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
2019+
let uncurried =
2020+
Ext_list.exists sexp.pexp_attributes (fun ({txt },_) -> txt = "res.uapp")
2021+
&& not @@ is_automatic_curried_application env funct in
20252022
let (args, ty_res, fully_applied) = type_application uncurried env funct sargs in
20262023
end_def ();
20272024
unify_var env (newvar()) funct.exp_type;
@@ -2116,7 +2113,7 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
21162113
(* Disable Unerasable_optional_argument for uncurried functions *)
21172114
let unerasable_optional_argument = Warnings.number Unerasable_optional_argument in
21182115
Warnings.parse_options false ("-" ^ string_of_int unerasable_optional_argument);
2119-
let exp = type_construct env loc lid sarg ty_expected sexp.pexp_attributes in
2116+
let exp = type_construct env loc lid sarg uncurried_typ sexp.pexp_attributes in
21202117
Warnings.restore state;
21212118
exp
21222119
| Pexp_construct(lid, sarg) ->
@@ -2982,25 +2979,10 @@ and type_argument ?recarg env sarg ty_expected' ty_expected =
29822979
texp
29832980
and is_automatic_curried_application env funct =
29842981
(* When a curried function is used with uncurried application, treat it as a curried application *)
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
2982+
!Config.use_automatic_curried_application &&
2983+
match (expand_head env funct.exp_type).desc with
2984+
| Tarrow _ -> true
2985+
| _ -> false
30042986
and type_application uncurried env funct (sargs : sargs) : targs * Types.type_expr * bool =
30052987
(* funct.exp_type may be generic *)
30062988
let result_type omitted ty_fun =

0 commit comments

Comments
 (0)