Skip to content

Commit ba78715

Browse files
authored
Represent the arity of uncurried function definitions directly in the… (#7197)
* Represent the arity of uncurried function definitions directly in the AST. * Use uncurried type for `@deriving(jsConverter)` This goes towards making all arities explicit. * Clean up arity in derive projector. The arity is later overridden anyway, but being explicit can help cleaning up later on if `Function$` is removed entirely. * Most of Untypeast is never used. * More explicit translation for `Function$`. Cosmetic change that will be needed later to remove `Function$`. * Update CHANGELOG.md
1 parent 55f12e0 commit ba78715

File tree

104 files changed

+1643
-2229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1643
-2229
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Ast cleanup: remove exp object and exp unreachable. https://github.com/rescript-lang/rescript/pull/7189
2424
- Ast cleanup: explicit representation for optional record fields in types. https://github.com/rescript-lang/rescript/pull/7190 https://github.com/rescript-lang/rescript/pull/7191
2525
- AST cleanup: first-class expression and patterns for records with optional fields. https://github.com/rescript-lang/rescript/pull/7192
26+
- AST cleanup: Represent the arity of uncurried function definitions directly in the AST. https://github.com/rescript-lang/rescript/pull/7197
2627

2728

2829
# 12.0.0-alpha.5

analysis/src/CompletionFrontEnd.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
13181318
match exprToContextPath lhs with
13191319
| Some contextPath -> setResult (Cpath (CPObj (contextPath, label)))
13201320
| None -> ())
1321-
| Pexp_fun (lbl, defaultExpOpt, pat, e) ->
1321+
| Pexp_fun (lbl, defaultExpOpt, pat, e, _) ->
13221322
let oldScope = !scope in
13231323
(match (!processingFun, !currentCtxPath) with
13241324
| None, Some ctxPath -> processingFun := Some (ctxPath, 0)

analysis/src/DumpAst.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ and printExprItem expr ~pos ~indentation =
213213
| None -> ""
214214
| Some expr -> "," ^ printExprItem expr ~pos ~indentation)
215215
^ ")"
216-
| Pexp_fun (arg, _maybeDefaultArgExpr, pattern, nextExpr) ->
216+
| Pexp_fun (arg, _maybeDefaultArgExpr, pattern, nextExpr, _) ->
217217
"Pexp_fun(\n"
218218
^ addIndentation (indentation + 1)
219219
^ "arg: "

analysis/src/Xform.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ module AddBracesToFn = struct
261261
| _ -> false
262262
in
263263
(match e.pexp_desc with
264-
| Pexp_fun (_, _, _, bodyExpr)
264+
| Pexp_fun (_, _, _, bodyExpr, _)
265265
when Loc.hasPos ~pos bodyExpr.pexp_loc
266266
&& isBracedExpr bodyExpr = false
267267
&& isFunction bodyExpr = false ->
@@ -303,10 +303,10 @@ module AddTypeAnnotation = struct
303303
in
304304
let rec processFunction ~argNum (e : Parsetree.expression) =
305305
match e.pexp_desc with
306-
| Pexp_fun (argLabel, _, pat, e)
306+
| Pexp_fun (argLabel, _, pat, e, _)
307307
| Pexp_construct
308308
( {txt = Lident "Function$"},
309-
Some {pexp_desc = Pexp_fun (argLabel, _, pat, e)} ) ->
309+
Some {pexp_desc = Pexp_fun (argLabel, _, pat, e, _)} ) ->
310310
let isUnlabeledOnlyArg =
311311
argNum = 1 && argLabel = Nolabel
312312
&&

compiler/frontend/ast_compatible.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ let app3 ?(loc = default_loc) ?(attrs = []) fn arg1 arg2 arg3 : expression =
6363
Pexp_apply (fn, [(Nolabel, arg1); (Nolabel, arg2); (Nolabel, arg3)]);
6464
}
6565

