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

proc_macro::LexError doesn't implement std::error::Error #68896

Closed
kinseytamsin opened this issue Feb 6, 2020 · 0 comments · Fixed by #68899
Closed

proc_macro::LexError doesn't implement std::error::Error #68896

kinseytamsin opened this issue Feb 6, 2020 · 0 comments · Fixed by #68899
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@kinseytamsin
Copy link
Contributor

LexError, the error type used by proc_macro::TokenStream's FromStr implementation, doesn't implement std::error::Error. This makes it very un-ergonomic to work with when trying to use the ? operator to propagate errors in a function that returns a non-specific error type like Box<dyn std::error::Error> or the Error type provided by the anyhow crate.

I wrote the following code to demonstrate what I mean:

extern crate proc_macro;

use std::error::Error;
use proc_macro::TokenStream;

fn returns_result_with_nonspecific_error() -> Result<TokenStream, Box<dyn Error>> {
    Ok(r#"println!("hello, world");"#.parse()?)
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `proc_macro::LexError: std::error::Error` is not satisfied
 --> src/lib.rs:7:46
  |
7 |     Ok(r#"println!("hello, world");"#.parse()?)
  |                                              ^ the trait `std::error::Error` is not implemented for `proc_macro::LexError`
  |
  = note: required because of the requirements on the impl of `std::convert::From<proc_macro::LexError>` for `std::boxed::Box<dyn std::error::Error>`
  = note: required by `std::convert::From::from`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

So while this does feel like an easy PR (and if it is, I plan to submit it!), I just wanted to make sure beforehand that there isn't some good reason that this is the case. I've never really worked with procedural macros at all (I noticed this issue through the proc-macro2 crate) so I'm not sure if there's something special going on here that I'm not aware of. Thanks in advance for the help :)

@kinseytamsin kinseytamsin changed the title proc_macro::LexError doesn't implement std::error::Error proc_macro::LexError doesn't implement std::error::Error Feb 6, 2020
@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Feb 6, 2020
@bors bors closed this as completed in 703dcff Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants