Can you deprecate an attribute in Rust?

67 Views Asked by At

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?

1

There are 1 best solutions below

1
Chayim Friedman On BEST ANSWER

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.