Skip to content

Spans lost with feature(proc_macro_hygiene) #55232

Closed
@HMPerson1

Description

@HMPerson1

(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 {()=>()}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions