Skip to content

Commit bb3f249

Browse files
authored
Cleanup uncurried (#6080)
* Clean up uncurried: uncurried.swap and config Rename `@@uncurried` to `@@uncurried.swap` so relegating it to tests only. Also simplify the setting in bsconfig, which now takes just a boolean. * Remove @toUncurried, which is not essential. * Update CHANGELOG.md * Rename `@@uncurried.always` to simply `@@uncurried`. * Move all the uncurried config to module Config. The Config module is used by the compiler, so the `uncurried` setting needs to be reset on each file as tests compile several files on a single command line (actual project builds don't). * Update CHANGELOG.md * Clean up isSwap
1 parent 881b82f commit bb3f249

39 files changed

+82
-183
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
#### :rocket: New Feature
1616

17-
- Introduce experimental uncurried by default mode. Can be turned on mid-file by adding standalone annotation `@@uncurried`. For experimentation only. https://github.com/rescript-lang/rescript-compiler/pull/5796
18-
- Adding `@@toUncurried` to the file and reformat will convert to uncurried syntax https://github.com/rescript-lang/rescript-compiler/pull/5800
17+
- Introduce experimental uncurried by default mode. Can be turned on mid-file by adding standalone annotation `@@uncurried.swap`. For experimentation only. https://github.com/rescript-lang/rescript-compiler/pull/5796
18+
- ~Adding `@@toUncurried` to the file and reformat will convert to uncurried syntax https://github.com/rescript-lang/rescript-compiler/pull/5800~
1919
- Add support for unary uncurried pipe in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/5804
2020
- Add support for partial application of uncurried functions: with uncurried application one can provide a
2121
subset of the arguments, and return a curried type with the remaining ones https://github.com/rescript-lang/rescript-compiler/pull/5805
@@ -24,10 +24,10 @@ subset of the arguments, and return a curried type with the remaining ones https
2424
- Add support for default arguments in uncurried functions https://github.com/rescript-lang/rescript-compiler/pull/5835
2525
- Inline uncurried application when it is safe https://github.com/rescript-lang/rescript-compiler/pull/5847
2626
- Support optional named arguments without a final unit in uncurried functions https://github.com/rescript-lang/rescript-compiler/pull/5907
27-
- Add support for uncurried-always: a mode where everything is considered uncurried, whether with or without the `.`. This can be turned on with `@@uncurriedAlways` locally in a file. Added a project config `"uncurried"`, which propagates to dependencies, and takes the values: `"legacy"` which changes nothing, or `"default"` for uncurried by default, or `"always"` for uncurried-always.
27+
- Add support for uncurried mode: a mode where everything is considered uncurried, whether with or without the `.`. This can be turned on with `@@uncurried` locally in a file. For project-level configuration in `bsconfig.json`, there's a boolean config `"uncurried"`, which propagates to dependencies, to turn on uncurried mode.
2828
Since there's no syntax for partial application in this new mode, introduce `@res.partial foo(x)` to express partial application. This is temporary and will later have some surface syntax.
2929
Use best effort to determine the config when formatting a file.
30-
https://github.com/rescript-lang/rescript-compiler/pull/5968
30+
https://github.com/rescript-lang/rescript-compiler/pull/5968 https://github.com/rescript-lang/rescript-compiler/pull/6080
3131

3232
#### :boom: Breaking Change
3333

docs/docson/build-schema.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,6 @@
359359
"additionalProperties": false,
360360
"required": ["version"]
361361
},
362-
"uncurried-specs": {
363-
"type": "string",
364-
"enum": ["legacy", "default", "always"]
365-
},
366362
"bsc-flags": {
367363
"oneOf": [
368364
{
@@ -445,7 +441,7 @@
445441
"description": "Configuration for the JSX transformation."
446442
},
447443
"uncurried": {
448-
"$ref": "#/definitions/uncurried-specs",
444+
"type": "boolean",
449445
"description": "Configuration for the uncurried mode."
450446
},
451447
"reason": {

jscomp/bsb/bsb_config_parse.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,13 @@ let extract_gentype_config (map : json_map) : Bsb_config_types.gentype_config =
9595
| Some config ->
9696
Bsb_exception.config_error config "gentypeconfig expect an object"
9797

98-
let extract_uncurried (map : json_map) : Res_uncurried.config =
98+
let extract_uncurried (map : json_map) : bool =
9999
match map.?(Bsb_build_schemas.uncurried) with
100-
| None -> Legacy
101-
| Some (Str { str = "legacy" }) -> Legacy
102-
| Some (Str { str = "default" }) -> Default
103-
| Some (Str { str = "always" }) -> Always
100+
| None -> false
101+
| Some (True _) -> true
102+
| Some (False _) -> false
104103
| Some config ->
105-
Bsb_exception.config_error config "uncurried expects one of: \"legacy\", \"default\", \"always\"."
104+
Bsb_exception.config_error config "uncurried expects one of: true, false."
106105

107106
let extract_string (map : json_map) (field : string) cb =
108107
match map.?(field) with

jscomp/bsb/bsb_config_parse.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
val deps_from_bsconfig : unit -> Bsb_package_specs.t * Bsb_jsx.t * Res_uncurried.config * Set_string.t
25+
val deps_from_bsconfig : unit -> Bsb_package_specs.t * Bsb_jsx.t * bool * Set_string.t
2626

2727
val interpret_json :
2828
package_kind:Bsb_package_kind.t -> per_proj_dir:string -> Bsb_config_types.t

jscomp/bsb/bsb_config_types.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ type t = {
6464
cut_generators : bool;
6565
(* note when used as a dev mode, we will always ignore it *)
6666
gentype_config : gentype_config;
67-
uncurried: Res_uncurried.config;
67+
uncurried: bool;
6868
}

jscomp/bsb/bsb_ninja_rule.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
9191
~(has_postbuild : string option) ~(pp_file : string option)
9292
~(has_builtin : bool)
9393
~(reason_react_jsx : Bsb_config_types.reason_react_jsx option)
94-
~(jsx : Bsb_jsx.t) ~(uncurried: Res_uncurried.config) ~(digest : string) ~(package_specs : Bsb_package_specs.t)
94+
~(jsx : Bsb_jsx.t) ~(uncurried: bool) ~(digest : string) ~(package_specs : Bsb_package_specs.t)
9595
~(namespace : string option) ~package_name ~warnings
9696
~(ppx_files : Bsb_config_types.ppx list) ~bsc_flags ~(dpkg_incls : string)
9797
~(lib_incls : string) ~(dev_incls : string) ~bs_dependencies
@@ -102,10 +102,8 @@ 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
105+
let add_uncurried_flag b =
106+
if b then Ext_buffer.add_string buf " -uncurried" in
109107
let mk_ml_cmj_cmd ~(read_cmi : [ `yes | `is_cmi | `no ]) ~is_dev ~postbuild :
110108
string =
111109
Ext_buffer.clear buf;

jscomp/bsb/bsb_ninja_rule.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ val make_custom_rules :
7272
has_builtin:bool ->
7373
reason_react_jsx:Bsb_config_types.reason_react_jsx option ->
7474
jsx:Bsb_jsx.t ->
75-
uncurried:Res_uncurried.config ->
75+
uncurried:bool ->
7676
digest:string ->
7777
package_specs:Bsb_package_specs.t ->
7878
namespace:string option ->

jscomp/bsb/bsb_package_kind.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
type dep_payload = { package_specs : Bsb_package_specs.t; jsx : Bsb_jsx.t; uncurried : Res_uncurried.config }
25+
type dep_payload = { package_specs : Bsb_package_specs.t; jsx : Bsb_jsx.t; uncurried : bool }
2626

2727
type t =
2828
| Toplevel

jscomp/bsc/rescript_compiler_main.ml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ let process_file sourcefile ?(kind ) ppf =
5151
The {!Location.input_name} relies on that we write the binary ast
5252
properly
5353
*)
54+
let uncurried = !Config.uncurried in
5455
let kind =
5556
match kind with
5657
| None -> Ext_file_extensions.classify_input (Ext_filename.get_extension_maybe sourcefile)
5758
| Some kind -> kind in
58-
match kind with
59+
let res = match kind with
5960
| Ml ->
6061
let sourcefile = set_abs_input_name sourcefile in
6162
setup_compiler_printer `ml;
@@ -103,6 +104,9 @@ let process_file sourcefile ?(kind ) ppf =
103104
Format.pp_print_newline Format.std_formatter ()
104105
| Unknown ->
105106
Bsc_args.bad_arg ("don't know what to do with " ^ sourcefile)
107+
in
108+
Config.uncurried := uncurried;
109+
res
106110
let usage = "Usage: bsc <options> <files>\nOptions are:"
107111

108112
let ppf = Format.err_formatter
@@ -406,13 +410,7 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
406410

407411
"-nopervasives", set Clflags.nopervasives,
408412
"*internal*";
409-
"-uncurried", string_call (fun i ->
410-
match i with
411-
| "default" -> Res_uncurried.init := Default
412-
| "always" -> Res_uncurried.init := Always; Config.use_automatic_curried_application := true
413-
| "legacy" -> Res_uncurried.init := Legacy
414-
| _ -> Bsc_args.bad_arg (" Not supported -uncurried option : " ^ i)
415-
),
413+
"-uncurried", unit_call (fun () -> Config.uncurried := Uncurried),
416414
"*internal* Set jsx module";
417415
"-v", unit_call print_version_string,
418416
"Print compiler version and location of standard library and exit";

jscomp/build_tests/uncurried-always/bsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"dir" : "src",
66
"subdirs" : true
77
},
8-
"uncurried": "always"
8+
"uncurried": true
99
}

jscomp/core/res_compmisc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let initial_env () =
4646
let initial = Env.initial_safe_string in
4747
let env =
4848
if !Clflags.nopervasives then initial
49-
else open_implicit_module (if !Config.use_automatic_curried_application then "PervasivesU" else "Pervasives") initial
49+
else open_implicit_module (if !Config.uncurried = Uncurried then "PervasivesU" else "Pervasives") initial
5050
in
5151
List.fold_left
5252
(fun env m -> open_implicit_module m env)

jscomp/ext/config.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ let bs_only = ref true
1313

1414
let unsafe_empty_array = ref false
1515

16-
let use_automatic_curried_application = ref false
16+
type uncurried = Legacy | Uncurried | Swap
17+
let uncurried = ref Legacy
1718

1819
and cmi_magic_number = "Caml1999I022"
1920

jscomp/ext/config.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ val cmt_magic_number : string
4747

4848
val print_config : out_channel -> unit
4949

50-
val use_automatic_curried_application : bool ref
50+
type uncurried = Legacy | Uncurried | Swap
51+
val uncurried : uncurried ref

jscomp/frontend/ast_config.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ let process_directives str =
4444
| Pstr_attribute ({ txt = "directive" },
4545
PStr [ { pstr_desc = Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (d, _)) }, _) } ]) ->
4646
Js_config.directives := !Js_config.directives @ [d]
47-
| Pstr_attribute ({txt = "uncurriedAlways"}, _) -> Config.use_automatic_curried_application := true
47+
| Pstr_attribute ({txt = "uncurried"}, _) -> Config.uncurried := Uncurried
4848
| _ -> ())
4949

