Skip to content

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

Closed
@eddyb

Description

@eddyb
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions