@@ -8,6 +8,7 @@ module Ormolu.Printer.Meat.Common
8 , p_hsmodName
8 , p_hsmodName
9 , p_ieWrappedName
9 , p_ieWrappedName
10 , p_rdrName
10 , p_rdrName
11+ , doesNotNeedExtraParens
11 , p_qualName
12 , p_qualName
12 , p_ieWildcard
13 , p_ieWildcard
13 , p_infixDefHelper
14 , p_infixDefHelper
@@ -63,12 +64,12 @@ p_rdrName l@(L spn _) = located l $ \x -> do
66− (m, avoidParens) =
67+ m =
68 Unqual occName ->
69 Unqual occName ->
69− (atom occName, False)
70+ atom occName
70 Qual mname occName ->
71 Qual mname occName ->
71− (p_qualName mname occName, False)
72+ p_qualName mname occName
72 Orig _ occName ->
73 Orig _ occName ->
73 -- NOTE This is used when GHC generates code that will be fed
74 -- NOTE This is used when GHC generates code that will be fed
74 -- into the renamer (e.g. from deriving clauses), but where we
75 -- into the renamer (e.g. from deriving clauses), but where we
@@ -77,19 +78,30 @@ p_rdrName l@(L spn _) = located l $ \x -> do
78 -- My current understanding is that the provided module name
79 -- My current understanding is that the provided module name
79 -- serves no purpose for us and can be safely ignored.
80 -- serves no purpose for us and can be safely ignored.
80− (atom occName, False)
81+ atom occName
81 Exact name ->
82 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
83+ atom name
86− , "$ghc-prim$GHC.Tuple$" `isPrefixOf` nameStableString name
88 m' = backticksWrapper (singleQuoteWrapper m)
84 m' = backticksWrapper (singleQuoteWrapper m)
89− if avoidParens
85+ if doesNotNeedExtraParens x
91 else parensWrapper m'
87 else parensWrapper m'
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.
94+doesNotNeedExtraParens :: RdrName -> Bool
95+doesNotNeedExtraParens = \case
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)
93p_qualName :: ModuleName -> OccName -> R ()
105p_qualName :: ModuleName -> OccName -> R ()
94p_qualName mname occName = do
106p_qualName mname occName = do
95 atom mname
107 atom mname