Purely using the parser entry points

Matthew Pickering matthewtpickering at gmail.com
Fri Jul 3 16:16:44 UTC 2015

I've been working with the new parser entry points - they have been
very useful.

Ostensibly the new parser entry points are pure functions but they
require a copy of DynFlags. As far as I can work out there's no pure
way to reliably construct DynFlags but it also seems that the Parser
doesn't use any of the impure mutable parts of it. This makes it
tricky to write pure functions such as "parseExprFromString :: String
-> LHsExpr RdrName" which would use some sort of dummy configuration.

So far I've tried using "unsafeGlobalDynFlags" which (expectedly)
fails occasionally. I've found a more reliable method to be the
following which relies on unsafePerformIO.

getDynFlags = unsafePerformIO $
  GHC.defaultErrorHandler GHC.defaultFatalMessager GHC.defaultFlushOut $
    GHC.runGhc (Just libdir) GHC.getSessionDynFlags

The question is - is this use of unsafePerformIO safe? Is there a
better way to purely initialise a copy of DynFlags?

Thanks, Matt

More information about the ghc-devs mailing list