Skip to content

Commit b5a7309

Browse files
committed
AST cleanup: use inline record for Pexp_fun.
1 parent ad568e0 commit b5a7309

27 files changed

+151
-130
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- AST cleanup: store arity in function type. https://github.com/rescript-lang/rescript/pull/7195
4141
- AST cleanup: remove explicit uses of `function$` in preparation for removing the type entirely. https://github.com/rescript-lang/rescript/pull/7206
4242
- AST cleanup: remove `function$` entirely. https://github.com/rescript-lang/rescript/pull/7208
43+
- AST cleanup: use inline record for Pexp_fun.
4344

4445
# 12.0.0-alpha.5
4546

analysis/src/CompletionFrontEnd.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,8 @@ 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
1322+
{arg_label = lbl; default = defaultExpOpt; lhs = pat; rhs = e} ->
13221323
let oldScope = !scope in
13231324
(match (!processingFun, !currentCtxPath) with
13241325
| None, Some ctxPath -> processingFun := Some (ctxPath, 0)

analysis/src/DumpAst.ml

+1-1
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_label = arg; lhs = pattern; rhs = nextExpr} ->
217217
"Pexp_fun(\n"
218218
^ addIndentation (indentation + 1)
219219
^ "arg: "

analysis/src/Hint.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let inlay ~path ~pos ~maxLength ~debug =
6161
| Pexp_apply _ | Pexp_match _ | Pexp_construct _ | Pexp_ifthenelse _
6262
| Pexp_array _ | Pexp_ident _ | Pexp_try _ | Pexp_lazy _
6363
| Pexp_send _ | Pexp_field _ | Pexp_open _
64-
| Pexp_fun (_, _, _, _, Some _) );
64+
| Pexp_fun {arity = Some _} );
6565
};
6666
} ->
6767
push vb.pvb_pat.ppat_loc Type

analysis/src/Xform.ml

+3-3
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 {rhs = bodyExpr}
265265
when Loc.hasPos ~pos bodyExpr.pexp_loc
266266
&& isBracedExpr bodyExpr = false
267267
&& isFunction bodyExpr = false ->
@@ -303,9 +303,9 @@ 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 {arg_label; lhs = pat; rhs = e} ->
307307
let isUnlabeledOnlyArg =
308-
argNum = 1 && argLabel = Nolabel
308+
argNum = 1 && arg_label = Nolabel
309309
&&
310310
match e.pexp_desc with
311311
| Pexp_fun _ -> false

compiler/frontend/ast_compatible.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ let fun_ ?(loc = default_loc) ?(attrs = []) ~arity pat exp =
6868
{
6969
pexp_loc = loc;
7070
pexp_attributes = attrs;
71-
pexp_desc = Pexp_fun (Nolabel, None, pat, exp, arity);
71+
pexp_desc =
72+
Pexp_fun
73+
{arg_label = Nolabel; default = None; lhs = pat; rhs = exp; arity};
7274
}
7375

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

compiler/frontend/ast_pat.ml

+2-2
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 {rhs = 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 {arg_label = l; rhs = e} -> l :: labels_of_fun e
4949
| _ -> []
5050

5151
let rec is_single_variable_pattern_conservative (p : t) =

compiler/frontend/ast_uncurry_gen.ml

+1-1
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; lhs = arg; rhs = 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))

compiler/frontend/bs_ast_mapper.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ module E = struct
315315
sub vbs)
316316
(sub.expr sub e)
317317
(* #end *)
318-
| Pexp_fun (lab, def, p, e, arity) ->
318+
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity} ->
319319
fun_ ~loc ~attrs ~arity lab
320320
(map_opt (sub.expr sub) def)
321321
(sub.pat sub p) (sub.expr sub e)

