Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macro_rules attempts to expand tokens it needs to only pass through. #28415

Closed
eddyb opened this issue Sep 15, 2015 · 4 comments
Closed

macro_rules attempts to expand tokens it needs to only pass through. #28415

eddyb opened this issue Sep 15, 2015 · 4 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

Comments

@eddyb
Copy link
Member

eddyb commented Sep 15, 2015

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.

@eddyb eddyb added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Sep 15, 2015
@Mark-Simulacrum
Copy link
Member

stringify!($c), along with any other macro invocation with $foo as an argument, now errors with "error: unknown macro variable c", so this can either be closed or renamed/repurposed; a decision as to what we want would be nice.

@Mark-Simulacrum
Copy link
Member

Not sure what I was talking about before, but today the following error is reported. I believe this indicates that $c is now two tokens, so accepting a single tt doesn't work. @jseyfried Can you confirm that interpretation as correct?

error: no rules expected the token `c`
  --> test.rs:15:23
   |
15 |     let y = str_tts!($c);
   |             ----------^- in this macro invocation

@eddyb
Copy link
Member Author

eddyb commented May 7, 2017

I believe this has been fixed by one of @jseyfried's many token/macro refactors.
(Feel free to wait for proper confirmation, of course)

@jseyfried
Copy link
Contributor

jseyfried commented May 8, 2017

@Mark-Simulacrum
Yeah, that interpretation is correct -- that changed was caused by fixing #39403 in #39419.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
Projects
None yet
Development

No branches or pull requests

3 participants