Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST cleanup: use inline record for Pexp_fun. #7213

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
compiler/js_parser/**
compiler/ml/cmt_format.ml
compiler/ml/pprintast.ml
compiler/core/js_name_of_module_id.ml
compiler/core/js_pass_debug.ml
compiler/core/lam_util.ml
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

# 12.0.0-alpha.7 (Unreleased)

#### :house: Internal

- AST cleanup: use inline record for Pexp_fun. https://github.com/rescript-lang/rescript/pull/7213

# 12.0.0-alpha.6

#### :rocket: New Feature
Expand Down
3 changes: 2 additions & 1 deletion analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
match exprToContextPath lhs with
| Some contextPath -> setResult (Cpath (CPObj (contextPath, label)))
| None -> ())
| Pexp_fun (lbl, defaultExpOpt, pat, e, _) ->
| Pexp_fun
{arg_label = lbl; default = defaultExpOpt; lhs = pat; rhs = e} ->
let oldScope = !scope in
(match (!processingFun, !currentCtxPath) with
| None, Some ctxPath -> processingFun := Some (ctxPath, 0)
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/DumpAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ and printExprItem expr ~pos ~indentation =
| None -> ""
| Some expr -> "," ^ printExprItem expr ~pos ~indentation)
^ ")"
| Pexp_fun (arg, _maybeDefaultArgExpr, pattern, nextExpr, _) ->
| Pexp_fun {arg_label = arg; lhs = pattern; rhs = nextExpr} ->
"Pexp_fun(\n"
^ addIndentation (indentation + 1)
^ "arg: "
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/Hint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ let inlay ~path ~pos ~maxLength ~debug =
| Pexp_apply _ | Pexp_match _ | Pexp_construct _ | Pexp_ifthenelse _
| Pexp_array _ | Pexp_ident _ | Pexp_try _ | Pexp_lazy _
| Pexp_send _ | Pexp_field _ | Pexp_open _
| Pexp_fun (_, _, _, _, Some _) );
| Pexp_fun {arity = Some _} );
};
} ->
push vb.pvb_pat.ppat_loc Type
Expand Down
6 changes: 3 additions & 3 deletions analysis/src/Xform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ module AddBracesToFn = struct
| _ -> false
in
(match e.pexp_desc with
| Pexp_fun (_, _, _, bodyExpr, _)
| Pexp_fun {rhs = bodyExpr}
when Loc.hasPos ~pos bodyExpr.pexp_loc
&& isBracedExpr bodyExpr = false
&& isFunction bodyExpr = false ->
Expand Down Expand Up @@ -303,9 +303,9 @@ module AddTypeAnnotation = struct
in
let rec processFunction ~argNum (e : Parsetree.expression) =
match e.pexp_desc with
| Pexp_fun (argLabel, _, pat, e, _) ->
| Pexp_fun {arg_label; lhs = pat; rhs = e} ->
let isUnlabeledOnlyArg =
argNum = 1 && argLabel = Nolabel
argNum = 1 && arg_label = Nolabel
&&
match e.pexp_desc with
| Pexp_fun _ -> false
Expand Down
4 changes: 3 additions & 1 deletion compiler/frontend/ast_compatible.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ let fun_ ?(loc = default_loc) ?(attrs = []) ~arity pat exp =
{
pexp_loc = loc;
pexp_attributes = attrs;
pexp_desc = Pexp_fun (Nolabel, None, pat, exp, arity);
pexp_desc =
Pexp_fun
{arg_label = Nolabel; default = None; lhs = pat; rhs = exp; arity};
}

let const_exp_string ?(loc = default_loc) ?(attrs = []) ?delimiter (s : string)
Expand Down
4 changes: 2 additions & 2 deletions compiler/frontend/ast_pat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let is_unit_cont ~yes ~no (p : t) =
let arity_of_fun (pat : Parsetree.pattern) (e : Parsetree.expression) =
let rec aux (e : Parsetree.expression) =
match e.pexp_desc with
| Pexp_fun (_, _, _, e, _) -> 1 + aux e (*FIXME error on optional*)
| Pexp_fun {rhs = e} -> 1 + aux e (*FIXME error on optional*)
(* | Pexp_fun _
-> Location.raise_errorf
~loc:e.pexp_loc "Label is not allowed in JS object" *)
Expand All @@ -45,7 +45,7 @@ let arity_of_fun (pat : Parsetree.pattern) (e : Parsetree.expression) =

let rec labels_of_fun (e : Parsetree.expression) =
match e.pexp_desc with
| Pexp_fun (l, _, _, e, _) -> l :: labels_of_fun e
| Pexp_fun {arg_label = l; rhs = e} -> l :: labels_of_fun e
| _ -> []

let rec is_single_variable_pattern_conservative (p : t) =
Expand Down
2 changes: 1 addition & 1 deletion compiler/frontend/ast_uncurry_gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
match Ast_attributes.process_attributes_rev body.pexp_attributes with
| Nothing, attrs -> (
match body.pexp_desc with
| Pexp_fun (arg_label, _, arg, body, _) ->
| Pexp_fun {arg_label; lhs = arg; rhs = body} ->
Bs_syntaxerr.optional_err loc arg_label;
aux ((arg_label, self.pat self arg, attrs) :: acc) body
| _ -> (self.expr self body, acc))
Expand Down
2 changes: 1 addition & 1 deletion compiler/frontend/bs_ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ module E = struct
sub vbs)
(sub.expr sub e)
(* #end *)
| Pexp_fun (lab, def, p, e, arity) ->
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity} ->
fun_ ~loc ~attrs ~arity lab
(map_opt (sub.expr sub) def)
(sub.pat sub p) (sub.expr sub e)
Expand Down
4 changes: 2 additions & 2 deletions compiler/frontend/bs_builtin_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
let body = Ast_async.add_async_attribute ~async body in
let res = self.expr self body in
{e with pexp_desc = Pexp_newtype (s, res)}
| Pexp_fun (label, _, pat, body, _arity) -> (
| Pexp_fun {arg_label = label; lhs = pat; rhs = body} -> (
let async = Ast_attributes.has_async_payload e.pexp_attributes <> None in
match Ast_attributes.process_attributes_rev e.pexp_attributes with
| Nothing, _ ->
Expand Down Expand Up @@ -579,7 +579,7 @@ let rec structure_mapper ~await_context (self : mapper) (stru : Ast_structure.t)
| Pexp_ifthenelse (_, then_expr, Some else_expr) ->
aux then_expr @ aux else_expr
| Pexp_construct (_, Some expr) -> aux expr
| Pexp_fun (_, _, _, expr, _) | Pexp_newtype (_, expr) -> aux expr
| Pexp_fun {rhs = expr} | Pexp_newtype (_, expr) -> aux expr
| _ -> acc
in
aux pvb_expr @ spelunk_vbs acc tl
Expand Down
8 changes: 4 additions & 4 deletions compiler/ml/ast_async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ let add_async_attribute ~async (body : Parsetree.expression) =

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

let make_function_async ~async (e : Parsetree.expression) =
if async then
match e.pexp_desc with
| Pexp_fun (_, _, {ppat_loc}, _, _) -> add_promise_to_result ~loc:ppat_loc e
| Pexp_fun {lhs = {ppat_loc}} -> add_promise_to_result ~loc:ppat_loc e
| _ -> assert false
else e
3 changes: 2 additions & 1 deletion compiler/ml/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ module Exp = struct
let constant ?loc ?attrs a = mk ?loc ?attrs (Pexp_constant a)
let let_ ?loc ?attrs a b c = mk ?loc ?attrs (Pexp_let (a, b, c))
let fun_ ?loc ?attrs ~arity a b c d =
mk ?loc ?attrs (Pexp_fun (a, b, c, d, arity))
mk ?loc ?attrs
(Pexp_fun {arg_label = a; default = b; lhs = c; rhs = d; arity})
let apply ?loc ?attrs a b = mk ?loc ?attrs (Pexp_apply (a, b))
let match_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_match (a, b))
let try_ ?loc ?attrs a b = mk ?loc ?attrs (Pexp_try (a, b))
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/ast_iterator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ module E = struct
| Pexp_let (_r, vbs, e) ->
List.iter (sub.value_binding sub) vbs;
sub.expr sub e
| Pexp_fun (_lab, def, p, e, _) ->
| Pexp_fun {default = def; lhs = p; rhs = e} ->
iter_opt (sub.expr sub) def;
sub.pat sub p;
sub.expr sub e
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/ast_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ module E = struct
| Pexp_constant x -> constant ~loc ~attrs x
| Pexp_let (r, vbs, e) ->
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs) (sub.expr sub e)
| Pexp_fun (lab, def, p, e, arity) ->
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity} ->
fun_ ~loc ~attrs ~arity lab
(map_opt (sub.expr sub) def)
(sub.pat sub p) (sub.expr sub e)
Expand Down
6 changes: 3 additions & 3 deletions compiler/ml/ast_mapper_from0.ml
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ module E = struct
| [] -> assert false
in
match arg1 with
| Some ({pexp_desc = Pexp_fun (l, eo, p, e, _)} as e1) ->
let arity = attributes_to_arity attrs in
{e1 with pexp_desc = Pexp_fun (l, eo, p, e, Some arity)}
| Some ({pexp_desc = Pexp_fun f} as e1) ->
let arity = Some (attributes_to_arity attrs) in
{e1 with pexp_desc = Pexp_fun {f with arity}}
| _ -> exp1)
| _ -> exp1)
| Pexp_variant (lab, eo) ->
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/ast_mapper_to0.ml
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ module E = struct
| Pexp_constant x -> constant ~loc ~attrs (map_constant x)
| Pexp_let (r, vbs, e) ->
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs) (sub.expr sub e)
| Pexp_fun (lab, def, p, e, arity) -> (
| Pexp_fun {arg_label = lab; default = def; lhs = p; rhs = e; arity} -> (
let e =
fun_ ~loc ~attrs lab
(map_opt (sub.expr sub) def)
Expand Down
8 changes: 4 additions & 4 deletions compiler/ml/ast_uncurried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ let uncurried_type ~arity (t_arg : Parsetree.core_type) =
let uncurried_fun ~arity fun_expr =
let fun_expr =
match fun_expr.Parsetree.pexp_desc with
| Pexp_fun (l, eo, p, e, _) ->
{fun_expr with pexp_desc = Pexp_fun (l, eo, p, e, Some arity)}
| Pexp_fun f ->
{fun_expr with pexp_desc = Pexp_fun {f with arity = Some arity}}
| _ -> assert false
in
fun_expr

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

let expr_extract_uncurried_fun (expr : Parsetree.expression) =
match expr.pexp_desc with
| Pexp_fun (_, _, _, _, Some _) -> expr
| Pexp_fun {arity = Some _} -> expr
| _ -> assert false
2 changes: 1 addition & 1 deletion compiler/ml/depend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ let rec add_expr bv exp =
| Pexp_let (rf, pel, e) ->
let bv = add_bindings rf bv pel in
add_expr bv e
| Pexp_fun (_, opte, p, e, _) ->
| Pexp_fun {default = opte; lhs = p; rhs = e} ->
add_opt add_expr bv opte;
add_expr (add_pattern bv p) e
| Pexp_apply (e, el) ->
Expand Down
8 changes: 7 additions & 1 deletion compiler/ml/parsetree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ and expression_desc =
(* let P1 = E1 and ... and Pn = EN in E (flag = Nonrecursive)
let rec P1 = E1 and ... and Pn = EN in E (flag = Recursive)
*)
| Pexp_fun of arg_label * expression option * pattern * expression * arity
| Pexp_fun of {
arg_label: arg_label;
default: expression option;
lhs: pattern;
rhs: expression;
arity: arity;
}
(* fun P -> E1 (Simple, None)
fun ~l:P -> E1 (Labelled l, None)
fun ?l:P -> E1 (Optional l, None)
Expand Down
Loading
Loading