compiler/frontend/bs_builtin_ppx.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
115115
let body = Ast_async.add_async_attribute ~async body in
116116
let res = self.expr self body in
117117
{e with pexp_desc = Pexp_newtype (s, res)}
118-
| Pexp_fun (label, _, pat, body, _arity) -> (
118+
| Pexp_fun {arg_label = label; lhs = pat; rhs = body} -> (
119119
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
120120
match Ast_attributes.process_attributes_rev e.pexp_attributes with
121121
| Nothing, _ ->
@@ -579,7 +579,7 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
579579
| Pexp_ifthenelse (_, then_expr, Some else_expr) ->
580580
aux then_expr @ aux else_expr
581581
| Pexp_construct (_, Some expr) -> aux expr
582-
| Pexp_fun (_, _, _, expr, _) | Pexp_newtype (_, expr) -> aux expr
582+
| Pexp_fun {rhs = expr} | Pexp_newtype (_, expr) -> aux expr
583583
| _ -> acc
584584
in
585585
aux pvb_expr @ spelunk_vbs acc tl

compiler/ml/ast_async.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ let add_async_attribute ~async (body : Parsetree.expression) =
4040

4141
let rec add_promise_to_result ~loc (e : Parsetree.expression) =
4242
match e.pexp_desc with
43-
| Pexp_fun (label, eo, pat, body, arity) ->
44-
let body = add_promise_to_result ~loc body in
45-
{e with pexp_desc = Pexp_fun (label, eo, pat, body, arity)}
43+
| Pexp_fun f ->
44+
let rhs = add_promise_to_result ~loc f.rhs in
45+
{e with pexp_desc = Pexp_fun {f with rhs}}
4646
| _ -> add_promise_type ~loc ~async:true e
4747

4848
let make_function_async ~async (e : Parsetree.expression) =
4949
if async then
5050
match e.pexp_desc with
51-
| Pexp_fun (_, _, {ppat_loc}, _, _) -> add_promise_to_result ~loc:ppat_loc e
51+
| Pexp_fun {lhs = {ppat_loc}} -> add_promise_to_result ~loc:ppat_loc e
5252
| _ -> assert false
5353
else e

compiler/ml/ast_helper.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ module Exp = struct
152152
let constant ?loc ?attrs a = mk ?loc ?attrs (Pexp_constant a)
153153
let let_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_let (a, b, c))
154154
let fun_ ?loc ?attrs ~arity a b c d =
155-
mk ?loc ?attrs (Pexp_fun (a, b, c, d, arity))
155+
mk ?loc ?attrs
156+
(Pexp_fun {arg_label = a; default = b; lhs = c; rhs = d; arity})
156157
let apply ?loc ?attrs a b = mk ?loc ?attrs (Pexp_apply (a, b))
157158
let match_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_match (a, b))
158159
let try_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_try (a, b))

compiler/ml/ast_iterator.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ module E = struct
282282
| Pexp_let (_r, vbs, e) ->
283283
List.iter (sub.value_binding sub) vbs;
284284
sub.expr sub e
285-
| Pexp_fun (_lab, def, p, e, _) ->
285+
| Pexp_fun {default = def; lhs = p; rhs = e} ->
286286
iter_opt (sub.expr sub) def;
287287
sub.pat sub p;
288288
sub.expr sub e

compiler/ml/ast_mapper.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ module E = struct
278278
| Pexp_constant x -> constant ~loc ~attrs x
279279
| Pexp_let (r, vbs, e) ->
280280
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs) (sub.expr sub e)
281-
| Pexp_fun (lab, def, p, e, arity) ->
281+
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity} ->
282282
fun_ ~loc ~attrs ~arity lab
283283
(map_opt (sub.expr sub) def)
284284
(sub.pat sub p) (sub.expr sub e)

compiler/ml/ast_mapper_from0.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ module E = struct
338338
| [] -> assert false
339339
in
340340
match arg1 with
341-
| Some ({pexp_desc = Pexp_fun (l, eo, p, e, _)} as e1) ->
342-
let arity = attributes_to_arity attrs in
343-
{e1 with pexp_desc = Pexp_fun (l, eo, p, e, Some arity)}
341+
| Some ({pexp_desc = Pexp_fun f} as e1) ->
342+
let arity = Some (attributes_to_arity attrs) in
343+
{e1 with pexp_desc = Pexp_fun {f with arity}}
344344
| _ -> exp1)
345345
| _ -> exp1)
346346
| Pexp_variant (lab, eo) ->

