Skip to content

Commit f22be9c

Browse files
committed
Implement handle uncurried in deriving accessors
1 parent c03abaa commit f22be9c

9 files changed

+111
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var p = require("child_process");
2+
var assert = require("assert");
3+
4+
var rescript_exe = require("../../../scripts/bin_path").rescript_exe;
5+
var o = p.spawnSync(rescript_exe, {
6+
encoding: "utf8",
7+
cwd: __dirname,
8+
});
9+
10+
if (o.status !== 0) {
11+
assert.fail(
12+
"Deriving accessors should work in both curried and uncurried mode \n" +
13+
o.stdout
14+
);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "uncurried",
3+
"version": "0.1.0",
4+
"sources": {
5+
"dir": "src",
6+
"subdirs": true
7+
},
8+
"uncurried": false
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@deriving(accessors)
2+
type myRecord = { myField: int }
3+
4+
//Asserts the correct signature for derived accessor
5+
let _myFieldAlias: myRecord => int = myField
6+
7+
//Asserts that inference works when composing
8+
//with derived functions
9+
let compose = (a, accessor) => accessor(a)
10+
let _composedNum = compose({ myField: 1 }, myField)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@@uncurried
2+
@deriving(accessors)
3+
type myRecord = { myField: int }
4+
5+
//Asserts the correct signature for derived accessor
6+
let _myFieldAlias: myRecord => int = myField
7+
8+
//Asserts that inference works when composing
9+
//with derived functions
10+
let compose = (a, accessor) => accessor(a)
11+
let _composedNum = compose({ myField: 1 }, myField)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@deriving(accessors)
2+
type variant = | Num(int)
3+
4+
//Asserts the correct signature for derived accessor
5+
let _numAlias: int => variant = num
6+
7+
//Asserts that inference works when composing
8+
//with derived functions
9+
let compose = (a, accessor) => accessor(a)
10+
let _composedNum = compose(1, num)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@deriving(accessors)
2+
type variant = | DoubleNum(int, int)
3+
4+
//Asserts the correct signature for derived accessor
5+
let _numAlias: (int, int) => variant = doubleNum
6+
7+
//Asserts that inference works when composing
8+
//with derived functions
9+
let compose = (a, b, accessor) => accessor(a, b)
10+
let _composedNum = compose(1, 2, doubleNum)
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@@uncurried
2+
@deriving(accessors)
3+
type variant = | Num(int)
4+
5+
//Asserts the correct signature for derived accessor
6+
let _numAlias: int => variant = num
7+
8+
//Asserts that inference works when composing
9+
//with derived functions
10+
let compose = (a, accessor) => accessor(a)
11+
let _composedNum = compose(1, num)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@@uncurried
2+
@deriving(accessors)
3+
type variant = | DoubleNum(int, int)
4+
5+
//Asserts the correct signature for derived accessor
6+
let _numAlias: (int, int) => variant = doubleNum
7+
8+
//Asserts that inference works when composing
9+
//with derived functions
10+
let compose = (a, b, accessor) => accessor(a, b)
11+
let _composedNum = compose(1, 2, doubleNum)
12+

jscomp/frontend/ast_derive_projector.ml

+22-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ let init () =
1414
{
1515
structure_gen =
1616
(fun (tdcls : tdcls) _explict_nonrec ->
17+
let handle_uncurried_fn_tranform ~loc ~arity fn =
18+
match Config.uncurried.contents with
19+
| Uncurried -> Ast_uncurried.uncurriedFun ~loc ~arity fn
20+
| Legacy | Swap -> fn
21+
in
1722
let handle_tdcl tdcl =
1823
let core_type =
1924
Ast_derive_util.core_type_of_type_declaration tdcl
@@ -39,7 +44,9 @@ let init () =
3944
(Pat.constraint_ (Pat.var {txt; loc}) core_type)
4045
(Exp.field
4146
(Exp.ident {txt = Lident txt; loc})
42-
{txt = Longident.Lident pld_label; loc})))
47+
{txt = Longident.Lident pld_label; loc})
48+
(*arity will alwys be 1 since these are single param functions*)
49+
|> handle_uncurried_fn_tranform ~arity:1 ~loc))
4350
| Ptype_variant constructor_declarations ->
4451
Ext_list.map constructor_declarations
4552
(fun
@@ -94,7 +101,8 @@ let init () =
94101
annotate_type
95102
in
96103
Ext_list.fold_right vars exp (fun var b ->
97-
Ast_compatible.fun_ (Pat.var {loc; txt = var}) b)))
104+
Ast_compatible.fun_ (Pat.var {loc; txt = var}) b)
105+
|> handle_uncurried_fn_tranform ~loc ~arity))
98106
| Ptype_abstract | Ptype_open ->
99107
Ast_derive_util.notApplicable tdcl.ptype_loc derivingName;
100108
[]
@@ -103,6 +111,11 @@ let init () =
103111
Ext_list.flat_map tdcls handle_tdcl);
104112
signature_gen =
105113
(fun (tdcls : Parsetree.type_declaration list) _explict_nonrec ->
114+
let handle_uncurried_type_tranform ~loc ~arity t =
115+
match Config.uncurried.contents with
116+
| Uncurried -> Ast_uncurried.uncurriedType ~loc ~arity t
117+
| Legacy | Swap -> t
118+
in
106119
let handle_tdcl tdcl =
107120
let core_type =
108121
Ast_derive_util.core_type_of_type_declaration tdcl
@@ -119,7 +132,10 @@ let init () =
119132
| Ptype_record label_declarations ->
120133
Ext_list.map label_declarations (fun {pld_name; pld_type} ->
121134
Ast_comb.single_non_rec_val ?attrs:gentype_attrs pld_name
122-
(Ast_compatible.arrow core_type pld_type))
135+
(Ast_compatible.arrow core_type pld_type
136+
(*arity will alwys be 1 since these are single param functions*)
137+
|> handle_uncurried_type_tranform ~arity:1
138+
~loc:pld_name.loc))
123139
| Ptype_variant constructor_declarations ->
124140
Ext_list.map constructor_declarations
125141
(fun
@@ -135,6 +151,7 @@ let init () =
135151
| Pcstr_tuple pcd_args -> pcd_args
136152
| Pcstr_record _ -> assert false
137153
in
154+
let arity = pcd_args |> List.length in
138155
let annotate_type =
139156
match pcd_res with
140157
| Some x -> x
@@ -143,7 +160,8 @@ let init () =
143160
Ast_comb.single_non_rec_val ?attrs:gentype_attrs
144161
{loc; txt = Ext_string.uncapitalize_ascii con_name}
145162
(Ext_list.fold_right pcd_args annotate_type (fun x acc ->
146-
Ast_compatible.arrow x acc)))
163+
Ast_compatible.arrow x acc)
164+
|> handle_uncurried_type_tranform ~arity ~loc))
147165
| Ptype_open | Ptype_abstract ->
148166
Ast_derive_util.notApplicable tdcl.ptype_loc derivingName;
149167
[]

0 commit comments

Comments
 (0)