Merge pull request #266 from dtolnay/provider
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use backtrace via generic member access
authored by David Tolnay · committed by GitHub · parents 9f40a1e, 46d3d2c · Browse files · 7 files·+60 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+60 added−38 removed
- other:+60 added−38 removed
Provenance
- Commit
546342c- Tree
3b6d0de- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
9f40a1e- Parent 2
46d3d2c
7 files changed+60 added−38 removed
modified:src/backtrace.rs+1 added-1 removed
36 unmodified lines37 38 #[cfg(backtrace)]39 macro_rules! backtrace_if_absent {40 ($err:expr) => {41- match $err.backtrace() {41+ match ($err as &dyn std::error::Error).request_ref::<std::backtrace::Backtrace>() {42 Some(_) => None,43 None => backtrace!(),44 }45 };356 unmodified linesmodified:src/context.rs+13 added-12 removed
2 unmodified lines3 use core::convert::Infallible;4 use core::fmt::{self, Debug, Display, Write};5 6 #[cfg(backtrace)]7-use std::backtrace::Backtrace;7+use std::any::Demand;8 9 mod ext {10 use super::*;11 11 unmodified lines23 fn ext_context<C>(self, context: C) -> Error24 where25 C: Display + Send + Sync + 'static,26 {27- let backtrace = backtrace_if_absent!(self);27+ let backtrace = backtrace_if_absent!(&self);28 Error::from_context(context, self, backtrace)29 }30 }31 90 unmodified lines122 where123 C: Display,124 E: StdError + 'static,125 {126- #[cfg(backtrace)]127- fn backtrace(&self) -> Option<&Backtrace> {128- self.error.backtrace()129- }130-126 fn source(&self) -> Option<&(dyn StdError + 'static)> {127 Some(&self.error)128 }129+130+ #[cfg(backtrace)]131+ fn provide<'a>(&'a self, req: &mut Demand<'a>) {132+ self.error.provide(req);133+ }134 }135 136 impl<C> StdError for ContextError<C, Error>137 where138 C: Display,139 {140- #[cfg(backtrace)]141- fn backtrace(&self) -> Option<&Backtrace> {142- Some(self.error.backtrace())143- }144-140 fn source(&self) -> Option<&(dyn StdError + 'static)> {141 Some(unsafe { crate::ErrorImpl::error(self.error.inner.by_ref()) })142 }143+144+ #[cfg(backtrace)]145+ fn provide<'a>(&'a self, req: &mut Demand<'a>) {146+ req.provide_ref(self.error.backtrace());147+ self.error.provide(req);148+ }149 }150 151 struct Quoted<C>(C);152 26 unmodified linesmodified:src/error.rs+19 added-9 removed
3 unmodified lines4 use crate::ptr::Mut;5 use crate::ptr::{Own, Ref};6 use crate::{Error, StdError};7 use alloc::boxed::Box;8+#[cfg(backtrace)]9+use core::any::Demand;10 use core::any::TypeId;11 use core::fmt::{self, Debug, Display};12 use core::mem::ManuallyDrop;13 #[cfg(not(anyhow_no_ptr_addr_of))]18 unmodified lines32 pub fn new<E>(error: E) -> Self33 where34 E: StdError + Send + Sync + 'static,35 {34- let backtrace = backtrace_if_absent!(error);36+ let backtrace = backtrace_if_absent!(&error);37 Error::from_std(error, backtrace)38 }39 40 /// Create a new error object from a printable error message.490 unmodified lines531 E: StdError + Send + Sync + 'static,532 {533 #[cold]534 fn from(error: E) -> Self {533- let backtrace = backtrace_if_absent!(error);535+ let backtrace = backtrace_if_absent!(&error);536 Error::from_std(error, backtrace)537 }538 }539 347 unmodified lines887 .backtrace888 .as_ref()889 .or_else(|| {890 #[cfg(backtrace)]889- return Self::error(this).backtrace();890- #[cfg(all(not(backtrace), feature = "backtrace"))]891+ return Self::error(this).request_ref::<Backtrace>();892+ #[cfg(not(backtrace))]893 return (vtable(this.ptr).object_backtrace)(this);894 })895 .expect("backtrace capture failed")896 }897 898+ #[cfg(backtrace)]899+ unsafe fn provide<'a>(this: Ref<'a, Self>, req: &mut Demand<'a>) {900+ if let Some(backtrace) = &this.deref().backtrace {901+ req.provide_ref(backtrace);902+ }903+ Self::error(this).provide(req);904+ }905+906 #[cold]907 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {908 Chain::new(Self::error(this))909 }2 unmodified lines912 impl<E> StdError for ErrorImpl<E>913 where914 E: StdError,915 {906- #[cfg(backtrace)]907- fn backtrace(&self) -> Option<&Backtrace> {908- Some(unsafe { ErrorImpl::backtrace(self.erase()) })909- }910-916 fn source(&self) -> Option<&(dyn StdError + 'static)> {917 unsafe { ErrorImpl::error(self.erase()).source() }918 }919+920+ #[cfg(backtrace)]921+ fn provide<'a>(&'a self, req: &mut Demand<'a>) {922+ unsafe { ErrorImpl::provide(self.erase(), req) }923+ }924 }925 926 impl<E> Debug for ErrorImpl<E>927 where53 unmodified linesmodified:src/kind.rs+1 added-1 removed
108 unmodified lines109 #[cfg(feature = "std")]110 impl Boxed {111 #[cold]112 pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error {113- let backtrace = backtrace_if_absent!(error);113+ let backtrace = backtrace_if_absent!(&*error);114 Error::from_boxed(error, backtrace)115 }116 }modified:src/lib.rs+1 added-1 removed
209 unmodified lines210 //! 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.212 213 #![doc(html_root_url = "https://docs.rs/anyhow/1.0.62")]214-#![cfg_attr(backtrace, feature(backtrace))]214+#![cfg_attr(backtrace, feature(error_generic_member_access, provide_any))]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 #![allow(462 unmodified linesmodified:src/wrapper.rs+8 added-5 removed
1 use crate::StdError;2 use core::fmt::{self, Debug, Display};3 4+#[cfg(backtrace)]5+use std::any::Demand;6+7 #[repr(transparent)]8 pub struct MessageError<M>(pub M);9 10 impl<M> Debug for MessageError<M>58 unmodified lines69 }70 71 #[cfg(feature = "std")]72 impl StdError for BoxedError {70- #[cfg(backtrace)]71- fn backtrace(&self) -> Option<&crate::backtrace::Backtrace> {72- self.0.backtrace()73- }74-73 fn source(&self) -> Option<&(dyn StdError + 'static)> {74 self.0.source()75 }76+77+ #[cfg(backtrace)]78+ fn provide<'a>(&'a self, req: &mut Demand<'a>) {79+ self.0.provide(req);80+ }81 }modified:build.rs+17 added-9 removed
13 unmodified lines14 // This code exercises the surface area that we expect of the std Backtrace15 // type. If the current toolchain is able to compile it, we go ahead and use16 // backtrace in anyhow.17 const PROBE: &str = r#"18- #![feature(backtrace)]19- #![allow(dead_code)]18+ #![feature(error_generic_member_access, provide_any)]19 20+ use std::any::Demand;21 use std::backtrace::{Backtrace, BacktraceStatus};22 use std::error::Error;23 use std::fmt::{self, Display};24 25 #[derive(Debug)]26- struct E;26+ struct E {27+ backtrace: Backtrace,28+ }29 30 impl Display for E {31 fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {32 unimplemented!()33 }34 }35 36 impl Error for E {35- fn backtrace(&self) -> Option<&Backtrace> {36- let backtrace = Backtrace::capture();37- match backtrace.status() {38- BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}39- }40- unimplemented!()37+ fn provide<'a>(&'a self, req: &mut Demand<'a>) {38+ req.provide_ref(&self.backtrace);39 }40 }41+42+ const _: fn() = || {43+ let backtrace: Backtrace = Backtrace::capture();44+ let status: BacktraceStatus = backtrace.status();45+ match status {46+ BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}47+ }48+ };49+50+ const _: fn(&dyn Error) -> Option<&Backtrace> = |err| err.request_ref::<Backtrace>();51 "#;52 53 fn main() {54 if cfg!(feature = "std") {71 unmodified lines