Proc-macro-attribute: panicked at 'ItemFn: Error("unexpected token")

Hi, I've hit the subject error in a unit test while developing a proc-macro attribute.

thread 'codegen::tests::output_is_function_item' panicked at 'ItemFn: Error("unexpected token")', src/codegen.rs
:58:23

I've managed to distill the example down to the following file, codegen.rs,

where the offending code appears to be:

let pq2 = syn::parse2(quote!(
                fn f() {};
            ).into()).expect("ItemFn");

The Cargo file is:

[package]
edition = "2021"
name = "tpm"
publish = false
version = "0.0.0"

[lib]
proc-macro = true

[dependencies]
darling = "0.13"
proc-macro-error = "1"
proc-macro2 = "1"
quote = "1"
syn = { version = "1", features = ["extra-traits", "full"] }

[dev-dependencies]
trybuild = "1"

[features]
# Nothing by default
default = []

The playground error matches what I see in my larger project:

failures:

---- codegen::tests::output_is_function_item stdout ----One ExprAssign { attrs: [], left: Path(ExprPath { attrs: [], qself: None, path: Path { leading_colon: None, segm
ents: [PathSegment { ident: Ident(x), arguments: None }] } }), eq_token: Eq, right: Lit(ExprLit { attrs: [], lit
: Int(LitInt { token: 0 }) }) }
Two
thread 'codegen::tests::output_is_function_item' panicked at 'ItemFn: Error("unexpected token")', src/codegen.rs
:58:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I have iterated using parse_quote! in place of syn::parse2(quote!(...)) to no avail.

Appreciate any pointers to what I have done wrong, or how to further isolate the offending syntax.

The following change avoids the panic.

         let pq2 = syn::parse2(quote!(
-                fn f() {};
+                fn f() {}
             ).into()).expect("ItemFn");
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.