compiler/ml/ast_mapper_to0.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ module E = struct
295295
| Pexp_constant x -> constant ~loc ~attrs (map_constant x)
296296
| Pexp_let (r, vbs, e) ->
297297
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs) (sub.expr sub e)
298-
| Pexp_fun (lab, def, p, e, arity) -> (
298+
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity} -> (
299299
let e =
300300
fun_ ~loc ~attrs lab
301301
(map_opt (sub.expr sub) def)

compiler/ml/ast_uncurried.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ let uncurried_type ~arity (t_arg : Parsetree.core_type) =
99
let uncurried_fun ~arity fun_expr =
1010
let fun_expr =
1111
match fun_expr.Parsetree.pexp_desc with
12-
| Pexp_fun (l, eo, p, e, _) ->
13-
{fun_expr with pexp_desc = Pexp_fun (l, eo, p, e, Some arity)}
12+
| Pexp_fun f ->
13+
{fun_expr with pexp_desc = Pexp_fun {f with arity = Some arity}}
1414
| _ -> assert false
1515
in
1616
fun_expr
1717

1818
let expr_is_uncurried_fun (expr : Parsetree.expression) =
1919
match expr.pexp_desc with
20-
| Pexp_fun (_, _, _, _, Some _) -> true
20+
| Pexp_fun {arity = Some _} -> true
2121
| _ -> false
2222

2323
let expr_extract_uncurried_fun (expr : Parsetree.expression) =
2424
match expr.pexp_desc with
25-
| Pexp_fun (_, _, _, _, Some _) -> expr
25+
| Pexp_fun {arity = Some _} -> expr
2626
| _ -> assert false

compiler/ml/depend.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ let rec add_expr bv exp =
218218
| Pexp_let (rf, pel, e) ->
219219
let bv = add_bindings rf bv pel in
220220
add_expr bv e
221-
| Pexp_fun (_, opte, p, e, _) ->
221+
| Pexp_fun {default = opte; lhs = p; rhs = e} ->
222222
add_opt add_expr bv opte;
223223
add_expr (add_pattern bv p) e
224224
| Pexp_apply (e, el) ->

compiler/ml/parsetree.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,13 @@ and expression_desc =
224224
(* let P1 = E1 and ... and Pn = EN in E (flag = Nonrecursive)
225225
let rec P1 = E1 and ... and Pn = EN in E (flag = Recursive)
226226
*)
227-
| Pexp_fun of arg_label * expression option * pattern * expression * arity
227+
| Pexp_fun of {
228+
arg_label: arg_label;
229+
default: expression option;
230+
lhs: pattern;
231+
rhs: expression;
232+
arity: arity;
233+
}
228234
(* fun P -> E1 (Simple, None)
229235
fun ~l:P -> E1 (Labelled l, None)
230236
fun ?l:P -> E1 (Optional l, None)

compiler/ml/pprintast.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ and expression ctxt f x =
543543
| Pexp_let _ | Pexp_letmodule _ | Pexp_open _ | Pexp_letexception _
544544
when ctxt.semi ->
545545
paren true (expression reset_ctxt) f x
546-
| Pexp_fun (l, e0, p, e, arity) ->
546+
| Pexp_fun {arg_label=l; default= e0; lhs= p; rhs= e; arity} ->
547547
let arity_str = match arity with
548548
| None -> ""
549549
| Some arity -> "[arity:" ^ string_of_int arity ^ "]"
@@ -951,7 +951,7 @@ and binding ctxt f {pvb_pat=p; pvb_expr=x; _} =
951951
let rec pp_print_pexp_function f x =
952952
if x.pexp_attributes <> [] then pp f "=@;%a" (expression ctxt) x
953953
else match x.pexp_desc with
954-
| Pexp_fun (label, eo, p, e, arity) ->
954+
| Pexp_fun {arg_label=label; default= eo; lhs= p; rhs= e; arity} ->
955955
let arity_str = match arity with
956956
| None -> ""
957957
| Some arity -> "[arity:" ^ string_of_int arity ^ "]"

compiler/ml/printast.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ and expression i ppf x =
238238
line i ppf "Pexp_let %a\n" fmt_rec_flag rf;
239239
list i value_binding ppf l;
240240
expression i ppf e
241-
| Pexp_fun (l, eo, p, e, arity) ->
241+
| Pexp_fun {arg_label = l; default = eo; lhs = p; rhs = e; arity} ->
242242
line i ppf "Pexp_fun\n";
243243
let () =
244244
match arity with

compiler/ml/typecore.ml

+5-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ let iter_expression f e =
136136
| Pexp_extension _ (* we don't iterate under extension point *)
137137
| Pexp_ident _ | Pexp_new _ | Pexp_constant _ ->
138138
()
139-
| Pexp_fun (_, eo, _, e, _) ->
139+
| Pexp_fun {default = eo; rhs = e} ->
140140
may expr eo;
141141
expr e
142142
| Pexp_apply (e, lel) ->
@@ -1905,7 +1905,7 @@ let rec approx_type env sty =
19051905
let rec type_approx env sexp =
19061906
match sexp.pexp_desc with
19071907
| Pexp_let (_, _, e) -> type_approx env e
1908-
| Pexp_fun (p, _, _, e, arity) ->
1908+
| Pexp_fun {arg_label = p; rhs = e; arity} ->
19091909
let ty = if is_optional p then type_option (newvar ()) else newvar () in
19101910
newty (Tarrow (p, ty, type_approx env e, Cok, arity))
19111911
| Pexp_match (_, {pc_rhs = e} :: _) -> type_approx env e
@@ -2363,7 +2363,8 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
23632363
exp_attributes = sexp.pexp_attributes;
23642364
exp_env = env;
23652365
}
2366-
| Pexp_fun (l, Some default, spat, sbody, arity) ->
2366+
| Pexp_fun
2367+
{arg_label = l; default = Some default; lhs = spat; rhs = sbody; arity} ->
23672368
assert (is_optional l);
23682369
(* default allowed only with optional argument *)
23692370
let open Ast_helper in
@@ -2403,7 +2404,7 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
24032404
in
24042405
type_function ?in_function ~arity loc sexp.pexp_attributes env ty_expected l
24052406
[Exp.case pat body]
2406-
| Pexp_fun (l, None, spat, sbody, arity) ->
2407+
| Pexp_fun {arg_label = l; default = None; lhs = spat; rhs = sbody; arity} ->
24072408
type_function ?in_function ~arity loc sexp.pexp_attributes env ty_expected l
24082409
[Ast_helper.Exp.case spat sbody]
24092410
| Pexp_apply (sfunct, sargs) ->

0 commit comments

Comments
 (0)