66-
let fun_ ?(loc = default_loc) ?(attrs = []) pat exp =
66+
let fun_ ?(loc = default_loc) ?(attrs = []) ~arity pat exp =
6767
{
6868
pexp_loc = loc;
6969
pexp_attributes = attrs;
70-
pexp_desc = Pexp_fun (Nolabel, None, pat, exp);
70+
pexp_desc = Pexp_fun (Nolabel, None, pat, exp, arity);
7171
}
7272

7373
let const_exp_string ?(loc = default_loc) ?(attrs = []) ?delimiter (s : string)

compiler/frontend/ast_compatible.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ val apply_labels :
7272
*)
7373

7474
val fun_ :
75-
?loc:Location.t -> ?attrs:attrs -> pattern -> expression -> expression
75+
?loc:Location.t ->
76+
?attrs:attrs ->
77+
arity:int option ->
78+
pattern ->
79+
expression ->
80+
expression
7681

7782
(* val opt_label : string -> Asttypes.arg_label *)
7883

compiler/frontend/ast_derive_js_mapper.ml

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ let app1 = Ast_compatible.app1
128128

129129
let app2 = Ast_compatible.app2
130130

131-
let ( ->~ ) a b = Ast_compatible.arrow a b
131+
let ( ->~ ) a b =
132+
Ast_uncurried.uncurried_type ~loc:Location.none ~arity:1
133+
(Ast_compatible.arrow a b)
132134

133135
let raise_when_not_found_ident =
134136
Longident.Ldot (Lident Primitive_modules.util, "raiseWhenNotFound")
@@ -167,9 +169,10 @@ let init () =
167169
in
168170
let to_js_body body =
169171
Ast_comb.single_non_rec_value pat_to_js
170-
(Ast_compatible.fun_
171-
(Pat.constraint_ (Pat.var pat_param) core_type)
172-
body)
172+
(Ast_uncurried.uncurried_fun ~loc:Location.none ~arity:1
173+
(Ast_compatible.fun_ ~arity:None
174+
(Pat.constraint_ (Pat.var pat_param) core_type)
175+
body))
173176
in
174177
let ( +> ) a ty = Exp.constraint_ (erase_type a) ty in
175178
let ( +: ) a ty = erase_type (Exp.constraint_ a ty) in
@@ -211,12 +214,16 @@ let init () =
211214
in
212215
let from_js =
213216
Ast_comb.single_non_rec_value pat_from_js
214-
(Ast_compatible.fun_ (Pat.var pat_param)
215-
(if create_type then
216-
Exp.let_ Nonrecursive
217-
[Vb.mk (Pat.var pat_param) (exp_param +: new_type)]
218-
(Exp.constraint_ obj_exp core_type)
219-
else Exp.constraint_ obj_exp core_type))
217+
(Ast_uncurried.uncurried_fun ~loc:Location.none ~arity:1
218+
(Ast_compatible.fun_ ~arity:(Some 1) (Pat.var pat_param)
219+
(if create_type then
220+
Exp.let_ Nonrecursive
221+
[
222+
Vb.mk (Pat.var pat_param)
223+
(exp_param +: new_type);
224+
]
225+
(Exp.constraint_ obj_exp core_type)
226+
else Exp.constraint_ obj_exp core_type)))
220227
in
221228
let rest = [to_js; from_js] in
222229
if create_type then erase_type_str :: new_type_str :: rest
@@ -253,12 +260,14 @@ let init () =
253260
app2 unsafe_index_get_exp exp_map exp_param
254261
else app1 erase_type_exp exp_param);
255262
Ast_comb.single_non_rec_value pat_from_js
256-
(Ast_compatible.fun_ (Pat.var pat_param)
257-
(let result =
258-
app2 unsafe_index_get_exp rev_exp_map exp_param
259-
in
260-
if create_type then raise_when_not_found result
261-
else result));
263+
(Ast_uncurried.uncurried_fun ~loc:Location.none ~arity:1
264+
(Ast_compatible.fun_ ~arity:(Some 1)
265+
(Pat.var pat_param)
266+
(let result =
267+
app2 unsafe_index_get_exp rev_exp_map exp_param
268+
in
269+
if create_type then raise_when_not_found result
270+
else result)));
262271
]
263272
in
264273
if create_type then new_type_str :: v else v
@@ -285,7 +294,8 @@ let init () =
285294
let pat_from_js = {Asttypes.loc; txt = from_js} in
286295
let to_js_type result =
287296
Ast_comb.single_non_rec_val pat_to_js
288-
(Ast_compatible.arrow core_type result)
297+
(Ast_uncurried.uncurried_type ~loc:Location.none ~arity:1
298+
(Ast_compatible.arrow core_type result))
289299
in
290300
let new_type, new_tdcl =
291301
U.new_type_of_type_declaration tdcl ("abs_" ^ name)

