How to handle “panic: the impossible happened” and continue in Haskell

穿精又带淫゛_ 提交于 2019-12-07 01:48:55

问题


I have the following code that uses the GHC API to load modules and get the type of an expression:

typeObjects :: [String] -> [String] -> IO [Type]
typeObjects modules objects = do
  defaultErrorHandler defaultDynFlags $ do
    runGhc (Just libdir) $ do
      dflags <- getSessionDynFlags
      setSessionDynFlags dflags
      targets <- mapM ((flip guessTarget) Nothing) modules
      setTargets targets
      result <- load LoadAllTargets
      case result of
          Failed -> error "Compilation failed"
          Succeeded -> do
            m <- mapM (((flip findModule) Nothing) . mkModuleName) modules
            setContext m []
            values <- mapM exprType objects
            return values

If the expressions don't typecheck, the whole program crashes with:

TestDynamicLoad: panic! (the 'impossible' happened)
   (GHC version 7.0.3.20110330 for x86_64-unknown-linux):
    Couldn't match expected type `GHC.Types.Int'
            with actual type `[GHC.Types.Char]'

How can I make it so it won't crash the program? I just want to know which expressions type checked successfully and which did not.


回答1:


You can't handle it - this is like a kernel 'oops', and means the runtime or compiler is in an inconsistent state. Report it as a bug.



来源:https://stackoverflow.com/questions/9242996/how-to-handle-panic-the-impossible-happened-and-continue-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!