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

Bug fix: Pexp_constraint with attributes should get parenthesized #2513

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ profile. This started with version 0.26.0.
- \* Fix unwanted alignment after comment (#2507, @Julow)
- \* Fix unwanted alignment in if-then-else (#2511, @Julow)
- Fix position of comments around and within `(type ...)` function arguments (#2503, @gpetiot)
- Fix missing parentheses around constraint expressions with attributes (#2513, @alanechang)

## 0.26.1 (2023-09-15)

Expand Down
5 changes: 4 additions & 1 deletion lib/Ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2337,11 +2337,14 @@ end = struct
, Some
( { pexp_desc=
( Pexp_ident _ | Pexp_constant _ | Pexp_record _
| Pexp_field _ )
| Pexp_constraint _ | Pexp_field _ )
; _ } as e0 ) )
when e0 == exp ->
false
| Pexp_record (_, Some e0) when e0 == exp -> true
| Pexp_override fields
when List.exists fields ~f:(fun (_, e0) -> e0 == exp) ->
exposed_right_exp Sequence exp
| Pexp_sequence (lhs, rhs) -> exp_in_sequence lhs rhs exp
| Pexp_apply (_, args)
when List.exists args ~f:(fun (_, e0) ->
Expand Down
11 changes: 6 additions & 5 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2159,11 +2159,12 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
pro
$ hvbox
(Params.Indent.exp_constraint c.conf)
( wrap_fits_breaks ~space:false c.conf "(" ")"
( fmt_expression c (sub_exp ~ctx e)
$ fmt "@ : "
$ fmt_core_type c (sub_typ ~ctx t) )
$ fmt_atrs )
(Params.parens_if parens c.conf
( wrap_fits_breaks ~space:false c.conf "(" ")"
( fmt_expression c (sub_exp ~ctx e)
$ fmt "@ : "
$ fmt_core_type c (sub_typ ~ctx t) )
$ fmt_atrs ) )
| Pexp_construct ({txt= Lident (("()" | "[]") as txt); loc}, None) ->
let opn = char txt.[0] and cls = char txt.[1] in
pro
Expand Down
6 changes: 6 additions & 0 deletions test/passing/tests/attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,9 @@ let raise_length_mismatch name n1 n2 =
external unsafe_memset : t -> pos:int -> len:int -> char -> unit
= "bigstring_memset_stub"
[@@noalloc]

let _ = f ((1 : int) [@a])

let _ = f ((1 : int) [@a]) ((1 : int) [@a])

let _ = f ((((1 : int) [@a]) : (int[@b])) [@a]) ((1 : int) [@a])
2 changes: 1 addition & 1 deletion test/passing/tests/override.ml.ref
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ let _ = {<x = ((x [@a]) : (t[@b])) [@c]>}

let _ = {<x>}

let _ = {<x = (x [@a])>}
let _ = {<x = x [@a]>}

let _ = {<x>}
Loading