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

Unable to get #[from] + backtrace example to compile #257

Closed
metasim opened this issue Oct 17, 2023 · 5 comments · Fixed by #312
Closed

Unable to get #[from] + backtrace example to compile #257

metasim opened this issue Oct 17, 2023 · 5 comments · Fixed by #312

Comments

@metasim
Copy link

metasim commented Oct 17, 2023

I have a use case where this example from the README is exactly what I need:

#[derive(Error, Debug)]
pub enum MyError {
    Io {
        #[from]
        source: io::Error,
        backtrace: Backtrace,
    },
}

However, when I compile it (Rust Playground link) I get the following error:

no `ThiserrorProvide` in `__private`
   Compiling playground v0.0.1 (/playground)
error[E0432]: unresolved import `thiserror`
   --> src/lib.rs:5:10
    |
5   | #[derive(Error, Debug)]
    |          ^^^^^ no `ThiserrorProvide` in `__private`
    |
note: found an item that was configured out
   --> /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/thiserror-1.0.48/src/lib.rs:256:29
    |
256 |     pub use crate::provide::ThiserrorProvide;
    |                             ^^^^^^^^^^^^^^^^
    = note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: use of unstable library feature 'error_generic_member_access'
--> src/lib.rs:5:10
|
5 | #[derive(Error, Debug)]
| ^^^^^
|
= note: see issue #99301 rust-lang/rust#99301 for more information
= note: this error originates in the derive macro Error (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: MyError doesn't implement std::fmt::Display
--> src/lib.rs:6:10
|
6 | pub enum MyError {
| ^^^^^^^ MyError cannot be formatted with the default formatter
|
= help: the trait std::fmt::Display is not implemented for MyError
= note: in format strings you may be able to use {:?} (or {:#?} for pretty-print) instead
note: required by a bound in std::error::Error
--> /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/error.rs:32:1

@ZZT32
Copy link

ZZT32 commented Nov 16, 2023

You need to use the nightly compiler and enable the error_generic_member_access feature. See working playground. Thiserror's documentation should be more clear that this depends on a nightly feature.

@Bauxitedev
Copy link

Also, in case the previous solution doesn't work, make sure you're on a recent nightly version. I was on nightly-2023-06-24 and I was confused why thiserror wasn't compiling.

@metasim
Copy link
Author

metasim commented Nov 28, 2023

Perhaps this feature should be under a feature flag or cfg associated with error_generic_member_access? Or at least be clarified in the documentation?

@bart-sensirion-qm
Copy link

I just had this exact same problem and was very confused as the nightly + feature gate requirement isn't mentioned in the documentation.

@jondot
Copy link

jondot commented Dec 19, 2023

I gave up on this, I ended up doing something like this to work around the limitation:
https://github.com/loco-rs/loco/blob/master/src/errors.rs#L129-L139

in other words, code the from yourself, and capture the backtrace manually, which is the same approach taken by candle. I recommend doing a search around github to find some approaches that suit you -- I didn't find anything else that worked well for stable.

I needed to control the backtrace because I produce clean, pretty backtraces in Loco which look like this:

loco-rs/loco#41 (comment)

Which also include backtrace filtering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants