【问题标题】:How to handle "panic: the impossible happened" and continue in Haskell如何处理“恐慌:不可能发生”并在 Haskell 中继续
【发布时间】:2012-03-03 19:41:27
【问题描述】:

我有以下代码使用 GHC API 来加载模块并获取表达式的类型:

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

如果表达式不进行类型检查,整个程序会崩溃:

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]'

我怎样才能使它不会使程序崩溃?我只想知道哪些表达式类型检查成功,哪些没有。

【问题讨论】:

  • 如果您设法使编译器崩溃,这是编译器错误。检查这是否是一个已知的错误,否则报告它。
  • 我不认为这是一个错误。这是正确的。我试图在运行时编译一个表达式,该表达式试图传入一个预期为 Int 的字符串。我只想“捕获”该错误并将该表达式“标记”为失败,而其他不会导致错误的表达式,我会认为是成功的。
  • @taotree 报告错误是正确的行为。因“恐慌!(‘不可能’发生)”而崩溃是不正确的行为。将其报告为错误。

标签: haskell dynamic-loading typechecking ghc-api


【解决方案1】:

您无法处理它 - 这就像内核“糟糕”,意味着运行时或编译器处于不一致状态。将其报告为错误。

【讨论】:

    猜你喜欢
    • 2022-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2015-03-29
    • 2021-07-23
    • 2023-02-04
    相关资源
    最近更新 更多