Skip to content
Myr

provide_api -> error_generic_member_access

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 d6acd22 · Browse files · 6 files+26 added−26 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+26 added−26 removed

  • other:+26 added−26 removed

6 files+26 added−26 removed

modified:src/backtrace.rs+2 added-2 removed
33 unmodified lines
34 None35 };36}3738#[cfg(provide_api)]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(provide_api), any(backtrace, feature = "backtrace")))]48#[cfg(all(feature = "std", not(error_generic_member_access), any(backtrace, feature = "backtrace")))]49macro_rules! backtrace_if_absent {50 ($err:expr) => {51 backtrace!()52 };
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(provide_api)]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(provide_api)]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(provide_api)]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+16 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(provide_api)]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(provide_api), any(backtrace, feature = "backtrace")))]102 #[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]103 object_backtrace: no_backtrace,104 };105106 // Safety: passing vtable that operates on the right type E.
16 unmodified lines
123 object_downcast: object_downcast::<M>,124 #[cfg(anyhow_no_ptr_addr_of)]125 object_downcast_mut: object_downcast_mut::<M>,126 object_drop_rest: object_drop_front::<M>,127 #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]127 #[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]128 object_backtrace: no_backtrace,129 };130131 // Safety: MessageError is repr(transparent) so it is okay for the
17 unmodified lines
149 object_downcast: object_downcast::<M>,150 #[cfg(anyhow_no_ptr_addr_of)]151 object_downcast_mut: object_downcast_mut::<M>,152 object_drop_rest: object_drop_front::<M>,153 #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]153 #[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]154 object_backtrace: no_backtrace,155 };156157 // Safety: DisplayError is repr(transparent) so it is okay for the
19 unmodified lines
177 object_downcast: context_downcast::<C, E>,178 #[cfg(anyhow_no_ptr_addr_of)]179 object_downcast_mut: context_downcast_mut::<C, E>,180 object_drop_rest: context_drop_rest::<C, E>,181 #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]181 #[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]182 object_backtrace: no_backtrace,183 };184185 // Safety: passing vtable that operates on the right type.
17 unmodified lines
203 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,204 #[cfg(anyhow_no_ptr_addr_of)]205 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,206 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,207 #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]207 #[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]208 object_backtrace: no_backtrace,209 };210211 // Safety: BoxedError is repr(transparent) so it is okay for the vtable
104 unmodified lines
316 object_downcast: context_chain_downcast::<C>,317 #[cfg(anyhow_no_ptr_addr_of)]318 object_downcast_mut: context_chain_downcast_mut::<C>,319 object_drop_rest: context_chain_drop_rest::<C>,320 #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]320 #[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]321 object_backtrace: context_backtrace::<C>,322 };323324 // As the cause is anyhow::Error, we already have a backtrace for it.
193 unmodified lines
518 Some(addr.cast::<E>().deref_mut())519 }520 }521522 #[cfg(provide_api)]522 #[cfg(error_generic_member_access)]523 pub(crate) fn provide<'a>(&'a self, request: &mut Request<'a>) {524 unsafe { ErrorImpl::provide(self.inner.by_ref(), request) }525 }526
1 unmodified line
528 // implementation includes the anyhow::Error's Backtrace if any, unlike529 // deref'ing to dyn Error where the provide implementation would include530 // only the original error's Backtrace from before it got wrapped into an531 // anyhow::Error.532 #[cfg(provide_api)]532 #[cfg(error_generic_member_access)]533 #[doc(hidden)]534 pub fn thiserror_provide<'a>(&'a self, request: &mut Request<'a>) {535 Self::provide(self, request);536 }
60 unmodified lines
597 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,598 #[cfg(anyhow_no_ptr_addr_of)]599 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,600 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),601 #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]601 #[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]602 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,603}604605// Safety: requires layout of *e to match ErrorImpl<E>.
96 unmodified lines
702 None703 }704}705706#[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]706#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]707fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {708 let _ = e;709 None710}
112 unmodified lines
823 }824}825826// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.827#[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]827#[cfg(all(not(error_generic_member_access), any(backtrace, feature = "backtrace")))]828#[allow(clippy::unnecessary_wraps)]829unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>830where831 C: 'static,
71 unmodified lines
903 unsafe { this.deref() }904 .backtrace905 .as_ref()906 .or_else(|| {907 #[cfg(provide_api)]907 #[cfg(error_generic_member_access)]908 return error::request_ref::<Backtrace>(unsafe { Self::error(this) });909 #[cfg(not(provide_api))]909 #[cfg(not(error_generic_member_access))]910 return unsafe { (vtable(this.ptr).object_backtrace)(this) };911 })912 .expect("backtrace capture failed")913 }914915 #[cfg(provide_api)]915 #[cfg(error_generic_member_access)]916 unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {917 if let Some(backtrace) = unsafe { &this.deref().backtrace } {918 request.provide_ref(backtrace);919 }
13 unmodified lines
933 fn source(&self) -> Option<&(dyn StdError + 'static)> {934 unsafe { ErrorImpl::error(self.erase()).source() }935 }936937 #[cfg(provide_api)]937 #[cfg(error_generic_member_access)]938 fn provide<'a>(&'a self, request: &mut Request<'a>) {939 unsafe { ErrorImpl::provide(self.erase(), request) }940 }941}
56 unmodified lines
modified:src/lib.rs+1 added-1 removed
205 unmodified lines
206//! will require an explicit `.map_err(Error::msg)` when working with a207//! non-Anyhow error type inside a function that returns Anyhow's error type.208209#![doc(html_root_url = "https://docs.rs/anyhow/1.0.76")]210#![cfg_attr(provide_api, feature(error_generic_member_access))]210#![cfg_attr(error_generic_member_access, feature(error_generic_member_access))]211#![cfg_attr(doc_cfg, feature(doc_cfg))]212#![cfg_attr(not(feature = "std"), no_std)]213#![deny(dead_code, unused_imports, unused_mut)]214#![cfg_attr(
477 unmodified lines
modified:src/wrapper.rs+2 added-2 removed
1use crate::StdError;2use core::fmt::{self, Debug, Display};34#[cfg(provide_api)]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(provide_api)]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+2 added-2 removed
46 unmodified lines
4748fn main() {49 // If the compile probe succeeds, i.e. if a nightly compiler is used and the50 // provide API exists, then `std::backtrace` is also usable. So `cfg51 // (provide_api)` implies `cfg(backtrace)`. We do set `cfg(backtrace)` in51 // (error_generic_member_access)` implies `cfg(backtrace)`. We do set `cfg(backtrace)` in52 // this script explicitly anyway to make the `cfg()` attributes in the main53 // code less complex.54 let mut nightly_backtrace_support = false;55 if cfg!(feature = "std") {56 println!("cargo:rerun-if-env-changed=RUSTC_BOOTSTRAP");5758 match compile_probe() {59 Some(status) if status.success() => {60 println!("cargo:rustc-cfg=provide_api");60 println!("cargo:rustc-cfg=error_generic_member_access");61 nightly_backtrace_support = true;62 }63 _ => {}64 }
90 unmodified lines