Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #369 (Loss of Diagnostic information when Report is converted to Box<dyn Diagnostic>) #370

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

TheLostLambda
Copy link
Contributor

@TheLostLambda TheLostLambda commented Apr 25, 2024

Fixes #369

The culprit was:

impl<E> Diagnostic for ErrorImpl<E> where E: Diagnostic {}

In conjunction with:

miette/src/eyreish/error.rs

Lines 531 to 537 in ea4296d

unsafe fn object_boxed<E>(e: Own<ErasedErrorImpl>) -> Box<dyn Diagnostic + Send + Sync + 'static>
where
E: Diagnostic + Send + Sync + 'static,
{
// Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.
e.cast::<ErrorImpl<E>>().boxed()
}

Which converts a Box<ErrorImpl<E>> into the Box<dyn Diagnostic>, hence the resultant object ends up with the empty Diagnostic impl defined above!

This fix moves the _object out of the Box<ErrorImpl<E>> to create a Box<E> before conversion to Box<dyn Diagnostic>, attaching the correct Diagnostic impl for the original error E.

Miri seems happy, and I've actually only added safe code, even though it's in an unsafe function because of .boxed() which I left untouched.


As a side note, the only reason converting to Box<dyn Error> worked is because of this manual pass-through done here:

miette/src/eyreish/error.rs

Lines 719 to 726 in ea4296d

impl<E> StdError for ErrorImpl<E>
where
E: StdError,
{
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
unsafe { ErrorImpl::diagnostic(self.erase()).source() }
}
}

But now that we aren't ever trying to convert ErrorImpl<E>s into trait objects anymore — directly converting Es instead — I've deleted that unused code.

I didn't even fix this one, it was just passing!
Diagnostic impls are no longer reset to default when converting a
`Report` into a `Box<dyn Diagnostic>`. Also prevented `Report` vtables
and handlers from being kept around on the heap after conversion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Converting a Report to a Box<dyn Diagnostic> wipes all Diagnostic information
1 participant