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

Ensure UnwindSafe even with "backtrace" feature enabled and old Rust #386

Merged
merged 1 commit into from
Sep 15, 2024

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Sep 15, 2024

On Rust version 1.72 and older, this code didn't used to compile:

// [dependencies]
// anyhow = { version = "1", features = ["backtrace"] }

fn main() {
    let err = anyhow::Error::msg("...");
    let _ = std::panic::catch_unwind(move || err);
}
error[E0277]: the type `UnsafeCell<backtrace::Capture>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
   --> src/main.rs:6:38
    |
6   |     let _ = std::panic::catch_unwind(move || err);
    |             ------------------------ ^^^^^^^^^^^ `UnsafeCell<backtrace::Capture>` may contain interior mutability and a reference may not be safely transferrable across a catch_unwind boundary
    |             |
    |             required by a bound introduced by this call
    |
    = help: within `anyhow::error::ErrorImpl`, the trait `RefUnwindSafe` is not implemented for `UnsafeCell<backtrace::Capture>`
note: required because it appears within the type `LazilyResolvedCapture`
   --> ~/.rustup/toolchains/1.72-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/backtrace.rs:433:8
    |
433 | struct LazilyResolvedCapture {
    |        ^^^^^^^^^^^^^^^^^^^^^
note: required because it appears within the type `Inner`
   --> ~/.rustup/toolchains/1.72-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/backtrace.rs:133:6
    |
133 | enum Inner {
    |      ^^^^^
note: required because it appears within the type `Backtrace`
   --> ~/.rustup/toolchains/1.72-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/backtrace.rs:109:12
    |
109 | pub struct Backtrace {
    |            ^^^^^^^^^
note: required because it appears within the type `Option<Backtrace>`
   --> ~/.rustup/toolchains/1.72-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:563:10
    |
563 | pub enum Option<T> {
    |          ^^^^^^
note: required because it appears within the type `ErrorImpl`
   --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/anyhow-1.0.88/src/error.rs:863:19
    |
863 | pub(crate) struct ErrorImpl<E = ()> {
    |                   ^^^^^^^^^
    = note: required for `NonNull<anyhow::error::ErrorImpl>` to implement `UnwindSafe`
note: required because it appears within the type `Own<ErrorImpl>`
   --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/anyhow-1.0.88/src/ptr.rs:6:12
    |
6   | pub struct Own<T>
    |            ^^^
note: required because it appears within the type `Error`
   --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/anyhow-1.0.88/src/lib.rs:389:12
    |
389 | pub struct Error {
    |            ^^^^^
note: required because it's used within this closure
   --> src/main.rs:6:38
    |
6   |     let _ = std::panic::catch_unwind(move || err);
    |                                      ^^^^^^^
note: required by a bound in `catch_unwind`
   --> ~/.rustup/toolchains/1.72-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:141:40
    |
141 | pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(f: F) -> Result<R> {
    |                                        ^^^^^^^^^^ required by this bound in `catch_unwind`

@dtolnay dtolnay merged commit 830c399 into master Sep 15, 2024
27 checks passed
@dtolnay dtolnay deleted the unwindsafe branch September 15, 2024 02:45
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 this pull request may close these issues.

1 participant