Stream: git-wasmtime

Topic: wasmtime / PR #12689 Fix using `downcast` with `anyhow::E...


view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 17:35):

alexcrichton requested fitzgen for a review on PR #12689.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 17:35):

alexcrichton requested wasmtime-core-reviewers for a review on PR #12689.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 17:35):

alexcrichton opened PR #12689 from alexcrichton:fix-anyhow-downcast to bytecodealliance:main:

Previously when converting wasmtime::Error into anyhow::Error it ended up breaking the downcast method. This is because anyhow::Error::from_boxed looks like it does not implement the downcast method which is how all errors were previously converted to anyhow::Error. This commit adds a new vtable method for specifically converting to anyhow::Error which enables using the typed construction methods of anyhow which preserves downcast-ness.

<!--
Please make sure you include the following information:

Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.html

Please ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:01):

fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:01):

fitzgen created PR review comment:

To make sure I understand, it isn't enough to change this impl into something like

self.downcast::<anyhow::Error>().unwrap_or_else(|e| }
    anyhow::Error::from_boxed(e.into_boxed_dyn_error())
})

and expose the underlying anyhow::Error instead of dyn boxing it because anyhow itself doesn't implement is/downcast/etc... for anyhow::Errors created via anyhow::Error::from_boxed?

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:02):

fitzgen edited PR review comment.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:02):

fitzgen edited PR review comment.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:03):

fitzgen edited PR review comment.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:09):

fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:09):

fitzgen created PR review comment:

Ah, I guess anyhow's is/downcast could never see through our context chains when all they have is a Box<dyn core::error::Error>. Okay, I get it.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:20):

fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:20):

fitzgen created PR review comment:

This makes me realize wasmtime::Error probably doesn't handle is/downcast* correctly when created from an anyhow::Error that has a context chain:

let e = anyhow::Error::from(TestError(1));
let e = e.context("str");
let e = wasmtime::Error::from(e);
assert!(e.is::<anyhow::Error>()); // should pass right now
assert!(e.is::<TestError>());     // I think will fail
assert!(e.is::<&str>());          // I think will also fail

And in fact, I don't think we can support this, given that our vtable methods can't be generic over E but instead must take TypeIds. But maybe I am missing something and you have other ideas?

I guess we could at the wasmtime::Error implementation level fall back to something like

impl wasmtime::Error {
    pub fn is<E>(&self) -> bool {
        let result = existing_is_impl();

        #[cfg(feature = "anyhow")]
        let result = result || self.downcast_ref::<anyhow::Error>().is_some_and(|e| e.is::<E>());

        result
    }
}

?

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:22):

fitzgen edited PR review comment.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:44):

alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:44):

alexcrichton created PR review comment:

The specific case that I ran into updating Spin was that a Trap in Wasmtime was converted to wasmtime::Error and then that was converted to anyhow::Error. Calling anyhow_error.downcast::<Trap>() was failing due to being created from anyhow::Error::from_boxed which ends up severing any downcast methods.

So I believe what you've gisted will solve anyhow-and-back but wouldn't solve acquiring Wasmtime's error types which never went through anyhow

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:46):

alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:46):

alexcrichton created PR review comment:

That's a good point, and yeah I think hooking is and downcast and friends with type parameters to explicitly handle the anyhow special case is the only way to go here. I'll see if I can't hook that up.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:47):

fitzgen created PR review comment:

That's a good point, and yeah I think hooking is and downcast and friends with type parameters to explicitly handle the anyhow special case is the only way to go here. I'll see if I can't hook that up.

Fine to do as a follow up too, ofc, if you prefer

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:47):

fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:58):

alexcrichton updated PR #12689.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:59):

alexcrichton created PR review comment:

Hm ok yeah this is going to be a little gnarly, so I've added a test for the current behavior documenting that it's a bit buggy, but it at least preserves the anyhow roundtrip. I'll file a follow-up issue for this.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 19:59):

alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 20:03):

alexcrichton has enabled auto merge for PR #12689.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 20:12):

alexcrichton added PR #12689 Fix using downcast with anyhow::Error to the merge queue

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 20:32):

github-merge-queue[bot] removed PR #12689 Fix using downcast with anyhow::Error from the merge queue

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 20:35):

alexcrichton added PR #12689 Fix using downcast with anyhow::Error to the merge queue

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 21:00):

alexcrichton merged PR #12689.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 27 2026 at 21:00):

alexcrichton removed PR #12689 Fix using downcast with anyhow::Error from the merge queue


Last updated: Mar 23 2026 at 16:19 UTC