alexcrichton requested fitzgen for a review on PR #12689.
alexcrichton requested wasmtime-core-reviewers for a review on PR #12689.
alexcrichton opened PR #12689 from alexcrichton:fix-anyhow-downcast to bytecodealliance:main:
Previously when converting
wasmtime::Errorintoanyhow::Errorit ended up breaking thedowncastmethod. This is becauseanyhow::Error::from_boxedlooks like it does not implement thedowncastmethod which is how all errors were previously converted toanyhow::Error. This commit adds a new vtable method for specifically converting toanyhow::Errorwhich enables using the typed construction methods ofanyhowwhich preservesdowncast-ness.<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
fitzgen submitted PR review.
fitzgen created PR review comment:
To make sure I understand, it isn't enough to change this
implinto something likeself.downcast::<anyhow::Error>().unwrap_or_else(|e| } anyhow::Error::from_boxed(e.into_boxed_dyn_error()) })and expose the underlying
anyhow::Errorinstead ofdynboxing it becauseanyhowitself doesn't implementis/downcast/etc... foranyhow::Errors created viaanyhow::Error::from_boxed?
fitzgen edited PR review comment.
fitzgen edited PR review comment.
fitzgen edited PR review comment.
fitzgen submitted PR review.
fitzgen created PR review comment:
Ah, I guess
anyhow'sis/downcastcould never see through our context chains when all they have is aBox<dyn core::error::Error>. Okay, I get it.
fitzgen submitted PR review.
fitzgen created PR review comment:
This makes me realize
wasmtime::Errorprobably doesn't handleis/downcast*correctly when created from ananyhow::Errorthat 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 failAnd in fact, I don't think we can support this, given that our vtable methods can't be generic over
Ebut instead must takeTypeIds. But maybe I am missing something and you have other ideas?I guess we could at the
wasmtime::Errorimplementation level fall back to something likeimpl 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 } }?
fitzgen edited PR review comment.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
The specific case that I ran into updating Spin was that a
Trapin Wasmtime was converted towasmtime::Errorand then that was converted toanyhow::Error. Callinganyhow_error.downcast::<Trap>()was failing due to being created fromanyhow::Error::from_boxedwhich ends up severing anydowncastmethods.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
alexcrichton submitted PR review.
alexcrichton created PR review comment:
That's a good point, and yeah I think hooking
isanddowncastand friends with type parameters to explicitly handle theanyhowspecial case is the only way to go here. I'll see if I can't hook that up.
fitzgen created PR review comment:
That's a good point, and yeah I think hooking
isanddowncastand friends with type parameters to explicitly handle theanyhowspecial 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
fitzgen submitted PR review.
alexcrichton updated PR #12689.
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
anyhowroundtrip. I'll file a follow-up issue for this.
alexcrichton submitted PR review.
alexcrichton has enabled auto merge for PR #12689.
alexcrichton added PR #12689 Fix using downcast with anyhow::Error to the merge queue
github-merge-queue[bot] removed PR #12689 Fix using downcast with anyhow::Error from the merge queue
alexcrichton added PR #12689 Fix using downcast with anyhow::Error to the merge queue
alexcrichton merged PR #12689.
alexcrichton removed PR #12689 Fix using downcast with anyhow::Error from the merge queue
Last updated: Mar 23 2026 at 16:19 UTC