Merge pull request #319 from dtolnay/errorprovide
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Update to nightly's new Error::provide API
authored by David Tolnay · committed by GitHub · parents 496b958, 31c8dff · Browse files · 7 files·+46 added−53 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+46 added−53 removed
- other:+46 added−53 removed
Provenance
- Commit
8cf1d15- Tree
a1b8b92- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
496b958- Parent 2
31c8dff
7 files changed+46 added−53 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 as &dyn std::error::Error).request_ref::<std::backtrace::Backtrace>() {41+ match std::error::request_ref::<std::backtrace::Backtrace>($err as &dyn std::error::Error) {42 Some(_) => None,43 None => backtrace!(),44 }45 };356 unmodified linesmodified:src/context.rs+5 added-5 removed
2 unmodified lines3 use core::convert::Infallible;4 use core::fmt::{self, Debug, Display, Write};5 6 #[cfg(backtrace)]7-use std::any::{Demand, Provider};7+use std::error::Request;8 9 mod ext {10 use super::*;11 131 unmodified lines143 Some(&self.error)144 }145 146 #[cfg(backtrace)]147- fn provide<'a>(&'a self, demand: &mut Demand<'a>) {148- StdError::provide(&self.error, demand);147+ fn provide<'a>(&'a self, request: &mut Request<'a>) {148+ StdError::provide(&self.error, request);149 }150 }151 152 impl<C> StdError for ContextError<C, Error>4 unmodified lines157 Some(unsafe { crate::ErrorImpl::error(self.error.inner.by_ref()) })158 }159 160 #[cfg(backtrace)]161- fn provide<'a>(&'a self, demand: &mut Demand<'a>) {162- Provider::provide(&self.error, demand);161+ fn provide<'a>(&'a self, request: &mut Request<'a>) {162+ Error::provide(&self.error, request);163 }164 }165 166 struct Quoted<C>(C);27 unmodified linesmodified:src/error.rs+17 added-13 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;8 use core::any::TypeId;9 use core::fmt::{self, Debug, Display};10 use core::mem::ManuallyDrop;11 #[cfg(not(anyhow_no_ptr_addr_of))]12 use core::ptr;13 use core::ptr::NonNull;14+#[cfg(backtrace)]15+use std::error::{self, Request};16 17 #[cfg(feature = "std")]18 use core::ops::{Deref, DerefMut};19 501 unmodified lines521 522 Some(addr.cast::<E>().deref_mut())523 }524 }525-}525 527-#[cfg(backtrace)]528-impl std::any::Provider for Error {526+ #[cfg(backtrace)]527+ pub(crate) fn provide<'a>(&'a self, request: &mut Request<'a>) {528+ unsafe { ErrorImpl::provide(self.inner.by_ref(), request) }529+ }530+531 // Called by thiserror when you have `#[source] anyhow::Error`. This provide532 // implementation includes the anyhow::Error's Backtrace if any, unlike533 // deref'ing to dyn Error where the provide implementation would include534 // only the original error's Backtrace from before it got wrapped into an535 // anyhow::Error.534- fn provide<'a>(&'a self, demand: &mut Demand<'a>) {535- unsafe { ErrorImpl::provide(self.inner.by_ref(), demand) }536+ #[cfg(backtrace)]537+ #[doc(hidden)]538+ pub fn thiserror_provide<'a>(&'a self, request: &mut Request<'a>) {539+ Self::provide(self, request);540 }541 }542 543 #[cfg(feature = "std")]359 unmodified lines903 .backtrace904 .as_ref()905 .or_else(|| {906 #[cfg(backtrace)]903- return Self::error(this).request_ref::<Backtrace>();907+ return error::request_ref::<Backtrace>(Self::error(this));908 #[cfg(not(backtrace))]909 return (vtable(this.ptr).object_backtrace)(this);910 })911 .expect("backtrace capture failed")912 }913 914 #[cfg(backtrace)]911- unsafe fn provide<'a>(this: Ref<'a, Self>, demand: &mut Demand<'a>) {915+ unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {916 if let Some(backtrace) = &this.deref().backtrace {913- demand.provide_ref(backtrace);917+ request.provide_ref(backtrace);918 }915- Self::error(this).provide(demand);919+ Self::error(this).provide(request);920 }921 922 #[cold]923 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {9 unmodified lines933 unsafe { ErrorImpl::error(self.erase()).source() }934 }935 936 #[cfg(backtrace)]933- fn provide<'a>(&'a self, demand: &mut Demand<'a>) {934- unsafe { ErrorImpl::provide(self.erase(), demand) }937+ fn provide<'a>(&'a self, request: &mut Request<'a>) {938+ unsafe { ErrorImpl::provide(self.erase(), request) }939 }940 }941 942 impl<E> Debug for ErrorImpl<E>54 unmodified linesmodified: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.72")]214-#![cfg_attr(backtrace, feature(error_generic_member_access, provide_any))]214+#![cfg_attr(backtrace, 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 #![allow(465 unmodified linesmodified:src/wrapper.rs+3 added-3 removed
1 use crate::StdError;2 use core::fmt::{self, Debug, Display};3 4 #[cfg(backtrace)]5-use std::any::Demand;5+use std::error::Request;6 7 #[repr(transparent)]8 pub struct MessageError<M>(pub M);9 64 unmodified lines74 self.0.source()75 }76 77 #[cfg(backtrace)]78- fn provide<'a>(&'a self, demand: &mut Demand<'a>) {79- self.0.provide(demand);78+ fn provide<'a>(&'a self, request: &mut Request<'a>) {79+ self.0.provide(request);80 }81 }modified:build.rs+18 added-29 removed
9 unmodified lines10 compile_error! {11 "`backtrace` feature without `std` feature is not supported"12 }13 14-// 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.14+// This code exercises the surface area that we expect of the Error generic15+// member access API. If the current toolchain is able to compile it, then16+// anyhow is able to provide backtrace support.17 const PROBE: &str = r#"18- #![feature(error_generic_member_access, provide_any)]18+ #![feature(error_generic_member_access)]19 20- use std::any::{Demand, Provider};21- use std::backtrace::{Backtrace, BacktraceStatus};22- use std::error::Error;23- use std::fmt::{self, Display};20+ use std::backtrace::Backtrace;21+ use std::error::{self, Error, Request};22+ use std::fmt::{self, Debug, Display};23 25- #[derive(Debug)]26- struct E {27- backtrace: Backtrace,28- }24+ struct MyError(Thing);25+ struct Thing;26 30- impl Display for E {27+ impl Debug for MyError {28 fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {29 unimplemented!()30 }31 }32 36- impl Error for E {37- fn provide<'a>(&'a self, demand: &mut Demand<'a>) {38- demand.provide_ref(&self.backtrace);33+ impl Display for MyError {34+ fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {35+ unimplemented!()36 }37 }38 42- struct P;43-44- impl Provider for P {45- fn provide<'a>(&'a self, _demand: &mut Demand<'a>) {}39+ impl Error for MyError {40+ fn provide<'a>(&'a self, request: &mut Request<'a>) {41+ request.provide_ref(&self.0);42+ }43 }44 48- const _: fn() = || {49- let backtrace: Backtrace = Backtrace::capture();50- let status: BacktraceStatus = backtrace.status();51- match status {52- BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}53- }54- };55-56- const _: fn(&dyn Error) -> Option<&Backtrace> = |err| err.request_ref::<Backtrace>();45+ const _: fn(&dyn Error) -> Option<&Backtrace> = |err| error::request_ref::<Backtrace>(err);46 "#;47 48 fn main() {49 if cfg!(feature = "std") {71 unmodified linesmodified:Cargo.toml+1 added-1 removed
20 unmodified lines212223242525262728294 unmodified lines20 unmodified lines[dev-dependencies]futures = { version = "0.3", default-features = false }rustversion = "1.0.6"syn = { version = "2.0", features = ["full"] }thiserror = "1.0"thiserror = "1.0.45"trybuild = { version = "1.0.66", features = ["diff"] }
[lib]doc-scrape-examples = false4 unmodified lines