0

I have the following trivial procedural macro in one crate (it was more than this, but I distilled it to be as simple as possible while still causing the issue to occur)

#[proc_macro]
pub fn the_macro(input: TokenStream) -> TokenStream {
   input
}

and the following test code in another crate:

use set_macro::the_macro;
fn main() {
    let asdf = "zzz";
    let qqq = "aaa";
    println!("blah {}", the_macro!(asdf, qqq));
}

But running it gives me this:

error: macro expansion ignores token `,` and any following
 --> src/main.rs:7:52
  |
7 |     println!("blah {}", the_macro!(asdf, qqq));

My understanding is that this can happen if for some reason the macro stops prior to processing a token, but in this case I'm literally just hot passing the input straight to the return value. Fwiw this works just fine if I only pass in one token, ie the_macro!(asdf)

I did try cargo expand, and it appears that the output of the macro is indeed just 'asdf'.

What might be going on here?

me163
  • 1
  • 3
    The tokens output by your macro must be valid expression syntax when used in an expression position. `asdf, qqq` is not a valid expression. Try `the_macro!( (asdf, qqq) )` or `the_macro!( [asdf, qqq] )` – PitaJ Oct 21 '22 at 20:32
  • Got it, thanks for the reply - what I was expecting is that the macro is expanded and becomes "asdf, qqq" which become the second and third arguments of `println!`. I guess I could have the macro strip out the ( or [ in the case of your suggestions, but I'd rather avoid that if possible. I'm trying to figure out how my macro is different from something like vec!, for which you can do vec!(a, b) etc – me163 Oct 21 '22 at 20:40
  • 4
    Again: the OUTPUT of your macro must be valid expression syntax if used in expression position. Your proc macro can't EXPAND to `asdf, qqq` because that's not valid expression syntax. What are you actually trying to accomplish? – PitaJ Oct 21 '22 at 20:46

0 Answers0