5050
let rec iter_on_bs_config_str (x : Parsetree.structure) =

jscomp/ml/ctype.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,7 @@ 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 ->
2344+
| (Tconstr (Pident {name="function$"}, [tFun; _], _), Tarrow _) when !Config.uncurried = Uncurried ->
23452345
(* subtype: an uncurried function is cast to a curried one *)
23462346
unify2 env tFun t2
23472347
| _ ->

jscomp/ml/typecore.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2980,7 +2980,7 @@ and type_argument ?recarg env sarg ty_expected' ty_expected =
29802980
texp
29812981
and is_automatic_curried_application env funct =
29822982
(* When a curried function is used with uncurried application, treat it as a curried application *)
2983-
!Config.use_automatic_curried_application &&
2983+
!Config.uncurried = Uncurried &&
29842984
match (expand_head env funct.exp_type).desc with
29852985
| Tarrow _ -> true
29862986
| _ -> false

jscomp/stdlib-406/pervasivesU.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/* */
1414
/* ************************************************************************ */
1515

16-
@@uncurriedAlways
16+
@@uncurried
1717

1818
/* Internal */
1919
external __unsafe_cast: 'a => 'b = "%identity"

jscomp/stdlib-406/pervasivesU.resi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/* */
1414
/* ************************************************************************ */
1515

16-
@@uncurriedAlways
16+
@@uncurried
1717

1818
" The initially opened module.
1919

jscomp/test/UncurriedAlways.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@@uncurriedAlways
1+
@@uncurried
22

33
let foo = (x, y) => x + y
44

jscomp/test/UncurriedExternals.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module StandardNotation = {
3939
let (get, set) = useState(() => 3)
4040
}
4141

42-
@@uncurried
42+
@@uncurried.swap
4343

4444
external raise: exn => 'a = "%raise"
4545
let dd = (. ()) => raise(Not_found)

jscomp/test/UncurriedPervasives.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
@@uncurriedAlways
1+
@@uncurried
22
let n : _ => unit = ignore // Check that we're pulling in uncurried pervasives

jscomp/test/async_await.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@@uncurried
1+
@@uncurried.swap
22

33
let next = n => n + 1
44
let useNext = async () => next(3)

jscomp/test/uncurried_cast.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module StandardNotation = {
2323
let anInt = still2Args(~z=3)(. 5)
2424
}
2525

26-
@@uncurried
26+
@@uncurried.swap
2727

2828
open Uncurried
2929

jscomp/test/uncurried_default.args.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module StandardNotation = {
1414
let r3 = foo3(. )
1515
}
1616

17-
@@uncurried
17+
@@uncurried.swap
1818

1919
open StandardNotation
2020

jscomp/test/uncurried_pipe.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module StandardNotation = {
1010
let unary = (. x) => x + 1
1111
}
1212

13-
@@uncurried
13+
@@uncurried.swap
1414

1515
open StandardNotation
1616

res_syntax/src/res_core.ml

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ let buildLongident words =
386386
let makeInfixOperator (p : Parser.t) token startPos endPos =
387387
let stringifiedToken =
388388
if token = Token.MinusGreater then
389-
if p.uncurried_config |> Res_uncurried.isDefault then "|.u" else "|."
389+
if p.uncurried_config = Legacy then "|." else "|.u"
390390
else if token = Token.PlusPlus then "^"
391391
else if token = Token.BangEqual then "<>"
392392
else if token = Token.BangEqualEqual then "!="
@@ -1558,8 +1558,7 @@ and parseEs6ArrowExpression ?(arrowAttrs = []) ?(arrowStartPos = None) ?context
15581558
| TermParameter {dotted} :: _
15591559
when p.uncurried_config |> Res_uncurried.fromDotted ~dotted && isFun ->
15601560
true
1561-
| TermParameter _ :: rest
1562-
when (not (p.uncurried_config |> Res_uncurried.isDefault)) && isFun ->
1561+
| TermParameter _ :: rest when p.uncurried_config = Legacy && isFun ->
15631562
rest
15641563
|> List.exists (function
15651564
| TermParameter {dotted} -> dotted
@@ -1594,11 +1593,7 @@ and parseEs6ArrowExpression ?(arrowAttrs = []) ?(arrowStartPos = None) ?context
15941593
let uncurried =
15951594
p.uncurried_config |> Res_uncurried.fromDotted ~dotted
15961595
in
1597-
if
1598-
uncurried
1599-
&& (termParamNum = 1
1600-
|| not (p.uncurried_config |> Res_uncurried.isDefault))
1601-
then
1596+
if uncurried && (termParamNum = 1 || p.uncurried_config = Legacy) then
16021597
(termParamNum - 1, Ast_uncurried.uncurriedFun ~loc ~arity funExpr, 1)
16031598
else (termParamNum - 1, funExpr, arity + 1)
16041599
| TypeParameter {dotted = _; attrs; locs = newtypes; pos = startPos} ->
@@ -3922,9 +3917,8 @@ and parsePolyTypeExpr p =
39223917
let returnType = parseTypExpr ~alias:false p in
39233918
let loc = mkLoc typ.Parsetree.ptyp_loc.loc_start p.prevEndPos in
39243919
let tFun = Ast_helper.Typ.arrow ~loc Asttypes.Nolabel typ returnType in
3925-
if p.uncurried_config |> Res_uncurried.isDefault then
3926-
Ast_uncurried.uncurriedType ~loc ~arity:1 tFun
3927-
else tFun
3920+
if p.uncurried_config = Legacy then tFun
3921+
else Ast_uncurried.uncurriedType ~loc ~arity:1 tFun
39283922
| _ -> Ast_helper.Typ.var ~loc:var.loc var.txt)
39293923
| _ -> assert false)
39303924
| _ -> parseTypExpr p
@@ -4252,7 +4246,7 @@ and parseEs6ArrowType ~attrs p =
42524246
let endPos = p.prevEndPos in
42534247
let returnTypeArity =
42544248
match parameters with
4255-
| _ when p.uncurried_config |> Res_uncurried.isDefault -> 0
4249+
| _ when p.uncurried_config <> Legacy -> 0
42564250
| _ ->
42574251
if parameters |> List.exists (function {dotted; typ = _} -> dotted)
42584252
then 0
@@ -4266,11 +4260,7 @@ and parseEs6ArrowType ~attrs p =
42664260
let uncurried =
42674261
p.uncurried_config |> Res_uncurried.fromDotted ~dotted
42684262
in
4269-
if
4270-
uncurried
4271-
&& (paramNum = 1
4272-
|| not (p.uncurried_config |> Res_uncurried.isDefault))
4273-
then
4263+
if uncurried && (paramNum = 1 || p.uncurried_config = Legacy) then
42744264
let loc = mkLoc startPos endPos in
42754265
let tArg = Ast_helper.Typ.arrow ~loc ~attrs argLbl typ t in
42764266
(paramNum - 1, Ast_uncurried.uncurriedType ~loc ~arity tArg, 1)
@@ -4335,9 +4325,8 @@ and parseArrowTypeRest ~es6Arrow ~startPos typ p =
43354325
let returnType = parseTypExpr ~alias:false p in
43364326
let loc = mkLoc startPos p.prevEndPos in
43374327
let arrowTyp = Ast_helper.Typ.arrow ~loc Asttypes.Nolabel typ returnType in
4338-
if p.uncurried_config |> Res_uncurried.isDefault then
4339-
Ast_uncurried.uncurriedType ~loc ~arity:1 arrowTyp
4340-
else arrowTyp
4328+
if p.uncurried_config = Legacy then arrowTyp
4329+
else Ast_uncurried.uncurriedType ~loc ~arity:1 arrowTyp
43414330
| _ -> typ
43424331

43434332
and parseTypExprRegion p =
@@ -6409,13 +6398,12 @@ and parseStandaloneAttribute p =
64096398
let attrId = parseAttributeId ~startPos p in
64106399
let attrId =
64116400
match attrId.txt with
6412-
| "uncurried" ->
6413-
p.uncurried_config <- Res_uncurried.Default;
6401+
| "uncurried.swap" ->
6402+
p.uncurried_config <- Config.Swap;
64146403
attrId
6415-
| "uncurriedAlways" ->
6416-
p.uncurried_config <- Res_uncurried.Always;
6404+
| "uncurried" ->
6405+
p.uncurried_config <- Config.Uncurried;
64176406
attrId
6418-
| "toUncurried" -> {attrId with txt = "uncurried"}
64196407
| _ -> attrId
64206408
in
64216409
let payload = parsePayload p in

0 commit comments

Comments
 (0)