compiler/frontend/ast_derive_projector.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let init () =
4646
->
4747
let txt = "param" in
4848
Ast_comb.single_non_rec_value ?attrs:gentype_attrs pld_name
49-
(Ast_compatible.fun_
49+
(Ast_compatible.fun_ ~arity:(Some 1)
5050
(Pat.constraint_ (Pat.var {txt; loc}) core_type)
5151
(Exp.field
5252
(Exp.ident {txt = Lident txt; loc})
@@ -108,7 +108,9 @@ let init () =
108108
annotate_type
109109
in
110110
Ext_list.fold_right vars exp (fun var b ->
111-
Ast_compatible.fun_ (Pat.var {loc; txt = var}) b)
111+
Ast_compatible.fun_ ~arity:(Some 1)
112+
(Pat.var {loc; txt = var})
113+
b)
112114
|> handle_uncurried_accessor_tranform ~loc ~arity))
113115
| Ptype_abstract | Ptype_open ->
114116
Ast_derive_util.not_applicable tdcl.ptype_loc deriving_name;

compiler/frontend/ast_pat.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let is_unit_cont ~yes ~no (p : t) =
3535
let arity_of_fun (pat : Parsetree.pattern) (e : Parsetree.expression) =
3636
let rec aux (e : Parsetree.expression) =
3737
match e.pexp_desc with
38-
| Pexp_fun (_, _, _, e) -> 1 + aux e (*FIXME error on optional*)
38+
| Pexp_fun (_, _, _, e, _) -> 1 + aux e (*FIXME error on optional*)
3939
(* | Pexp_fun _
4040
-> Location.raise_errorf
4141
~loc:e.pexp_loc "Label is not allowed in JS object" *)
@@ -45,7 +45,7 @@ let arity_of_fun (pat : Parsetree.pattern) (e : Parsetree.expression) =
4545

4646
let rec labels_of_fun (e : Parsetree.expression) =
4747
match e.pexp_desc with
48-
| Pexp_fun (l, _, _, e) -> l :: labels_of_fun e
48+
| Pexp_fun (l, _, _, e, _) -> l :: labels_of_fun e
4949
| _ -> []
5050

5151
let rec is_single_variable_pattern_conservative (p : t) =

compiler/frontend/ast_uncurry_gen.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
3636
match Ast_attributes.process_attributes_rev body.pexp_attributes with
3737
| Nothing, attrs -> (
3838
match body.pexp_desc with
39-
| Pexp_fun (arg_label, _, arg, body) ->
39+
| Pexp_fun (arg_label, _, arg, body, _) ->
4040
Bs_syntaxerr.optional_err loc arg_label;
4141
aux ((arg_label, self.pat self arg, attrs) :: acc) body
4242
| _ -> (self.expr self body, acc))
@@ -45,7 +45,7 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
4545
let result, rev_extra_args = aux [(label, self_pat, [])] body in
4646
let body =
4747
Ext_list.fold_left rev_extra_args result (fun e (label, p, attrs) ->
48-
Ast_helper.Exp.fun_ ~loc ~attrs label None p e)
48+
Ast_helper.Exp.fun_ ~loc ~attrs ~arity:None label None p e)
4949
in
5050
let arity = List.length rev_extra_args in
5151
let arity_s = string_of_int arity in

0 commit comments

Comments
 (0)