Support error sources in no-std on Rust 1.81+
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 1eabf69 · Browse files · 9 files·+64 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+64 added−58 removed
- other:+64 added−58 removed
Provenance
- Commit
bee814a- Tree
637e2da- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
1eabf69
9 files changed+64 added−58 removed
modified:.github/workflows/ci.yml+1 added-1 removed
22 unmodified lines23242526272728293031116 unmodified linesExpand all22 unmodified lines runs-on: ubuntu-latest strategy: fail-fast: false matrix: rust: [nightly, beta, stable, 1.70.0] rust: [nightly, beta, stable, 1.80.0, 1.70.0] timeout-minutes: 45 steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master116 unmodified linesExpand allmodified:src/backtrace.rs+6 added-2 removed
45 unmodified lines46 };47 }48 49 #[cfg(all(50- feature = "std",50+ any(feature = "std", not(anyhow_no_core_error)),51 not(error_generic_member_access),52 any(std_backtrace, feature = "backtrace")53 ))]54 macro_rules! backtrace_if_absent {1 unmodified line56 backtrace!()57 };58 }59 60-#[cfg(all(feature = "std", not(std_backtrace), not(feature = "backtrace")))]60+#[cfg(all(61+ any(feature = "std", not(anyhow_no_core_error)),62+ not(std_backtrace),63+ not(feature = "backtrace"),64+))]65 macro_rules! backtrace_if_absent {66 ($err:expr) => {67 None68 };343 unmodified linesmodified:src/chain.rs+8 added-8 removed
1 use self::ChainState::*;2 use crate::StdError;3 4-#[cfg(feature = "std")]4+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]5 use alloc::vec::{self, Vec};6 7-#[cfg(feature = "std")]7+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]8 pub(crate) use crate::Chain;9 10-#[cfg(not(feature = "std"))]10+#[cfg(all(not(feature = "std"), anyhow_no_core_error))]11 pub(crate) struct Chain<'a> {12 state: ChainState<'a>,13 }14 1 unmodified line16 pub(crate) enum ChainState<'a> {17 Linked {18 next: Option<&'a (dyn StdError + 'static)>,19 },20- #[cfg(feature = "std")]20+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]21 Buffered {22 rest: vec::IntoIter<&'a (dyn StdError + 'static)>,23 },24 }16 unmodified lines41 let error = (*next)?;42 *next = error.source();43 Some(error)44 }45- #[cfg(feature = "std")]45+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]46 Buffered { rest } => rest.next(),47 }48 }49 2 unmodified lines52 (len, Some(len))53 }54 }55 56-#[cfg(feature = "std")]56+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]57 impl DoubleEndedIterator for Chain<'_> {58 fn next_back(&mut self) -> Option<Self::Item> {59 match &mut self.state {60 Linked { mut next } => {22 unmodified lines83 len += 1;84 }85 len86 }87- #[cfg(feature = "std")]87+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]88 Buffered { rest } => rest.len(),89 }90 }91 }92 93-#[cfg(feature = "std")]93+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]94 impl Default for Chain<'_> {95 fn default() -> Self {96 Chain {97 state: ChainState::Buffered {5 unmodified linesmodified:src/context.rs+2 added-2 removed
13 unmodified lines14 where15 C: Display + Send + Sync + 'static;16 }17 18- #[cfg(feature = "std")]18+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]19 impl<E> StdError for E20 where21- E: std::error::Error + Send + Sync + 'static,21+ E: crate::StdError + Send + Sync + 'static,22 {23 fn ext_context<C>(self, context: C) -> Error24 where25 C: Display + Send + Sync + 'static,168 unmodified linesmodified:src/error.rs+22 added-29 removed
1 use crate::backtrace::Backtrace;2 use crate::chain::Chain;3-#[cfg(any(feature = "std", anyhow_no_ptr_addr_of))]3+#[cfg(any(feature = "std", not(anyhow_no_core_error), anyhow_no_ptr_addr_of))]4 use crate::ptr::Mut;5 use crate::ptr::{Own, Ref};6 use crate::{Error, StdError};7 use alloc::boxed::Box;1 unmodified line9 #[cfg(error_generic_member_access)]10 use core::error::{self, Request};11 use core::fmt::{self, Debug, Display};12 use core::mem::ManuallyDrop;13+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]14+use core::ops::{Deref, DerefMut};15 #[cfg(not(anyhow_no_ptr_addr_of))]16 use core::ptr;17 use core::ptr::NonNull;18 17-#[cfg(feature = "std")]18-use core::ops::{Deref, DerefMut};19-19 impl Error {20 /// Create a new error object from any error type.21 ///22 /// The error type must be threadsafe and `'static`, so that the `Error`23 /// will be as well.24 ///25 /// If the error type does not provide a backtrace, a backtrace will be26 /// created here to ensure that a backtrace exists.28- #[cfg(feature = "std")]29- #[cfg_attr(docsrs, doc(cfg(feature = "std")))]27+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]28 #[cold]29 #[must_use]30 pub fn new<E>(error: E) -> Self31 where48 unmodified lines80 {81 Error::from_adhoc(message, backtrace!())82 }83 86- #[cfg(feature = "std")]84+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]85 #[cold]86 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self87 where88 E: StdError + Send + Sync + 'static,28 unmodified lines117 let error: MessageError<M> = MessageError(message);118 let vtable = &ErrorVTable {119 object_drop: object_drop::<MessageError<M>>,120 object_ref: object_ref::<MessageError<M>>,123- #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]121+ #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]122 object_mut: object_mut::<MessageError<M>>,123 object_boxed: object_boxed::<MessageError<M>>,124 object_downcast: object_downcast::<M>,125 #[cfg(anyhow_no_ptr_addr_of)]20 unmodified lines146 let error: DisplayError<M> = DisplayError(message);147 let vtable = &ErrorVTable {148 object_drop: object_drop::<DisplayError<M>>,149 object_ref: object_ref::<DisplayError<M>>,152- #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]150+ #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]151 object_mut: object_mut::<DisplayError<M>>,152 object_boxed: object_boxed::<DisplayError<M>>,153 object_downcast: object_downcast::<M>,154 #[cfg(anyhow_no_ptr_addr_of)]10 unmodified lines165 // vtable to allow casting the DisplayError<M> to M.166 unsafe { Error::construct(error, vtable, backtrace) }167 }168 171- #[cfg(feature = "std")]169+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]170 #[cold]171 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self172 where173 C: Display + Send + Sync + 'static,21 unmodified lines195 // Safety: passing vtable that operates on the right type.196 unsafe { Error::construct(error, vtable, backtrace) }197 }198 201- #[cfg(feature = "std")]199+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]200 #[cold]201 pub(crate) fn from_boxed(202 error: Box<dyn StdError + Send + Sync>,203 backtrace: Option<Backtrace>,118 unmodified lines322 323 let vtable = &ErrorVTable {324 object_drop: object_drop::<ContextError<C, Error>>,325 object_ref: object_ref::<ContextError<C, Error>>,328- #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]326+ #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]327 object_mut: object_mut::<ContextError<C, Error>>,328 object_boxed: object_boxed::<ContextError<C, Error>>,329 object_downcast: context_chain_downcast::<C>,330 #[cfg(anyhow_no_ptr_addr_of)]65 unmodified lines396 /// }397 /// None398 /// }399 /// ```402- #[cfg(feature = "std")]403- #[cfg_attr(docsrs, doc(cfg(feature = "std")))]400+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]401 #[cold]402 pub fn chain(&self) -> Chain {403 unsafe { ErrorImpl::chain(self.inner.by_ref()) }404 }2 unmodified lines407 /// cause's cause etc.408 ///409 /// The root cause is the last error in the iterator produced by410 /// [`chain()`][Error::chain].414- #[cfg(feature = "std")]415- #[cfg_attr(docsrs, doc(cfg(feature = "std")))]411+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]412 pub fn root_cause(&self) -> &(dyn StdError + 'static) {413 self.chain().last().unwrap()414 }415 133 unmodified lines549 Self::provide(self, request);550 }551 }552 557-#[cfg(feature = "std")]558-#[cfg_attr(docsrs, doc(cfg(feature = "std")))]553+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]554 impl<E> From<E> for Error555 where556 E: StdError + Send + Sync + 'static,557 {3 unmodified lines561 Error::from_std(error, backtrace)562 }563 }564 570-#[cfg(feature = "std")]571-#[cfg_attr(docsrs, doc(cfg(feature = "std")))]565+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]566 impl Deref for Error {567 type Target = dyn StdError + Send + Sync + 'static;568 569 fn deref(&self) -> &Self::Target {570 unsafe { ErrorImpl::error(self.inner.by_ref()) }571 }572 }573 580-#[cfg(feature = "std")]581-#[cfg_attr(docsrs, doc(cfg(feature = "std")))]574+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]575 impl DerefMut for Error {576 fn deref_mut(&mut self) -> &mut Self::Target {577 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }578 }22 unmodified lines601 602 struct ErrorVTable {603 object_drop: unsafe fn(Own<ErrorImpl>),604 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,612- #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]605+ #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]606 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),607 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,608 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,609 #[cfg(anyhow_no_ptr_addr_of)]43 unmodified lines653 }654 655 // Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived656 // from a `&mut`664-#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]657+#[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]658 unsafe fn object_mut<E>(e: Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static)659 where660 E: StdError + Send + Sync + 'static,661 {64 unmodified lines726 None727 }728 729 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.737-#[cfg(feature = "std")]730+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]731 unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>732 where733 C: 'static,734 E: 'static,31 unmodified lines766 }767 }768 769 // Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.777-#[cfg(feature = "std")]770+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]771 unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)772 where773 C: 'static,774 E: 'static,123 unmodified lines898 // original type E.899 unsafe { (vtable(this.ptr).object_ref)(this).deref() }900 }901 909- #[cfg(feature = "std")]902+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]903 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {904 // Use vtable to attach E's native StdError vtable for the right905 // original type E.906 94 unmodified lines1001 Box::<dyn StdError + Send + Sync>::from(error)1002 }1003 }1004 1012-#[cfg(feature = "std")]1005+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]1006 impl AsRef<dyn StdError + Send + Sync> for Error {1007 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {1008 &**self1009 }1010 }1011 1019-#[cfg(feature = "std")]1012+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]1013 impl AsRef<dyn StdError> for Error {1014 fn as_ref(&self) -> &(dyn StdError + 'static) {1015 &**self1016 }1 unmodified linemodified:src/kind.rs+6 added-6 removed
45 unmodified lines46 47 use crate::Error;48 use core::fmt::{Debug, Display};49 50-#[cfg(feature = "std")]50+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]51 use crate::StdError;52-#[cfg(feature = "std")]52+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]53 use alloc::boxed::Box;54 55 pub struct Adhoc;56 38 unmodified lines95 error.into()96 }97 }98 99-#[cfg(feature = "std")]99+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]100 pub struct Boxed;101 102-#[cfg(feature = "std")]102+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]103 #[doc(hidden)]104 pub trait BoxedKind: Sized {105 #[inline]106 fn anyhow_kind(&self) -> Boxed {107 Boxed108 }109 }110 111-#[cfg(feature = "std")]111+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]112 impl BoxedKind for Box<dyn StdError + Send + Sync> {}113 114-#[cfg(feature = "std")]114+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]115 impl Boxed {116 #[cold]117 pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error {118 let backtrace = backtrace_if_absent!(&*error);3 unmodified linesmodified:src/lib.rs+7 added-5 removed
264 unmodified lines265 use crate::error::ErrorImpl;266 use crate::ptr::Own;267 use core::fmt::Display;268 269-#[cfg(not(feature = "std"))]269+#[cfg(all(not(feature = "std"), anyhow_no_core_error))]270 use core::fmt::Debug;271 272 #[cfg(feature = "std")]273 use std::error::Error as StdError;274 275-#[cfg(not(feature = "std"))]275+#[cfg(not(any(feature = "std", anyhow_no_core_error)))]276+use core::error::Error as StdError;277+278+#[cfg(all(not(feature = "std"), anyhow_no_core_error))]279 trait StdError: Debug + Display {280 fn source(&self) -> Option<&(dyn StdError + 'static)> {281 None282 }126 unmodified lines409 /// }410 /// None411 /// }412 /// ```410-#[cfg(feature = "std")]411-#[cfg_attr(docsrs, doc(cfg(feature = "std")))]413+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]414 #[derive(Clone)]415 pub struct Chain<'a> {416 state: crate::chain::ChainState<'a>,417 }253 unmodified lines671 pub mod kind {672 #[doc(hidden)]673 pub use crate::kind::{AdhocKind, TraitKind};674 673- #[cfg(feature = "std")]675+ #[cfg(any(feature = "std", not(anyhow_no_core_error)))]676 #[doc(hidden)]677 pub use crate::kind::BoxedKind;678 }679 53 unmodified linesmodified:src/wrapper.rs+5 added-5 removed
1 use crate::StdError;2 use core::fmt::{self, Debug, Display};3 4-#[cfg(feature = "std")]4+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]5 use alloc::boxed::Box;6 7 #[cfg(error_generic_member_access)]8 use core::error::Request;43 unmodified lines52 }53 54 impl<M> StdError for DisplayError<M> where M: Display + 'static {}55 56-#[cfg(feature = "std")]56+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]57 #[repr(transparent)]58 pub struct BoxedError(pub Box<dyn StdError + Send + Sync>);59 60-#[cfg(feature = "std")]60+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]61 impl Debug for BoxedError {62 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {63 Debug::fmt(&self.0, f)64 }65 }66 67-#[cfg(feature = "std")]67+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]68 impl Display for BoxedError {69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {70 Display::fmt(&self.0, f)71 }72 }73 74-#[cfg(feature = "std")]74+#[cfg(any(feature = "std", not(anyhow_no_core_error)))]75 impl StdError for BoxedError {76 fn source(&self) -> Option<&(dyn StdError + 'static)> {77 self.0.source()78 }6 unmodified linesmodified:build.rs+7 added
65 unmodified lines66 };67 68 if rustc >= 80 {69 println!("cargo:rustc-check-cfg=cfg(anyhow_nightly_testing)");70+ println!("cargo:rustc-check-cfg=cfg(anyhow_no_core_error)");71 println!("cargo:rustc-check-cfg=cfg(anyhow_no_fmt_arguments_as_str)");72 println!("cargo:rustc-check-cfg=cfg(anyhow_no_ptr_addr_of)");73 println!("cargo:rustc-check-cfg=cfg(anyhow_no_unsafe_op_in_unsafe_fn_lint)");74 println!("cargo:rustc-check-cfg=cfg(error_generic_member_access)");20 unmodified lines95 // std::backtrace::Backtrace96 // https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#stabilized-apis97 println!("cargo:rustc-cfg=std_backtrace");98 }99+100+ if rustc < 81 {101+ // core::error::Error102+ // https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#coreerrorerror103+ println!("cargo:rustc-cfg=anyhow_no_core_error");104+ }105 }106 107 fn compile_probe(rustc_bootstrap: bool) -> bool {108 if env::var_os("RUSTC_STAGE").is_some() {77 unmodified lines