Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-stabilizing comments attached to private/virtual/mutable keywords #2272

Merged
merged 8 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Bug fixes

- Fix non-stabilizing comments attached to private/virtual/mutable keywords (#2272, @gpetiot)

### Changes

### New features
Expand Down
30 changes: 15 additions & 15 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -341,37 +341,37 @@ let fmt_direction_flag = function
| Upto -> fmt "@ to "
| Downto -> fmt "@ downto "

let fmt_private c loc = hvbox 0 @@ Cmts.fmt c loc @@ str "private"

let fmt_virtual c loc = hvbox 0 @@ Cmts.fmt c loc @@ str "virtual"

let fmt_mutable c loc = hvbox 0 @@ Cmts.fmt c loc @@ str "mutable"

let fmt_private_flag c = function
| Private loc -> fmt " " $ Cmts.fmt c loc @@ str "private"
| Private loc -> fmt "@ " $ fmt_private c loc
| Public -> noop

let fmt_virtual_flag c = function
| Virtual loc -> fmt " " $ Cmts.fmt c loc @@ str "virtual"
| Virtual loc -> fmt "@ " $ fmt_virtual c loc
| Concrete -> noop

let fmt_mutable_flag c = function
| Mutable loc -> Cmts.fmt c loc @@ str "mutable" $ fmt " "
| Mutable loc -> fmt_mutable c loc $ fmt "@ "
| Immutable -> noop

let opt_flag ~f c x = opt x (fun x -> fmt "@ " $ f c x)

let fmt_mutable_virtual_flag c = function
| {mv_mut= Some m; mv_virt= Some v} when Location.compare_start v m < 1 ->
fmt " "
$ Cmts.fmt c v @@ str "virtual"
$ fmt " "
$ Cmts.fmt c m @@ str "mutable"
opt_flag c (Some v) ~f:fmt_virtual $ opt_flag c (Some m) ~f:fmt_mutable
Julow marked this conversation as resolved.
Show resolved Hide resolved
| {mv_mut; mv_virt} ->
opt mv_mut (fun m -> fmt " " $ Cmts.fmt c m @@ str "mutable")
$ opt mv_virt (fun v -> fmt " " $ Cmts.fmt c v @@ str "virtual")
opt_flag c mv_mut ~f:fmt_mutable $ opt_flag c mv_virt ~f:fmt_virtual

let fmt_private_virtual_flag c = function
| {pv_priv= Some p; pv_virt= Some v} when Location.compare_start v p < 1 ->
fmt " "
$ Cmts.fmt c v @@ str "virtual"
$ fmt " "
$ Cmts.fmt c p @@ str "private"
opt_flag c ~f:fmt_virtual (Some v) $ opt_flag c ~f:fmt_private (Some p)
| {pv_priv; pv_virt} ->
opt pv_priv (fun p -> fmt " " $ Cmts.fmt c p @@ str "private")
$ opt pv_virt (fun v -> fmt " " $ Cmts.fmt c v @@ str "virtual")
opt_flag c ~f:fmt_private pv_priv $ opt_flag c ~f:fmt_virtual pv_virt

let virtual_or_override = function
| Cfk_virtual _ -> noop
Expand Down
4 changes: 4 additions & 0 deletions test/passing/tests/comments.ml
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,7 @@ let _ = (*x*)!(*a*) (*b*) x (*c*) y
let _ = f ( (*x*)!(*a*) (*b*) x (*c*) y ) y

type a = b (* a *) as (* b *) 'c (* c *)

type t = { (* comment before mutable *) mutable
(* really long comment that doesn't fit on the same line as other stuff *)
x : int }
8 changes: 8 additions & 0 deletions test/passing/tests/comments.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,11 @@ let _ =
y

type a = b (* a *) as (* b *) 'c (* c *)

type t =
{ (* comment before mutable *)
mutable
(* really long comment that doesn't fit on the same line as other
stuff *)
x:
int }