I generally use VS Code with the rust-analyzer to write code. I am happy with it in general. But debugging procedural macros that create a TokenStream (typically to automate the implementation of some functions) has been very difficult. VS Code tells me what the underlying error in the output TokenStream is, but there is no way for me to "see" the token stream to identify what otherwise might be an obvious syntactic error.
Does anybody know of a way to "preview" the token stream resulting from #[derive(Whatever)]?
#[proc_macro_derive(DerivedStuff)]
pub fn my_trait(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
// ... lots of stuff ...
println!("{}", &tokenstream); // ADDED THIS LINE
tokenstream.into()
}
Back in the "main" library, I made a test on a struct that has #[derive(DerivedStuff)] fail so it would show the output. Not sure if this is more complicated than what you imagined, but I was flying blind and now I can see.
I recommend using cargo expand for the general case, and for the case of a proc-macro of your own, print can be very useful but, alas, doesn't yield such a nice looking output on its own (not formatted nor colored).
In that case, you can write your own helper pretty_print_tokenstream to palliate that, and it will yield cargo expand-looking code: