Skip to content

Panic in proc_macro::TokenStream::from_str #58736

@dtolnay

Description

@dtolnay

TokenStream::from_str is supposed to return Result<proc_macro::TokenStream, proc_macro::LexError>. Currently it appears to print errors to stderr and then panic, rather than returning errors.

Cargo.toml

[package]
name = "repro"
version = "0.0.0"
edition = "2018"

[lib]
proc-macro = true

src/lib.rs

extern crate proc_macro;

use proc_macro::TokenStream;
use std::str::FromStr;

#[proc_macro_derive(ParseTokenStream)]
pub fn derive(_input: TokenStream) -> TokenStream {
    let _ = TokenStream::from_str("\\s");
    TokenStream::new()
}

src/main.rs

#[derive(repro::ParseTokenStream)]
struct S;

fn main() {}

Output of cargo check:

$ cargo check

error: unknown start of token: \
 --> src/main.rs:1:10
  |
1 | #[derive(repro::ParseTokenStream)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^

error: proc-macro derive panicked
 --> src/main.rs:1:10
  |
1 | #[derive(repro::ParseTokenStream)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Mentioning @eddyb, @petrochenkov, @alexcrichton.
Mentioning @canndrew who reported this in dtolnay/proc-macro2#168.

Metadata

Metadata

Assignees

Labels

A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macrosT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions