From fefa883096b4ad0f2da0d4dd4c358d55c24cb854 Mon Sep 17 00:00:00 2001 From: Alexander Esgen Date: Tue, 26 Nov 2024 21:31:03 +0100 Subject: [PATCH] Correctly format non-promoted type-level tuples This can only occur with `NoListTuplePuns`. Closes #1146 --- CHANGELOG.md | 3 +++ .../declaration/type/promotion-no-puns-out.hs | 5 +++++ data/examples/declaration/type/promotion-no-puns.hs | 5 +++++ src/Ormolu/Printer/Meat/Type.hs | 12 ++++++++---- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 data/examples/declaration/type/promotion-no-puns-out.hs create mode 100644 data/examples/declaration/type/promotion-no-puns.hs diff --git a/CHANGELOG.md b/CHANGELOG.md index 79aaa392b..783e96306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,9 @@ * GHC proposal [#281](https://github.com/ghc-proposals/ghc-proposals/blob/c9401f037cb22d1661931b2ec621925101052997/proposals/0281-visible-forall.rst): accept more types in terms: `forall` quantifications, constraint arrows `=>`, type arrows `->` (enabled by default) * Part of GHC proposal [#425](https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0425-decl-invis-binders.rst): wildcard binders (enabled by default) +* Correctly format non-promoted type-level tuples with `NoListTuplePuns`. [Issue + 1146](https://github.com/tweag/ormolu/issues/1146). + ## Ormolu 0.7.7.0 * Use single-line layout for parens around single-line content. [Issue diff --git a/data/examples/declaration/type/promotion-no-puns-out.hs b/data/examples/declaration/type/promotion-no-puns-out.hs new file mode 100644 index 000000000..8579e5389 --- /dev/null +++ b/data/examples/declaration/type/promotion-no-puns-out.hs @@ -0,0 +1,5 @@ +{-# LANGUAGE NoListTuplePuns #-} + +type X = (Int, String) + +type Y = [String, Int] diff --git a/data/examples/declaration/type/promotion-no-puns.hs b/data/examples/declaration/type/promotion-no-puns.hs new file mode 100644 index 000000000..16574ebbb --- /dev/null +++ b/data/examples/declaration/type/promotion-no-puns.hs @@ -0,0 +1,5 @@ +{-# Language NoListTuplePuns #-} + +type X = (Int, String) + +type Y = [String, Int] diff --git a/src/Ormolu/Printer/Meat/Type.hs b/src/Ormolu/Printer/Meat/Type.hs index 770a0e596..622ec431f 100644 --- a/src/Ormolu/Printer/Meat/Type.hs +++ b/src/Ormolu/Printer/Meat/Type.hs @@ -156,11 +156,15 @@ p_hsType' multilineArgs = \case (IsPromoted, L _ t : _) | startsWithSingleQuote t -> space _ -> return () sep commaDel (sitcc . located' p_hsType) xs - HsExplicitTupleTy _ _p xs -> do - txt "'" + HsExplicitTupleTy _ p xs -> do + case p of + IsPromoted -> txt "'" + NotPromoted -> return () parens N $ do - case xs of - L _ t : _ | startsWithSingleQuote t -> space + -- If this tuple is promoted and the first element starts with a single + -- quote, we need to put a space in between or it fails to parse. + case (p, xs) of + (IsPromoted, L _ t : _) | startsWithSingleQuote t -> space _ -> return () sep commaDel (located' p_hsType) xs HsTyLit _ t ->