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

Parens inserted by parser, messing up spans #266

Closed
smoelius opened this issue Oct 17, 2020 · 2 comments
Closed

Parens inserted by parser, messing up spans #266

smoelius opened this issue Oct 17, 2020 · 2 comments

Comments

@smoelius
Copy link

Under certain conditions, the parser inserts parentheses that do not appear literally in the source code, and this seems to mess up the associated spans.

A small example is attached (print_span.zip). Here are the highlights:

print_span/src/lib.rs:

use proc_macro::TokenStream;
use quote::ToTokens;
use syn::{parse_macro_input, spanned::Spanned, ItemFn};

#[proc_macro_attribute]
pub fn print_span(_: TokenStream, item: TokenStream) -> TokenStream {
    let item = parse_macro_input!(item as ItemFn);
    println!("{:?}: {}", item.span(), item.to_token_stream());
    item.to_token_stream().into()
}

print_span/example/src/lib.rs:

#[print_span::print_span]
pub fn no_parens() {
    &|_: ()| ();
}

#[print_span::print_span]
pub fn parens() {
    &(|_: ()| ());
}

And here is some sample output:

$ cargo clean && cargo build 2>/dev/null
#7 bytes(0..25): pub fn no_parens() { & (| _ : () | ()) ; }
#0 bytes(93..131): pub fn parens() { & (| _ : () | ()) ; }

$ tail -c+1 src/lib.rs | head -c 26
#[print_span::print_span]

$ tail -c+94 src/lib.rs | head -c $((131 - 93 + 1))
pub fn parens() {
    &(|_: ()| ());
}

Note that for both parens and no_parens, the parsed output includes parentheses. For parens, the outputted span appears correct. But for no_parens the outputted span is the call site (I think).

Small changes to the source code make the problem go away. For example, swapping the order of parens and no_parens seems to do this.

I apologize if this is not a proc-macro2 problem.

$ rustc --version
rustc 1.49.0-nightly (91a79fb29 2020-10-07)
@taiki-e
Copy link

taiki-e commented Oct 17, 2020

I think this is rustc bug (rust-lang/rust#75734), and fixed in latest nightly (IIRC nightly-2020-10-15 or later)

@smoelius
Copy link
Author

Thanks, @taiki-e. Using nightly-2020-10-15 does seem to make the problem go away. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants