Issuing a warning at compile time?

前提是你 提交于 2020-06-22 21:24:09

问题


I want to issue a warning at compile time, perhaps from a macro. It should not be silenceable by cap_lints. My current use case is feature deprecation, but there's other possible uses for this.


回答1:


This currently isn't possible in stable Rust. However, there is an unstable feature, procedural macro diagnostics, which provides this functionality for procedural macros, via the Diagnostic API.

To emit a compiler warning from inside a procedural macro, you would use it like this:

#![feature(proc_macro_diagnostic)]

use proc_macro::Diagnostic;

Diagnostic::new()
    .warning("This method is deprecated")
    .emit();

To associate the warning with a specific token span, you'd use spanned_warning instead. This makes the warning output show the relevant source tokens underlined along with the message.



来源:https://stackoverflow.com/questions/57025894/issuing-a-warning-at-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!