Skip to content

Pretty-printing of derived multi-parameter classes omits necessary parentheses

Take this file:

{-# 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:

{-# 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:

Trac metadata
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
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information