Description
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()?)
}
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 :)