Merge pull request 293 from LukasKalbertodt/use-stable-backtrace
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 · parents 5121cd2, 49eeec1 · Browse files · 7 files·+48 added−46 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+48 added−46 removed
- other:+48 added−46 removed
Provenance
- Commit
d6acd22- Tree
06f8a31- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent 1
5121cd2- Parent 2
49eeec1
7 files changed+48 added−46 removed
modified:src/backtrace.rs+2 added-2 removed
33 unmodified lines34 None35 };36 }37 38-#[cfg(backtrace)]38+#[cfg(provide_api)]39 macro_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 line44 }45 };46 }47 48-#[cfg(all(feature = "std", not(backtrace), feature = "backtrace"))]48+#[cfg(all(feature = "std", not(provide_api), any(backtrace, feature = "backtrace")))]49 macro_rules! backtrace_if_absent {50 ($err:expr) => {51 backtrace!()52 };349 unmodified linesmodified:src/context.rs+3 added-3 removed
1 unmodified line2 use crate::{Context, Error, StdError};3 use core::convert::Infallible;4 use core::fmt::{self, Debug, Display, Write};5 6-#[cfg(backtrace)]6+#[cfg(provide_api)]7 use std::error::Request;8 9 mod ext {10 use super::*;131 unmodified lines142 fn source(&self) -> Option<&(dyn StdError + 'static)> {143 Some(&self.error)144 }145 146- #[cfg(backtrace)]146+ #[cfg(provide_api)]147 fn provide<'a>(&'a self, request: &mut Request<'a>) {148 StdError::provide(&self.error, request);149 }150 }5 unmodified lines156 fn source(&self) -> Option<&(dyn StdError + 'static)> {157 Some(unsafe { crate::ErrorImpl::error(self.error.inner.by_ref()) })158 }159 160- #[cfg(backtrace)]160+ #[cfg(provide_api)]161 fn provide<'a>(&'a self, request: &mut Request<'a>) {162 Error::provide(&self.error, request);163 }164 }29 unmodified linesmodified:src/error.rs+18 added-22 removed
9 unmodified lines10 use core::mem::ManuallyDrop;11 #[cfg(not(anyhow_no_ptr_addr_of))]12 use core::ptr;13 use core::ptr::NonNull;14-#[cfg(backtrace)]14+#[cfg(provide_api)]15 use std::error::{self, Request};16 17 #[cfg(feature = "std")]18 use core::ops::{Deref, DerefMut};79 unmodified lines98 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(not(provide_api), any(backtrace, feature = "backtrace")))]103 object_backtrace: no_backtrace,104 };105 106 // Safety: passing vtable that operates on the right type E.16 unmodified lines123 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(backtrace), feature = "backtrace"))]127+ #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]128 object_backtrace: no_backtrace,129 };130 131 // Safety: MessageError is repr(transparent) so it is okay for the17 unmodified lines149 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(backtrace), feature = "backtrace"))]153+ #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]154 object_backtrace: no_backtrace,155 };156 157 // Safety: DisplayError is repr(transparent) so it is okay for the19 unmodified lines177 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(backtrace), feature = "backtrace"))]181+ #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]182 object_backtrace: no_backtrace,183 };184 185 // Safety: passing vtable that operates on the right type.17 unmodified lines203 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(backtrace), feature = "backtrace"))]207+ #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]208 object_backtrace: no_backtrace,209 };210 211 // Safety: BoxedError is repr(transparent) so it is okay for the vtable104 unmodified lines316 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(backtrace), feature = "backtrace"))]320+ #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]321 object_backtrace: context_backtrace::<C>,322 };323 324 // As the cause is anyhow::Error, we already have a backtrace for it.19 unmodified lines344 /// `RUST_LIB_BACKTRACE=0`.345 ///346 /// # Stability347 ///348- /// Standard library backtraces are only available on the nightly channel.349- /// Tracking issue: [rust-lang/rust#53487][tracking].350- ///351- /// On stable compilers, this function is only available if the crate's348+ /// Standard library backtraces are only available when using Rust ≥ 1.65.349+ /// On older compilers, this function is only available if the crate's350 /// "backtrace" feature is enabled, and will use the `backtrace` crate as351 /// the underlying backtrace implementation.352 ///353 /// ```toml354 /// [dependencies]355 /// anyhow = { version = "1.0", features = ["backtrace"] }356 /// ```359- ///360- /// [tracking]: https://github.com/rust-lang/rust/issues/53487357 #[cfg(any(backtrace, feature = "backtrace"))]358 #[cfg_attr(doc_cfg, doc(cfg(any(nightly, feature = "backtrace"))))]359 pub fn backtrace(&self) -> &impl_backtrace!() {360 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }157 unmodified lines518 Some(addr.cast::<E>().deref_mut())519 }520 }521 526- #[cfg(backtrace)]522+ #[cfg(provide_api)]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 line528 // 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.536- #[cfg(backtrace)]532+ #[cfg(provide_api)]533 #[doc(hidden)]534 pub fn thiserror_provide<'a>(&'a self, request: &mut Request<'a>) {535 Self::provide(self, request);536 }60 unmodified lines597 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),605- #[cfg(all(not(backtrace), feature = "backtrace"))]601+ #[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]602 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,603 }604 605 // Safety: requires layout of *e to match ErrorImpl<E>.96 unmodified lines702 None703 }704 }705 710-#[cfg(all(not(backtrace), feature = "backtrace"))]706+#[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]707 fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {708 let _ = e;709 None710 }112 unmodified lines823 }824 }825 826 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.831-#[cfg(all(not(backtrace), feature = "backtrace"))]827+#[cfg(all(not(provide_api), any(backtrace, feature = "backtrace")))]828 #[allow(clippy::unnecessary_wraps)]829 unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>830 where831 C: 'static,71 unmodified lines903 unsafe { this.deref() }904 .backtrace905 .as_ref()906 .or_else(|| {911- #[cfg(backtrace)]907+ #[cfg(provide_api)]908 return error::request_ref::<Backtrace>(unsafe { Self::error(this) });913- #[cfg(not(backtrace))]909+ #[cfg(not(provide_api))]910 return unsafe { (vtable(this.ptr).object_backtrace)(this) };911 })912 .expect("backtrace capture failed")913 }914 919- #[cfg(backtrace)]915+ #[cfg(provide_api)]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 lines933 fn source(&self) -> Option<&(dyn StdError + 'static)> {934 unsafe { ErrorImpl::error(self.erase()).source() }935 }936 941- #[cfg(backtrace)]937+ #[cfg(provide_api)]938 fn provide<'a>(&'a self, request: &mut Request<'a>) {939 unsafe { ErrorImpl::provide(self.erase(), request) }940 }941 }56 unmodified linesmodified:src/lib.rs+5 added-9 removed
126 unmodified lines127 //! }128 //! # ;129 //! ```130 //!131-//! - If using the nightly channel, or stable with `features = ["backtrace"]`, a132-//! backtrace is captured and printed with the error if the underlying error133-//! type does not already provide its own. In order to see backtraces, they134-//! must be enabled through the environment variables described in135-//! [`std::backtrace`]:131+//! - If using Rust ≥ 1.65 or `features = ["backtrace"]`, a backtrace is captured132+//! and printed with the error if the underlying error type does not already133+//! provide its own. In order to see backtraces, they must be enabled through the134+//! environment variables described in [`std::backtrace`]:135 //!136 //! - If you want panics and errors to both have backtraces, set137 //! `RUST_BACKTRACE=1`;138 //! - If you want only errors to have backtraces, set `RUST_LIB_BACKTRACE=1`;139 //! - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and140 //! `RUST_LIB_BACKTRACE=0`.141 //!143-//! The tracking issue for this feature is [rust-lang/rust#53487].144-//!142 //! [`std::backtrace`]: https://doc.rust-lang.org/std/backtrace/index.html#environment-variables146-//! [rust-lang/rust#53487]: https://github.com/rust-lang/rust/issues/53487143 //!144 //! - Anyhow works with any error type that has an impl of `std::error::Error`,145 //! including ones defined in your crate. We do not bundle a `derive(Error)`146 //! macro but you can write the impls yourself or use a standalone macro like59 unmodified lines206 //! 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.208 209 #![doc(html_root_url = "https://docs.rs/anyhow/1.0.76")]214-#![cfg_attr(backtrace, feature(error_generic_member_access))]210+#![cfg_attr(provide_api, 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 linesmodified:src/wrapper.rs+2 added-2 removed
1 use crate::StdError;2 use core::fmt::{self, Debug, Display};3 4-#[cfg(backtrace)]4+#[cfg(provide_api)]5 use std::error::Request;6 7 #[repr(transparent)]8 pub struct MessageError<M>(pub M);64 unmodified lines73 fn source(&self) -> Option<&(dyn StdError + 'static)> {74 self.0.source()75 }76 77- #[cfg(backtrace)]77+ #[cfg(provide_api)]78 fn provide<'a>(&'a self, request: &mut Request<'a>) {79 self.0.provide(request);80 }81 }modified:build.rs+14 added-1 removed
44 unmodified lines45 const _: fn(&dyn Error) -> Option<&Backtrace> = |err| error::request_ref::<Backtrace>(err);46 "#;47 48 fn 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)` 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");57 58 match compile_probe() {53- Some(status) if status.success() => println!("cargo:rustc-cfg=backtrace"),59+ Some(status) if status.success() => {60+ println!("cargo:rustc-cfg=provide_api");61+ nightly_backtrace_support = true;62+ }63 _ => {}64 }65 }66 16 unmodified lines83 // #![deny(unsafe_op_in_unsafe_fn)]84 // https://github.com/rust-lang/rust/issues/7166885 println!("cargo:rustc-cfg=anyhow_no_unsafe_op_in_unsafe_fn_lint");86 }87+88+ if nightly_backtrace_support || (cfg!(feature = "std") && rustc >= 65) {89+ println!("cargo:rustc-cfg=backtrace");90+ }91 }92 93 fn compile_probe() -> Option<ExitStatus> {94 if env::var_os("RUSTC_STAGE").is_some() {60 unmodified linesmodified:README.md+4 added-7 removed
73 unmodified lines74757677787980817879808182838485868788899089929091929386 unmodified lines73 unmodified lines None => Err(error), } ```
- 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 using Rust ≥ 1.65 or `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`; - If you want only errors to have backtraces, set `RUST_LIB_BACKTRACE=1`; - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and `RUST_LIB_BACKTRACE=0`.
The tracking issue for this feature is [rust-lang/rust#53487].
[`std::backtrace`]: https://doc.rust-lang.org/std/backtrace/index.html#environment-variables [rust-lang/rust#53487]: https://github.com/rust-lang/rust/issues/53487
- Anyhow works with any error type that has an impl of `std::error::Error`, including ones defined in your crate. We do not bundle a `derive(Error)` macro but you can write the impls yourself or use a standalone macro like86 unmodified lines