-
-
Notifications
You must be signed in to change notification settings - Fork 131
Closed
Description
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)
Metadata
Metadata
Assignees
Labels
No labels