macro_rules! expr {
($a:expr) => ($a)
}
macro_rules! str_tts {
($b:tt) => {
expr!(stringify!($b))
}
}
fn main () {
// This works ("$ c").
let x = expr!(stringify!($c));
// This errors with "unknown macro variable `c`".
let y = str_tts!($c);
println!("{} {}", x, y);
}
On playpen.
It seems that while expanding the str_tts invocation, the $c token ends up being interpreted as a macro variable, even though it should be packaged in a tt non-terminal.
Also to be noted is the fact that stringify!($c) produces "$ c", implying there are two tokens, a dollar sign and an identifier, whereas when passed to a macro_rules macro, it "fits" in a single tt.
Maybe it was a bad idea to ever have a $ token and a $var token type, the $ token would suffice, although macro_rules expansion may become more complicated.
On playpen.
It seems that while expanding the
str_ttsinvocation, the$ctoken ends up being interpreted as a macro variable, even though it should be packaged in attnon-terminal.Also to be noted is the fact that
stringify!($c)produces"$ c", implying there are two tokens, a dollar sign and an identifier, whereas when passed to amacro_rulesmacro, it "fits" in a singlett.Maybe it was a bad idea to ever have a
$token and a$vartoken type, the$token would suffice, althoughmacro_rulesexpansion may become more complicated.