Macros are too good at being AST

macro_rules! attributes {
    () => {
        #[cfg(true)]
    };
}

macro_rules! struct_with_attrs {
    () => {
        attributes! {}
        struct Foo;
    };
}


struct_with_attrs! {}

I wanted to implement macro_rules! with a similar syntax to the thiserror crate, but I'm struggling with filtering out the custom directives in $(#[$meta])*.

It would be super useful to be able to call a macro for each $meta item to match on it, but this doesn't seem to be possible. Either macros are expanded in the wrong order, or can't even generate attributes, because they end up being a separate AST node.

Is there a trick for this, other than making the whole macro recursive?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.