ghc-exactprint icon indicating copy to clipboard operation
ghc-exactprint copied to clipboard

Roundtripping breaks when using exactPrint on single LHsDecl

Open lspitzner opened this issue 8 years ago • 0 comments

code to reproduce:

module Main where



import           DynFlags ( getDynFlags )
import           GHC ( runGhc, GenLocated(L), moduleNameString )
import qualified DynFlags      as GHC
import qualified GHC           as GHC hiding (parseModule)
import qualified Parser        as GHC
import qualified SrcLoc        as GHC
import           RdrName ( RdrName(..) )
import           HsSyn
import           SrcLoc ( SrcSpan, Located )
import           RdrName ( RdrName(..) )

import Control.Monad.Trans.Either as EitherT
import qualified System.IO
import Control.Monad.IO.Class
import Control.Monad.Trans.Class

import qualified Language.Haskell.GHC.ExactPrint            as ExactPrint
import qualified Language.Haskell.GHC.ExactPrint.Parsers    as ExactPrint

import qualified Data.Generics as SYB

import Control.Monad



(<&>) = flip (<$>)

parseModuleFromString
  :: [String]
  -> System.IO.FilePath
  -> String
  -> IO (Either String (ExactPrint.Anns, GHC.ParsedSource))
parseModuleFromString args fp str =
  ExactPrint.ghcWrapper $ EitherT.runEitherT $ do
    dflags0                       <- lift $ ExactPrint.initDynFlagsPure fp str
    (dflags1, leftover, warnings) <-
      lift $ GHC.parseDynamicFlagsCmdLine dflags0 (GHC.noLoc <$> args)
    when (not $ null leftover)
      $  EitherT.left
      $  "when parsing ghc flags: leftover flags: "
      ++ show (leftover <&> \(L _ s) -> s)
    when (not $ null warnings)
      $  EitherT.left
      $  "when parsing ghc flags: encountered warnings: "
      ++ show (warnings <&> \(L _ s) -> s)
    EitherT.hoistEither
      $ either (\(span, err) -> Left $ show span ++ ": " ++ err)
               (\(a, m) -> Right (a, m))
      $ ExactPrint.parseWith dflags1 fp GHC.parseModule str

main = do
  let extensions = []
  let input = "type instance HReplicateR 'HZero e = '[]"
  Right (anns, parsedSource@(L _ modul)) <- parseModuleFromString extensions "stdin" input
  putStrLn $ ExactPrint.exactPrint parsedSource anns
  hsmodDecls modul `forM_` \d -> putStrLn $ ExactPrint.exactPrint d anns

then

> ghc Main.hs -package ghc-8.0.2
> [1 of 1] Compiling Main             ( Main.hs, Main.o )
Linking Main ...
> ./Main
type instance HReplicateR 'HZero e = '[]
type HReplicateR 'HZero e = '[]            # this is problematic.

lspitzner avatar Jun 03 '17 18:06 lspitzner