Skip to content

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

Closed

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

(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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API 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