Skip to content

Commit a700ed3

Browse files
authored
Use type function_param in Pcl_fun (#2498)
1 parent aa123b2 commit a700ed3

File tree

7 files changed

+37
-51
lines changed

7 files changed

+37
-51
lines changed

lib/Ast.ml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ module Cl = struct
302302
| Pcl_structure {pcstr_fields= _ :: _; _}
303303
|Pcl_let _ | Pcl_open _ | Pcl_extension _ ->
304304
false
305-
| Pcl_apply (e, _) | Pcl_fun (_, _, _, e) -> is_simple e
305+
| Pcl_apply (e, _) | Pcl_fun (_, e) -> is_simple e
306306
| Pcl_constraint (e, t) -> is_simple e && Cty.is_simple t
307307

308308
(** [mem_cls cls cl] holds if [cl] is in the named class of expressions
@@ -1125,7 +1125,7 @@ end = struct
11251125
List.exists l ~f:(fun {pci_expr; _} ->
11261126
let rec loop x =
11271127
match x.pcl_desc with
1128-
| Pcl_fun (_, _, _, x) -> loop x
1128+
| Pcl_fun (_, x) -> loop x
11291129
| Pcl_constraint (_, x) -> x == cty
11301130
| _ -> false
11311131
in
@@ -1151,7 +1151,7 @@ end = struct
11511151
| Cl ctx ->
11521152
assert (
11531153
match ctx.pcl_desc with
1154-
| Pcl_fun (_, _, _, _) -> false
1154+
| Pcl_fun _ -> false
11551155
| Pcl_constr _ -> false
11561156
| Pcl_structure _ -> false
11571157
| Pcl_apply _ -> false
@@ -1196,7 +1196,7 @@ end = struct
11961196
cl == x
11971197
||
11981198
match x.pcl_desc with
1199-
| Pcl_fun (_, _, _, x) -> loop x
1199+
| Pcl_fun (_, x) -> loop x
12001200
| Pcl_constraint (x, _) -> loop x
12011201
| _ -> false
12021202
in
@@ -1213,7 +1213,7 @@ end = struct
12131213
assert (
12141214
match pcl_desc with
12151215
| Pcl_structure _ -> false
1216-
| Pcl_fun (_, _, _, x) -> x == cl
1216+
| Pcl_fun (_, x) -> x == cl
12171217
| Pcl_apply (x, _) -> x == cl
12181218
| Pcl_let (_, x) -> x == cl
12191219
| Pcl_constraint (x, _) -> x == cl
@@ -1316,7 +1316,7 @@ end = struct
13161316
| Cl ctx ->
13171317
assert (
13181318
match ctx.pcl_desc with
1319-
| Pcl_fun (_, _, p, _) -> p == pat
1319+
| Pcl_fun (p, _) -> check_function_param p
13201320
| Pcl_constr _ -> false
13211321
| Pcl_structure {pcstr_self; _} ->
13221322
Option.exists ~f:(fun self_ -> self_ == pat) pcstr_self
@@ -1462,8 +1462,7 @@ end = struct
14621462
| Cl ctx ->
14631463
let rec loop ctx =
14641464
match ctx.pcl_desc with
1465-
| Pcl_fun (_, eopt, _, e) ->
1466-
Option.exists eopt ~f:(fun e -> e == exp) || loop e
1465+
| Pcl_fun (param, e) -> check_function_param param || loop e
14671466
| Pcl_constr _ -> false
14681467
| Pcl_structure _ -> false
14691468
| Pcl_apply (_, l) -> List.exists l ~f:(fun (_, e) -> e == exp)
@@ -2068,7 +2067,7 @@ end = struct
20682067
let exp = snd (List.last_exn args) in
20692068
(not (parenze_exp (sub_exp ~ctx:(Cl cl) exp)))
20702069
&& exposed_right_exp cls exp
2071-
| Pcl_fun (_, _, _, e) ->
2070+
| Pcl_fun (_, e) ->
20722071
(not (parenze_cl (sub_cl ~ctx:(Cl cl) e)))
20732072
&& exposed_right_cl cls e
20742073
| _ -> false

lib/Sugar.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ let cl_fun ?(will_keep_first_ast_node = true) cmts xexp =
5656
let {pcl_desc; pcl_loc; pcl_attributes; _} = exp in
5757
if will_keep_first_ast_node || List.is_empty pcl_attributes then
5858
match pcl_desc with
59-
| Pcl_fun (label, default, pattern, body) ->
60-
let before = pattern.ppat_loc and after = body.pcl_loc in
59+
| Pcl_fun (p, body) ->
60+
let before = p.pparam_loc and after = body.pcl_loc in
6161
if not will_keep_first_ast_node then
6262
Cmts.relocate cmts ~src:pcl_loc ~before ~after ;
6363
let xargs, xbody = fun_ (sub_cl ~ctx body) in
64-
let param = Pparam_val (label, default, pattern) in
65-
(mk_function_param before after param :: xargs, xbody)
64+
(p :: xargs, xbody)
6665
| _ -> ([], xexp)
6766
else ([], xexp)
6867
in

vendor/parser-extended/ast_helper.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ module Cl = struct
280280

281281
let constr ?loc ?attrs a b = mk ?loc ?attrs (Pcl_constr (a, b))
282282
let structure ?loc ?attrs a = mk ?loc ?attrs (Pcl_structure a)
283-
let fun_ ?loc ?attrs a b c d = mk ?loc ?attrs (Pcl_fun (a, b, c, d))
283+
let fun_ ?loc ?attrs a b = mk ?loc ?attrs (Pcl_fun (a, b))
284284
let apply ?loc ?attrs a b = mk ?loc ?attrs (Pcl_apply (a, b))
285285
let let_ ?loc ?attrs a b = mk ?loc ?attrs (Pcl_let (a, b))
286286
let constraint_ ?loc ?attrs a b = mk ?loc ?attrs (Pcl_constraint (a, b))

vendor/parser-extended/ast_mapper.ml

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ let map_value_constraint sub = function
114114
let coercion = sub.typ sub coercion in
115115
Pvc_coercion { ground; coercion }
116116

117+
let map_function_param sub { pparam_loc = loc; pparam_desc = desc } =
118+
let loc = sub.location sub loc in
119+
let desc =
120+
match desc with
121+
| Pparam_val (lab, def, p) ->
122+
Pparam_val
123+
(sub.arg_label sub lab,
124+
map_opt (sub.expr sub) def,
125+
sub.pat sub p)
126+
| Pparam_newtype ty ->
127+
Pparam_newtype (List.map (map_loc sub) ty)
128+
in
129+
{ pparam_loc = loc; pparam_desc = desc }
130+
117131
module Flag = struct
118132
open Asttypes
119133

@@ -477,20 +491,6 @@ end
477491
module E = struct
478492
(* Value expressions for the core language *)
479493

480-
let map_function_param sub { pparam_loc = loc; pparam_desc = desc } =
481-
let loc = sub.location sub loc in
482-
let desc =
483-
match desc with
484-
| Pparam_val (lab, def, p) ->
485-
Pparam_val
486-
(sub.arg_label sub lab,
487-
map_opt (sub.expr sub) def,
488-
sub.pat sub p)
489-
| Pparam_newtype ty ->
490-
Pparam_newtype (List.map (map_loc sub) ty)
491-
in
492-
{ pparam_loc = loc; pparam_desc = desc }
493-
494494
let map_constraint sub c =
495495
match c with
496496
| Pconstraint ty -> Pconstraint (sub.typ sub ty)
@@ -692,11 +692,9 @@ module CE = struct
692692
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tys)
693693
| Pcl_structure s ->
694694
structure ~loc ~attrs (sub.class_structure sub s)
695-
| Pcl_fun (lab, e, p, ce) ->
695+
| Pcl_fun (p, ce) ->
696696
fun_ ~loc ~attrs
697-
(sub.arg_label sub lab)
698-
(map_opt (sub.expr sub) e)
699-
(sub.pat sub p)
697+
(map_function_param sub p)
700698
(sub.class_expr sub ce)
701699
| Pcl_apply (ce, l) ->
702700
apply ~loc ~attrs (sub.class_expr sub ce)

vendor/parser-extended/parser.mly

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,8 +1806,8 @@ class_fun_binding:
18061806
| mkclass(
18071807
COLON class_type EQUAL class_expr
18081808
{ Pcl_constraint($4, $2) }
1809-
| labeled_simple_pattern class_fun_binding
1810-
{ let (l,o,p) = $1 in Pcl_fun(l, o, p, $2) }
1809+
| fun_param class_fun_binding
1810+
{ Pcl_fun($1, $2) }
18111811
) { $1 }
18121812
;
18131813

@@ -1861,9 +1861,9 @@ class_simple_expr:
18611861

18621862
class_fun_def:
18631863
mkclass(
1864-
labeled_simple_pattern MINUSGREATER e = class_expr
1865-
| labeled_simple_pattern e = class_fun_def
1866-
{ let (l,o,p) = $1 in Pcl_fun(l, o, p, e) }
1864+
fun_param MINUSGREATER e = class_expr
1865+
| fun_param e = class_fun_def
1866+
{ Pcl_fun($1, e) }
18671867
) { $1 }
18681868
;
18691869
%inline class_structure:

vendor/parser-extended/parsetree.mli

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -779,20 +779,12 @@ and class_expr_desc =
779779
| Pcl_constr of Longident.t loc * core_type list
780780
(** [c] and [['a1, ..., 'an] c] *)
781781
| Pcl_structure of class_structure (** [object ... end] *)
782-
| Pcl_fun of arg_label * expression option * pattern * class_expr
783-
(** [Pcl_fun(lbl, exp0, P, CE)] represents:
782+
| Pcl_fun of function_param * class_expr
783+
(** [Pcl_fun(P, CE)] represents:
784784
- [fun P -> CE]
785-
when [lbl] is {{!Asttypes.arg_label.Nolabel}[Nolabel]}
786-
and [exp0] is [None],
787785
- [fun ~l:P -> CE]
788-
when [lbl] is {{!Asttypes.arg_label.Labelled}[Labelled l]}
789-
and [exp0] is [None],
790786
- [fun ?l:P -> CE]
791-
when [lbl] is {{!Asttypes.arg_label.Optional}[Optional l]}
792-
and [exp0] is [None],
793787
- [fun ?l:(P = E0) -> CE]
794-
when [lbl] is {{!Asttypes.arg_label.Optional}[Optional l]}
795-
and [exp0] is [Some E0].
796788
*)
797789
| Pcl_apply of class_expr * (arg_label * expression) list
798790
(** [Pcl_apply(CE, [(l1,E1) ; ... ; (ln,En)])]

vendor/parser-extended/printast.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,9 @@ and class_expr i ppf x =
727727
| Pcl_structure (cs) ->
728728
line i ppf "Pcl_structure\n";
729729
class_structure i ppf cs;
730-
| Pcl_fun (l, eo, p, e) ->
730+
| Pcl_fun (p, e) ->
731731
line i ppf "Pcl_fun\n";
732-
arg_label i ppf l;
733-
option i expression ppf eo;
734-
pattern i ppf p;
732+
function_param i ppf p;
735733
class_expr i ppf e;
736734
| Pcl_apply (ce, l) ->
737735
line i ppf "Pcl_apply\n";

0 commit comments

Comments
 (0)