Skip to content

Commit 5afb603

Browse files
authored
Fix error chaining and casting with anyhow errors in the chain (#12705)
* Fix error chaining and casting with `anyhow` errors in the chain Fixes #12690 * add cfg that got lost * avoid raw identifiers * Add missing cfg for test function
1 parent 8de60f1 commit 5afb603

5 files changed

Lines changed: 487 additions & 348 deletions

File tree

crates/core/src/error/context.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use crate::error::{
2-
Error, ErrorExt, OutOfMemory, Result,
3-
boxed::try_new_uninit_box,
4-
error::{OomOrDynError, OomOrDynErrorMut, OomOrDynErrorRef},
5-
};
1+
use crate::error::{Error, ErrorExt, OutOfMemory, Result, boxed::try_new_uninit_box};
62
use core::any::TypeId;
73
use core::fmt;
84
use core::ptr::NonNull;
@@ -245,7 +241,7 @@ where
245241
{
246242
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
247243
let source = self.ext_source()?;
248-
Some(source.as_dyn_core_error())
244+
Some(source.inner.as_dyn_core_error())
249245
}
250246
}
251247

@@ -264,19 +260,12 @@ where
264260
Ok(Box::write(boxed, self) as _)
265261
}
266262

267-
fn ext_source(&self) -> Option<OomOrDynErrorRef<'_>> {
268-
let error = self.error.as_ref()?;
269-
Some(error.inner.unpack())
263+
fn ext_source(&self) -> Option<&Error> {
264+
self.error.as_ref()
270265
}
271266

272-
fn ext_source_mut(&mut self) -> Option<OomOrDynErrorMut<'_>> {
273-
let error = self.error.as_mut()?;
274-
Some(error.inner.unpack_mut())
275-
}
276-
277-
fn ext_take_source(&mut self) -> Option<OomOrDynError> {
278-
let error = self.error.take()?;
279-
Some(error.inner)
267+
fn ext_source_mut(&mut self) -> Option<&mut Error> {
268+
self.error.as_mut()
280269
}
281270

282271
fn ext_is(&self, type_id: TypeId) -> bool {
@@ -294,14 +283,7 @@ where
294283

295284
#[cfg(feature = "backtrace")]
296285
fn take_backtrace(&mut self) -> Option<std::backtrace::Backtrace> {
297-
let error = self.error.as_mut()?;
298-
match error.inner.unpack_mut() {
299-
OomOrDynErrorMut::Oom(_) => None,
300-
OomOrDynErrorMut::DynError(mut e) => {
301-
let r = unsafe { e.as_mut() };
302-
r.backtrace.take()
303-
}
304-
}
286+
self.error.as_mut()?.take_backtrace()
305287
}
306288

307289
#[cfg(feature = "anyhow")]

0 commit comments

Comments
 (0)