Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit b82cabc

Browse files
author
Iwan
committed
Fix printing of comments with ghost unit at the end of Pexp_let
1 parent 32ed878 commit b82cabc

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

src/res_comments_table.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ let log t =
8585
]
8686
) |> Doc.toString ~width:80 |> print_endline
8787
[@@live]
88+
8889
let attach tbl loc comments =
8990
match comments with
9091
| [] -> ()
@@ -776,6 +777,12 @@ let rec walkStructure s t comments =
776777
partitionLeadingTrailing comments longident.loc in
777778
attach t.leading longident.loc leading;
778779
attach t.trailing longident.loc trailing;
780+
| Pexp_let (
781+
_recFlag,
782+
valueBindings,
783+
{pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, None)}
784+
) ->
785+
walkValueBindings valueBindings t comments
779786
| Pexp_let (_recFlag, valueBindings, expr2) ->
780787
let comments = visitListButContinueWithRemainingComments
781788
~getLoc:(fun n ->

src/res_printer.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ let printMultilineCommentContent txt =
9090
Doc.text "*/";
9191
]
9292

93-
let printTrailingComment (nodeLoc : Location.t) comment =
93+
let printTrailingComment (prevLoc: Location.t) (nodeLoc : Location.t) comment =
9494
let singleLine = Comment.isSingleLineComment comment in
9595
let content =
9696
let txt = Comment.txt comment in
@@ -101,8 +101,7 @@ let printTrailingComment (nodeLoc : Location.t) comment =
101101
in
102102
let diff =
103103
let cmtStart = (Comment.loc comment).loc_start in
104-
let prevTokEndPos = Comment.prevTokEndPos comment in
105-
cmtStart.pos_lnum - prevTokEndPos.pos_lnum
104+
cmtStart.pos_lnum - prevLoc.loc_end.pos_lnum
106105
in
107106
let isBelow =
108107
(Comment.loc comment).loc_start.pos_lnum > nodeLoc.loc_end.pos_lnum in
@@ -224,12 +223,12 @@ let printLeadingComments node tbl loc =
224223
loop [] comments
225224

226225
let printTrailingComments node tbl loc =
227-
let rec loop acc comments =
226+
let rec loop prev acc comments =
228227
match comments with
229228
| [] -> Doc.concat (List.rev acc)
230229
| comment::comments ->
231-
let cmtDoc = printTrailingComment loc comment in
232-
loop (cmtDoc::acc) comments
230+
let cmtDoc = printTrailingComment prev loc comment in
231+
loop (Comment.loc comment) (cmtDoc::acc) comments
233232
in
234233
match Hashtbl.find tbl loc with
235234
| exception Not_found -> node
@@ -238,7 +237,7 @@ let printTrailingComments node tbl loc =
238237
(* Remove comments from tbl: Some ast nodes have the same location.
239238
* We only want to print comments once *)
240239
Hashtbl.remove tbl loc;
241-
let cmtsDoc = loop [] comments in
240+
let cmtsDoc = loop loc [] comments in
242241
Doc.concat [
243242
node;
244243
cmtsDoc;

tests/conversion/reason/__snapshots__/render.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ module type my_module_type = {
997997
module M: {
998998
@ocaml.doc(\\" The comment for value y. \\")
999999
let y: int
1000+
10001001
/* ... */
10011002
}
10021003
}

tests/idempotency/lwt/core/lwt.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,10 +1448,8 @@ struct
14481448
'a 'u 'c. packed_callbacks list -> ('a, 'u, 'c) promise ->
14491449
packed_callbacks list =
14501450
fun (type c) callbacks_accumulator (p : (_, _, c) promise) ->
1451-
14521451
let p = underlying p in
14531452
match p.state with
1454-
(* If the promise is not still pending, it can't be canceled. *)
14551453
| Fulfilled _ ->
14561454
callbacks_accumulator
14571455
| Rejected _ ->

tests/printer/comments/__snapshots__/render.spec.js.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,27 @@ switch x {
561561
let catch = 34
562562
563563
let promiseCatch = x => Js.Promise.catch(x)
564+
565+
// aggregating intersections
566+
let () = {
567+
let s = Sphere.make()
568+
let i1 = make(~t=1., ~sphere=s)
569+
let i2 = make(~t=2., ~sphere=s)
570+
571+
// let is = interesections
572+
573+
// assert
574+
}
575+
576+
let () = {
577+
let s = Sphere.make()
578+
let i1 = make(~t=1., ~sphere=s)
579+
let i2 = make(~t=2., ~sphere=s)
580+
581+
// let is = interesections
582+
583+
// assert
584+
}
564585
"
565586
`;
566587

tests/printer/comments/blockExpr.res

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,26 @@ switch x {
514514

515515
let catch = 34
516516

517-
let promiseCatch = x => Js.Promise.catch(x)
517+
let promiseCatch = x => Js.Promise.catch(x)
518+
519+
// aggregating intersections
520+
let () = {
521+
let s = Sphere.make()
522+
let i1 = make(~t=1., ~sphere=s)
523+
let i2 = make(~t=2., ~sphere=s)
524+
525+
// let is = interesections
526+
527+
// assert
528+
}
529+
530+
let () = {
531+
let s = Sphere.make()
532+
let i1 = make(~t=1., ~sphere=s)
533+
let i2 = make(~t=2., ~sphere=s)
534+
535+
()
536+
// let is = interesections
537+
538+
// assert
539+
}

0 commit comments

Comments
 (0)