A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix using `downcast` with `anyhow::Error` (#12689)
Alex Crichton4fd25c073d4c
diff --git a/crates/core/src/error/context.rs b/crates/core/src/error/context.rsindex f20b997eba..e8b3590b03 100644--- a/crates/core/src/error/context.rs+++ b/crates/core/src/error/context.rs@@ -303,4 +303,12 @@ where}}}++ #[cfg(feature = "anyhow")]+ fn ext_into_anyhow(mut self) -> anyhow::Error {+ match self.error.take() {+ Some(error) => anyhow::Error::from(error).context(self.context),+ None => anyhow::Error::msg(self),+ }+ }}diff --git a/crates/core/src/error/error.rs b/crates/core/src/error/error.rsindex 8bede3f8f9..a597577459 100644--- a/crates/core/src/error/error.rs+++ b/crates/core/src/error/error.rs@@ -64,6 +64,10 @@ pub(crate) unsafe trait ErrorExt: Send + Sync + 'static {/// Take the backtrace from this error, if any.#[cfg(feature = "backtrace")]fn take_backtrace(&mut self) -> Option<Backtrace>;++ /// Conversion into an `anyhow::Error`.+ #[cfg(feature = "anyhow")]+ fn ext_into_anyhow(self) -> anyhow::Error;}/// Morally a `dyn ErrorExt` trait object that holds its own vtable.@@ -542,7 +546,7 @@ impl From<Error> for Box<dyn core::error::Error + 'static> {impl From<Error> for anyhow::Error {#[inline]fn from(e: Error) -> Self {- anyhow::Error::from_boxed(e.into_boxed_dyn_error())+ e.inner.into_anyhow()}}@@ -1241,6 +1245,11 @@ wherefn take_backtrace(&mut self) -> Option<Backtrace> {None}++ #[cfg(feature = "anyhow")]+ fn ext_into_anyhow(self) -> anyhow::Error {+ anyhow::Error::new(self.0)+ }}/// `ErrorExt` wrapper for types given to `Error::msg`.@@ -1319,6 +1328,11 @@ wherefn take_backtrace(&mut self) -> Option<Backtrace> {None}++ #[cfg(feature = "anyhow")]+ fn ext_into_anyhow(self) -> anyhow::Error {+ anyhow::Error::msg(self.0)+ }}/// `ErrorExt` wrapper for `Box<dyn core::error::Error>`.@@ -1374,6 +1388,11 @@ unsafe impl ErrorExt for BoxedError {fn take_backtrace(&mut self) -> Option<Backtrace> {None}++ #[cfg(feature = "anyhow")]+ fn ext_into_anyhow(self) -> anyhow::Error {+ anyhow::Error::from_boxed(self.0)+ }}/// `ErrorExt` wrapper for `anyhow::Error`.@@ -1430,6 +1449,11 @@ unsafe impl ErrorExt for AnyhowError {fn take_backtrace(&mut self) -> Option<Backtrace> {None}++ #[cfg(feature = "anyhow")]+ fn ext_into_anyhow(self) -> anyhow::Error {+ self.0+ }}pub(crate) enum OomOrDynErrorRef<'a> {@@ -1754,6 +1778,23 @@ impl OomOrDynError {}}+ #[cfg(feature = "anyhow")]+ pub(crate) fn into_anyhow(self) -> anyhow::Error {+ if self.is_oom() {+ // Safety: `self.is_oom()` is true.+ anyhow::Error::from(unsafe { *self.unchecked_oom() })+ } else {+ debug_assert!(self.is_boxed_dyn_error());+ // Safety: this is a boxed dyn error.+ let ptr = unsafe { self.unchecked_into_dyn_error() };+ // Safety: invariant of the type that the pointer is valid.+ let vtable = unsafe { ptr.as_ref().vtable };+ // Safety: the pointer is valid and the vtable is associated with+ // this pointer's concrete error type.+ unsafe { (vtable.into_anyhow)(ptr) }+ }+ }+/// 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 thediff --git a/crates/core/src/error/vtable.rs b/crates/core/src/error/vtable.rsindex 7e8e64d22b..27dfa76914 100644--- a/crates/core/src/error/vtable.rs+++ b/crates/core/src/error/vtable.rs@@ -45,6 +45,10 @@ 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>),++ /// Conversion into `anyhow::Error` from `Box<Self>`.+ #[cfg(feature = "anyhow")]+ pub(crate) into_anyhow: unsafe fn(OwnedPtr<DynError>) -> anyhow::Error,}impl Vtable {@@ -63,6 +67,8 @@ impl Vtable {into_boxed_dyn_core_error: into_boxed_dyn_core_error::<E>,drop_and_deallocate: drop_and_deallocate::<E>,downcast: downcast::<E>,+ #[cfg(feature = "anyhow")]+ into_anyhow: into_anyhow::<E>,}}}@@ -174,3 +180,14 @@ where}}}++#[cfg(feature = "anyhow")]+unsafe fn into_anyhow<E>(error: OwnedPtr<DynError>) -> anyhow::Error+where+ E: ErrorExt,+{+ let error = error.cast::<ConcreteError<E>>();+ // Safety: implied by all vtable functions' safety contract.+ let error = unsafe { error.into_box() };+ error.error.ext_into_anyhow()+}diff --git a/crates/core/tests/tests.rs b/crates/core/tests/tests.rsindex 39a5b1666d..d5e7473e25 100644--- a/crates/core/tests/tests.rs+++ b/crates/core/tests/tests.rs@@ -905,3 +905,40 @@ fn oom_requested_allocation_size() {let oom = OutOfMemory::new(usize::MAX);assert!(oom.requested_allocation_size() <= usize::try_from(isize::MAX).unwrap());}++#[test]+#[cfg(feature = "anyhow")]+fn anyhow_preserves_downcast() -> Result<()> {+ {+ let e: Error = TestError(1).into();+ assert!(e.downcast_ref::<TestError>().is_some());+ let e: anyhow::Error = e.into();+ assert!(e.downcast_ref::<TestError>().is_some());+ }+ {+ let e = Error::from(TestError(1)).context("hi");+ assert!(e.downcast_ref::<TestError>().is_some());+ assert!(e.downcast_ref::<&str>().is_some());+ let e: anyhow::Error = e.into();+ assert!(e.downcast_ref::<TestError>().is_some());+ assert!(e.downcast_ref::<&str>().is_some());+ }+ Ok(())+}++#[test]+#[cfg(feature = "anyhow")]+fn anyhow_source_loses_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());++ // 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(())+}