7

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.

Davorak
  • 7,362
  • 1
  • 38
  • 48
mentics
  • 6,852
  • 5
  • 39
  • 93
  • 4
    If you managed to crash the compiler, it's a compiler error. Check if this is a known bug, else report it. – Landei Feb 11 '12 at 19:09
  • I don't think it's a bug. It's correct. I tried to compile at runtime an expression that tried to pass in a String where an Int was expected. I just want to "catch" that error and "mark" that expression as failed, whereas the others that do not cause an error, I'll consider as successful. – mentics Feb 11 '12 at 19:13
  • 10
    @taotree Reporting an error is correct behavior. Crashing with a "panic! (the 'impossible' happened)" is not correct behavior. Report it as a bug. – Daniel Wagner Feb 11 '12 at 20:55

1 Answers1

12

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.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468