Skip to content

Commit 5582edd

Browse files
authored
5.5 support: Structure item embedded in expressions (#2780)
`Pexp_struct_item` replaces `Pexp_letexception`, `Pexp_letmodule` and `Pexp_letopen`. The code for the old constructs is removed and replaced by a call to `fmt_structure_item`. The code for formatting structure items is changed to propagate the `~pro` and `~epi` arguments. * Extend the context for embedded struct items The `Str_exp` context is needed to apply the `let_module` option. The `Mb` context constructor is extended to carry the parent context, which would otherwise be erased before `fmt_module` gets called.
1 parent e1027ea commit 5582edd

60 files changed

Lines changed: 550 additions & 603 deletions

Some content is hidden

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

CHANGES.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ profile. This started with version 0.26.0.
88

99
### Highlight
1010

11-
- Support OCaml 5.5 syntax (#2772, #2774, #2775, #2777, @Julow)
11+
- \* Support OCaml 5.5 syntax (#2772, #2774, #2775, #2777, #2780, @Julow)
12+
The update brings several tiny changes, they are listed below.
1213

1314
- \* Update Odoc's parser to 3.0 (#2757, @Julow)
1415
The indentation of code-blocks containing OCaml code is reduced by 2 to avoid
@@ -110,6 +111,23 @@ profile. This started with version 0.26.0.
110111
In exchange, code that use `effect` as an identifier must use
111112
`ocaml-version=5.2`.
112113

114+
- The work to support OCaml 5.5 come with several improvements:
115+
+ Improve the indentation of `let structure-item` with the
116+
`[@ocamlformat "disable"]` attribute.
117+
`let structure-item` means `let module`, `let open`, `let include` and
118+
`let exception`.
119+
+ `(let open M in e)[@a]` is turned into `let[@a] open M in e`.
120+
+ Long `let open ... in` no longer exceed the margin.
121+
+ Improve indentation of `let structure-item` within parentheses:
122+
```ocaml
123+
(* before *)
124+
(let module M = M in
125+
M.foo)
126+
(* after *)
127+
(let module M = M in
128+
M.foo)
129+
```
130+
113131
## 0.28.1
114132
115133
### Highlight

lib/Ast.ml

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,11 @@ module Infix_ext_attrs = struct
129129
|Pexp_while (_, _, iea)
130130
|Pexp_for (_, _, _, _, _, iea)
131131
|Pexp_new (_, iea)
132-
|Pexp_letmodule (_, _, _, _, iea)
133-
|Pexp_letexception (_, _, iea)
132+
|Pexp_struct_item (_, _, iea)
134133
|Pexp_assert (_, iea)
135134
|Pexp_lazy (_, iea)
136135
|Pexp_object (_, iea)
137136
|Pexp_pack (_, _, iea)
138-
|Pexp_letopen (_, _, iea)
139137
|Pexp_beginend (_, iea) ->
140138
iea.infix_attrs
141139
| _ -> []
@@ -202,8 +200,7 @@ module Exp = struct
202200
, (Match | Let_match | Non_apply) )
203201
|( { pexp_desc=
204202
( Pexp_function (_, _, Pfunction_body _, _)
205-
| Pexp_let _ | Pexp_letop _ | Pexp_letexception _
206-
| Pexp_letmodule _ | Pexp_open _ | Pexp_letopen _ )
203+
| Pexp_let _ | Pexp_letop _ | Pexp_struct_item _ | Pexp_open _ )
207204
; _ }
208205
, (Let_match | Non_apply) ) ->
209206
true
@@ -660,13 +657,14 @@ module T = struct
660657
| Vc of value_constraint
661658
| Lb of value_binding
662659
| Bo of binding_op
663-
| Mb of module_binding
660+
| Mb of t * module_binding
664661
| Md of module_declaration
665662
| Cl of class_expr
666663
| Mty of module_type
667664
| Mod of module_expr
668665
| Sig of signature_item
669666
| Str of structure_item
667+
| Str_exp of structure_item
670668
| Clf of class_field
671669
| Ctf of class_type_field
672670
| Tli of toplevel_item
@@ -684,7 +682,7 @@ module T = struct
684682
| Vc c -> Format.fprintf fs "Vc:@\n%a" Printast.value_constraint c
685683
| Lb b -> Format.fprintf fs "Lb:@\n%a" Printast.value_binding b
686684
| Bo b -> Format.fprintf fs "Bo:@\n%a" Printast.binding_op b
687-
| Mb m -> Format.fprintf fs "Mb:@\n%a" Printast.module_binding m
685+
| Mb (_, m) -> Format.fprintf fs "Mb:@\n%a" Printast.module_binding m
688686
| Md m -> Format.fprintf fs "Md:@\n%a" Printast.module_declaration m
689687
| Cl cl -> Format.fprintf fs "Cl:@\n%a" Printast.class_expr cl
690688
| Mty mt -> Format.fprintf fs "Mty:@\n%a" Printast.module_type mt
@@ -694,7 +692,7 @@ module T = struct
694692
Format.fprintf fs "Ctd:@\n%a" Printast.class_type_declaration ctd
695693
| Mod m -> Format.fprintf fs "Mod:@\n%a" Printast.module_expr m
696694
| Sig s -> Format.fprintf fs "Sig:@\n%a" Printast.signature_item s
697-
| Str s | Tli (`Item s) ->
695+
| Str s | Str_exp s | Tli (`Item s) ->
698696
Format.fprintf fs "Str:@\n%a" Printast.structure_item s
699697
| Clf clf -> Format.fprintf fs "Clf:@\n%a@\n" Printast.class_field clf
700698
| Ctf ctf ->
@@ -722,7 +720,7 @@ let attributes = function
722720
| Vc _ -> []
723721
| Lb x -> attrs_of_ext_attrs x.pvb_attributes
724722
| Bo _ -> []
725-
| Mb x -> attrs_of_ext_attrs x.pmb_ext_attrs
723+
| Mb (_, x) -> attrs_of_ext_attrs x.pmb_ext_attrs
726724
| Md x -> attrs_of_ext_attrs x.pmd_ext_attrs
727725
| Cl x -> x.pcl_attributes
728726
| Cd x -> attrs_of_ext_attrs x.pci_attributes
@@ -731,6 +729,7 @@ let attributes = function
731729
| Mod x -> x.pmod_attributes
732730
| Sig _ -> []
733731
| Str _ -> []
732+
| Str_exp _ -> []
734733
| Clf x -> x.pcf_attributes
735734
| Ctf x -> x.pctf_attributes
736735
| Top -> []
@@ -749,15 +748,15 @@ let location = function
749748
| Vc _ -> Location.none
750749
| Lb x -> x.pvb_loc
751750
| Bo x -> x.pbop_loc
752-
| Mb x -> x.pmb_loc
751+
| Mb (_, x) -> x.pmb_loc
753752
| Md x -> x.pmd_loc
754753
| Cl x -> x.pcl_loc
755754
| Cd x -> x.pci_loc
756755
| Ctd x -> x.pci_loc
757756
| Mty x -> x.pmty_loc
758757
| Mod x -> x.pmod_loc
759758
| Sig x -> x.psig_loc
760-
| Str x -> x.pstr_loc
759+
| Str x | Str_exp x -> x.pstr_loc
761760
| Clf x -> x.pcf_loc
762761
| Ctf x -> x.pctf_loc
763762
| Tli (`Item x) -> x.pstr_loc
@@ -780,7 +779,7 @@ let break_between s cc (i1, c1) (i2, c2) =
780779
| Str i1, Str i2 -> Structure_item.break_between s cc (i1, c1) (i2, c2)
781780
| Sig i1, Sig i2 -> Signature_item.break_between s cc (i1, c1) (i2, c2)
782781
| Lb i1, Lb i2 -> Lb.break_between s cc (i1, c1) (i2, c2)
783-
| Mb i1, Mb i2 -> Mb.break_between s cc (i1, c1) (i2, c2)
782+
| Mb (_, i1), Mb (_, i2) -> Mb.break_between s cc (i1, c1) (i2, c2)
784783
| Md i1, Md i2 -> Md.break_between s cc (i1, c1) (i2, c2)
785784
| Mty _, Mty _ -> break_between_modules s cc (i1, c1) (i2, c2)
786785
| Mod _, Mod _ -> break_between_modules s cc (i1, c1) (i2, c2)
@@ -1038,7 +1037,7 @@ end = struct
10381037
|Pexp_extension (_, PTyp t1) ->
10391038
assert (typ == t1)
10401039
| Pexp_coerce (_, Some t1, t2) -> assert (typ == t1 || typ == t2)
1041-
| Pexp_letexception (ext, _, _) -> assert (check_ext ext)
1040+
| Pexp_struct_item _ -> assert false
10421041
| Pexp_object _ -> assert false
10431042
| Pexp_record (en1, _) ->
10441043
assert (
@@ -1091,7 +1090,7 @@ end = struct
10911090
| Psig_typext typext -> assert (check_typext typext)
10921091
| Psig_exception ext -> assert (check_typexn ext)
10931092
| _ -> assert false )
1094-
| Str ctx -> (
1093+
| Str ctx | Str_exp ctx -> (
10951094
match ctx.pstr_desc with
10961095
| Pstr_primitive {pval_type= t1; _} -> assert (typ == t1)
10971096
| Pstr_type (_, _) -> assert false
@@ -1131,16 +1130,9 @@ end = struct
11311130

11321131
let check_cty {ctx; ast= cty} =
11331132
match (ctx : t) with
1134-
| Exp _ -> assert false
1135-
| Fpe _ | Fpc _ -> assert false
1136-
| Vc _ -> assert false
1137-
| Lb _ -> assert false
1138-
| Bo _ -> assert false
1139-
| Mb _ -> assert false
1140-
| Md _ -> assert false
1141-
| Pld _ -> assert false
1142-
| Str _ -> assert false
1143-
| Sig _ -> assert false
1133+
| Exp _ | Fpe _ | Fpc _ | Vc _ | Lb _ | Bo _ | Mb _ | Md _ | Pld _
1134+
|Str _ | Str_exp _ | Sig _ ->
1135+
assert false
11441136
| Cty {pcty_desc; _} -> (
11451137
match pcty_desc with
11461138
| Pcty_arrow (_, t) -> assert (t == cty)
@@ -1190,22 +1182,10 @@ end = struct
11901182

11911183
let check_cl {ctx; ast= cl} =
11921184
match (ctx : t) with
1193-
| Exp _ -> assert false
1194-
| Fpe _ | Fpc _ -> assert false
1195-
| Vc _ -> assert false
1196-
| Lb _ -> assert false
1197-
| Bo _ -> assert false
1198-
| Mb _ -> assert false
1199-
| Md _ -> assert false
1200-
| Pld _ -> assert false
1201-
| Str _ -> assert false
1202-
| Sig _ -> assert false
1203-
| Cty _ -> assert false
1204-
| Top -> assert false
1205-
| Tli _ -> assert false
1206-
| Typ _ -> assert false
1207-
| Td _ -> assert false
1208-
| Pat _ -> assert false
1185+
| Exp _ | Fpe _ | Fpc _ | Vc _ | Lb _ | Bo _ | Mb _ | Md _ | Pld _
1186+
|Str _ | Str_exp _ | Sig _ | Cty _ | Top | Tli _ | Typ _ | Td _ | Pat _
1187+
->
1188+
assert false
12091189
| Cl {pcl_desc; _} ->
12101190
assert (
12111191
match pcl_desc with
@@ -1302,13 +1282,13 @@ end = struct
13021282
| Pexp_apply _ | Pexp_array _ | Pexp_list _ | Pexp_assert _
13031283
|Pexp_coerce _ | Pexp_constant _ | Pexp_constraint _
13041284
|Pexp_construct _ | Pexp_field _ | Pexp_ident _ | Pexp_ifthenelse _
1305-
|Pexp_lazy _ | Pexp_letexception _ | Pexp_letmodule _ | Pexp_new _
1306-
|Pexp_open _ | Pexp_override _ | Pexp_pack _ | Pexp_record _
1307-
|Pexp_send _ | Pexp_sequence _ | Pexp_setfield _ | Pexp_setinstvar _
1285+
|Pexp_lazy _ | Pexp_struct_item _ | Pexp_new _ | Pexp_open _
1286+
|Pexp_override _ | Pexp_pack _ | Pexp_record _ | Pexp_send _
1287+
|Pexp_sequence _ | Pexp_setfield _ | Pexp_setinstvar _
13081288
|Pexp_tuple _ | Pexp_unreachable | Pexp_variant _ | Pexp_while _
13091289
|Pexp_hole | Pexp_beginend _ | Pexp_parens _ | Pexp_cons _
1310-
|Pexp_letopen _ | Pexp_indexop_access _ | Pexp_prefix _
1311-
|Pexp_infix _ | Pexp_construct_unit_beginend _ ->
1290+
|Pexp_indexop_access _ | Pexp_prefix _ | Pexp_infix _
1291+
|Pexp_construct_unit_beginend _ ->
13121292
assert false
13131293
| Pexp_extension (_, ext) -> assert (check_extensions ext)
13141294
| Pexp_object ({pcstr_self; _}, _) ->
@@ -1353,7 +1333,7 @@ end = struct
13531333
| Cd _ -> assert false
13541334
| Ctd _ -> assert false
13551335
| Mty _ | Mod _ | Sig _ -> assert false
1356-
| Str str -> (
1336+
| Str str | Str_exp str -> (
13571337
match str.pstr_desc with
13581338
| Pstr_value {pvbs_bindings; _} -> assert (check_bindings pvbs_bindings)
13591339
| Pstr_extension ((_, ext), _) -> assert (check_extensions ext)
@@ -1465,10 +1445,8 @@ end = struct
14651445
|Pexp_coerce (e, _, _)
14661446
|Pexp_field (e, _)
14671447
|Pexp_lazy (e, _)
1468-
|Pexp_letexception (_, e, _)
1469-
|Pexp_letmodule (_, _, _, e, _)
1448+
|Pexp_struct_item (_, e, _)
14701449
|Pexp_open (_, e)
1471-
|Pexp_letopen (_, e, _)
14721450
|Pexp_send (e, _)
14731451
|Pexp_setinstvar (_, e) ->
14741452
assert (e == exp)
@@ -1489,7 +1467,7 @@ end = struct
14891467
| Bo x -> assert (x.pbop_exp == exp)
14901468
| Mb _ -> assert false
14911469
| Md _ -> assert false
1492-
| Str str -> (
1470+
| Str str | Str_exp str -> (
14931471
match str.pstr_desc with
14941472
| Pstr_eval (e0, _) -> assert (e0 == exp)
14951473
| Pstr_value {pvbs_bindings; _} ->
@@ -1619,6 +1597,7 @@ end = struct
16191597
constructor_cxt_prec_of_inner typ
16201598
| { ctx=
16211599
( Str {pstr_desc= Pstr_typext {ptyext_constructors= l; _}; _}
1600+
| Str_exp {pstr_desc= Pstr_typext {ptyext_constructors= l; _}; _}
16221601
| Sig {psig_desc= Psig_typext {ptyext_constructors= l; _}; _} )
16231602
; ast=
16241603
Typ
@@ -1628,15 +1607,25 @@ end = struct
16281607
constructor_cxt_prec_of_inner typ
16291608
| { ctx=
16301609
( Str {pstr_desc= Pstr_exception {ptyexn_constructor= constr; _}; _}
1610+
| Str_exp
1611+
{pstr_desc= Pstr_exception {ptyexn_constructor= constr; _}; _}
16311612
| Sig {psig_desc= Psig_exception {ptyexn_constructor= constr; _}; _}
1632-
| Exp {pexp_desc= Pexp_letexception (constr, _, _); _} )
1613+
| Exp
1614+
{ pexp_desc=
1615+
Pexp_struct_item
1616+
( { pstr_desc=
1617+
Pstr_exception {ptyexn_constructor= constr; _}
1618+
; _ }
1619+
, _
1620+
, _ )
1621+
; _ } )
16331622
; ast=
16341623
Typ
16351624
({ptyp_desc= Ptyp_tuple _ | Ptyp_arrow _ | Ptyp_poly _; _} as typ)
16361625
}
16371626
when is_tuple_lvl1_in_ext_constructor typ constr ->
16381627
constructor_cxt_prec_of_inner typ
1639-
| {ctx= Str _; ast= Typ _; _} -> None
1628+
| {ctx= Str _ | Str_exp _; ast= Typ _; _} -> None
16401629
| {ctx= Typ {ptyp_desc; _}; ast= Typ typ; _} -> (
16411630
match ptyp_desc with
16421631
| Ptyp_arrow (t, _) ->
@@ -1744,7 +1733,7 @@ end = struct
17441733
| { ctx= Exp _
17451734
; ast=
17461735
( Pld _ | Top | Tli _ | Pat _ | Cl _ | Mty _ | Mod _ | Sig _
1747-
| Str _ | Clf _ | Ctf _ | Rep | Mb _ | Md _ ) }
1736+
| Str _ | Str_exp _ | Clf _ | Ctf _ | Rep | Mb _ | Md _ ) }
17481737
|{ctx= Fpe _ | Fpc _; ast= _}
17491738
|{ctx= _; ast= Fpe _ | Fpc _}
17501739
|{ctx= Vc _; ast= _}
@@ -1762,13 +1751,14 @@ end = struct
17621751
|{ ctx= Cl _
17631752
; ast=
17641753
( Pld _ | Top | Tli _ | Pat _ | Mty _ | Mod _ | Sig _ | Str _
1765-
| Clf _ | Ctf _ | Rep | Mb _ | Md _ ) }
1754+
| Str_exp _ | Clf _ | Ctf _ | Rep | Mb _ | Md _ ) }
17661755
|{ ctx=
17671756
( Pld _ | Top | Tli _ | Typ _ | Cty _ | Pat _ | Mty _ | Mod _
1768-
| Sig _ | Str _ | Clf _ | Ctf _ | Rep | Mb _ | Md _ )
1757+
| Sig _ | Str _ | Str_exp _ | Clf _ | Ctf _ | Rep | Mb _ | Md _ )
17691758
; ast=
17701759
( Pld _ | Top | Tli _ | Pat _ | Exp _ | Cl _ | Mty _ | Mod _
1771-
| Sig _ | Str _ | Clf _ | Ctf _ | Rep | Mb _ | Md _ ) } ->
1760+
| Sig _ | Str _ | Str_exp _ | Clf _ | Ctf _ | Rep | Mb _ | Md _ )
1761+
} ->
17721762
None
17731763

17741764
(** [prec_ast ast] is the precedence of [ast]. Meaningful for binary
@@ -1845,8 +1835,8 @@ end = struct
18451835
| Pcl_structure _ -> Some Apply
18461836
| Pcl_let _ -> Some Low
18471837
| _ -> None )
1848-
| Top | Pat _ | Mty _ | Mod _ | Sig _ | Str _ | Tli _ | Clf _ | Ctf _
1849-
|Rep | Mb _ | Md _ | Cd _ | Ctd _ ->
1838+
| Top | Pat _ | Mty _ | Mod _ | Sig _ | Str _ | Str_exp _ | Tli _
1839+
|Clf _ | Ctf _ | Rep | Mb _ | Md _ | Cd _ | Ctd _ ->
18501840
None
18511841

18521842
(** [ambig_prec {ctx; ast}] holds when [ast] is ambiguous in its context
@@ -1885,6 +1875,7 @@ end = struct
18851875
| { ast= {ptyp_desc= Ptyp_alias _; _}
18861876
; ctx=
18871877
( Str {pstr_desc= Pstr_typext _; _}
1878+
| Str_exp {pstr_desc= Pstr_typext _; _}
18881879
| Sig {psig_desc= Psig_typext _; _} ) } ->
18891880
true
18901881
| { ast= {ptyp_desc= Ptyp_alias _; _}
@@ -1900,6 +1891,7 @@ end = struct
19001891
; _ }
19011892
; ctx=
19021893
( Str {pstr_desc= Pstr_exception _; _}
1894+
| Str_exp {pstr_desc= Pstr_exception _; _}
19031895
| Sig {psig_desc= Psig_exception _; _} ) } ->
19041896
true
19051897
| { ast= {ptyp_desc= Ptyp_tuple ({lte_label= Some _; _} :: _); _}
@@ -2049,7 +2041,7 @@ end = struct
20492041
|( (Exp {pexp_desc= Pexp_letop _; _} | Bo _)
20502042
, (Ppat_exception _ | Ppat_effect _) ) ->
20512043
true
2052-
| (Str _ | Exp _ | Lb _), Ppat_lazy _ -> true
2044+
| (Str _ | Str_exp _ | Exp _ | Lb _), Ppat_lazy _ -> true
20532045
| ( (Fpe _ | Fpc _)
20542046
, ( Ppat_tuple _ | Ppat_construct _ | Ppat_alias _ | Ppat_variant _
20552047
| Ppat_lazy _ | Ppat_exception _ | Ppat_effect _ | Ppat_or _ ) )
@@ -2123,11 +2115,11 @@ end = struct
21232115
|Pexp_infix (_, _, e)
21242116
|Pexp_lazy (e, _)
21252117
|Pexp_open (_, e)
2126-
|Pexp_letopen (_, e, _)
21272118
|Pexp_sequence (_, e, _)
21282119
|Pexp_setfield (_, _, e)
21292120
|Pexp_setinstvar (_, e)
2130-
|Pexp_variant (_, Some e) ->
2121+
|Pexp_variant (_, Some e)
2122+
|Pexp_struct_item (_, e, _) ->
21312123
continue e
21322124
| Pexp_cons l -> continue (List.last_exn l)
21332125
| Pexp_ifthenelse (eN, None) -> continue (List.last_exn eN).if_body
@@ -2138,10 +2130,7 @@ end = struct
21382130
; _ } ] )
21392131
when Source.extension_using_sugar ~name:ext ~payload:e.pexp_loc ->
21402132
continue e
2141-
| Pexp_let (_, e, _)
2142-
|Pexp_letop {body= e; _}
2143-
|Pexp_letexception (_, e, _)
2144-
|Pexp_letmodule (_, _, _, e, _) -> (
2133+
| Pexp_let (_, e, _) | Pexp_letop {body= e; _} -> (
21452134
match cls with Match | Then | ThenElse -> continue e | _ -> false )
21462135
| Pexp_match _ when match cls with Then -> true | _ -> false ->
21472136
false
@@ -2198,19 +2187,16 @@ end = struct
21982187
|Pexp_infix (_, _, e)
21992188
|Pexp_lazy (e, _)
22002189
|Pexp_open (_, e)
2201-
|Pexp_letopen (_, e, _)
22022190
|Pexp_function (_, _, Pfunction_body e, _)
22032191
|Pexp_sequence (_, e, _)
22042192
|Pexp_setfield (_, _, e)
22052193
|Pexp_setinstvar (_, e)
2206-
|Pexp_variant (_, Some e) ->
2207-
continue e
2208-
| Pexp_cons l -> continue (List.last_exn l)
2209-
| Pexp_let (_, e, _)
2194+
|Pexp_variant (_, Some e)
2195+
|Pexp_let (_, e, _)
22102196
|Pexp_letop {body= e; _}
2211-
|Pexp_letexception (_, e, _)
2212-
|Pexp_letmodule (_, _, _, e, _) ->
2197+
|Pexp_struct_item (_, e, _) ->
22132198
continue e
2199+
| Pexp_cons l -> continue (List.last_exn l)
22142200
| Pexp_ifthenelse (eN, None) -> continue (List.last_exn eN).if_body
22152201
| Pexp_extension (ext, PStr [{pstr_desc= Pstr_eval (e, _); _}])
22162202
when Source.extension_using_sugar ~name:ext ~payload:e.pexp_loc -> (

0 commit comments

Comments
 (0)