Pretty-printing of derived multi-parameter classes omits necessary parentheses
Take this file:
```hs
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS -ddump-splices #-}
import Language.Haskell.TH
class C a b
main :: IO ()
main = putStrLn $([d| data Foo a = Foo a deriving (C a) |] >>= stringE . show)
```
When this is compiled, `-ddump-splices` does not faithfully print back what was written by the user:
```
[1 of 1] Compiling Main ( Bug.hs, interpreted )
Bug.hs:10:19-77: Splicing expression
[d| data Foo_a4l1 a_a4l3
= Foo_a4l2 a_a4l3
deriving C a_a4l3 |]
>>= stringE . show
======>
"[DataD [] Foo_6989586621679026555 [PlainTV a_6989586621679026557] Nothing [NormalC Foo_6989586621679026556 [(Bang NoSourceUnpackedness NoSourceStrictness,VarT a_6989586621679026557)]] [DerivClause Nothing [AppT (ConT Main.C) (VarT a_6989586621679026557)]]]"
```
In particular, this pretty-prints `deriving C a_a4l3`, which doesn't even parse correctly. It should print `deriving (C a_a4l3)`. Ultimately, there is an off-by-one error in the number of parentheses being printed, since if you tweak the original example by adding another set of parentheses:
```hs
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS -ddump-splices #-}
import Language.Haskell.TH
class C a b
main :: IO ()
main = putStrLn $([d| data Foo a = Foo a deriving ((C a)) |] >>= stringE . show)
```
Then it pretty-prints exactly one set of parentheses (as opposed to two):
```
[1 of 1] Compiling Main ( Bug.hs, interpreted )
Bug.hs:10:19-79: Splicing expression
[d| data Foo_a1zI a_a1zK
= Foo_a1zJ a_a1zK
deriving (C a_a1zK) |]
>>= stringE . show
======>
"[DataD [] Foo_6989586621679026522 [PlainTV a_6989586621679026524] Nothing [NormalC Foo_6989586621679026523 [(Bang NoSourceUnpackedness NoSourceStrictness,VarT a_6989586621679026524)]] [DerivClause Nothing [AppT (ConT Main.C) (VarT a_6989586621679026524)]]]"
```
There are two suspicious parts of the GHC codebase that contribute to this bug:
- This parser production: http://git.haskell.org/ghc.git/blob/4364f1e7543b6803cfaef321105d253e0bdf08a4:/compiler/parser/Parser.y\#l2162
This recognizes singleton derived classes that are surrounded by a set of parentheses, and "strips off" the parentheses.
- This `Outputable` instance: http://git.haskell.org/ghc.git/blob/4364f1e7543b6803cfaef321105d253e0bdf08a4:/compiler/hsSyn/HsDecls.hs\#l1102
This code detects lists of two or more derived classes and surrounds them with extra parentheses to make up for the set that was removed during parsing. But this fails to detect the case of `deriving (C a)`, since this only contains a single class.
Indeed, trying to distinguish between, say, `deriving T` and `deriving (T)` at this level would be quite tricky.
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ----------------- |
| Version | 8.2.1 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler (Parser) |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | alanz |
| Operating system | |
| Architecture | |
</details>
<!-- {"blocked_by":[],"summary":"Pretty-printing of derived multi-parameter classes omits necessary parentheses","status":"New","operating_system":"","component":"Compiler (Parser)","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"8.2.1","keywords":[],"differentials":[],"test_case":"","architecture":"","cc":["alanz"],"type":"Bug","description":"Take this file:\r\n\r\n{{{#!hs\r\n{-# LANGUAGE MultiParamTypeClasses #-}\r\n{-# LANGUAGE TemplateHaskell #-}\r\n{-# OPTIONS -ddump-splices #-}\r\n\r\nimport Language.Haskell.TH\r\n\r\nclass C a b\r\n\r\nmain :: IO ()\r\nmain = putStrLn $([d| data Foo a = Foo a deriving (C a) |] >>= stringE . show)\r\n}}}\r\n\r\nWhen this is compiled, `-ddump-splices` does not faithfully print back what was written by the user:\r\n\r\n{{{\r\n[1 of 1] Compiling Main ( Bug.hs, interpreted )\r\nBug.hs:10:19-77: Splicing expression\r\n [d| data Foo_a4l1 a_a4l3\r\n = Foo_a4l2 a_a4l3\r\n deriving C a_a4l3 |]\r\n >>= stringE . show\r\n ======>\r\n \"[DataD [] Foo_6989586621679026555 [PlainTV a_6989586621679026557] Nothing [NormalC Foo_6989586621679026556 [(Bang NoSourceUnpackedness NoSourceStrictness,VarT a_6989586621679026557)]] [DerivClause Nothing [AppT (ConT Main.C) (VarT a_6989586621679026557)]]]\"\r\n}}}\r\n\r\nIn particular, this pretty-prints `deriving C a_a4l3`, which doesn't even parse correctly. It should print `deriving (C a_a4l3)`. Ultimately, there is an off-by-one error in the number of parentheses being printed, since if you tweak the original example by adding another set of parentheses:\r\n\r\n{{{#!hs\r\n{-# LANGUAGE MultiParamTypeClasses #-}\r\n{-# LANGUAGE TemplateHaskell #-}\r\n{-# OPTIONS -ddump-splices #-}\r\n\r\nimport Language.Haskell.TH\r\n\r\nclass C a b\r\n\r\nmain :: IO ()\r\nmain = putStrLn $([d| data Foo a = Foo a deriving ((C a)) |] >>= stringE . show)\r\n}}}\r\n\r\nThen it pretty-prints exactly one set of parentheses (as opposed to two):\r\n\r\n{{{\r\n[1 of 1] Compiling Main ( Bug.hs, interpreted )\r\nBug.hs:10:19-79: Splicing expression\r\n [d| data Foo_a1zI a_a1zK\r\n = Foo_a1zJ a_a1zK\r\n deriving (C a_a1zK) |]\r\n >>= stringE . show\r\n ======>\r\n \"[DataD [] Foo_6989586621679026522 [PlainTV a_6989586621679026524] Nothing [NormalC Foo_6989586621679026523 [(Bang NoSourceUnpackedness NoSourceStrictness,VarT a_6989586621679026524)]] [DerivClause Nothing [AppT (ConT Main.C) (VarT a_6989586621679026524)]]]\"\r\n}}}\r\n\r\nThere are two suspicious parts of the GHC codebase that contribute to this bug:\r\n\r\n* This parser production: http://git.haskell.org/ghc.git/blob/4364f1e7543b6803cfaef321105d253e0bdf08a4:/compiler/parser/Parser.y#l2162\r\n\r\n This recognizes singleton derived classes that are surrounded by a set of parentheses, and \"strips off\" the parentheses.\r\n\r\n* This `Outputable` instance: http://git.haskell.org/ghc.git/blob/4364f1e7543b6803cfaef321105d253e0bdf08a4:/compiler/hsSyn/HsDecls.hs#l1102\r\n\r\n This code detects lists of two or more derived classes and surrounds them with extra parentheses to make up for the set that was removed during parsing. But this fails to detect the case of `deriving (C a)`, since this only contains a single class.\r\n\r\n Indeed, trying to distinguish between, say, `deriving T` and `deriving (T)` at this level would be quite tricky.","type_of_failure":"OtherFailure","blocking":[]} -->
issue