Fix printing of splices; part of #4124
authorIan Lynagh <igloo@earth.li>
Sun, 13 Jun 2010 15:48:38 +0000 (15:48 +0000)
committerIan Lynagh <igloo@earth.li>
Sun, 13 Jun 2010 15:48:38 +0000 (15:48 +0000)
Just putting parens around non-atomic expressions isn't sufficient
for splices, as only the $x and $(e) forms are valid input.

compiler/hsSyn/HsExpr.lhs

index 7930caa..84901ee 100644 (file)
@@ -1066,8 +1066,16 @@ instance OutputableBndr id => Outputable (HsSplice id) where
 
 pprSplice :: OutputableBndr id => HsSplice id -> SDoc
 pprSplice (HsSplice n e)
-    = char '$' <> ifPprDebug (brackets (ppr n)) <> pprParendExpr e
-
+    = char '$' <> ifPprDebug (brackets (ppr n)) <> eDoc
+    where
+          -- We use pprLExpr to match pprParendExpr:
+          --     Using pprLExpr makes sure that we go 'deeper'
+          --     I think that is usually (always?) right
+          pp_as_was = pprLExpr e
+          eDoc = case unLoc e of
+                 HsPar _ -> pp_as_was
+                 HsVar _ -> pp_as_was
+                 _ -> parens pp_as_was
 
 data HsBracket id = ExpBr (LHsExpr id)   -- [|  expr  |]
                   | PatBr (LPat id)      -- [p| pat   |]