Testing Proc Macro Crates panics - procedural macro API is used outside of a procedural macro

74 views Asked by At

I would like to test some functionality of the syn crate outside of a proc macro, but am getting a panic whenever I create a proc_macro::TokenStream.

use quote::quote;
use syn::parse::Parser;
use syn::punctuated::Punctuated;

fn main(){

  let foo: proc_macro::TokenStream = quote!(Bar, Bazz).into();
  let parsed = Punctuated::<syn::Ident, syn::Token![,]>::parse_terminated.parse(foo);

}

// procedural macro API is used outside of a procedural macro
1

There are 1 answers

0
chantey On

syn and other crates have parse2 and equivelant functions that allow us to use a proc_macro2::TokenStream instead:

let foo = quote!(Bar, Bazz);
let parsed = Punctuated::<syn::Ident, syn::Token![,]>::parse_terminated.parse2(foo);