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

Panic in proc_macro::TokenStream::from_str #58736

Open
dtolnay opened this issue Feb 25, 2019 · 2 comments
Open

Panic in proc_macro::TokenStream::from_str #58736

dtolnay opened this issue Feb 25, 2019 · 2 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Feb 25, 2019

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.

@dtolnay dtolnay added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Feb 25, 2019
@dtolnay
Copy link
Member Author

dtolnay commented Feb 25, 2019

Maybe this will involve adding an emit method to LexError to format the error in compiler style to stderr.

@petrochenkov
Copy link
Contributor

The impl even has a dedicated comment for this

NOTE: some errors may cause panics instead of returning LexError. We reserve the right to change these errors into LexErrors later.

rustdoc doesn't display it though.
In practice LexError is never returned :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-proc-macros Area: Procedural macros T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants