Skip to content
Myr

Merge pull request #331 from dtolnay/backtrace

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

Separate cfgs for Backtrace vs Error::provide

authored by David Tolnay · committed by GitHub · parents 5121cd2, 53d6076 · Browse files · 6 files+59 added−25 removed
This commit

State

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

Diff stats

6 files+59 added−25 removed

  • other:+59 added−25 removed

6 files+59 added−25 removed

modified:src/backtrace.rs+6 added-2 removed
33 unmodified lines
34 None35 };36}3738#[cfg(backtrace)]38#[cfg(error_generic_member_access)]39macro_rules! backtrace_if_absent {40 ($err:expr) => {41 match std::error::request_ref::<std::backtrace::Backtrace>($err as &dyn std::error::Error) {42 Some(_) => None,
1 unmodified line
44 }45 };46}4748#[cfg(all(feature = "std", not(backtrace), feature = "backtrace"))]48#[cfg(all(49 feature = "std",50 not(error_generic_member_access),51 any(backtrace, feature = "backtrace")52))]53macro_rules! backtrace_if_absent {54 ($err:expr) => {55 backtrace!()56 };
349 unmodified lines
modified:src/context.rs+3 added-3 removed
1 unmodified line
2use crate::{Context, Error, StdError};3use core::convert::Infallible;4use core::fmt::{self, Debug, Display, Write};56#[cfg(backtrace)]6#[cfg(error_generic_member_access)]7use std::error::Request;89mod ext {10 use super::*;
131 unmodified lines
142 fn source(&self) -> Option<&(dyn StdError + 'static)> {143 Some(&self.error)144 }145146 #[cfg(backtrace)]146 #[cfg(error_generic_member_access)]147 fn provide<'a>(&'a self, request: &mut Request<'a>) {148 StdError::provide(&self.error, request);149 }150}
5 unmodified lines
156 fn source(&self) -> Option<&(dyn StdError + 'static)> {157 Some(unsafe { crate::ErrorImpl::error(self.error.inner.by_ref()) })158 }159160 #[cfg(backtrace)]160 #[cfg(error_generic_member_access)]161 fn provide<'a>(&'a self, request: &mut Request<'a>) {162 Error::provide(&self.error, request);163 }164}
29 unmodified lines
modified:src/error.rs+43 added-16 removed
9 unmodified lines
10use core::mem::ManuallyDrop;11#[cfg(not(anyhow_no_ptr_addr_of))]12use core::ptr;13use core::ptr::NonNull;14#[cfg(backtrace)]14#[cfg(error_generic_member_access)]15use std::error::{self, Request};1617#[cfg(feature = "std")]18use core::ops::{Deref, DerefMut};
79 unmodified lines
98 object_downcast: object_downcast::<E>,99 #[cfg(anyhow_no_ptr_addr_of)]100 object_downcast_mut: object_downcast_mut::<E>,101 object_drop_rest: object_drop_front::<E>,102 #[cfg(all(not(backtrace), feature = "backtrace"))]102 #[cfg(all(103 not(error_generic_member_access),104 any(backtrace, feature = "backtrace")105 ))]106 object_backtrace: no_backtrace,107 };108109 // Safety: passing vtable that operates on the right type E.
16 unmodified lines
126 object_downcast: object_downcast::<M>,127 #[cfg(anyhow_no_ptr_addr_of)]128 object_downcast_mut: object_downcast_mut::<M>,129 object_drop_rest: object_drop_front::<M>,127 #[cfg(all(not(backtrace), feature = "backtrace"))]130 #[cfg(all(131 not(error_generic_member_access),132 any(backtrace, feature = "backtrace")133 ))]134 object_backtrace: no_backtrace,135 };136137 // Safety: MessageError is repr(transparent) so it is okay for the
17 unmodified lines
155 object_downcast: object_downcast::<M>,156 #[cfg(anyhow_no_ptr_addr_of)]157 object_downcast_mut: object_downcast_mut::<M>,158 object_drop_rest: object_drop_front::<M>,153 #[cfg(all(not(backtrace), feature = "backtrace"))]159 #[cfg(all(160 not(error_generic_member_access),161 any(backtrace, feature = "backtrace")162 ))]163 object_backtrace: no_backtrace,164 };165166 // Safety: DisplayError is repr(transparent) so it is okay for the
19 unmodified lines
186 object_downcast: context_downcast::<C, E>,187 #[cfg(anyhow_no_ptr_addr_of)]188 object_downcast_mut: context_downcast_mut::<C, E>,189 object_drop_rest: context_drop_rest::<C, E>,181 #[cfg(all(not(backtrace), feature = "backtrace"))]190 #[cfg(all(191 not(error_generic_member_access),192 any(backtrace, feature = "backtrace")193 ))]194 object_backtrace: no_backtrace,195 };196197 // Safety: passing vtable that operates on the right type.
17 unmodified lines
215 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,216 #[cfg(anyhow_no_ptr_addr_of)]217 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,218 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,207 #[cfg(all(not(backtrace), feature = "backtrace"))]219 #[cfg(all(220 not(error_generic_member_access),221 any(backtrace, feature = "backtrace")222 ))]223 object_backtrace: no_backtrace,224 };225226 // Safety: BoxedError is repr(transparent) so it is okay for the vtable
104 unmodified lines
331 object_downcast: context_chain_downcast::<C>,332 #[cfg(anyhow_no_ptr_addr_of)]333 object_downcast_mut: context_chain_downcast_mut::<C>,334 object_drop_rest: context_chain_drop_rest::<C>,320 #[cfg(all(not(backtrace), feature = "backtrace"))]335 #[cfg(all(336 not(error_generic_member_access),337 any(backtrace, feature = "backtrace")338 ))]339 object_backtrace: context_backtrace::<C>,340 };341342 // As the cause is anyhow::Error, we already have a backtrace for it.
197 unmodified lines
540 Some(addr.cast::<E>().deref_mut())541 }542 }543526 #[cfg(backtrace)]544 #[cfg(error_generic_member_access)]545 pub(crate) fn provide<'a>(&'a self, request: &mut Request<'a>) {546 unsafe { ErrorImpl::provide(self.inner.by_ref(), request) }547 }548
1 unmodified line
550 // implementation includes the anyhow::Error's Backtrace if any, unlike551 // deref'ing to dyn Error where the provide implementation would include552 // only the original error's Backtrace from before it got wrapped into an553 // anyhow::Error.536 #[cfg(backtrace)]554 #[cfg(error_generic_member_access)]555 #[doc(hidden)]556 pub fn thiserror_provide<'a>(&'a self, request: &mut Request<'a>) {557 Self::provide(self, request);558 }
60 unmodified lines
619 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,620 #[cfg(anyhow_no_ptr_addr_of)]621 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,622 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),605 #[cfg(all(not(backtrace), feature = "backtrace"))]623 #[cfg(all(624 not(error_generic_member_access),625 any(backtrace, feature = "backtrace")626 ))]627 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,628}629630// Safety: requires layout of *e to match ErrorImpl<E>.
96 unmodified lines
727 None728 }729}730710#[cfg(all(not(backtrace), feature = "backtrace"))]731#[cfg(all(732 not(error_generic_member_access),733 any(backtrace, feature = "backtrace")734))]735fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {736 let _ = e;737 None738}
112 unmodified lines
851 }852}853854// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.831#[cfg(all(not(backtrace), feature = "backtrace"))]855#[cfg(all(856 not(error_generic_member_access),857 any(backtrace, feature = "backtrace")858))]859#[allow(clippy::unnecessary_wraps)]860unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>861where862 C: 'static,
71 unmodified lines
934 unsafe { this.deref() }935 .backtrace936 .as_ref()937 .or_else(|| {911 #[cfg(backtrace)]938 #[cfg(error_generic_member_access)]939 return error::request_ref::<Backtrace>(unsafe { Self::error(this) });913 #[cfg(not(backtrace))]940 #[cfg(not(error_generic_member_access))]941 return unsafe { (vtable(this.ptr).object_backtrace)(this) };942 })943 .expect("backtrace capture failed")944 }945919 #[cfg(backtrace)]946 #[cfg(error_generic_member_access)]947 unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {948 if let Some(backtrace) = unsafe { &this.deref().backtrace } {949 request.provide_ref(backtrace);950 }
13 unmodified lines
964 fn source(&self) -> Option<&(dyn StdError + 'static)> {965 unsafe { ErrorImpl::error(self.erase()).source() }966 }967941 #[cfg(backtrace)]968 #[cfg(error_generic_member_access)]969 fn provide<'a>(&'a self, request: &mut Request<'a>) {970 unsafe { ErrorImpl::provide(self.erase(), request) }971 }972}
56 unmodified lines
modified:src/lib.rs+1 added-1 removed
209 unmodified lines
210//! will require an explicit `.map_err(Error::msg)` when working with a211//! non-Anyhow error type inside a function that returns Anyhow's error type.212213#![doc(html_root_url = "https://docs.rs/anyhow/1.0.76")]214#![cfg_attr(backtrace, feature(error_generic_member_access))]214#![cfg_attr(error_generic_member_access, feature(error_generic_member_access))]215#![cfg_attr(doc_cfg, feature(doc_cfg))]216#![cfg_attr(not(feature = "std"), no_std)]217#![deny(dead_code, unused_imports, unused_mut)]218#![cfg_attr(
477 unmodified lines
modified:src/wrapper.rs+2 added-2 removed
1use crate::StdError;2use core::fmt::{self, Debug, Display};34#[cfg(backtrace)]4#[cfg(error_generic_member_access)]5use std::error::Request;67#[repr(transparent)]8pub struct MessageError<M>(pub M);
64 unmodified lines
73 fn source(&self) -> Option<&(dyn StdError + 'static)> {74 self.0.source()75 }7677 #[cfg(backtrace)]77 #[cfg(error_generic_member_access)]78 fn provide<'a>(&'a self, request: &mut Request<'a>) {79 self.0.provide(request);80 }81}
modified:build.rs+4 added-1 removed
48 unmodified lines
49 if cfg!(feature = "std") {50 println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");5152 match compile_probe() {53 Some(status) if status.success() => println!("cargo:rustc-cfg=backtrace"),53 Some(status) if status.success() => {54 println!("cargo:rustc-cfg=backtrace");55 println!("cargo:rustc-cfg=error_generic_member_access");56 }57 _ => {}58 }59 }60
84 unmodified lines