Skip to content

Commit 3386dca

Browse files
JulowGuillaume PetiotEmileTrotignon
authored
Backport Pexp_function from OCaml 5.2 (ocaml-ppx#2544)
Backport the OCaml 5.2 `Pexp_function` node to the extended parser. All the different pieces of code that format functions are merged into a single place. This requires adding a lot of special cases to re-implement the various ways functions were formatted before. Some of these special cases could be removed in the future. Co-authored-by: Guillaume Petiot <[email protected]> Co-authored-by: Emile Trotignon <[email protected]>
1 parent 51800ac commit 3386dca

Some content is hidden

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

44 files changed

+1489
-848
lines changed

.github/workflows/build-linux.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ jobs:
6767
- conventional
6868
- ocamlformat
6969
- janestreet
70-
include:
71-
- ocp_indent: true
72-
ocp_indent_config: JaneStreet
73-
profile: janestreet
70+
# To enable comparing with ocp-indent:
71+
# include:
72+
# - ocp_indent: true
73+
# ocp_indent_config: JaneStreet
74+
# profile: janestreet
7475

7576
steps:
7677
- name: Install ocp-indent

CHANGES.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ profile. This started with version 0.26.0.
88

99
### Added
1010

11-
- Support OCaml 5.2 syntax (#2519, @Julow)
12-
This includes:
13-
+ Local open in types.
11+
- \* Support OCaml 5.2 syntax (#2519, #2544, @Julow, @EmileTrotignon)
12+
This includes local open in types and changed syntax for functions.
13+
This might change the formatting of some functions due to the formatting code
14+
being completely rewritten.
1415
- Allow a custom command to be used to run ocamlformat in the emacs plugin (#2577, @gridbugs)
1516

1617
### Changed

lib-rpc/protocol.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module Make (IO : IO.S) = struct
4949
let to_sexp =
5050
let open Csexp in
5151
function
52-
| `Version v -> List [Atom "Version"; Atom v] | _ -> assert false
52+
| `Version v -> List [Atom "Version"; Atom v]
53+
| _ -> assert false
5354

5455
let output oc t = IO.write oc [to_sexp t]
5556
end
@@ -109,8 +110,7 @@ module Make (IO : IO.S) = struct
109110
let csexp_to_config csexpl =
110111
List.filter_map
111112
(function
112-
| List [Atom name; Atom value] -> Some (name, value) | _ -> None
113-
)
113+
| List [Atom name; Atom value] -> Some (name, value) | _ -> None )
114114
csexpl
115115
in
116116
read ic

lib/Ast.ml

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ module Exp = struct
145145

146146
let has_trailing_attributes {pexp_desc; pexp_attributes; _} =
147147
match pexp_desc with
148-
| Pexp_fun _ | Pexp_function _ | Pexp_ifthenelse _ | Pexp_match _
149-
|Pexp_try _ ->
148+
| Pexp_function _ | Pexp_ifthenelse _ | Pexp_match _ | Pexp_try _ ->
150149
false
151150
| _ -> List.exists pexp_attributes ~f:(Fn.non Attr.is_doc)
152151

@@ -180,12 +179,14 @@ module Exp = struct
180179
|( {pexp_desc= Pexp_sequence _; _}
181180
, (Non_apply | Sequence | Then | ThenElse) )
182181
|( { pexp_desc=
183-
( Pexp_function _ | Pexp_match _ | Pexp_try _
184-
| Pexp_fun (_, {pexp_desc= Pexp_constraint _; _}) )
182+
( Pexp_function (_, Some _, _)
183+
| Pexp_function (_, _, Pfunction_cases _)
184+
| Pexp_match _ | Pexp_try _ )
185185
; _ }
186186
, (Match | Let_match | Non_apply) )
187187
|( { pexp_desc=
188-
( Pexp_fun _ | Pexp_let _ | Pexp_letop _ | Pexp_letexception _
188+
( Pexp_function (_, _, Pfunction_body _)
189+
| Pexp_let _ | Pexp_letop _ | Pexp_letexception _
189190
| Pexp_letmodule _ | Pexp_open _ | Pexp_letopen _ )
190191
; _ }
191192
, (Let_match | Non_apply) ) ->
@@ -1016,6 +1017,7 @@ end = struct
10161017
List.exists en1 ~f:(fun (_, c, _) ->
10171018
Option.exists c ~f:check_type_constraint ) )
10181019
| Pexp_let (lbs, _, _) -> assert (check_let_bindings lbs)
1020+
| Pexp_function (_, Some t1, _) -> assert (check_type_constraint t1)
10191021
| _ -> assert false )
10201022
| Fpe _ | Fpc _ -> assert false
10211023
| Vc c -> assert (check_value_constraint c)
@@ -1206,15 +1208,16 @@ end = struct
12061208
let check_param_val (_, _, p) = p == pat in
12071209
let check_expr_function_param param =
12081210
match param.pparam_desc with
1209-
| Param_val x -> check_param_val x
1210-
| Param_newtype _ -> false
1211+
| Pparam_val x -> check_param_val x
1212+
| Pparam_newtype _ -> false
12111213
in
12121214
let check_class_function_param param =
12131215
check_param_val param.pparam_desc
12141216
in
12151217
let check_class_function_params =
12161218
List.exists ~f:check_class_function_param
12171219
in
1220+
let check_cases = List.exists ~f:(fun c -> c.pc_lhs == pat) in
12181221
match ctx with
12191222
| Pld (PPat (p1, _)) -> assert (p1 == pat)
12201223
| Pld _ -> assert false
@@ -1266,13 +1269,17 @@ end = struct
12661269
| Pexp_letop {let_; ands; _} ->
12671270
let f {pbop_pat; _} = check_subpat pbop_pat in
12681271
assert (f let_ || List.exists ~f ands)
1269-
| Pexp_function cases | Pexp_match (_, cases) | Pexp_try (_, cases) ->
1270-
assert (
1271-
List.exists cases ~f:(function
1272-
| {pc_lhs; _} when pc_lhs == pat -> true
1273-
| _ -> false ) )
1272+
| Pexp_match (_, cases) | Pexp_try (_, cases) ->
1273+
assert (check_cases cases)
12741274
| Pexp_for (p, _, _, _, _) -> assert (p == pat)
1275-
| Pexp_fun (p, _) -> assert (check_expr_function_param p) )
1275+
| Pexp_function (params, _, body) ->
1276+
let check_body =
1277+
match body with
1278+
| Pfunction_body _ -> false
1279+
| Pfunction_cases (cases, _, _) -> check_cases cases
1280+
in
1281+
assert (
1282+
List.exists ~f:check_expr_function_param params || check_body ) )
12761283
| Fpe ctx -> assert (check_expr_function_param ctx)
12771284
| Fpc ctx -> assert (check_class_function_param ctx)
12781285
| Vc _ -> assert false
@@ -1329,15 +1336,21 @@ end = struct
13291336
let check_param_val (_, e, _) = Option.exists e ~f:(fun x -> x == exp) in
13301337
let check_expr_function_param param =
13311338
match param.pparam_desc with
1332-
| Param_val x -> check_param_val x
1333-
| Param_newtype _ -> false
1339+
| Pparam_val x -> check_param_val x
1340+
| Pparam_newtype _ -> false
13341341
in
13351342
let check_class_function_param param =
13361343
check_param_val param.pparam_desc
13371344
in
13381345
let check_class_function_params =
13391346
List.exists ~f:check_class_function_param
13401347
in
1348+
let check_cases =
1349+
List.exists ~f:(function
1350+
| {pc_guard= Some g; _} when g == exp -> true
1351+
| {pc_rhs; _} when pc_rhs == exp -> true
1352+
| _ -> false )
1353+
in
13411354
match ctx with
13421355
| Pld (PPat (_, Some e1)) -> assert (e1 == exp)
13431356
| Pld _ -> assert false
@@ -1359,15 +1372,16 @@ end = struct
13591372
let f {pbop_exp; _} = pbop_exp == exp in
13601373
assert (f let_ || List.exists ~f ands || body == exp)
13611374
| (Pexp_match (e, _) | Pexp_try (e, _)) when e == exp -> ()
1362-
| Pexp_function cases | Pexp_match (_, cases) | Pexp_try (_, cases)
1363-
->
1375+
| Pexp_match (_, cases) | Pexp_try (_, cases) ->
1376+
assert (check_cases cases)
1377+
| Pexp_function (params, _, body) ->
1378+
let check_body =
1379+
match body with
1380+
| Pfunction_body body -> body == exp
1381+
| Pfunction_cases (cases, _, _) -> check_cases cases
1382+
in
13641383
assert (
1365-
List.exists cases ~f:(function
1366-
| {pc_guard= Some g; _} when g == exp -> true
1367-
| {pc_rhs; _} when pc_rhs == exp -> true
1368-
| _ -> false ) )
1369-
| Pexp_fun (param, body) ->
1370-
assert (check_expr_function_param param || body == exp)
1384+
List.exists ~f:check_expr_function_param params || check_body )
13711385
| Pexp_indexop_access {pia_lhs; pia_kind= Builtin idx; pia_rhs; _} ->
13721386
assert (
13731387
pia_lhs == exp || idx == exp
@@ -1867,7 +1881,7 @@ end = struct
18671881
| Ppat_cons _ -> true
18681882
| Ppat_construct _ | Ppat_record _ | Ppat_variant _ -> false
18691883
| _ -> true )
1870-
| Fpe {pparam_desc= Param_val (_, _, _); _}, Ppat_cons _ -> true
1884+
| Fpe {pparam_desc= Pparam_val (_, _, _); _}, Ppat_cons _ -> true
18711885
| Fpc {pparam_desc= _; _}, Ppat_cons _ -> true
18721886
| Pat {ppat_desc= Ppat_construct _; _}, Ppat_cons _ -> true
18731887
| _, Ppat_constraint (_, {ptyp_desc= Ptyp_poly _; _}) -> false
@@ -1901,7 +1915,7 @@ end = struct
19011915
( Ppat_construct _ | Ppat_exception _ | Ppat_or _
19021916
| Ppat_lazy _ | Ppat_tuple _ | Ppat_variant _ | Ppat_list _ )
19031917
; _ }
1904-
| Exp {pexp_desc= Pexp_fun _; _} )
1918+
| Exp {pexp_desc= Pexp_function (_, _, Pfunction_body _); _} )
19051919
, Ppat_alias _ )
19061920
|( Pat {ppat_desc= Ppat_lazy _; _}
19071921
, ( Ppat_construct _ | Ppat_cons _
@@ -1917,14 +1931,14 @@ end = struct
19171931
|Pat {ppat_desc= Ppat_tuple _; _}, Ppat_tuple _
19181932
|Pat _, Ppat_lazy _
19191933
|Pat _, Ppat_exception _
1920-
|Exp {pexp_desc= Pexp_fun _; _}, Ppat_or _
1934+
|Exp {pexp_desc= Pexp_function (_, _, Pfunction_body _); _}, Ppat_or _
19211935
|Cl {pcl_desc= Pcl_fun _; _}, Ppat_variant (_, Some _)
19221936
|Cl {pcl_desc= Pcl_fun _; _}, Ppat_tuple _
19231937
|Cl {pcl_desc= Pcl_fun _; _}, Ppat_construct _
19241938
|Cl {pcl_desc= Pcl_fun _; _}, Ppat_alias _
19251939
|Cl {pcl_desc= Pcl_fun _; _}, Ppat_lazy _
19261940
|(Exp {pexp_desc= Pexp_letop _; _} | Bo _), Ppat_exception _
1927-
|( Exp {pexp_desc= Pexp_fun _; _}
1941+
|( Exp {pexp_desc= Pexp_function (_, _, Pfunction_body _); _}
19281942
, ( Ppat_construct _ | Ppat_cons _ | Ppat_lazy _ | Ppat_tuple _
19291943
| Ppat_variant _ ) ) ->
19301944
true
@@ -1976,7 +1990,7 @@ end = struct
19761990
match exp.pexp_desc with
19771991
| Pexp_assert e
19781992
|Pexp_construct (_, Some e)
1979-
|Pexp_fun (_, e)
1993+
|Pexp_function (_, _, Pfunction_body e)
19801994
|Pexp_ifthenelse (_, Some e)
19811995
|Pexp_prefix (_, e)
19821996
|Pexp_infix (_, _, e)
@@ -2004,8 +2018,9 @@ end = struct
20042018
match cls with Match | Then | ThenElse -> continue e | _ -> false )
20052019
| Pexp_match _ when match cls with Then -> true | _ -> false ->
20062020
false
2007-
| Pexp_function cases | Pexp_match (_, cases) | Pexp_try (_, cases)
2008-
->
2021+
| Pexp_function (_, _, Pfunction_cases (cases, _, _))
2022+
|Pexp_match (_, cases)
2023+
|Pexp_try (_, cases) ->
20092024
continue (List.last_exn cases).pc_rhs
20102025
| Pexp_apply (_, args) -> continue (snd (List.last_exn args))
20112026
| Pexp_tuple es -> continue (List.last_exn es)
@@ -2057,7 +2072,7 @@ end = struct
20572072
|Pexp_lazy e
20582073
|Pexp_open (_, e)
20592074
|Pexp_letopen (_, e)
2060-
|Pexp_fun (_, e)
2075+
|Pexp_function (_, _, Pfunction_body e)
20612076
|Pexp_sequence (_, e)
20622077
|Pexp_setfield (_, _, e)
20632078
|Pexp_setinstvar (_, e)
@@ -2073,13 +2088,16 @@ end = struct
20732088
| Pexp_extension (ext, PStr [{pstr_desc= Pstr_eval (e, _); _}])
20742089
when Source.extension_using_sugar ~name:ext ~payload:e.pexp_loc -> (
20752090
match e.pexp_desc with
2076-
| Pexp_function cases | Pexp_match (_, cases) | Pexp_try (_, cases)
2077-
->
2091+
| Pexp_function (_, _, Pfunction_cases (cases, _, _))
2092+
|Pexp_match (_, cases)
2093+
|Pexp_try (_, cases) ->
20782094
List.iter cases ~f:(fun case ->
20792095
mark_parenzed_inner_nested_match case.pc_rhs ) ;
20802096
true
20812097
| _ -> continue e )
2082-
| Pexp_function cases | Pexp_match (_, cases) | Pexp_try (_, cases) ->
2098+
| Pexp_function (_, _, Pfunction_cases (cases, _, _))
2099+
|Pexp_match (_, cases)
2100+
|Pexp_try (_, cases) ->
20832101
List.iter cases ~f:(fun case ->
20842102
mark_parenzed_inner_nested_match case.pc_rhs ) ;
20852103
true
@@ -2241,6 +2259,10 @@ end = struct
22412259
, {pexp_desc= Pexp_construct _ | Pexp_cons _; _} )
22422260
when e == exp ->
22432261
true
2262+
| ( Exp {pexp_desc= Pexp_function (_, _, Pfunction_body e); _}
2263+
, {pexp_desc= Pexp_function (_, _, Pfunction_cases _); _} )
2264+
when e == exp ->
2265+
true
22442266
| Exp {pexp_desc; _}, _ -> (
22452267
match pexp_desc with
22462268
| Pexp_extension
@@ -2249,13 +2271,14 @@ end = struct
22492271
[ { pstr_desc=
22502272
Pstr_eval
22512273
( { pexp_desc=
2252-
( Pexp_function cases
2274+
( Pexp_function
2275+
(_, _, Pfunction_cases (cases, _, _))
22532276
| Pexp_match (_, cases)
22542277
| Pexp_try (_, cases) )
22552278
; _ }
22562279
, _ )
22572280
; _ } ] )
2258-
|Pexp_function cases
2281+
|Pexp_function (_, _, Pfunction_cases (cases, _, _))
22592282
|Pexp_match (_, cases)
22602283
|Pexp_try (_, cases) ->
22612284
if !leading_nested_match_parens then

lib/Cmts.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ let break_comment_group source a b =
457457
Location.line_difference a b = 0
458458
&& List.is_empty
459459
(Source.tokens_between source a.loc_end b.loc_start
460-
~filter:(function _ -> true) )
460+
~filter:(function _ -> true ))
461461
in
462462
not (vertical_align || horizontal_align)
463463

lib/Conf.ml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,7 @@ module Formatting = struct
442442
in
443443
Decl.choice ~names ~all ~default ~doc ~kind
444444
(fun conf elt ->
445-
update conf ~f:(fun f -> {f with break_collection_expressions= elt})
446-
)
445+
update conf ~f:(fun f -> {f with break_collection_expressions= elt}) )
447446
(fun conf -> conf.fmt_opts.break_collection_expressions)
448447

