A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix error chaining and casting with `anyhow` errors in the chain (#12705)
Nick Fitzgerald5afb60308a85
diff --git a/crates/core/src/error/context.rs b/crates/core/src/error/context.rsindex e8b3590b03..16b54e91a8 100644--- a/crates/core/src/error/context.rs+++ b/crates/core/src/error/context.rs@@ -1,8 +1,4 @@-use crate::error::{- Error, ErrorExt, OutOfMemory, Result,- boxed::try_new_uninit_box,- error::{OomOrDynError, OomOrDynErrorMut, OomOrDynErrorRef},-};+use crate::error::{Error, ErrorExt, OutOfMemory, Result, boxed::try_new_uninit_box};use core::any::TypeId;use core::fmt;use core::ptr::NonNull;@@ -245,7 +241,7 @@ where{fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {let source = self.ext_source()?;- Some(source.as_dyn_core_error())+ Some(source.inner.as_dyn_core_error())}}@@ -264,19 +260,12 @@ whereOk(Box::write(boxed, self) as _)}- fn ext_source(&self) -> Option<OomOrDynErrorRef<'_>> {- let error = self.error.as_ref()?;- Some(error.inner.unpack())+ fn ext_source(&self) -> Option<&Error> {+ self.error.as_ref()}- fn ext_source_mut(&mut self) -> Option<OomOrDynErrorMut<'_>> {- let error = self.error.as_mut()?;- Some(error.inner.unpack_mut())- }-- fn ext_take_source(&mut self) -> Option<OomOrDynError> {- let error = self.error.take()?;- Some(error.inner)+ fn ext_source_mut(&mut self) -> Option<&mut Error> {+ self.error.as_mut()}fn ext_is(&self, type_id: TypeId) -> bool {@@ -294,14 +283,7 @@ where#[cfg(feature = "backtrace")]fn take_backtrace(&mut self) -> Option<std::backtrace::Backtrace> {- let error = self.error.as_mut()?;- match error.inner.unpack_mut() {- OomOrDynErrorMut::Oom(_) => None,- OomOrDynErrorMut::DynError(mut e) => {- let r = unsafe { e.as_mut() };- r.backtrace.take()- }- }+ self.error.as_mut()?.take_backtrace()}#[cfg(feature = "anyhow")]diff --git a/crates/core/src/error/error.rs b/crates/core/src/error/error.rsindex a597577459..bf46b05b90 100644--- a/crates/core/src/error/error.rs+++ b/crates/core/src/error/error.rs@@ -33,13 +33,10 @@ pub(crate) unsafe trait ErrorExt: Send + Sync + 'static {) -> Result<Box<dyn core::error::Error + Send + Sync + 'static>, OutOfMemory>;/// Get a shared borrow of the next error in the chain.- fn ext_source(&self) -> Option<OomOrDynErrorRef<'_>>;+ fn ext_source(&self) -> Option<&Error>;/// Get an exclusive borrow of the next error in the chain.- fn ext_source_mut(&mut self) -> Option<OomOrDynErrorMut<'_>>;-- /// Take ownership of the next error in the chain.- fn ext_take_source(&mut self) -> Option<OomOrDynError>;+ fn ext_source_mut(&mut self) -> Option<&mut Error>;/// Is this error an instance of `T`, where `type_id == TypeId::of::<T>()`/// or a newtype wrapper around that type?@@ -439,19 +436,16 @@ const _RESULT_OF_UNIT_IS_ONE_WORD_LARGE: () =impl fmt::Debug for Error {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {if f.alternate() {- return f- .debug_struct("Error")- .field("inner", &self.inner.unpack())- .finish();+ return f.debug_struct("Error").field("inner", &self.inner).finish();}- let inner = self.inner.unpack();+ let inner = &self.inner;inner.display(f)?;if let Some(source) = inner.source() {f.write_str("\n\nCaused by:\n")?;let multiple_causes = source.source().is_some();- for (i, e) in Chain::new(source).enumerate() {+ for (i, e) in source.chain().enumerate() {if multiple_causes {write!(f, "{i: >5}: ")?;} else {@@ -476,12 +470,12 @@ impl fmt::Debug for Error {impl fmt::Display for Error {fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- let inner = self.inner.unpack();+ let inner = &self.inner;inner.display(f)?;if f.alternate() {if let Some(e) = inner.source() {- for e in Chain::new(e) {+ for e in e.chain() {write!(f, ": {e}")?;}}@@ -561,14 +555,14 @@ impl core::ops::Deref for Error {impl AsRef<dyn core::error::Error> for Error {#[inline]fn as_ref(&self) -> &(dyn core::error::Error + 'static) {- self.inner.unpack().as_dyn_core_error()+ self.inner.as_dyn_core_error()}}impl AsRef<dyn core::error::Error + Send + Sync> for Error {#[inline]fn as_ref(&self) -> &(dyn core::error::Error + Send + Sync + 'static) {- self.inner.unpack().as_dyn_core_error()+ self.inner.as_dyn_core_error()}}@@ -807,7 +801,7 @@ impl Error {#[inline]#[cfg(feature = "backtrace")]pub fn backtrace(&self) -> &Backtrace {- self.inner.unpack().backtrace()+ self.inner.backtrace()}/// Iterate over this error's context chain.@@ -835,7 +829,7 @@ impl Error {/// ```#[inline]pub fn chain(&self) -> Chain<'_> {- Chain::new(self.inner.unpack())+ Chain::new(self)}/// Get the last error in the context chain.@@ -860,6 +854,56 @@ impl Error {self.chain().last().expect("chain is always non-empty")}+ #[inline]+ fn error_ext_chain<'a>(&'a self) -> impl Iterator<Item = &'a Error> + 'a {+ let mut cur = Some(self);+ core::iter::from_fn(move || {+ let ret = cur.take()?;+ cur = ret.inner.source();+ Some(ret)+ })+ }++ #[inline]+ fn error_ext_chain_mut<T>(+ &mut self,+ mut f: impl FnMut(&mut Error) -> Option<&mut T>,+ ) -> Option<&mut T> {+ let mut cur = self;+ loop {+ {+ // Safety: We are running into "problem case #3" of non-lexical+ // lifetimes: we are returning a mutable reference, which must+ // be live for the whole function's scope, but accessing it+ // again below in `cur.inner.source_mut()`. This is safe because+ // control flow ensures that the borrow is not actually active+ // anymore in the case where we didn't return.+ //+ // https://github.com/rust-lang/rfcs/blob/master/text/2094-nll.md#problem-case-3-conditional-control-flow-across-functions+ let cur = unsafe { NonNull::from(&mut *cur).as_mut() };+ if let Some(x) = f(cur) {+ return Some(x);+ }+ }++ cur = cur.inner.source_mut()?;+ }+ }++ #[inline]+ fn error_ext_zip<T>(mut self, mut f: impl FnMut(Error) -> Result<T, Error>) -> Result<T, Self> {+ let mut maybe_link = Some(&mut self);+ while let Some(link) = maybe_link.take() {+ let e = mem::replace(link, OutOfMemory::new(1).into());+ *link = match f(e) {+ Ok(x) => return Ok(x),+ Err(e) => e,+ };+ maybe_link = link.inner.source_mut();+ }+ Err(self)+ }+/// Is this an `E` error?////// Returns true if any error in the context chain is an `E`.@@ -894,15 +938,17 @@ impl Error {whereE: fmt::Display + fmt::Debug + Send + Sync + 'static,{- let mut error = Some(self.inner.unpack());- while let Some(e) = error {- if e.is::<E>() {- return true;- } else {- error = e.source();- }- }- false+ self.error_ext_chain().any(|e| {+ let result = e.inner.is::<E>();++ #[cfg(feature = "anyhow")]+ let result = result+ || e.inner+ .downcast_ref::<anyhow::Error>()+ .is_some_and(|e| e.is::<E>());++ result+ })}/// Downcast this error into an `E`, taking ownership.@@ -947,22 +993,27 @@ impl Error {whereE: fmt::Display + fmt::Debug + Send + Sync + 'static,{- if !self.is::<E>() {- return Err(self);- }-- let mut value = mem::MaybeUninit::<E>::uninit();-- // Safety: this error is an `E` and the given pointer is valid to write- // an `E` to.- unsafe {- self.inner- .downcast(TypeId::of::<E>(), NonNull::from(&mut value).cast::<u8>());- }+ self.error_ext_zip(|e| {+ let result = e.inner.downcast::<E>().map_err(|inner| Self { inner });++ #[cfg(feature = "anyhow")]+ let result = result.or_else(|e| {+ if e.inner+ .downcast_ref::<anyhow::Error>()+ .is_some_and(|e| e.is::<E>())+ {+ let anyhow = match e.inner.downcast::<anyhow::Error>() {+ Ok(e) => e,+ Err(_) => unreachable!(),+ };+ Ok(anyhow.downcast::<E>().unwrap())+ } else {+ Err(e)+ }+ });- // Safety: `OomOrDynError::downcast` guarantees that the given pointer's- // data is initialized upon successful return.- Ok(unsafe { value.assume_init() })+ result+ })}/// Downcast this error into a shared `&E` borrow.@@ -1002,34 +1053,18 @@ impl Error {whereE: fmt::Display + fmt::Debug + Send + Sync + 'static,{- let mut error = Some(self.inner.unpack());- while let Some(e) = error {- if e.is::<E>() {- return Some(match e {- OomOrDynErrorRef::DynError(ptr) => {- let ptr = ptr.cast::<ConcreteError<E>>();- // Safety: we own the pointer, it is valid for reading,- // and we checked that it is an `E`.- let r = unsafe { ptr.as_ref() };- &r.error- }- OomOrDynErrorRef::Oom(oom) => {- // Note: Even though we know that `E == OutOfMemory`- // here, we still have to do this dance to satisfy the- // type system.- debug_assert_eq!(TypeId::of::<E>(), TypeId::of::<OutOfMemory>());- let ptr = NonNull::from(oom);- let ptr = ptr.cast::<E>();- // Safety: the pointer points to `oom`, which is valid- // for creating a shared reference to.- unsafe { ptr.as_ref() }- }- });- } else {- error = e.source();- }- }- None+ self.error_ext_chain().find_map(|e| {+ let result = e.inner.downcast_ref::<E>();++ #[cfg(feature = "anyhow")]+ let result = result.or_else(|| {+ e.inner+ .downcast_ref::<anyhow::Error>()+ .and_then(|anyhow| anyhow.downcast_ref::<E>())+ });++ result+ })}/// Downcast this error into an exclusive `&mut E` borrow.@@ -1071,34 +1106,35 @@ impl Error {whereE: fmt::Display + fmt::Debug + Send + Sync + 'static,{- let mut error = Some(self.inner.unpack_mut());- while let Some(mut e) = error.take() {- if e.as_ref().is::<E>() {- return Some(match e {- OomOrDynErrorMut::DynError(ptr) => {- let mut ptr = ptr.cast::<ConcreteError<E>>();- // Safety: we own the pointer, it is valid for reading- // and writing, and we checked that it is an `E`.- let r = unsafe { ptr.as_mut() };- &mut r.error- }- OomOrDynErrorMut::Oom(oom) => {- // Note: Even though we know that `E == OutOfMemory`- // here, we still have to do this dance to satisfy the- // type system.- debug_assert_eq!(TypeId::of::<E>(), TypeId::of::<OutOfMemory>());- let ptr = NonNull::from(oom);- let mut ptr = ptr.cast::<E>();- // Safety: the pointer points to `oom`, which is valid- // for creating an exclusive reference to.- unsafe { ptr.as_mut() }- }- });- } else {- error = e.source_mut();+ self.error_ext_chain_mut(|e| {+ {+ // Safety: We are running into "problem case #3" of non-lexical+ // lifetimes: we are returning a mutable reference, which must+ // be live for the whole function's scope, but are accessing it+ // again below in the `anyhow` downcast. This is safe because+ // control flow ensures that the original borrow is not actually+ // active anymore if we didn't return.+ //+ // https://github.com/rust-lang/rfcs/blob/master/text/2094-nll.md#problem-case-3-conditional-control-flow-across-functions+ let e = unsafe { NonNull::from(&mut *e).as_mut() };+ if let Some(r) = e.inner.downcast_mut::<E>() {+ return Some(r);+ }}- }- None++ #[cfg(feature = "anyhow")]+ {+ if let Some(result) = e+ .inner+ .downcast_mut::<anyhow::Error>()+ .and_then(|anyhow| anyhow.downcast_mut::<E>())+ {+ return Some(result);+ }+ }++ None+ })}/// Convert this error into a `Box<dyn core::error::Error>`.@@ -1188,6 +1224,17 @@ impl Error {}}}++ #[cfg(feature = "backtrace")]+ pub(crate) fn take_backtrace(&mut self) -> Option<Backtrace> {+ match self.inner.unpack_mut() {+ OomOrDynErrorMut::Oom(_) => None,+ OomOrDynErrorMut::DynError(mut e) => {+ let r = unsafe { e.as_mut() };+ r.backtrace.take()+ }+ }+ }}/// `ErrorExt` wrapper for foreign `core::error::Error` implementations.@@ -1216,15 +1263,11 @@ whereOk(Box::write(boxed, self.0) as _)}- fn ext_source(&self) -> Option<OomOrDynErrorRef<'_>> {- None- }-- fn ext_source_mut(&mut self) -> Option<OomOrDynErrorMut<'_>> {+ fn ext_source(&self) -> Option<&Error> {None}- fn ext_take_source(&mut self) -> Option<OomOrDynError> {+ fn ext_source_mut(&mut self) -> Option<&mut Error> {None}@@ -1299,15 +1342,11 @@ whereOk(Box::write(boxed, self) as _)}- fn ext_source(&self) -> Option<OomOrDynErrorRef<'_>> {- None- }-- fn ext_source_mut(&mut self) -> Option<OomOrDynErrorMut<'_>> {+ fn ext_source(&self) -> Option<&Error> {None}- fn ext_take_source(&mut self) -> Option<OomOrDynError> {+ fn ext_source_mut(&mut self) -> Option<&mut Error> {None}@@ -1358,15 +1397,11 @@ unsafe impl ErrorExt for BoxedError {Ok(self.0)}- fn ext_source(&self) -> Option<OomOrDynErrorRef<'_>> {+ fn ext_source(&self) -> Option<&Error> {None}- fn ext_source_mut(&mut self) -> Option<OomOrDynErrorMut<'_>> {- None- }-- fn ext_take_source(&mut self) -> Option<OomOrDynError> {+ fn ext_source_mut(&mut self) -> Option<&mut Error> {None}@@ -1420,15 +1455,11 @@ unsafe impl ErrorExt for AnyhowError {Ok(self.0.into_boxed_dyn_error())}- fn ext_source(&self) -> Option<OomOrDynErrorRef<'_>> {- None- }-- fn ext_source_mut(&mut self) -> Option<OomOrDynErrorMut<'_>> {+ fn ext_source(&self) -> Option<&Error> {None}- fn ext_take_source(&mut self) -> Option<OomOrDynError> {+ fn ext_source_mut(&mut self) -> Option<&mut Error> {None}@@ -1456,6 +1487,7 @@ unsafe impl ErrorExt for AnyhowError {}}+#[derive(Clone, Copy)]pub(crate) enum OomOrDynErrorRef<'a> {// Safety: this must always be a valid pointer to read a `DynError` from for// the `'a` lifetime.@@ -1464,113 +1496,6 @@ pub(crate) enum OomOrDynErrorRef<'a> {Oom(&'a OutOfMemory),}-impl<'a> Debug for OomOrDynErrorRef<'a> {- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- self.debug(f)- }-}--impl<'a> OomOrDynErrorRef<'a> {- fn display(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- match self {- OomOrDynErrorRef::DynError(e) => {- // Safety: invariant of this type.- let vtable = unsafe { e.as_ref().vtable };- // Safety: using the vtable associated with this pointer's- // concrete type and the pointer is valid.- unsafe { (vtable.display)(*e, f) }- }- OomOrDynErrorRef::Oom(oom) => fmt::Display::fmt(oom, f),- }- }-- fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- match *self {- OomOrDynErrorRef::Oom(oom) => f.debug_tuple("Oom").field(oom).finish(),- OomOrDynErrorRef::DynError(error) => {- struct DebugError<'a>(SharedPtr<'a, DynError>);- impl fmt::Debug for DebugError<'_> {- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {- // Safety: invariant of `OomOrDynError` that the pointer- // is valid.- let vtable = unsafe { self.0.as_ref().vtable };- // Safety: the pointer is valid and the vtable is- // associated with the pointer's concrete error type.- unsafe { (vtable.debug)(self.0, f) }- }- }-- let mut f = f.debug_struct("DynError");- f.field("error", &DebugError(error));- if let Some(source) = self.source() {- f.field("source", &source);- }- f.finish()- }- }- }-- fn source(&self) -> Option<OomOrDynErrorRef<'a>> {- match self {- OomOrDynErrorRef::DynError(e) => {- // Safety: invariant of this type.- let vtable = unsafe { e.as_ref().vtable };- // Safety: using the vtable associated with this pointer's- // concrete type and the pointer is valid.- unsafe { (vtable.source)(*e) }- }- OomOrDynErrorRef::Oom(_) => None,- }- }-- fn is<E>(&self) -> bool- where- E: fmt::Display + fmt::Debug + Send + Sync + 'static,- {- match self {- OomOrDynErrorRef::DynError(e) => {- // Safety: invariant of this type.- let vtable = unsafe { e.as_ref().vtable };- // Safety: using the vtable associated with this pointer's- // concrete type and the pointer is valid.- unsafe { (vtable.is)(*e, TypeId::of::<E>()) }- }- OomOrDynErrorRef::Oom(_) => TypeId::of::<E>() == TypeId::of::<OutOfMemory>(),- }- }-- pub(crate) fn as_dyn_core_error(&self) -> &'a (dyn core::error::Error + Send + Sync + 'static) {- match *self {- OomOrDynErrorRef::DynError(e) => {- // Safety: invariant of this type.- let vtable = unsafe { e.as_ref().vtable };- // Safety: using the vtable associated with this pointer's- // concrete type and the pointer is valid.- unsafe { (vtable.as_dyn_core_error)(e) }- }- OomOrDynErrorRef::Oom(oom) => oom as _,- }- }-- #[cfg(feature = "backtrace")]- fn backtrace(&self) -> &'a Backtrace {- match self {- OomOrDynErrorRef::DynError(e) => {- // Safety: invariant of this type.- let r = unsafe { e.as_ref() };- r.backtrace- .as_ref()- .expect("the first error in the chain always has the backtrace")- }-- OomOrDynErrorRef::Oom(_) => {- static DISABLED: Backtrace = Backtrace::disabled();- &DISABLED- }- }- }-}-pub(crate) enum OomOrDynErrorMut<'a> {// Safety: this must always be a valid pointer to read and write a// `DynError` from for the `'a` lifetime.@@ -1579,28 +1504,6 @@ pub(crate) enum OomOrDynErrorMut<'a> {Oom(&'a mut OutOfMemory),}-impl<'a> OomOrDynErrorMut<'a> {- fn as_ref(&self) -> OomOrDynErrorRef<'_> {- match self {- OomOrDynErrorMut::DynError(e) => OomOrDynErrorRef::DynError(e.as_shared_ptr()),- OomOrDynErrorMut::Oom(oom) => OomOrDynErrorRef::Oom(oom),- }- }-- fn source_mut(&mut self) -> Option<OomOrDynErrorMut<'a>> {- match self {- OomOrDynErrorMut::DynError(e) => {- // Safety: invariant of this type.- let vtable = unsafe { e.as_ref().vtable };- // Safety: using the vtable associated with this pointer's- // concrete type and the pointer is valid.- unsafe { (vtable.source_mut)(e.raw_copy()) }- }- OomOrDynErrorMut::Oom(_) => None,- }- }-}-/// Bit packed version of `enum { BoxedDynError, OutOfMemory }` that relies on/// implicit pointer tagging and `OutOfMemory` being zero-sized.#[repr(transparent)]@@ -1646,6 +1549,12 @@ impl From<BoxedDynError> for OomOrDynError {}}+impl fmt::Debug for OomOrDynError {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ self.debug(f)+ }+}+impl OomOrDynError {const _SIZE: () = assert!(mem::size_of::<OomOrDynError>() == mem::size_of::<usize>());@@ -1795,29 +1704,66 @@ impl OomOrDynError {}}- /// Given that this is known to be an instance of the type associated with- /// the given `TypeId`, do an owning-downcast to that type, writing the- /// result through the given `ret_ptr`, and deallocating `self` along the- /// way.- ///- /// The `ret_ptr`'s storage will contain an initialized instance of the- /// associated type upon this method's successful return.- ///- /// # Safety- ///- /// This error (or another in its chain) must be of the type associated with- /// `TypeId`.- ///- /// The given `ret_ptr` must point to a valid-but-uninitialized storage- /// location for an instance of the type associated with the given `TypeId`.- pub(crate) unsafe fn downcast(self, type_id: TypeId, ret_ptr: NonNull<u8>) {+ fn source(&self) -> Option<&Error> {+ match self.unpack() {+ OomOrDynErrorRef::DynError(e) => {+ // Safety: invariant of this type.+ let vtable = unsafe { e.as_ref().vtable };+ // Safety: using the vtable associated with this pointer's+ // concrete type and the pointer is valid.+ unsafe { (vtable.source)(e) }+ }+ OomOrDynErrorRef::Oom(_) => None,+ }+ }++ fn source_mut(&mut self) -> Option<&mut Error> {+ match self.unpack_mut() {+ OomOrDynErrorMut::DynError(e) => {+ // Safety: invariant of this type.+ let vtable = unsafe { e.as_ref().vtable };+ // Safety: using the vtable associated with this pointer's+ // concrete type and the pointer is valid.+ unsafe { (vtable.source_mut)(e.raw_copy()) }+ }+ OomOrDynErrorMut::Oom(_) => None,+ }+ }++ fn is<E>(&self) -> bool+ where+ E: fmt::Display + fmt::Debug + Send + Sync + 'static,+ {+ match self.unpack() {+ OomOrDynErrorRef::DynError(e) => {+ // Safety: invariant of this type.+ let vtable = unsafe { e.as_ref().vtable };+ // Safety: using the vtable associated with this pointer's+ // concrete type and the pointer is valid.+ unsafe { (vtable.is)(e, TypeId::of::<E>()) }+ }+ OomOrDynErrorRef::Oom(_) => TypeId::of::<E>() == TypeId::of::<OutOfMemory>(),+ }+ }++ pub(crate) fn downcast<E>(self) -> Result<E, Self>+ where+ E: fmt::Display + fmt::Debug + Send + Sync + 'static,+ {+ if !self.is::<E>() {+ return Err(self);+ }++ let mut ret = mem::MaybeUninit::<E>::uninit();if self.is_oom() {- debug_assert_eq!(type_id, TypeId::of::<OutOfMemory>());+ debug_assert_eq!(TypeId::of::<E>(), TypeId::of::<OutOfMemory>());// Safety: this is an OOM error.- let oom = unsafe { self.unchecked_oom() };- // Safety: implied by this method's safety contract.+ let oom = unsafe { *self.unchecked_oom() };+ let ret_ptr = NonNull::from(&mut ret).cast::<OutOfMemory>();+ // Safety: the pointer is valid for writing our OOM into because it+ // is a valid pointer to space for an `E` and `E == OutOfMemory`.unsafe {- ret_ptr.cast::<OutOfMemory>().write(*oom);+ ret_ptr.write(oom);}} else {debug_assert!(self.is_boxed_dyn_error());@@ -1825,9 +1771,144 @@ impl OomOrDynError {let ptr = unsafe { self.unchecked_into_dyn_error() };// Safety: invariant of this type that the pointer is valid.let vtable = unsafe { ptr.as_ref().vtable };+ let ret_ptr = NonNull::from(&mut ret).cast::<u8>();// Safety: the pointer is valid and the vtable is associated with// this pointer's concrete type.- unsafe { (vtable.downcast)(ptr, type_id, ret_ptr) }+ unsafe { (vtable.move_into)(ptr, ret_ptr) }+ }++ // Safety: `ret` was fully initialized in all control-flow paths leading+ // here.+ Ok(unsafe { ret.assume_init() })+ }++ fn downcast_mut<E>(&mut self) -> Option<&mut E>+ where+ E: fmt::Display + fmt::Debug + Send + Sync + 'static,+ {+ if !self.is::<E>() {+ return None;+ }++ match self.unpack_mut() {+ OomOrDynErrorMut::DynError(ptr) => {+ let mut ptr = ptr.cast::<ConcreteError<E>>();+ // Safety: we own the pointer, it is valid for reading+ // and writing, and we checked that it is an `E`.+ let r = unsafe { ptr.as_mut() };+ Some(&mut r.error)+ }+ OomOrDynErrorMut::Oom(oom) => {+ // Note: Even though we know that `E == OutOfMemory`+ // here, we still have to do this dance to satisfy the+ // type system.+ debug_assert_eq!(TypeId::of::<E>(), TypeId::of::<OutOfMemory>());+ let ptr = NonNull::from(oom);+ let mut ptr = ptr.cast::<E>();+ // Safety: the pointer points to `oom`, which is valid+ // for creating an exclusive reference to.+ Some(unsafe { ptr.as_mut() })+ }+ }+ }++ fn display(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ match self.unpack() {+ OomOrDynErrorRef::DynError(e) => {+ // Safety: invariant of this type.+ let vtable = unsafe { e.as_ref().vtable };+ // Safety: using the vtable associated with this pointer's+ // concrete type and the pointer is valid.+ unsafe { (vtable.display)(e, f) }+ }+ OomOrDynErrorRef::Oom(oom) => fmt::Display::fmt(oom, f),+ }+ }++ fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ match self.unpack() {+ OomOrDynErrorRef::Oom(oom) => f.debug_tuple("Oom").field(oom).finish(),+ OomOrDynErrorRef::DynError(error) => {+ struct DebugError<'a>(SharedPtr<'a, DynError>);+ impl fmt::Debug for DebugError<'_> {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ // Safety: invariant of `OomOrDynError` that the pointer+ // is valid.+ let vtable = unsafe { self.0.as_ref().vtable };+ // Safety: the pointer is valid and the vtable is+ // associated with the pointer's concrete error type.+ unsafe { (vtable.debug)(self.0, f) }+ }+ }++ let mut f = f.debug_struct("DynError");+ f.field("error", &DebugError(error));+ if let Some(source) = self.source() {+ f.field("source", &source);+ }+ f.finish()+ }+ }+ }++ fn downcast_ref<E>(&self) -> Option<&E>+ where+ E: fmt::Display + fmt::Debug + Send + Sync + 'static,+ {+ if !self.is::<E>() {+ return None;+ }++ match self.unpack() {+ OomOrDynErrorRef::DynError(ptr) => {+ let ptr = ptr.cast::<ConcreteError<E>>();+ // Safety: we own the pointer, it is valid for reading,+ // and we checked that it is an `E`.+ let r = unsafe { ptr.as_ref() };+ Some(&r.error)+ }+ OomOrDynErrorRef::Oom(oom) => {+ // Note: Even though we know that `E == OutOfMemory`+ // here, we still have to do this dance to satisfy the+ // type system.+ debug_assert_eq!(TypeId::of::<E>(), TypeId::of::<OutOfMemory>());+ let ptr = NonNull::from(oom);+ let ptr = ptr.cast::<E>();+ // Safety: the pointer points to `oom`, which is valid+ // for creating a shared reference to.+ Some(unsafe { ptr.as_ref() })+ }+ }+ }++ pub(crate) fn as_dyn_core_error(&self) -> &(dyn core::error::Error + Send + Sync + 'static) {+ match self.unpack() {+ OomOrDynErrorRef::DynError(e) => {+ // Safety: invariant of this type.+ let vtable = unsafe { e.as_ref().vtable };+ // Safety: using the vtable associated with this pointer's+ // concrete type and the pointer is valid.+ unsafe { (vtable.as_dyn_core_error)(e) }+ }+ OomOrDynErrorRef::Oom(oom) => oom as _,+ }+ }++ #[cfg(feature = "backtrace")]+ fn backtrace(&self) -> &Backtrace {+ match self.unpack() {+ OomOrDynErrorRef::DynError(e) => {+ // Safety: invariant of this type.+ let r = unsafe { e.as_ref() };+ r.backtrace+ .as_ref()+ .expect("the first error in the chain always has the backtrace")+ }++ OomOrDynErrorRef::Oom(_) => {+ static DISABLED: Backtrace = Backtrace::disabled();+ &DISABLED+ }}}}@@ -1845,12 +1926,12 @@ pub struct Chain<'a> {}enum ChainState<'a> {- Ours(OomOrDynErrorRef<'a>),+ Ours(&'a Error),Core(Option<&'a (dyn core::error::Error + 'static)>),}impl<'a> Chain<'a> {- fn new(error: OomOrDynErrorRef<'a>) -> Self {+ fn new(error: &'a Error) -> Self {Self {state: ChainState::Ours(error),}@@ -1864,8 +1945,8 @@ impl<'a> Iterator for Chain<'a> {fn next(&mut self) -> Option<Self::Item> {match &mut self.state {ChainState::Ours(e) => {- let core = e.as_dyn_core_error();- self.state = if let Some(e) = e.source() {+ let core = e.inner.as_dyn_core_error();+ self.state = if let Some(e) = e.inner.source() {ChainState::Ours(e)} else {ChainState::Core(core.source())diff --git a/crates/core/src/error/ptr.rs b/crates/core/src/error/ptr.rsindex 2854395005..9de272f1f3 100644--- a/crates/core/src/error/ptr.rs+++ b/crates/core/src/error/ptr.rs@@ -135,10 +135,6 @@ whereMutPtr::new(self.ptr.cast())}- pub(crate) fn as_shared_ptr(&self) -> SharedPtr<'_, T> {- SharedPtr::new(self.ptr)- }-/// # Safety////// It must be valid to call `NonNull::<T>::as_ref` on the underlying pointer.diff --git a/crates/core/src/error/vtable.rs b/crates/core/src/error/vtable.rsindex 27dfa76914..3d447f23ec 100644--- a/crates/core/src/error/vtable.rs+++ b/crates/core/src/error/vtable.rs@@ -1,7 +1,5 @@use crate::error::ptr::{MutPtr, OwnedPtr, SharedPtr};-use crate::error::{- ConcreteError, DynError, ErrorExt, OomOrDynErrorMut, OomOrDynErrorRef, OutOfMemory,-};+use crate::error::{ConcreteError, DynError, Error, ErrorExt, OutOfMemory};use core::{any::TypeId, fmt, ptr::NonNull};use std_alloc::boxed::Box;@@ -27,8 +25,8 @@ use std_alloc::boxed::Box;pub(crate) struct Vtable {pub(crate) display: unsafe fn(SharedPtr<'_, DynError>, &mut fmt::Formatter<'_>) -> fmt::Result,pub(crate) debug: unsafe fn(SharedPtr<'_, DynError>, &mut fmt::Formatter<'_>) -> fmt::Result,- pub(crate) source: unsafe fn(SharedPtr<'_, DynError>) -> Option<OomOrDynErrorRef<'_>>,- pub(crate) source_mut: unsafe fn(MutPtr<'_, DynError>) -> Option<OomOrDynErrorMut<'_>>,+ pub(crate) source: unsafe fn(SharedPtr<'_, DynError>) -> Option<&Error>,+ pub(crate) source_mut: unsafe fn(MutPtr<'_, DynError>) -> Option<&mut Error>,pub(crate) is: unsafe fn(SharedPtr<'_, DynError>, TypeId) -> bool,pub(crate) as_dyn_core_error:unsafe fn(SharedPtr<'_, DynError>) -> &(dyn core::error::Error + Send + Sync + 'static),@@ -44,7 +42,7 @@ pub(crate) struct Vtable {////// Upon successful return, a `T` will have been written to that memory/// block.- pub(crate) downcast: unsafe fn(OwnedPtr<DynError>, TypeId, NonNull<u8>),+ pub(crate) move_into: unsafe fn(OwnedPtr<DynError>, NonNull<u8>),/// Conversion into `anyhow::Error` from `Box<Self>`.#[cfg(feature = "anyhow")]@@ -66,7 +64,7 @@ impl Vtable {as_dyn_core_error: as_dyn_core_error::<E>,into_boxed_dyn_core_error: into_boxed_dyn_core_error::<E>,drop_and_deallocate: drop_and_deallocate::<E>,- downcast: downcast::<E>,+ move_into: move_into::<E>,#[cfg(feature = "anyhow")]into_anyhow: into_anyhow::<E>,}@@ -93,7 +91,7 @@ wherefmt::Debug::fmt(error.error.ext_as_dyn_core_error(), f)}-unsafe fn source<E>(error: SharedPtr<'_, DynError>) -> Option<OomOrDynErrorRef<'_>>+unsafe fn source<E>(error: SharedPtr<'_, DynError>) -> Option<&Error>whereE: ErrorExt,{@@ -103,7 +101,7 @@ whereerror.error.ext_source()}-unsafe fn source_mut<E>(error: MutPtr<'_, DynError>) -> Option<OomOrDynErrorMut<'_>>+unsafe fn source_mut<E>(error: MutPtr<'_, DynError>) -> Option<&mut Error>whereE: ErrorExt,{@@ -156,28 +154,16 @@ wherelet _ = unsafe { error.into_box() };}-unsafe fn downcast<E>(error: OwnedPtr<DynError>, type_id: TypeId, ret_ptr: NonNull<u8>)+unsafe fn move_into<E>(error: OwnedPtr<DynError>, ret_ptr: NonNull<u8>)whereE: ErrorExt,{let error = error.cast::<ConcreteError<E>>();// Safety: implied by all vtable functions' safety contract.- let mut error = unsafe { error.into_box() };-- if error.error.ext_is(type_id) {- // Safety: Implied by `downcast`'s additional safety safety requirement.- unsafe {- error.error.ext_move(ret_ptr);- }- } else {- let source = error- .error- .ext_take_source()- .expect("must have a source up the chain if `E` is not our target type");- // Safety: implied by downcast's additional safety requirement.- unsafe {- source.downcast(type_id, ret_ptr);- }+ let error = unsafe { error.into_box() };+ // Safety: Implied by `move`'s additional safety safety requirement.+ unsafe {+ error.error.ext_move(ret_ptr);}}diff --git a/crates/core/tests/tests.rs b/crates/core/tests/tests.rsindex d5e7473e25..79fd575ece 100644--- a/crates/core/tests/tests.rs+++ b/crates/core/tests/tests.rs@@ -717,21 +717,27 @@ fn fmt_debug_alternate() {Error {inner: DynError {error: ouch,- source: DynError {- error: yikes,- source: DynError {- error: TestError(- 42,- ),- source: DynError {- error: ChainError {- message: "whoops",- source: Some(- ChainError {- message: "root cause",- source: None,- },+ source: Error {+ inner: DynError {+ error: yikes,+ source: Error {+ inner: DynError {+ error: TestError(+ 42,),+ source: Error {+ inner: DynError {+ error: ChainError {+ message: "whoops",+ source: Some(+ ChainError {+ message: "root cause",+ source: None,+ },+ ),+ },+ },+ },},},},@@ -928,17 +934,105 @@ fn anyhow_preserves_downcast() -> Result<()> {#[test]#[cfg(feature = "anyhow")]-fn anyhow_source_loses_downcast() -> Result<()> {+fn anyhow_source_downcast() -> Result<()> {let e: anyhow::Error = TestError(1).into();assert!(e.downcast_ref::<TestError>().is_some());let e: Error = Error::from_anyhow(e);- // FIXME: this should actually test for `is_some`- assert!(e.downcast_ref::<TestError>().is_none());+ assert!(e.downcast_ref::<TestError>().is_some());- // Even while the above is broken, when going back to `anyhow` we should- // preserve the original error.let e: anyhow::Error = e.into();assert!(e.downcast_ref::<TestError>().is_some());Ok(())}++#[cfg(feature = "anyhow")]+fn assert_chain<T, U>(actual: impl IntoIterator<Item = T>, expected: impl IntoIterator<Item = U>)+where+ T: PartialEq<U>,+ T: fmt::Debug,+ U: fmt::Debug,+{+ let mut actual = actual.into_iter();+ let mut expected = expected.into_iter();+ loop {+ match (actual.next(), expected.next()) {+ (None, None) => return,+ (None, Some(x)) => {+ panic!("expected {x:?} next in chain, but actual chain is exhausted")+ }+ (Some(x), None) => panic!("expected chain to be exhausted, but found {x:?}"),+ (Some(x), Some(y)) => assert_eq!(x, y),+ }+ }+}++#[test]+#[cfg(feature = "anyhow")]+fn anyhow_wasmtime_anyhow_sandwich_chain() -> Result<()> {+ let e: anyhow::Error = TestError(1).into();+ let e = e.context(TestError(2));++ assert_chain(+ e.chain().map(|e| e.to_string()),+ ["TestError(2)", "TestError(1)"],+ );++ let e: Error = Error::from_anyhow(e);+ let e = e.context(TestError(3));++ assert_chain(+ e.chain().map(|e| e.to_string()),+ ["TestError(3)", "TestError(2)", "TestError(1)"],+ );++ let e: anyhow::Error = e.into();+ let e = e.context(TestError(4));++ assert_chain(+ e.chain().map(|e| e.to_string()),+ [+ "TestError(4)",+ "TestError(3)",+ "TestError(2)",+ "TestError(1)",+ ],+ );++ Ok(())+}++#[test]+#[cfg(feature = "anyhow")]+fn wasmtime_anyhow_wasmtime_sandwich_chain() -> Result<()> {+ let e: Error = TestError(1).into();+ let e = e.context(TestError(2));++ assert_chain(+ e.chain().map(|e| e.to_string()),+ ["TestError(2)", "TestError(1)"],+ );++ let e: anyhow::Error = e.into();+ let e = e.context(TestError(3));++ assert_chain(+ e.chain().map(|e| e.to_string()),+ ["TestError(3)", "TestError(2)", "TestError(1)"],+ );++ let e: Error = Error::from_anyhow(e);+ let e = e.context(TestError(4));++ assert_chain(+ e.chain().map(|e| e.to_string()),+ [+ "TestError(4)",+ "TestError(3)",+ "TestError(2)",+ "TestError(1)",+ ],+ );++ Ok(())+}