首页 > 解决方案 > 使用 GHC 插件解析类型

问题描述

我正在开发一个编译器插件,如果声明没有作为插件参数给出的类型,它应该中止编译。

我认为该功能Unify.tcMatchTy :: Type -> Type -> Maybe TCvSubstS可以解决问题。我可以从类型检查器插件的输入中找到其中一种参数类型,但另一种作为插件参数出现。也就是说,作为一个String.

我如何在类型检查器插件中Type从 a 中获取 a String?我尝试使用parseType后跟调用rnHsTypeand tcLHsType,它确实将 a 转换StringType. 这似乎适用于简单类型,但是当字符串包含类型变量时会导致这种错误:

Text:0:0: error: Not in scope: type variable ‘a’

Text:0:0: error:
    • GHC internal error: ‘a’ is not in scope during type checking, but it passed the renamer
      tcl_env of environment: []
    • In the type ‘a -> b’

我可能在这里遗漏了一些(或者可能是一大块)工作,但是我应该在哪里寻找呢?

(在输入前加上 a 会forall a b.导致编译器恐慌)

标签: haskellghcghc-api

解决方案


推荐阅读