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

Spans lost with feature(proc_macro_hygiene) #55232

Closed
HMPerson1 opened this issue Oct 20, 2018 · 2 comments
Closed

Spans lost with feature(proc_macro_hygiene) #55232

HMPerson1 opened this issue Oct 20, 2018 · 2 comments

Comments

@HMPerson1
Copy link
Contributor

(demo: https://github.com/HMPerson1/rustc-macros-span-bug)

If we have a proc_macro like this:

#[proc_macro_attribute]
pub fn reforest(_attr: TokenStream, item: TokenStream) -> TokenStream {
    item.into_iter().collect()
}

And we invoke it like this:

#![feature(proc_macro_hygiene)]
#[reforest]
fn foo() {
    macro_rules! m {()=>()}
}

All the tokens processed by reforest have their spans set to 0.

You can see this in the warning messages:

warning: unused macro definition                                                                                                                                            
  |                                                                                                                                                                         
  = note: #[warn(unused_macros)] on by default                                                                                                                              
                                                                                                                                                                                                                                                                                                                                     
warning: function is never used: `foo`                                                                                                                                      
  |                                                                                                                                                                         
  = note: #[warn(dead_code)] on by default                                                                                                                                  

Note that everything is fine we do any of the following:

  1. Change the proc_macro body to item
  2. Change the proc_macro body to TokenStream::from_str(&item.to_string())
  3. Comment out/delete macro_rules! m {()=>()}
@HMPerson1
Copy link
Contributor Author

Possibly related to #54727?

HMPerson1 added a commit to HMPerson1/rust-clippy that referenced this issue Oct 20, 2018
@dtolnay
Copy link
Member

dtolnay commented Oct 25, 2018

Closing as a consequence of #43081. For now you only get spans if the input is formatted in the way that rustc's ugly-printer would format it. In the case of macro_rules invocations that would be:

#[reforest]
fn foo() {
    let a = 0;
    macro_rules! m (
        () => {}
    );
}

@dtolnay dtolnay closed this as completed Oct 25, 2018
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