From 8454be372f2a7832436d7021b8f2f0a96db558b3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 14 Sep 2024 19:38:52 -0700 Subject: [PATCH] Ensure UnwindSafe even with "backtrace" feature enabled and old Rust --- build.rs | 7 +++++++ src/error.rs | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/build.rs b/build.rs index a065e04..200b2bf 100644 --- a/build.rs +++ b/build.rs @@ -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)"); @@ -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 diff --git a/src/error.rs b/src/error.rs index 6d8b54d..a83eb7e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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. @@ -1015,3 +1019,9 @@ impl AsRef 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 {}