Skip to content

template-haskell pretty printer prints syntactically wrong where blocks

Summary

the pretty printer in template-haskell has started pretty printing where declarations wrapped in braces, however multi line declarations don't end with a semicolon resulting in a parse error when the pretty printed code is compiled with GHC.

Steps to reproduce

Run the following with a newer ghc + template-haskell

{-# LANGUAGE QuasiQuotes, TemplateHaskell #-}

module T where

import Language.Haskell.TH
import Language.Haskell.TH.Ppr

main = runQ t1 >>= (putStrLn . pprint)

t1 = [d|
      fac n = go n
       where go 0 = 1
             go x = x * go (x - 1)
     |]
$ ghc T.hs -main-is T.main
$ ./T
fac_0 n_1 = go_2 n_1
          where {go_2 0 = 1
                 go_2 x_3 = x_3 GHC.Num.* go_2 (x_3 GHC.Num.- 1)}

This currently break openapi3-code-generator when used with a newer template-haskell library.

Expected behavior

It should instead print the following haskell code


fac_0 n_1 = go_2 n_1
          where {go_2 0 = 1;
                 go_2 x_3 = x_3 GHC.Num.* go_2 (x_3 GHC.Num.- 1)}

Environment

  • GHC version used: 9.8.1
  • template-haskell library: 2.21.0.0, bug seems to be present since 2.19.0.0

Optional:

  • Operating System: nixos
  • System Architecture: x86_64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information