449448
let break_colon =
@@ -597,8 +596,7 @@ module Formatting = struct
597596
Decl.choice ~names ~all ~default ~doc ~kind
598597
(fun conf elt ->
599598
update conf ~f:(fun f ->
600-
{f with break_struct= Elt.make Poly.(elt.v = `Force) elt.from} )
601-
)
599+
{f with break_struct= Elt.make Poly.(elt.v = `Force) elt.from} ) )
602600
(fun conf ->
603601
let elt = conf.fmt_opts.break_struct in
604602
if elt.v then Elt.make `Force elt.from
@@ -895,8 +893,7 @@ module Formatting = struct
895893
in
896894
Decl.choice ~names ~all ~default ~doc ~kind
897895
(fun conf elt ->
898-
update conf ~f:(fun f -> {f with indicate_nested_or_patterns= elt})
899-
)
896+
update conf ~f:(fun f -> {f with indicate_nested_or_patterns= elt}) )
900897
(fun conf -> conf.fmt_opts.indicate_nested_or_patterns)
901898

902899
let infix_precedence =
@@ -923,8 +920,7 @@ module Formatting = struct
923920
let names = ["leading-nested-match-parens"] in
924921
Decl.flag ~names ~default ~doc ~kind ~allow_inline:false
925922
(fun conf elt ->
926-
update conf ~f:(fun f -> {f with leading_nested_match_parens= elt})
927-
)
923+
update conf ~f:(fun f -> {f with leading_nested_match_parens= elt}) )
928924
(fun conf -> conf.fmt_opts.leading_nested_match_parens)
929925

930926
let let_and =
@@ -1400,7 +1396,9 @@ module Operational = struct
14001396
let debug =
14011397
let doc = "Generate debugging output." in
14021398
Decl.flag ~default ~names:["g"; "debug"] ~doc ~kind
1403-
(fun conf elt -> update conf ~f:(fun f -> {f with debug= elt}))
1399+
(fun conf elt ->
1400+
if elt.v then Box_debug.enable_stacktraces := true ;
1401+
update conf ~f:(fun f -> {f with debug= elt}) )
14041402
(fun conf -> conf.opr_opts.debug)
14051403

14061404
let disable =

lib/Extended_ast.ml

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ module Parse = struct
183183
{p with ppat_desc= Ppat_unpack (name, Some pt)}
184184
| p -> Ast_mapper.default_mapper.pat m p
185185
in
186-
let rec expr (m : Ast_mapper.mapper) = function
186+
let expr (m : Ast_mapper.mapper) = function
187187
| {pexp_desc= Pexp_cons (_ :: _ :: _ :: _ as l); _} as e
188188
when match List.last_exn l with
189189
(* Empty lists are always represented as Lident [] *)
@@ -233,25 +233,6 @@ module Parse = struct
233233
(module S) = (module M)] - [let _ = ((module M) : (module
234234
S))] *)
235235
{p with pexp_desc= Pexp_pack (name, Some pt)}
236-
| { pexp_desc=
237-
Pexp_fun
238-
({pparam_desc= Param_newtype types1; pparam_loc= loc1}, e1)
239-
; pexp_attributes= []
240-
; _ } as e ->
241-
let e =
242-
match (expr m e1).pexp_desc with
243-
| Pexp_fun
244-
({pparam_desc= Param_newtype types2; pparam_loc= loc2}, e2)
245-
->
246-
{ e with
247-
pexp_desc=
248-
Pexp_fun
249-
( { pparam_desc= Param_newtype (types1 @ types2)
250-
; pparam_loc= {loc1 with loc_end= loc2.loc_end} }
251-
, e2 ) }
252-
| _ -> e
253-
in
254-
Ast_mapper.default_mapper.expr m e
255236
| e -> Ast_mapper.default_mapper.expr m e
256237
in
257238
Ast_mapper.{default_mapper with expr; pat; binding_op}

0 commit comments

Comments
 (0)