4

I have one proc macro that looks like this:

#[proc_macro_attribute]
pub fn my_macro(_meta: CompilerTokenStream, input: CompilerTokenStream) -> CompilerTokenStream { //* bits of code */ }

Then I have a derive macro:

#[proc_macro_derive(Operations)]
pub fn operations(input: proc_macro::TokenStream) -> proc_macro::TokenStream { //* bits of code */ }

Is it possible to make the derive macro being expanded after the attribute one?

The reason is that I have a crate with some static variables to track data. And I need to read the data in the derive macro (the attribute macro is the writer)

Alex Vergara
  • 1,766
  • 1
  • 10
  • 29

1 Answers1

4

Macros are expanded starting outside-in in the syntax tree; for attributes this means starting with the topmost attribute first. So, you should get the effect you want by writing the attributes in this order:

#[my_macro]
#[derive(Operations)]

However, you should not use static variables in your macro crate to communicate information. It will work, right now, but the Rust compiler does not promise not to cache macro invocations, run each macro expansion in a separate process/sandbox, or other such changes that would break your macro's communication path.

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108