Skip to content

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

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

Closed
dtolnay opened this issue Nov 1, 2018 · 3 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Nov 1, 2018

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

@dtolnay dtolnay added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Nov 1, 2018
@petrochenkov
Copy link
Contributor

FWIW, libsyntax does the same trick for identifiers and lifetimes (literals may contain minus, so they are not always single-token).
Lifetimes are not single-token in proc macros, so this leaves only idents.

@alexcrichton
Copy link
Member

This makes sense to me as well, no need to place idents in a group!

@Enselic Enselic added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 16, 2023
@dtolnay
Copy link
Member Author

dtolnay commented Nov 17, 2024

Fixed by #92472.

@dtolnay dtolnay closed this as completed Nov 17, 2024
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, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants