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

#27 in Procedural macros

Download history 1049382/week @ 2025-12-13 580977/week @ 2025-12-20 469538/week @ 2025-12-27 886301/week @ 2026-01-03 1054143/week @ 2026-01-10 1182618/week @ 2026-01-17 1196443/week @ 2026-01-24 1167702/week @ 2026-01-31 1238979/week @ 2026-02-07 1131762/week @ 2026-02-14 1072115/week @ 2026-02-21 1184728/week @ 2026-02-28 1267886/week @ 2026-03-07 1062077/week @ 2026-03-14 877690/week @ 2026-03-21 841412/week @ 2026-03-28

4,265,455 downloads per month
Used in 2,895 crates (42 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

~100–480KB
~11K SLoC