From 58b873a3cd72cb70c67897ecd3bf3789b9e6eb53 Mon Sep 17 00:00:00 2001 From: Arkadiusz Piekarz Date: Sun, 1 Aug 2021 15:33:22 +0200 Subject: [PATCH 1/6] Add to README using backtraces on stable channel --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c6eb0f1..78e523a 100644 --- a/README.md +++ b/README.md @@ -75,10 +75,11 @@ anyhow = "1.0" } ``` -- If using the nightly channel, a backtrace is captured and printed with the - error if the underlying error type does not already provide its own. In order - to see backtraces, they must be enabled through the environment variables - described in [`std::backtrace`]: +- If using the stable channel with enabled "backtrace" feature or the nightly + channel, a backtrace is captured and printed with the error if the underlying + error type does not already provide its own. In order to see backtraces, they + must be enabled through the environment variables described in + [`std::backtrace`]: - If you want panics and errors to both have backtraces, set `RUST_BACKTRACE=1`; From b45d6490c4cf3b7679f97f877eea61941ebbdaae Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 14 Aug 2021 05:44:18 -0700 Subject: [PATCH 2/6] Ignore unhelpful Clippy lint in build script --- build.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.rs b/build.rs index a3585ef..968e68f 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,5 @@ +#![allow(clippy::option_if_let_else)] + use std::env; use std::fs; use std::path::Path; From bec8204d5f95c1272ead9a9bc0aa6b596b8b8f49 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 4 Sep 2021 21:57:52 -0700 Subject: [PATCH 3/6] Reword PR 162 --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 78e523a..e10063f 100644 --- a/README.md +++ b/README.md @@ -75,11 +75,10 @@ anyhow = "1.0" } ``` -- If using the stable channel with enabled "backtrace" feature or the nightly - channel, a backtrace is captured and printed with the error if the underlying - error type does not already provide its own. In order to see backtraces, they - must be enabled through the environment variables described in - [`std::backtrace`]: +- If using the nightly channel, or stable with `features = ["backtrace"]`, a + a backtrace is captured and printed with the error if the underlying error + type does not already provide its own. In order to see backtraces, they must + be enabled through the environment variables described in [`std::backtrace`]: - If you want panics and errors to both have backtraces, set `RUST_BACKTRACE=1`; From b3c5e258603a2a585038eed4b4187be758609b77 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 4 Sep 2021 21:59:22 -0700 Subject: [PATCH 4/6] Mirror PR 162 from readme to rustdoc --- src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d96f103..2a7e5d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,10 +128,11 @@ //! # ; //! ``` //! -//! - If using the nightly channel, a backtrace is captured and printed with the -//! error if the underlying error type does not already provide its own. In -//! order to see backtraces, they must be enabled through the environment -//! variables described in [`std::backtrace`]: +//! - If using the nightly channel, or stable with `features = ["backtrace"]`, a +//! backtrace is captured and printed with the error if the underlying error +//! type does not already provide its own. In order to see backtraces, they +//! must be enabled through the environment variables described in +//! [`std::backtrace`]: //! //! - If you want panics and errors to both have backtraces, set //! `RUST_BACKTRACE=1`; From fa9bcc0457a2e51593b874cc2f8bcb5608ad43fe Mon Sep 17 00:00:00 2001 From: Stiopa Koltsov Date: Sun, 12 Sep 2021 19:22:19 +0100 Subject: [PATCH 5/6] Mark error constructors cold All `#[cold]` to all error constructors. As discussed in the [issue #165](https://github.com/dtolnay/anyhow/issues/165), marking functions cold helps LLVM optimize code paths not leading to error. Although in our case marking only `from` was enough, I took the liberty of marking all error constructors `#[cold]`. Note, I marked cold both inner function and public functions, because LLVM does not always takes into account `#[cold]` attribute of inlined functions, see [an example in compiled explorer](https://rust.godbolt.org/z/vzxazMMnb). --- src/chain.rs | 1 + src/error.rs | 13 +++++++++++++ src/kind.rs | 3 +++ src/lib.rs | 1 + 4 files changed, 18 insertions(+) diff --git a/src/chain.rs b/src/chain.rs index 207e4f0..f7baff2 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -24,6 +24,7 @@ pub(crate) enum ChainState<'a> { } impl<'a> Chain<'a> { + #[cold] pub fn new(head: &'a (dyn StdError + 'static)) -> Self { Chain { state: ChainState::Linked { next: Some(head) }, diff --git a/src/error.rs b/src/error.rs index 92bb63d..3fa0835 100644 --- a/src/error.rs +++ b/src/error.rs @@ -25,6 +25,7 @@ impl Error { /// created here to ensure that a backtrace exists. #[cfg(feature = "std")] #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] + #[cold] pub fn new(error: E) -> Self where E: StdError + Send + Sync + 'static, @@ -70,6 +71,7 @@ impl Error { /// .await /// } /// ``` + #[cold] pub fn msg(message: M) -> Self where M: Display + Debug + Send + Sync + 'static, @@ -78,6 +80,7 @@ impl Error { } #[cfg(feature = "std")] + #[cold] pub(crate) fn from_std(error: E, backtrace: Option) -> Self where E: StdError + Send + Sync + 'static, @@ -100,6 +103,7 @@ impl Error { unsafe { Error::construct(error, vtable, backtrace) } } + #[cold] pub(crate) fn from_adhoc(message: M, backtrace: Option) -> Self where M: Display + Debug + Send + Sync + 'static, @@ -125,6 +129,7 @@ impl Error { unsafe { Error::construct(error, vtable, backtrace) } } + #[cold] pub(crate) fn from_display(message: M, backtrace: Option) -> Self where M: Display + Send + Sync + 'static, @@ -151,6 +156,7 @@ impl Error { } #[cfg(feature = "std")] + #[cold] pub(crate) fn from_context(context: C, error: E, backtrace: Option) -> Self where C: Display + Send + Sync + 'static, @@ -177,6 +183,7 @@ impl Error { } #[cfg(feature = "std")] + #[cold] pub(crate) fn from_boxed( error: Box, backtrace: Option, @@ -207,6 +214,7 @@ impl Error { // // Unsafe because the given vtable must have sensible behavior on the error // value of type E. + #[cold] unsafe fn construct( error: E, vtable: &'static ErrorVTable, @@ -284,6 +292,7 @@ impl Error { /// }) /// } /// ``` + #[cold] pub fn context(self, context: C) -> Self where C: Display + Send + Sync + 'static, @@ -373,6 +382,7 @@ impl Error { /// ``` #[cfg(feature = "std")] #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] + #[cold] pub fn chain(&self) -> Chain { unsafe { ErrorImpl::chain(self.inner.by_ref()) } } @@ -515,6 +525,7 @@ impl From for Error where E: StdError + Send + Sync + 'static, { + #[cold] fn from(error: E) -> Self { let backtrace = backtrace_if_absent!(error); Error::from_std(error, backtrace) @@ -879,6 +890,7 @@ impl ErrorImpl { .expect("backtrace capture failed") } + #[cold] pub(crate) unsafe fn chain(this: Ref) -> Chain { Chain::new(Self::error(this)) } @@ -917,6 +929,7 @@ where } impl From for Box { + #[cold] fn from(error: Error) -> Self { let outer = ManuallyDrop::new(error); unsafe { diff --git a/src/kind.rs b/src/kind.rs index eb8d604..5985705 100644 --- a/src/kind.rs +++ b/src/kind.rs @@ -62,6 +62,7 @@ pub trait AdhocKind: Sized { impl AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {} impl Adhoc { + #[cold] pub fn new(self, message: M) -> Error where M: Display + Debug + Send + Sync + 'static, @@ -82,6 +83,7 @@ pub trait TraitKind: Sized { impl TraitKind for E where E: Into {} impl Trait { + #[cold] pub fn new(self, error: E) -> Error where E: Into, @@ -106,6 +108,7 @@ impl BoxedKind for Box {} #[cfg(feature = "std")] impl Boxed { + #[cold] pub fn new(self, error: Box) -> Error { let backtrace = backtrace_if_absent!(error); Error::from_boxed(error, backtrace) diff --git a/src/lib.rs b/src/lib.rs index 2a7e5d5..7222344 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -626,6 +626,7 @@ pub mod private { pub use crate::kind::BoxedKind; } + #[cold] pub fn new_adhoc(message: M) -> Error where M: Display + Debug + Send + Sync + 'static, From 6c97c59375b24dad08c624cf61d06338f4dc9c3e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 12 Sep 2021 12:19:31 -0700 Subject: [PATCH 6/6] Release 1.0.44 --- Cargo.toml | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 30b821e..97fe475 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "anyhow" -version = "1.0.43" # remember to update html_root_url +version = "1.0.44" # remember to update html_root_url authors = ["David Tolnay "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/src/lib.rs b/src/lib.rs index 7222344..2b7a0b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,7 +210,7 @@ //! will require an explicit `.map_err(Error::msg)` when working with a //! non-Anyhow error type inside a function that returns Anyhow's error type. -#![doc(html_root_url = "https://docs.rs/anyhow/1.0.43")] +#![doc(html_root_url = "https://docs.rs/anyhow/1.0.44")] #![cfg_attr(backtrace, feature(backtrace))] #![cfg_attr(doc_cfg, feature(doc_cfg))] #![cfg_attr(not(feature = "std"), no_std)]