Skip to content

Panic iterating over proc_macro::TokenStream #55640

Closed
@dtolnay

Description

@dtolnay

In the following proc macro invocation the compiler passes in the input $crate as a TokenStream but panics when iterating over that TokenStream. Specifically the panic happens in next().

rustc 1.31.0-nightly (de9666f 2018-10-31)

repro/src/lib.rs

extern crate proc_macro;
use self::proc_macro::TokenStream;

#[proc_macro]
pub fn repro(input: TokenStream) -> TokenStream {
    input.into_iter().next();
    unimplemented!()
}

testing/src/lib.rs

macro_rules! m {
    () => {
        repro::repro!($crate);
    };
}

m!();
error: proc macro panicked                                                                                                     
 --> src/main.rs:3:9                                                                                                           
  |                                                                                                                            
3 |         repro::repro!($crate);                                                                                             
  |         ^^^^^^^^^^^^^^^^^^^^^^                                                                                             
...                                                                                                                            
7 | m!();                                                                                                                      
  | ----- in this macro invocation                                                                                             
  |                                                                                                                            
  = help: message: `"$crate"` is not a valid identifier

Originally reported in dtolnay/paste#3. Mentioning @softprops.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions