#proc #macro #warning


Emit warnings from inside proc macros

10 releases (1 stable)

1.0.0 Oct 8, 2023
0.4.2 Aug 28, 2023
0.4.1 May 14, 2023
0.3.1 Apr 13, 2023
0.1.2 Apr 3, 2023

#26 in Procedural macros

Download history 24165/week @ 2023-08-01 23472/week @ 2023-08-08 28553/week @ 2023-08-15 33998/week @ 2023-08-22 53156/week @ 2023-08-29 48062/week @ 2023-09-05 48450/week @ 2023-09-12 38486/week @ 2023-09-19 40213/week @ 2023-09-26 38254/week @ 2023-10-03 43783/week @ 2023-10-10 50893/week @ 2023-10-17 47897/week @ 2023-10-24 50153/week @ 2023-10-31 46602/week @ 2023-11-07 56614/week @ 2023-11-14

210,116 downloads per month
Used in 293 crates (via frame-support-procedural)

GPL-3.0 OR Apache-2.0

256 lines

Proc Macro Warning

Emit warnings from inside proc macros.

License: (GPL-3.0 or Apache-2.0)

Rust does not have native functions to produce warnings from inside proc macros. This crate provides "deprecated" warnings for your proc macro use-cases.


Building a warning is easy with the builder pattern.

use proc_macro_warning::Warning;

let warning = Warning::new_deprecated("OldStuffUsed")

// Use the warning in a proc macro
let tokens = quote::quote!(#warning);

This works in derive-macros, but you must set a span; otherwise it will not show up in the compile output.

The difference to a #[deprecated] attribute is that it emits the warning either way. For example when creating a custom Deprecated derive macro, it will warn without the struct being constructed.

struct Test {}

fn main() {
  // Warning triggers although we never used `Test`.  
  // Otherwise use a normal `#[deprecated]`.

Un-opinionated Formatting

The normal aforementioned way of creating a warning will impose specific unified grammar and formatting rules.
You can opt out of this and use your own instead by using FormattedWarning::new_deprecated:

use proc_macro_warning::FormattedWarning;

let warning = FormattedWarning::new_deprecated(
      "looooooooooooooooooooooooooooooong line that will not be brokeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeen ;)",

// Use the warning in a proc macro
let tokens = quote::quote!(#warning);

The output of a similar example is in derive_raw.stderr.

Used In

Substrate uses it to emit warnings for its eDSL (FRAME) on deprecated behaviour. The integration was done in #13798 and shows how to use these warnings in macro expansion.

The warnings are uniformly formatted and have consistent grammar:

warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_0::_w`:
                 It is deprecated to use implicit call indices.
                 Please instead ensure that all calls have the `pallet::call_index` attribute or that the `dev-mode` of the pallet is enabled.
                 For more info see:
    --> frame/nomination-pools/src/lib.rs:2621:10
2621 |         pub fn claim_commission(origin: OriginFor<T>, pool_id: PoolId) -> DispatchResult {
     |                ^^^^^^^^^^^^^^^^

A different one:

warning: use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`:
                 It is deprecated to use hard-coded constant as call weight.
                 Please instead benchmark all calls or put the pallet into `dev` mode.
                 For more info see:
    --> frame/nomination-pools/src/lib.rs:2620:20
2620 |         #[pallet::weight(0)]


Licensed under either of at your own choice:


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~19K SLoC