From dc6d8221657bfa608d80e7c197c18c804cee5c31 Mon Sep 17 00:00:00 2001 From: alanechang Date: Mon, 22 Jan 2024 15:45:41 -0500 Subject: [PATCH 1/4] add fix --- lib/Fmt_ast.ml | 12 +++++++----- test/passing/tests/attributes.ml | 6 ++++++ test/passing/tests/override.ml.ref | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/Fmt_ast.ml b/lib/Fmt_ast.ml index d39a838cfb..ebed246daa 100644 --- a/lib/Fmt_ast.ml +++ b/lib/Fmt_ast.ml @@ -2156,14 +2156,16 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens c.conf (fmt_constant c const $ fmt_atrs) | Pexp_constraint (e, t) -> + let outer_parens = has_attr && parens in 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 outer_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 diff --git a/test/passing/tests/attributes.ml b/test/passing/tests/attributes.ml index 2d7afb53f6..500429e940 100644 --- a/test/passing/tests/attributes.ml +++ b/test/passing/tests/attributes.ml @@ -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]) diff --git a/test/passing/tests/override.ml.ref b/test/passing/tests/override.ml.ref index ea238082d3..c686f0c84d 100644 --- a/test/passing/tests/override.ml.ref +++ b/test/passing/tests/override.ml.ref @@ -1,6 +1,6 @@ let _ = {} -let _ = {} +let _ = {} let _ = {} From fe117719e4b6951612b353464c795fb65304afbf Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 23 Jan 2024 11:24:54 +0100 Subject: [PATCH 2/4] Tweak Ast rules to avoid parentheses in records Avoid the extra parentheses in this case: {((a : b)) with c} --- lib/Ast.ml | 2 +- lib/Fmt_ast.ml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Ast.ml b/lib/Ast.ml index 522342075d..0d0f5591de 100644 --- a/lib/Ast.ml +++ b/lib/Ast.ml @@ -2337,7 +2337,7 @@ end = struct , Some ( { pexp_desc= ( Pexp_ident _ | Pexp_constant _ | Pexp_record _ - | Pexp_field _ ) + | Pexp_constraint _ | Pexp_field _ ) ; _ } as e0 ) ) when e0 == exp -> false diff --git a/lib/Fmt_ast.ml b/lib/Fmt_ast.ml index ebed246daa..f645e6f7d0 100644 --- a/lib/Fmt_ast.ml +++ b/lib/Fmt_ast.ml @@ -2156,11 +2156,10 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens c.conf (fmt_constant c const $ fmt_atrs) | Pexp_constraint (e, t) -> - let outer_parens = has_attr && parens in pro $ hvbox (Params.Indent.exp_constraint c.conf) - (Params.parens_if outer_parens c.conf + (Params.parens_if parens c.conf ( wrap_fits_breaks ~space:false c.conf "(" ")" ( fmt_expression c (sub_exp ~ctx e) $ fmt "@ : " From 2a5fc6e156880265304ab2bfa291f18f70dd3c6f Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Tue, 23 Jan 2024 11:33:10 +0100 Subject: [PATCH 3/4] Add missing Ast rule for override fields Avoid parentheses in this case: {} --- lib/Ast.ml | 3 +++ test/passing/tests/override.ml.ref | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/Ast.ml b/lib/Ast.ml index 0d0f5591de..36c45209da 100644 --- a/lib/Ast.ml +++ b/lib/Ast.ml @@ -2342,6 +2342,9 @@ end = struct 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) -> diff --git a/test/passing/tests/override.ml.ref b/test/passing/tests/override.ml.ref index c686f0c84d..9195efafd8 100644 --- a/test/passing/tests/override.ml.ref +++ b/test/passing/tests/override.ml.ref @@ -1,9 +1,9 @@ let _ = {} -let _ = {} +let _ = {} let _ = {} -let _ = {} +let _ = {} let _ = {} From bf21414e542159fbaa4d24805cce4ade811507e6 Mon Sep 17 00:00:00 2001 From: alanechang Date: Tue, 23 Jan 2024 10:04:10 -0500 Subject: [PATCH 4/4] add changelog entry --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index d454091d21..c58412d024 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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)