@@ -14,6 +14,7 @@ import Bag (bagToList)
1414import BasicTypes
1515import Control.Monad
1616import Data.Bool (bool)
17+import Data.Char (isAlphaNum)
1718import Data.Data hiding (Infix, Prefix)
1819import Data.List (intersperse, sortOn)
1920import Data.Text (Text)
@@ -803,7 +804,15 @@ p_hsBracket = \case
803804 TypBr NoExt ty -> quote "t" (located ty p_hsType)
804805 VarBr NoExt isSingleQuote name -> do
805806 txt (bool "''" "'" isSingleQuote)
806− p_rdrName (noLoc name)
807+ -- HACK As you can see we use 'noLoc' here to be able to pass name into
808+ -- 'p_rdrName' since the latter expects a "located" thing. The problem
809+ -- is that 'VarBr' doesn't provide us with location of the name. This in
810+ -- turn makes it impossible to detect if there are parentheses around
811+ -- it, etc. So we have to add parentheses manually assuming they are
812+ -- necessary for all operators.
813+ let isOperator = not (any isAlphaNum (showOutputable name))
814+ wrapper = if isOperator then parens else id
815+ wrapper $ p_rdrName (noLoc name)
807816 TExpBr NoExt expr -> do
808817 txt "[||"
809818 breakpoint'