Search code examples
rustattributesdeprecateddeprecation-warningrust-proc-macros

Can you deprecate an attribute in Rust?


Say I have this macro definition:

#[proc_macro_derive(Builder, attributes(builder, group, groups))]
#[proc_macro_error]
pub fn derive_builder(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    // -- snip --
}

And I want to deprecate group, and have users use groups instead. You can't simply do:

#[proc_macro_derive(Builder, attributes(builder, #[deprecated] group, groups))]

And throwing warnings is currently only available on nightly

How do I tell users to phase out the use of group?


Solution

  • Just like with compile_error!(), expand to a call to a deprecated function. Probably, the best way is to have a deprecated exported function from your macro's support lib:

    #[deprecated = "the `group` attribute is deprecated. Use `groups` instead"]
    #[doc(hidden)]
    pub const fn group() {}
    

    And expand to something like:

    const _: () = ::my_crate::group();
    

    Of course, make sure to set the correct span so the error points to the right location.