Skip to content

Commit

Permalink
Ensure UnwindSafe even with "backtrace" feature enabled and old Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 15, 2024
1 parent a85e414 commit 8454be3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn main() {
if rustc >= 80 {
println!("cargo:rustc-check-cfg=cfg(anyhow_nightly_testing)");
println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_error)");
println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_unwind_safe)");
println!("cargo:rustc-check-cfg=cfg(anyhow_no_fmt_arguments_as_str)");
println!("cargo:rustc-check-cfg=cfg(anyhow_no_ptr_addr_of)");
println!("cargo:rustc-check-cfg=cfg(anyhow_no_unsafe_op_in_unsafe_fn_lint)");
Expand All @@ -91,6 +92,12 @@ fn main() {
println!("cargo:rustc-cfg=anyhow_no_unsafe_op_in_unsafe_fn_lint");
}

if rustc < 56 {
// core::panic::{UnwindSafe, RefUnwindSafe}
// https://blog.rust-lang.org/2021/10/21/Rust-1.56.0.html#stabilized-apis
println!("cargo:rustc-cfg=anyhow_no_core_unwind_safe");
}

if !error_generic_member_access && cfg!(feature = "std") && rustc >= 65 {
// std::backtrace::Backtrace
// https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#stabilized-apis
Expand Down
10 changes: 10 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ use core::fmt::{self, Debug, Display};
use core::mem::ManuallyDrop;
#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
use core::ops::{Deref, DerefMut};
#[cfg(not(anyhow_no_core_unwind_safe))]
use core::panic::{RefUnwindSafe, UnwindSafe};
#[cfg(not(anyhow_no_ptr_addr_of))]
use core::ptr;
use core::ptr::NonNull;
#[cfg(all(feature = "std", anyhow_no_core_unwind_safe))]
use std::panic::{RefUnwindSafe, UnwindSafe};

impl Error {
/// Create a new error object from any error type.
Expand Down Expand Up @@ -1015,3 +1019,9 @@ impl AsRef<dyn StdError> for Error {
&**self
}
}

#[cfg(any(feature = "std", not(anyhow_no_core_unwind_safe)))]
impl UnwindSafe for Error {}

#[cfg(any(feature = "std", not(anyhow_no_core_unwind_safe)))]
impl RefUnwindSafe for Error {}

0 comments on commit 8454be3

Please sign in to comment.