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

None-delimited identifiers (sometimes) stringified with strange spaces #55658

Closed
dtolnay opened this issue Nov 3, 2018 · 1 comment
Closed
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Nov 3, 2018

I have the following procedural macro and invocation of the macro. In this code I construct two identical functions f and g which simply return stringify!(i32), but with a none-delimited group around the i32 (because of #55557). As far as I can tell the function bodies of f and g are identical token streams.

But f returns "i32" and g returns " i32 ".

I believe that as written, both functions should return "i32" without spaces.

extern crate proc_macro;
use self::proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};

use std::iter::FromIterator;

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

    TokenStream::from_iter(vec![
        // fn f() -> &'static str { $input }
        TokenTree::Ident(Ident::new("fn", Span::call_site())),
        TokenTree::Ident(Ident::new("f", Span::call_site())),
        TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())),
        TokenTree::Punct(Punct::new('-', Spacing::Joint)),
        TokenTree::Punct(Punct::new('>', Spacing::Alone)),
        TokenTree::Punct(Punct::new('&', Spacing::Alone)),
        TokenTree::Punct(Punct::new('\'', Spacing::Joint)),
        TokenTree::Ident(Ident::new("static", Span::call_site())),
        TokenTree::Ident(Ident::new("str", Span::call_site())),
        TokenTree::Group(Group::new(Delimiter::Brace, input)),

        // fn g() -> &'static str { stringify!(i32) }
        TokenTree::Ident(Ident::new("fn", Span::call_site())),
        TokenTree::Ident(Ident::new("g", Span::call_site())),
        TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::new())),
        TokenTree::Punct(Punct::new('-', Spacing::Joint)),
        TokenTree::Punct(Punct::new('>', Spacing::Alone)),
        TokenTree::Punct(Punct::new('&', Spacing::Alone)),
        TokenTree::Punct(Punct::new('\'', Spacing::Joint)),
        TokenTree::Ident(Ident::new("static", Span::call_site())),
        TokenTree::Ident(Ident::new("str", Span::call_site())),
        TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::from_iter(vec![
            TokenTree::Ident(Ident::new("stringify", Span::call_site())),
            TokenTree::Punct(Punct::new('!', Spacing::Alone)),
            TokenTree::Group(Group::new(Delimiter::Parenthesis, TokenStream::from_iter(vec![
                TokenTree::Group(Group::new(Delimiter::None, TokenStream::from_iter(vec![
                    TokenTree::Ident(Ident::new("i32", Span::call_site())),
                ]))),
            ]))),
        ])))
    ])
}
macro_rules! m {
    ($id:ident) => {
        repro::repro! {
            stringify!($id)
        }
    }
}

m!(i32);

fn main() {
    assert_eq!(f(), "i32", "f()");
    assert_eq!(g(), "i32", "g()"); // broken, is `"  i32  "`
}
@dtolnay dtolnay added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Nov 3, 2018
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue labels Apr 8, 2024
@dtolnay
Copy link
Member Author

dtolnay commented Nov 17, 2024

Fixed by #73345.

@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-bug Category: This is a bug. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants