Skip to content

Commit

Permalink
backtrace -> std_backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Dec 26, 2023
1 parent 2b79af8 commit 1a5f7c8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn main() {
}

if nightly_backtrace_support || (cfg!(feature = "std") && rustc >= 65) {
println!("cargo:rustc-cfg=backtrace");
println!("cargo:rustc-cfg=std_backtrace");
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/backtrace.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
#[cfg(backtrace)]
#[cfg(std_backtrace)]
pub(crate) use std::backtrace::{Backtrace, BacktraceStatus};

#[cfg(all(not(backtrace), feature = "backtrace"))]
#[cfg(all(not(std_backtrace), feature = "backtrace"))]
pub(crate) use self::capture::{Backtrace, BacktraceStatus};

#[cfg(not(any(backtrace, feature = "backtrace")))]
#[cfg(not(any(std_backtrace, feature = "backtrace")))]
pub(crate) enum Backtrace {}

#[cfg(backtrace)]
#[cfg(std_backtrace)]
macro_rules! impl_backtrace {
() => {
std::backtrace::Backtrace
};
}

#[cfg(all(not(backtrace), feature = "backtrace"))]
#[cfg(all(not(std_backtrace), feature = "backtrace"))]
macro_rules! impl_backtrace {
() => {
impl core::fmt::Debug + core::fmt::Display
};
}

#[cfg(any(backtrace, feature = "backtrace"))]
#[cfg(any(std_backtrace, feature = "backtrace"))]
macro_rules! backtrace {
() => {
Some(crate::backtrace::Backtrace::capture())
};
}

#[cfg(not(any(backtrace, feature = "backtrace")))]
#[cfg(not(any(std_backtrace, feature = "backtrace")))]
macro_rules! backtrace {
() => {
None
Expand All @@ -45,21 +45,21 @@ macro_rules! backtrace_if_absent {
};
}

#[cfg(all(feature = "std", not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(feature = "std", not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
macro_rules! backtrace_if_absent {
($err:expr) => {
backtrace!()
};
}

#[cfg(all(feature = "std", not(backtrace), not(feature = "backtrace")))]
#[cfg(all(feature = "std", not(std_backtrace), not(feature = "backtrace")))]
macro_rules! backtrace_if_absent {
($err:expr) => {
None
};
}

#[cfg(all(not(backtrace), feature = "backtrace"))]
#[cfg(all(not(std_backtrace), feature = "backtrace"))]
mod capture {
use backtrace::{BacktraceFmt, BytesOrWideString, Frame, PrintFmt, SymbolName};
use core::cell::UnsafeCell;
Expand Down
22 changes: 11 additions & 11 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Error {
#[cfg(anyhow_no_ptr_addr_of)]
object_downcast_mut: object_downcast_mut::<E>,
object_drop_rest: object_drop_front::<E>,
#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
object_backtrace: no_backtrace,
};

Expand All @@ -124,7 +124,7 @@ impl Error {
#[cfg(anyhow_no_ptr_addr_of)]
object_downcast_mut: object_downcast_mut::<M>,
object_drop_rest: object_drop_front::<M>,
#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
object_backtrace: no_backtrace,
};

Expand All @@ -150,7 +150,7 @@ impl Error {
#[cfg(anyhow_no_ptr_addr_of)]
object_downcast_mut: object_downcast_mut::<M>,
object_drop_rest: object_drop_front::<M>,
#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
object_backtrace: no_backtrace,
};

Expand Down Expand Up @@ -178,7 +178,7 @@ impl Error {
#[cfg(anyhow_no_ptr_addr_of)]
object_downcast_mut: context_downcast_mut::<C, E>,
object_drop_rest: context_drop_rest::<C, E>,
#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
object_backtrace: no_backtrace,
};

Expand All @@ -204,7 +204,7 @@ impl Error {
#[cfg(anyhow_no_ptr_addr_of)]
object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,
object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,
#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
object_backtrace: no_backtrace,
};

Expand Down Expand Up @@ -317,7 +317,7 @@ impl Error {
#[cfg(anyhow_no_ptr_addr_of)]
object_downcast_mut: context_chain_downcast_mut::<C>,
object_drop_rest: context_chain_drop_rest::<C>,
#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
object_backtrace: context_backtrace::<C>,
};

Expand Down Expand Up @@ -354,7 +354,7 @@ impl Error {
/// [dependencies]
/// anyhow = { version = "1.0", features = ["backtrace"] }
/// ```
#[cfg(any(backtrace, feature = "backtrace"))]
#[cfg(any(std_backtrace, feature = "backtrace"))]
#[cfg_attr(doc_cfg, doc(cfg(any(nightly, feature = "backtrace"))))]
pub fn backtrace(&self) -> &impl_backtrace!() {
unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }
Expand Down Expand Up @@ -598,7 +598,7 @@ struct ErrorVTable {
#[cfg(anyhow_no_ptr_addr_of)]
object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,
object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),
#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,
}

Expand Down Expand Up @@ -703,7 +703,7 @@ where
}
}

#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {
let _ = e;
None
Expand Down Expand Up @@ -824,7 +824,7 @@ where
}

// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.
#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]
#[cfg(all(not(error_generic_member_access), any(std_backtrace, feature = "backtrace")))]
#[allow(clippy::unnecessary_wraps)]
unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>
where
Expand Down Expand Up @@ -895,7 +895,7 @@ impl ErrorImpl {
return unsafe { (vtable(this.ptr).object_mut)(this) };
}

#[cfg(any(backtrace, feature = "backtrace"))]
#[cfg(any(std_backtrace, feature = "backtrace"))]
pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {
// This unwrap can only panic if the underlying error's backtrace method
// is nondeterministic, which would only happen in maliciously
Expand Down
2 changes: 1 addition & 1 deletion src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl ErrorImpl {
}
}

#[cfg(any(backtrace, feature = "backtrace"))]
#[cfg(any(std_backtrace, feature = "backtrace"))]
{
use crate::backtrace::BacktraceStatus;

Expand Down
2 changes: 1 addition & 1 deletion tests/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn test_altdisplay() {
}

#[test]
#[cfg_attr(not(backtrace), ignore)]
#[cfg_attr(not(std_backtrace), ignore)]
fn test_debug() {
assert_eq!(EXPECTED_DEBUG_F, format!("{:?}", f().unwrap_err()));
assert_eq!(EXPECTED_DEBUG_G, format!("{:?}", g().unwrap_err()));
Expand Down

0 comments on commit 1a5f7c8

Please sign in to comment.