@@ -8,6 +8,7 @@ module Ormolu.Printer.Meat.Common
88 , p_hsmodName
99 , p_ieWrappedName
1010 , p_rdrName
11+ , doesNotNeedExtraParens
1112 , p_qualName
1213 , p_ieWildcard
1314 , p_infixDefHelper
@@ -63,12 +64,12 @@ p_rdrName l@(L spn _) = located l $ \x -> do
6364 txt "'"
6465 y
6566 else id
66− (m, avoidParens) =
67+ m =
6768 case x of
6869 Unqual occName ->
69− (atom occName, False)
70+ atom occName
7071 Qual mname occName ->
71− (p_qualName mname occName, False)
72+ p_qualName mname occName
7273 Orig _ occName ->
7374 -- NOTE This is used when GHC generates code that will be fed
7475 -- into the renamer (e.g. from deriving clauses), but where we
@@ -77,19 +78,30 @@ p_rdrName l@(L spn _) = located l $ \x -> do
7778 --
7879 -- My current understanding is that the provided module name
7980 -- serves no purpose for us and can be safely ignored.
80− (atom occName, False)
81+ atom occName
8182 Exact name ->
82− -- NOTE I'm not sure this "stable string" is stable enough, but
83− -- it looks like this is the most robust way to tell if we're
84− -- looking at exactly this piece of built-in syntax.
85− ( atom name
86− , "$ghc-prim$GHC.Tuple$" `isPrefixOf` nameStableString name
87− )
83+ atom name
8884 m' = backticksWrapper (singleQuoteWrapper m)
89− if avoidParens
85+ if doesNotNeedExtraParens x
9086 then m'
9187 else parensWrapper m'
9288
89+-- | Whether given name should not have parentheses around it. This is used
90+-- to detect e.g. tuples for which annotations will indicate parentheses,
91+-- but the parentheses are already part of the symbol, so no extra layer of
92+-- parentheses should be added. It also detects the [] literal.
93+
94+doesNotNeedExtraParens :: RdrName -> Bool
95+doesNotNeedExtraParens = \case
96+ Exact name ->
97+ let s = nameStableString name
98+ -- NOTE I'm not sure this "stable string" is stable enough, but it looks
99+ -- like this is the most robust way to tell if we're looking at exactly
100+ -- this piece of built-in syntax.
101+ in ("$ghc-prim$GHC.Tuple$" `isPrefixOf` s) ||
102+ ("$ghc-prim$GHC.Types$[]" `isPrefixOf` s)
103+ _ -> False
104+
93105p_qualName :: ModuleName -> OccName -> R ()
94106p_qualName mname occName = do
95107 atom mname