Skip to content

Commit b8522b3

Browse files
committed
Introduce automatic curried application.
Using the uncurried-always mode is difficult when calling compiler libraries, as those are uncurried. This introduces automatic curried application in uncurried-only mode: when a curried function is used in application, the application is treated as curried application. This means that even in uncurried-always mode, one can use compiler libraries in most cases. Exceptions are functions that take callbacks, such as `Array.map`.
1 parent 86e0f05 commit b8522b3

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

jscomp/bsb/bsb_ninja_rule.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
102102
since the default is already good -- it does not*)
103103
let buf = Ext_buffer.create 100 in
104104
let ns_flag = match namespace with None -> "" | Some n -> " -bs-ns " ^ n in
105+
let add_uncurried_flag = function
106+
| Res_uncurried.Legacy -> ()
107+
| Default -> Ext_buffer.add_string buf " -uncurried default"
108+
| Always -> Ext_buffer.add_string buf " -uncurried always" in
105109
let mk_ml_cmj_cmd ~(read_cmi : [ `yes | `is_cmi | `no ]) ~is_dev ~postbuild :
106110
string =
107111
Ext_buffer.clear buf;
@@ -121,6 +125,7 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
121125
(match gentype_config with
122126
| false -> ()
123127
| true -> Ext_buffer.add_string buf " -bs-gentype");
128+
add_uncurried_flag uncurried;
124129
if read_cmi <> `is_cmi then (
125130
Ext_buffer.add_string buf " -bs-package-name ";
126131
Ext_buffer.add_string buf (Ext_filename.maybe_quote package_name);
@@ -171,10 +176,7 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
171176
| None -> ()
172177
| Some Classic -> Ext_buffer.add_string buf " -bs-jsx-mode classic"
173178
| Some Automatic -> Ext_buffer.add_string buf " -bs-jsx-mode automatic");
174-
(match uncurried with
175-
| Legacy -> ()
176-
| Default -> Ext_buffer.add_string buf " -uncurried default"
177-
| Always -> Ext_buffer.add_string buf " -uncurried always");
179+
add_uncurried_flag uncurried;
178180

179181
Ext_buffer.add_char_string buf ' ' bsc_flags;
180182
Ext_buffer.add_string buf " -absname -bs-ast -o $out $i";

jscomp/bsc/rescript_compiler_main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
406406
"-uncurried", string_call (fun i ->
407407
match i with
408408
| "default" -> Res_uncurried.init := Default
409-
| "always" -> Res_uncurried.init := Always
409+
| "always" -> Res_uncurried.init := Always; Config.use_automatic_curried_application := true
410410
| "legacy" -> Res_uncurried.init := Legacy
411411
| _ -> Bsc_args.bad_arg (" Not supported -uncurried option : " ^ i)
412412
),

jscomp/build_tests/uncurried/src/UncurriedAlways.res

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ let b = bar(3, 4)
88

99
let w = 3->foo(4)
1010

11-
let a = 3->foo(. 4)
11+
let a = 3->foo(. 4)
12+
13+
Js.log(a) // Test automatic uncurried applicatin

jscomp/ext/config.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ let bs_only = ref true
1313

1414
let unsafe_empty_array = ref false
1515

16+
let use_automatic_curried_application = ref false
17+
1618
and cmi_magic_number = "Caml1999I022"
1719

1820
and ast_impl_magic_number = "Caml1999M022"

jscomp/ext/config.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ val cmt_magic_number : string
4646
(* Magic number for compiled interface files *)
4747

4848
val print_config : out_channel -> unit
49+
50+
val use_automatic_curried_application : bool ref

jscomp/ml/typecore.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,7 +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 = Ext_list.exists sexp.pexp_attributes (fun ({txt },_) -> txt = "res.uapp") 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
20202022
let (args, ty_res, fully_applied) = type_application uncurried env funct sargs in
20212023
end_def ();
20222024
unify_var env (newvar()) funct.exp_type;
@@ -2975,7 +2977,12 @@ and type_argument ?recarg env sarg ty_expected' ty_expected =
29752977
let texp = type_expect ?recarg env sarg ty_expected' in
29762978
unify_exp env texp ty_expected;
29772979
texp
2978-
2980+
and is_automatic_curried_application env funct =
2981+
(* 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
29792986
and type_application uncurried env funct (sargs : sargs) : targs * Types.type_expr * bool =
29802987
(* funct.exp_type may be generic *)
29812988
let result_type omitted ty_fun =

0 commit comments

Comments
 (0)