Skip to content
Myr

Merge pull request #440 from dtolnay/backtrace

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Simplify `std_backtrace` conditional code

authored by David Tolnay · committed by GitHub · parents b8a9a70, efdb11a · 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

4 files+19 added−31 removed

modified:src/backtrace.rs+6 added-10 removed
1#[cfg(std_backtrace)]1#[cfg(feature = "std")]2pub(crate) use std::backtrace::{Backtrace, BacktraceStatus};34#[cfg(not(std_backtrace))]4#[cfg(not(feature = "std"))]5pub(crate) enum Backtrace {}67#[cfg(std_backtrace)]7#[cfg(feature = "std")]8macro_rules! backtrace {9 () => {10 Some(crate::backtrace::Backtrace::capture())11 };12}1314#[cfg(not(std_backtrace))]14#[cfg(not(feature = "std"))]15macro_rules! backtrace {16 () => {17 None18 };
8 unmodified lines
27 }28 };29}3031#[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"))]32macro_rules! backtrace_if_absent {33 ($err:expr) => {34 backtrace!()35 };36}3742#[cfg(all(any(feature = "std", not(anyhow_no_core_error)), not(std_backtrace)))]38#[cfg(all(not(anyhow_no_core_error), not(feature = "std")))]39macro_rules! backtrace_if_absent {40 ($err:expr) => {41 None42 };
6 unmodified lines
modified:src/error.rs+11 added-11 removed
153 unmodified lines
154 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 };161162 // Safety: passing vtable that operates on the right type E.
15 unmodified lines
178 #[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 };185186 // Safety: MessageError is repr(transparent) so it is okay for the
16 unmodified lines
203 #[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 };210211 // Safety: DisplayError is repr(transparent) so it is okay for the
22 unmodified lines
234 #[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 };241242 // Safety: passing vtable that operates on the right type.
16 unmodified lines
259 #[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 };266267 // Safety: BoxedError is repr(transparent) so it is okay for the vtable
103 unmodified lines
371 #[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 };378379 // As the cause is anyhow::Error, we already have a backtrace for it.
29 unmodified lines
409 /// ```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 lines
727 #[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}734735// Safety: requires layout of *e to match ErrorImpl<E>.
67 unmodified lines
803 None804 }805}806807#[cfg(all(not(error_generic_member_access), std_backtrace))]807#[cfg(all(not(error_generic_member_access), feature = "std"))]808fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {809 let _ = e;810 None811}
75 unmodified lines
887 }888}889890// 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)]893unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>894where895 C: 'static,
57 unmodified lines
953 .deref_mut()954 }955 }956957 #[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 lines
modified:src/fmt.rs+1 added-1 removed
38 unmodified lines
39 write!(indented, "{}", error)?;40 }41 }4243 #[cfg(std_backtrace)]43 #[cfg(feature = "std")]44 {45 use crate::backtrace::BacktraceStatus;46 use alloc::string::ToString;47
111 unmodified lines
modified:build.rs+1 added-9 removed
8 unmodified lines
9use std::process::{self, Command, Stdio};10use std::str;1112fn main() {13 let mut error_generic_member_access = false;13 if cfg!(feature = "std") {14 println!("cargo:rerun-if-changed=src/nightly.rs");1516 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 build
28 unmodified lines
49 consider_rustc_bootstrap = true;50 }5152 if error_generic_member_access {53 println!("cargo:rustc-cfg=std_backtrace");53 println!("cargo:rustc-cfg=error_generic_member_access");54 }5556 if consider_rustc_bootstrap {
10 unmodified lines
67 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 }7275 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 }8073 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