Mark struct with aux label

Besides using a comment, is there a way to mark a struct with some aux data ?

Context: I have my own codegen system (not using procedural macros) which looks at *.rs files, find structs w/ certain aux labels, then codegens on these structs. Thus, I need some way to tag/label structs that Rust proper just ignores.

1 Like

The thing you are asking for is basically tool attributes, but those have a hardcoded list of namespaces in the compiler — there's no way to extend it currently.

What you can do instead is write a library that defines an attribute macro which does nothing. Then, your tool can read that attribute, but the library tells rustc what to do with it (nothing). For example, this technique is used in https://crates.io/crates/mutants which assists the cargo-mutants tool.

1 Like

Is the core idea basically:

#[proc_macro_derive(FooBar)]
pub fn proc_foobar(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let t = quote! {};
    t.into()
}

so now #[derive(FooBar)] is an no-op to rustc, but our external cool can read it and do processing ?

Or just proc_macro_attribute which returns its input unchanged.

Could you expand on this? At my level of proc-maco-fu, it is not obvious to me how this solution works / what it looks like.

#[proc_macro_attribute]
pub fn proc_foobar(_attr: proc_macro::TokenStream, input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    input
}

That's all. That's exactly what does the mutants::skip attribute mentioned before do.

1 Like