【问题标题】:Issuing a warning at compile time?在编译时发出警告?
【发布时间】:2019-11-23 08:07:47
【问题描述】:

我想在编译时发出警告,可能来自宏。它不应该被cap_lints 静音。我目前的用例是功能弃用,但还有其他可能的用途。

【问题讨论】:

    标签: compiler-errors rust rust-macros


    【解决方案1】:

    目前这在稳定的 Rust 中是不可能的。但是,有一个不稳定的功能,procedural macro diagnostics,它通过Diagnostic API 为过程宏提供此功能。

    要从程序宏内部发出编译器警告,您可以这样使用它:

    #![feature(proc_macro_diagnostic)]
    
    use proc_macro::Diagnostic;
    
    Diagnostic::new()
        .warning("This method is deprecated")
        .emit();
    

    要将警告与特定令牌范围相关联,您可以改用spanned_warning。这使得警告输出显示相关的源标记以及下划线的消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      相关资源
      最近更新 更多