4 releases

0.10.1 Jul 10, 2023
0.10.0 Mar 22, 2023
0.9.1 Nov 1, 2020
0.9.0 Oct 10, 2020

#33 in Procedural macros

Download history 28421/week @ 2023-12-23 50767/week @ 2023-12-30 65490/week @ 2024-01-06 76772/week @ 2024-01-13 84197/week @ 2024-01-20 82566/week @ 2024-01-27 83964/week @ 2024-02-03 86357/week @ 2024-02-10 81265/week @ 2024-02-17 78991/week @ 2024-02-24 89055/week @ 2024-03-02 87793/week @ 2024-03-09 89114/week @ 2024-03-16 84275/week @ 2024-03-23 88079/week @ 2024-03-30 71455/week @ 2024-04-06

347,852 downloads per month
Used in 888 crates (16 directly)

MIT/Apache

25KB
389 lines

proc-macro2-diagnostics   crates.io docs.rs

Diagnostics for stable and nightly proc-macros!

Usage

  1. Depend on the library in your proc-macro.
[dependencies]
proc_macro2_diagnostics = "0.10"
  1. Import SpanDiagnosticExt and use its methods on a proc_macro2::Span to create Diagnostics:
use syn::spanned::Spanned;
use proc_macro2::TokenStream;
use proc_macro2_diagnostics::{SpanDiagnosticExt, Diagnostic};

fn my_macro(input: TokenStream) -> Result<TokenStream, Diagnostic> {
    Err(input.span().error("there's a problem here..."))
}
  1. If there's an error, emit the diagnostic as tokens:
extern crate proc_macro;

pub fn real_macro(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
    match my_macro(tokens.into()) {
        Ok(tokens) => tokens.into(),
        Err(diag) => diag.emit_as_expr_tokens().into()
    }
}

This does the right thing on nightly or stable.

Caveats

On stable, due to limitations, any top-level, non-error diagnostics are emitted as errors. This will abort compilation. To avoid this, you may want to cfg-gate emitting non-error diagnostics to nightly.

Colors

By default, error messages are colored on stable. To disable, disable default features:

[dependencies]
proc_macro2_diagnostics = { version = "0.10", default-features = false }

The compiler always colors diagnostics on nightly.

License

Licensed under either of the following, at your option:

Dependencies

~300–780KB
~18K SLoC