Custom inner attribute error at crate root

Hi guys, I have been developing a custom inner attribute as procedural-macro. The problem I found is that the input tokens seem to be wrapped into a pub mod without identifier and will throw an error if I parse it with crate. Is there any workaround on this or an easy way to obtain the tokens of entire crate instead of inner attribute?

Here is an example:

#![feature(custom_inner_attributes)]
#![crate::inner_attribute_name]
...//input

In my crate where the proc-macro is defined:

pub fn attribute_name(tokens: TokenStream) -> TokenStream {
  let input: ItemMod = syn::parse2(tokens); // This will be an error
  // If I call tokens.to_string() and print it, I have
  // pub mod {
  //              ... (all the input source codes)
  //}
}

I'd suggest that a build.rs script be added for this, that walkdirs through the Rust source file hierarchy, as well as it can. @matklad mentioned something similar recently in another post, and had a link to a more concrete example of this. Maybe they can provide further guidance.

If you need the proc-macro to modify the emitted code rather than just peek at it, then I suggest the output be emitted, inlined, within an generated.rs file under src/, and that the Cargo.toml be thus amended to end up loading that file:

[lib]
path = "src/generated.rs"
1 Like

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.