Skip to content

Remove the proc_macro::Group around metavariables which are always a single token #55557

Closed
@dtolnay

Description

@dtolnay

I have the following proc macro to print out tokens:

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn print_tts(input: TokenStream) -> TokenStream {
    println!("{:#?}", input);
    TokenStream::new()
}

And suppose I call it like this:

#![feature(proc_macro_hygiene)]

extern crate print;

macro_rules! print_ident {
    ($i:ident) => {
        print::print_tts!(2 * $i)
    };
}

fn main() {
    print_ident!(ident);
}

As of rustc 1.31.0-nightly (1cf82fd 2018-10-30) the input is passed to my macro as:

TokenStream [
    Literal {
        lit: Integer(
            2
        ),
        suffix: None,
        span: #2 bytes(127..128)
    },
    Punct {
        ch: '*',
        spacing: Alone,
        span: #2 bytes(129..130)
    },
    Group {
        delimiter: None,
        stream: TokenStream [
            Ident {
                ident: "ident",
                span: #0 bytes(174..179)
            }
        ],
        span: #2 bytes(131..133)
    }
]

I believe idents (and any other metavariables which are always a single token) should not be wrapped in a Group. It makes handling proc macro input unnecessarily confusing. This proc macro should be receiving a plain Literal Punct Ident rather than Literal Punct Group.

Metavariables that can be multiple tokens, including $:expr and $:ty, will need to continue having a surrounding None-delimited Group to preserve precedence.

cc @alexcrichton

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions