Simplify `std_backtrace` conditional code
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Tolnay · committed by David Tolnay · parent b8a9a70 · Browse files · 4 files·+19 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+19 added−31 removed
- other:+19 added−31 removed
Provenance
- Commit
efdb11a- Tree
c6d5575- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
b8a9a70
4 files changed+19 added−31 removed
modified:src/backtrace.rs+6 added-10 removed
1-#[cfg(std_backtrace)]1+#[cfg(feature = "std")]2 pub(crate) use std::backtrace::{Backtrace, BacktraceStatus};3 4-#[cfg(not(std_backtrace))]4+#[cfg(not(feature = "std"))]5 pub(crate) enum Backtrace {}6 7-#[cfg(std_backtrace)]7+#[cfg(feature = "std")]8 macro_rules! backtrace {9 () => {10 Some(crate::backtrace::Backtrace::capture())11 };12 }13 14-#[cfg(not(std_backtrace))]14+#[cfg(not(feature = "std"))]15 macro_rules! backtrace {16 () => {17 None18 };8 unmodified lines27 }28 };29 }30 31-#[cfg(all(32- any(feature = "std", not(anyhow_no_core_error)),33- not(error_generic_member_access),34- std_backtrace35-))]31+#[cfg(all(not(error_generic_member_access), feature = "std"))]32 macro_rules! backtrace_if_absent {33 ($err:expr) => {34 backtrace!()35 };36 }37 42-#[cfg(all(any(feature = "std", not(anyhow_no_core_error)), not(std_backtrace)))]38+#[cfg(all(not(anyhow_no_core_error), not(feature = "std")))]39 macro_rules! backtrace_if_absent {40 ($err:expr) => {41 None42 };6 unmodified linesmodified:src/error.rs+11 added-11 removed
153 unmodified lines154 object_boxed: object_boxed::<E>,155 object_reallocate_boxed: object_reallocate_boxed::<E>,156 object_downcast: object_downcast::<E>,157 object_drop_rest: object_drop_front::<E>,158- #[cfg(all(not(error_generic_member_access), std_backtrace))]158+ #[cfg(all(not(error_generic_member_access), feature = "std"))]159 object_backtrace: no_backtrace,160 };161 162 // Safety: passing vtable that operates on the right type E.15 unmodified lines178 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]179 object_reallocate_boxed: object_reallocate_boxed::<MessageError<M>>,180 object_downcast: object_downcast::<M>,181 object_drop_rest: object_drop_front::<M>,182- #[cfg(all(not(error_generic_member_access), std_backtrace))]182+ #[cfg(all(not(error_generic_member_access), feature = "std"))]183 object_backtrace: no_backtrace,184 };185 186 // Safety: MessageError is repr(transparent) so it is okay for the16 unmodified lines203 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]204 object_reallocate_boxed: object_reallocate_boxed::<DisplayError<M>>,205 object_downcast: object_downcast::<M>,206 object_drop_rest: object_drop_front::<M>,207- #[cfg(all(not(error_generic_member_access), std_backtrace))]207+ #[cfg(all(not(error_generic_member_access), feature = "std"))]208 object_backtrace: no_backtrace,209 };210 211 // Safety: DisplayError is repr(transparent) so it is okay for the22 unmodified lines234 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]235 object_reallocate_boxed: object_reallocate_boxed::<ContextError<C, E>>,236 object_downcast: context_downcast::<C, E>,237 object_drop_rest: context_drop_rest::<C, E>,238- #[cfg(all(not(error_generic_member_access), std_backtrace))]238+ #[cfg(all(not(error_generic_member_access), feature = "std"))]239 object_backtrace: no_backtrace,240 };241 242 // Safety: passing vtable that operates on the right type.16 unmodified lines259 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]260 object_reallocate_boxed: object_reallocate_boxed::<BoxedError>,261 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,262 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,263- #[cfg(all(not(error_generic_member_access), std_backtrace))]263+ #[cfg(all(not(error_generic_member_access), feature = "std"))]264 object_backtrace: no_backtrace,265 };266 267 // Safety: BoxedError is repr(transparent) so it is okay for the vtable103 unmodified lines371 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]372 object_reallocate_boxed: object_reallocate_boxed::<ContextError<C, Error>>,373 object_downcast: context_chain_downcast::<C>,374 object_drop_rest: context_chain_drop_rest::<C>,375- #[cfg(all(not(error_generic_member_access), std_backtrace))]375+ #[cfg(all(not(error_generic_member_access), feature = "std"))]376 object_backtrace: context_backtrace::<C>,377 };378 379 // As the cause is anyhow::Error, we already have a backtrace for it.29 unmodified lines409 /// ```toml410 /// [dependencies]411 /// anyhow = { version = "1.0", features = ["backtrace"] }412 /// ```413- #[cfg(std_backtrace)]413+ #[cfg(feature = "std")]414 pub fn backtrace(&self) -> &std::backtrace::Backtrace {415 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }416 }417 309 unmodified lines727 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]728 object_reallocate_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,729 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,730 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),731- #[cfg(all(not(error_generic_member_access), std_backtrace))]731+ #[cfg(all(not(error_generic_member_access), feature = "std"))]732 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,733 }734 735 // Safety: requires layout of *e to match ErrorImpl<E>.67 unmodified lines803 None804 }805 }806 807-#[cfg(all(not(error_generic_member_access), std_backtrace))]807+#[cfg(all(not(error_generic_member_access), feature = "std"))]808 fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {809 let _ = e;810 None811 }75 unmodified lines887 }888 }889 890 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.891-#[cfg(all(not(error_generic_member_access), std_backtrace))]891+#[cfg(all(not(error_generic_member_access), feature = "std"))]892 #[allow(clippy::unnecessary_wraps)]893 unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>894 where895 C: 'static,57 unmodified lines953 .deref_mut()954 }955 }956 957- #[cfg(std_backtrace)]957+ #[cfg(feature = "std")]958 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {959 // This unwrap can only panic if the underlying error's backtrace method960 // is nondeterministic, which would only happen in maliciously961 // constructed code.98 unmodified linesmodified:src/fmt.rs+1 added-1 removed
38 unmodified lines39 write!(indented, "{}", error)?;40 }41 }42 43- #[cfg(std_backtrace)]43+ #[cfg(feature = "std")]44 {45 use crate::backtrace::BacktraceStatus;46 use alloc::string::ToString;47 111 unmodified linesmodified:build.rs+1 added-9 removed
8 unmodified lines9 use std::process::{self, Command, Stdio};10 use std::str;11 12 fn main() {13- let mut error_generic_member_access = false;13 if cfg!(feature = "std") {14 println!("cargo:rerun-if-changed=src/nightly.rs");15 16+ let error_generic_member_access;17 let consider_rustc_bootstrap;18 if compile_probe(false) {19 // This is a nightly or dev compiler, so it supports unstable20 // features regardless of RUSTC_BOOTSTRAP. No need to rerun build28 unmodified lines49 consider_rustc_bootstrap = true;50 }51 52 if error_generic_member_access {53- println!("cargo:rustc-cfg=std_backtrace");53 println!("cargo:rustc-cfg=error_generic_member_access");54 }55 56 if consider_rustc_bootstrap {10 unmodified lines67 println!("cargo:rustc-check-cfg=cfg(anyhow_nightly_testing)");68 println!("cargo:rustc-check-cfg=cfg(anyhow_no_clippy_format_args)");69 println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_error)");70 println!("cargo:rustc-check-cfg=cfg(error_generic_member_access)");72- println!("cargo:rustc-check-cfg=cfg(std_backtrace)");71 }72 75- if !error_generic_member_access && cfg!(feature = "std") {76- // std::backtrace::Backtrace77- // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#stabilized-apis78- println!("cargo:rustc-cfg=std_backtrace");79- }80-73 if rustc < 81 {74 // core::error::Error75 // https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror76 println!("cargo:rustc-cfg=anyhow_no_core_error");118 unmodified lines