从模板 Haskell 拼接发出警告

发布于 2024-11-03 07:22:42 字数 502 浏览 1 评论 0原文

我知道我可以通过从拼接调用 fail 来导致编译时错误,但是是否有可能只生成警告?我特别希望在使用 -Werror 编译时能够将此警告转换为错误。

本质上我想做的是:

todo :: Q Exp
todo = do
    -- emit warning somehow

    loc <- location
    let message = ... -- generate message based on loc
    [| error $(litE (stringL message)) |]

这个想法是在编码时使用 this 而不是 undefined ,但确保它不会通过使用 编译来潜入生产代码 -错误

myFunc x | isSimpleCase x = 42
         | otherwise = $todo

I know that I can cause a compile-time error by calling fail from a splice, but is it possible to only generate a warning? In particular I would like it to be possible to turn this warning into an error when compiling with -Werror.

Essentially what I'm trying to do is this:

todo :: Q Exp
todo = do
    -- emit warning somehow

    loc <- location
    let message = ... -- generate message based on loc
    [| error $(litE (stringL message)) |]

The idea is to use this instead of undefined while coding, but make sure it doesn't sneak its way into production code by compiling with -Werror.

myFunc x | isSimpleCase x = 42
         | otherwise = $todo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

新人笑 2024-11-10 07:22:42

原来我想要的函数是 Template Haskell 函数 report。它的类型签名在文档中,但我必须阅读源代码才能弄清楚它的作用。 TH 文档确实需要一些改进。

不管怎样,我的 todo 占位符现在工作得很好,如果有人感兴趣的话,我很快就会在 Hackage 上发布一些东西。

Turns out the function I was after was the Template Haskell function report. Its type signature was in the documentation, but I had to read the source code to figure out what it did. The TH documentation sure could use some improvements.

Anyway, my todo placeholder is now working perfectly, and I'll put something up on Hackage soon if anyone is interested.

久随 2024-11-10 07:22:42

我不认为这在 TH 中是可能的,但这是一个非常酷的想法。

实现它的一种方法是通过绑定到 GHC-API 警告和 调试输出

例如,假装是 GHC,

import Panic
main = sorry "help!"

会产生

$ ./A                                
A: A: sorry! (unimplemented feature or known bug)
  (GHC version 7.0.2 for x86_64-unknown-linux):
    help!

构造 GHC 警告,其工作方式应该类似,检查 -Werror 是否已设置,并且您可以清理 API,使其非常有用。

I don't believe this is possible naively from TH, but it is a really cool idea.

One way to implement it would be via bindings to the GHC-API warning and debug output or error functions.

E.g. to pretend to be GHC,

import Panic
main = sorry "help!"

produces

$ ./A                                
A: A: sorry! (unimplemented feature or known bug)
  (GHC version 7.0.2 for x86_64-unknown-linux):
    help!

Constructing GHC warnings should work similarly, checking if -Werror is set, and you could clean up the API to be quite useful.

一城柳絮吹成雪 2024-11-10 07:22:42

要从 Template Haskell 拼接发出警告,
您可以使用 reportWarning :: String -> Q()

它已经包含位置(行和列)。
您可以简单地通过以下方式实现您的 todo 函数:

todo :: Q Exp
todo = do
  reportWarning "TODO"
  [| undefined |]

更多信息

@hammar 的答案指示了函数 report。它自 GHC 7.6 (2012) 起已被弃用,并且可能会在不久的将来从 API 中删除。
(但是,报告仍然可用
关于 GHC 7.10< /a>

在 GHC 主分支上
截至 2015 年。)

使用 reportError 报告错误并继续进行 Q 计算
(无论如何最终编译失败)。

使用 fail 停止并出现错误 (GHC ≤ 7.10)。那可能
不适用于 GHC 8.0

To emit warnings from Template Haskell splices,
you can use reportWarning :: String -> Q ().

It already includes the location (line and column).
You can implement your todo function simply by:

todo :: Q Exp
todo = do
  reportWarning "TODO"
  [| undefined |]

Further information

@hammar's answer indicates the function report. It has been deprecated since GHC 7.6 (2012) and will possibly be removed from the API in the near future.
(However, report is still available
on GHC 7.10
and
on GHC master branch
as of 2015.)

Use reportError to report an error and carry on with the Q computation
(ultimately failing compilation anyway).

Use fail to stop with an error (GHC ≤ 7.10). That might
not apply on GHC 8.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文