Syn and quote incompatible?


#1

When I try to compile some code using quote::ToTokens I get the following error:

note: expected type `proc_macro::TokenStream`
         found type `syn::export::TokenStream`

Is this because they’re using different versions of the same library? How do I work round it?


#2

It works ok if I use type deduction, like so

let var = expr.into_token_stream();`

But I have to be explicit when returning from a function. That’s slightly annoying but I guess I can work with that.


#3

@chrisd could you share some more code? I’ve been working on proc-macros in the last two days (and asked my fair share of questions here ), but @dtolnay kindly helped me out, and things are working as expected for me.

Of note is that dyn uses the proc_macro2 crate internally. When you write a function-like procedural macro, you get a proc_macro::TokenStream, and need to return a proc_macro::TokenStream.

proc_macro2::TokenStream implements From<proc_macro::TokenStream>, so you can use into() to go back to a regular proc_macro::TokenStream before you return it from your macro function.


#4

Ah thanks. It turned out I just needed to explicitly import proc-macro2 in my toml and then I can use both versions of TokenStream.