Skip to content

Commit

Permalink
Add documentation and make matchers more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
mystor committed Nov 5, 2019
1 parent e357682 commit 2d9479a
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,30 @@ pub use syn::{parse, parse_str, DeriveInput};
/// decl_derive!([Interesting, attributes(interesting_ignore)] => derive_interesting);
/// # };
/// ```
///
/// ### Decl Attributes & Doc Comments
/// ```
/// # fn main() {}
/// fn derive_interesting(_input: synstructure::Structure) -> proc_macro2::TokenStream {
/// quote::quote! { ... }
/// }
///
/// # const _IGNORE: &'static str = stringify! {
/// decl_derive! {
/// [Interesting] =>
/// #[allow(some_lint)]
/// /// Documentation Comments
/// derive_interesting
/// }
/// # };
/// ```
#[macro_export]
macro_rules! decl_derive {
// XXX: Switch to using this variant everywhere?
([$derives:ident $($derive_t:tt)*] => $(#[$attrs:meta])* $inner:path) => {
([$derives:ident $($derive_t:tt)*] => $(#[$($attrs:tt)*])* $inner:path) => {
#[proc_macro_derive($derives $($derive_t)*)]
#[allow(non_snake_case)]
$(#[$attrs])*
$(#[$($attrs)*])*
pub fn $derives(
i: $crate::macros::TokenStream
) -> $crate::macros::TokenStream {
Expand Down Expand Up @@ -102,12 +119,11 @@ macro_rules! decl_derive {
/// decl_attribute!([interesting] => attribute_interesting);
/// # };
/// ```
/// ```
#[macro_export]
macro_rules! decl_attribute {
([$attribute:ident] => $(#[$attrs:meta])* $inner:path) => {
([$attribute:ident] => $(#[$($attrs:tt)*])* $inner:path) => {
#[proc_macro_attribute]
$(#[$attrs])*
$(#[$($attrs)*])*
pub fn $attribute(
attr: $crate::macros::TokenStream,
i: $crate::macros::TokenStream,
Expand Down

2 comments on commit 2d9479a

@CreepySkeleton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mystor Out of curiosity, why did you change #[$attrs:meta] to #[$($attrs:tt)*]? Shouldn't it work with meta?

@mystor
Copy link
Owner Author

@mystor mystor commented on 2d9479a Nov 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, well this one is kinda funny.

Attributes can technically take the format of any tokens within a #[...] wrapper, rather than specifically something looking like a "Meta Item", which meant that, for a while, it was more correct to match attributes in the manner I wrote it than using $:meta (see rust-lang/rust#49629 for context). I was originally writing the change this way to try to be compatible with these more general attributes.

However, apparently the definition of a valid custom attribute was restricted more in rust-lang/rust#50120, and the $:meta matcher was upgraded to support this new syntax in rust-lang/rust#63674. This means that the $:meta matcher is still fully compatible, and could be used here. I was not aware of rust-lang/rust#63674 at the time, however, and changed it to try to be safe.

I'll probably not back this change out, as it's harmless, but I probably wouldn't make it again after looking up the bug to explain the reasoning to you :-).

Please sign in to comment.