Skip to content

Commit b6867b0

Browse files
authored
Improve the placement of begin..end attributes (#2551)
* Parenthese begin..end with attributes This fixes a AST changed bug by adding parentheses. * Improve the placement of begin..end attributes Render 'begin [@attr] .. end' instead of 'begin .. end [@attr]'. This removes parentheses. * Update changes
1 parent 099d6e1 commit b6867b0

File tree

10 files changed

+47
-35
lines changed

10 files changed

+47
-35
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ profile. This started with version 0.26.0.
5151
- Fix comments around underscore in record patterns (#2540, @Julow)
5252
- Fix dropped comments before `begin .. end` in a match case (#2541, @Julow)
5353
- Fix closing `*)` in doc-comments exceeding the margin (#2550, @Julow)
54+
- Fix invalid syntax geneated for begin..end attributes (#2551, @Julow)
55+
The attribute is moved from `begin .. end [@attr]` to `begin [@attr] .. end`.
5456

5557
### Changes
5658
- The location of attributes for structure items is now tracked and preserved. (#2247, @EmileTrotignon)

lib/Fmt_ast.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,9 +2851,10 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
28512851
| Pexp_hole -> pro $ hvbox 0 (fmt_hole () $ fmt_atrs)
28522852
| Pexp_beginend e ->
28532853
let wrap_beginend k =
2854-
let opn = str "begin" $ fmt_extension_suffix c ext
2854+
let opn =
2855+
hvbox 0 (str "begin" $ fmt_extension_suffix c ext $ fmt_atrs)
28552856
and cls = str "end" in
2856-
hvbox 0 (wrap opn cls (wrap (break 1 2) force_break k) $ fmt_atrs)
2857+
hvbox 0 (wrap opn cls (wrap (break 1 2) force_break k))
28572858
in
28582859
pro
28592860
$ wrap_beginend

test/passing/tests/attributes.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,23 @@ let _ = f ((1 : int) [@a]) ((1 : int) [@a])
439439
let _ = f ((((1 : int) [@a]) : (int[@b])) [@a]) ((1 : int) [@a])
440440

441441
include [@foo] M [@boo]
442+
443+
let () =
444+
let () =
445+
S.ntyp Cbor_type.Reserved
446+
@@ S.tok
447+
begin [@warning "-4"]
448+
fun ev ->
449+
match ev with Cbor_event.Reserved int -> Some int | _ -> None
450+
end
451+
in
452+
()
453+
454+
let () =
455+
let () =
456+
S.ntyp Cbor_type.Reserved
457+
@@ (S.tok (fun ev ->
458+
match ev with Cbor_event.Reserved int -> Some int | _ -> None )
459+
[@warning "-4"] )
460+
in
461+
()

test/passing/tests/exp_grouping-parens.ml.ref

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,9 @@ let _ = [%ext x y]
301301
let _ = [%ext (* foo *) x y]
302302

303303
let _ =
304-
begin
304+
begin [@landmark "parse_constant_dividends"]
305305
market_data_items := ()
306306
end
307-
[@landmark "parse_constant_dividends"]
308307

309308
let () = if a then b (* asd *)
310309

@@ -316,25 +315,23 @@ let x =
316315
match Tbl.find dist_tbl (pv1, pv2) with
317316
| None ->
318317
(* FIXME: temporary hack to avoid Jane Street's annoying warnings. *)
319-
begin
318+
begin [@warning "-3"]
320319
try
321320
let path', dist = Dijkstra.shortest_path pgraph pv1 pv2 in
322321
let path = unwrap_path path' in
323322
Tbl.set dist_tbl ~key:(pv1, pv2) ~data:(path, dist) ;
324323
Some (path, dist)
325324
with Not_found | Not_found_s _ -> None
326325
end
327-
[@warning "-3"]
328326
| pd -> pd
329327
in
330328
()
331329

332330
let _ =
333331
if something_changed then
334-
begin
332+
begin [@attr]
335333
loop
336334
end
337-
[@attr]
338335

339336
let _ =
340337
match x with
@@ -345,7 +342,6 @@ let _ =
345342
let _ =
346343
match x with
347344
| _ ->
348-
begin
345+
begin [@foo]
349346
y
350347
end
351-
[@foo]

test/passing/tests/exp_grouping.ml.ref

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,9 @@ let _ =
351351
end
352352

353353
let _ =
354-
begin
354+
begin [@landmark "parse_constant_dividends"]
355355
market_data_items := ()
356356
end
357-
[@landmark "parse_constant_dividends"]
358357

359358
let () =
360359
if a then begin
@@ -370,25 +369,23 @@ let x =
370369
match Tbl.find dist_tbl (pv1, pv2) with
371370
| None ->
372371
(* FIXME: temporary hack to avoid Jane Street's annoying warnings. *)
373-
begin
372+
begin [@warning "-3"]
374373
try
375374
let path', dist = Dijkstra.shortest_path pgraph pv1 pv2 in
376375
let path = unwrap_path path' in
377376
Tbl.set dist_tbl ~key:(pv1, pv2) ~data:(path, dist) ;
378377
Some (path, dist)
379378
with Not_found | Not_found_s _ -> None
380379
end
381-
[@warning "-3"]
382380
| pd -> pd
383381
in
384382
()
385383

386384
let _ =
387385
if something_changed then
388-
begin
386+
begin [@attr]
389387
loop
390388
end
391-
[@attr]
392389

393390
let _ =
394391
match x with
@@ -401,7 +398,6 @@ let _ =
401398
let _ =
402399
match x with
403400
| _ ->
404-
begin
401+
begin [@foo]
405402
y
406403
end
407-
[@foo]
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Warning: tests/js_source.ml:162 exceeds the margin
2-
Warning: tests/js_source.ml:9560 exceeds the margin
3-
Warning: tests/js_source.ml:9664 exceeds the margin
4-
Warning: tests/js_source.ml:9723 exceeds the margin
5-
Warning: tests/js_source.ml:9805 exceeds the margin
6-
Warning: tests/js_source.ml:10304 exceeds the margin
1+
Warning: tests/js_source.ml:161 exceeds the margin
2+
Warning: tests/js_source.ml:9559 exceeds the margin
3+
Warning: tests/js_source.ml:9663 exceeds the margin
4+
Warning: tests/js_source.ml:9722 exceeds the margin
5+
Warning: tests/js_source.ml:9804 exceeds the margin
6+
Warning: tests/js_source.ml:10303 exceeds the margin

test/passing/tests/js_source.ml.ocp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,9 @@ let () =
110110
[%foo lazy x [@foo]];
111111
[%foo object end [@foo]];
112112
[%foo
113-
begin
113+
begin [@foo]
114114
3
115-
end
116-
[@foo]];
115+
end];
117116
[%foo new x [@foo]];
118117
[%foo
119118
match[@foo] () with

test/passing/tests/js_source.ml.ref

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,9 @@ let () =
110110
[%foo lazy x [@foo]];
111111
[%foo object end [@foo]];
112112
[%foo
113-
begin
113+
begin [@foo]
114114
3
115-
end
116-
[@foo]];
115+
end];
117116
[%foo new x [@foo]];
118117
[%foo
119118
match[@foo] () with

test/passing/tests/source.ml.err

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Warning: tests/source.ml:704 exceeds the margin
2-
Warning: tests/source.ml:2321 exceeds the margin
1+
Warning: tests/source.ml:703 exceeds the margin
2+
Warning: tests/source.ml:2320 exceeds the margin

test/passing/tests/source.ml.ref

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ let () =
120120
[%foo lazy x [@foo]] ;
121121
[%foo object end [@foo]] ;
122122
[%foo
123-
begin
123+
begin [@foo]
124124
3
125-
end
126-
[@foo]] ;
125+
end] ;
127126
[%foo new x [@foo]] ;
128127
[%foo
129128
match[@foo] () with

0 commit comments

Comments
 (0)