I thought that procedural macros only had to have lexically-valid input, but it seems like all input must also parse as Rust code, unless I'm doing something wrong.
The RFC says:
By operating on tokens, code passed to procedural macros does not need to satisfy the Rust parser, only the lexer.
As soon as the input involves invalid Rust syntax I get parse errors.
Consider the following procedural macro:
#[proc_macro_attribute]
pub fn my_macro(_: TokenStream, _: TokenStream) -> TokenStream {
TokenStream::from(quote! {})
}
when run on:
#[my_macro]
fn test() { * } // lexically valid but syntactically invalid
I get the error:
error: expected expression, found `}`
--> blah.rs:38:2
|
33 | #[logic] fn omg () { * }
| ^ expected expression
cargo-expand
shows that the macro correctly deletes the function. Shouldn't it thus stop any parse error?
What am I doing wrong?