From bdb7f7caf72e3f24c43586354044a7940c0e51a9 Mon Sep 17 00:00:00 2001 From: Spotandjake <40705786+spotandjake@users.noreply.github.com> Date: Sun, 16 Feb 2025 17:49:39 -0500 Subject: [PATCH] fix(grainfmt): Correct formatting of nested constraints (#2245) --- compiler/src/formatting/fmt.re | 7 +++++++ compiler/test/grainfmt/constraints.expected.gr | 5 +++++ compiler/test/grainfmt/constraints.input.gr | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/compiler/src/formatting/fmt.re b/compiler/src/formatting/fmt.re index ad1bd25242..f64bfaf079 100644 --- a/compiler/src/formatting/fmt.re +++ b/compiler/src/formatting/fmt.re @@ -258,6 +258,13 @@ let needs_grouping = (~parent, ~side: infix_side, expr) => { | (PExpConstant(PConstNumber(PConstNumberRational(_))), _) when op_precedence('/') <= precedence(parent) => ParenGrouping + | (PExpConstraint(_, _), _) + when + switch (parent.pexp_desc) { + | PExpConstraint(_, _) => true + | _ => false + } => + ParenGrouping | _ => FormatterGrouping }; }; diff --git a/compiler/test/grainfmt/constraints.expected.gr b/compiler/test/grainfmt/constraints.expected.gr index 6f307c7fef..fb93f3e8e4 100644 --- a/compiler/test/grainfmt/constraints.expected.gr +++ b/compiler/test/grainfmt/constraints.expected.gr @@ -8,3 +8,8 @@ let test = test => { print(i) }, test: List) } + +// Regression #2244 +(1: Number): Number + +((1: Number): Number): Number diff --git a/compiler/test/grainfmt/constraints.input.gr b/compiler/test/grainfmt/constraints.input.gr index 6f307c7fef..fb93f3e8e4 100644 --- a/compiler/test/grainfmt/constraints.input.gr +++ b/compiler/test/grainfmt/constraints.input.gr @@ -8,3 +8,8 @@ let test = test => { print(i) }, test: List) } + +// Regression #2244 +(1: Number): Number + +((1: Number): Number): Number