GHC主张的神奇规则在哪里?

时间:2014-04-10 19:03:35

标签: haskell ghc assertions

http://hackage.haskell.org/package/base-4.6.0.1/docs/src/GHC-Base.html#assert似乎将assert定义为无操作。在启用断言时,将其转换为其他内容的逻辑在哪里?

1 个答案:

答案 0 :(得分:8)

该函数上面的注释提示:

-- Assertion function.  This simply ignores its boolean argument.
-- The compiler may rewrite it to @('assertError' line)@.

所以,只需使用github代码搜索并搜索assertErrorsearch results

这会调出文件RnExpr.lhs。在此文件中搜索断言,您将找到以下代码:

finishHsVar :: Name -> RnM (HsExpr Name, FreeVars)
-- Separated from rnExpr because it's also used
-- when renaming infix expressions
-- See Note [Adding the implicit parameter to 'assert']
finishHsVar name
 = do { this_mod <- getModule
      ; when (nameIsLocalOrFrom this_mod name) $
        checkThLocalName name

      ; ignore_asserts <- goptM Opt_IgnoreAsserts
      ; if ignore_asserts || not (name `hasKey` assertIdKey)
        then return (HsVar name, unitFV name)
        else do { e <- mkAssertErrorExpr
                ; return (e, unitFV name) } }

这就是assert取代assertError的地方,但只有在启用了断言的情况下才会这样做。 assertError中定义了{{1}}