Stream: wasmtime

Topic: anyhow and downcasts


view this post on Zulip Dan Gohman (May 01 2020 at 16:50):

[crates/api/src/error.rs:25] &e = Trap {
    message: "test 123",
    wasm_trace: [
...
[crates/api/src/error.rs:26] e.is::<Trap>() = false

view this post on Zulip Dan Gohman (May 01 2020 at 16:50):

Anyone know of ways an anyhow::Error doesn't want to be what it seems to be?

view this post on Zulip Alex Crichton (May 01 2020 at 17:01):

@Dan Gohman there's a weird difference in the downcasting methods

view this post on Zulip Alex Crichton (May 01 2020 at 17:01):

Error::downcast_ref is different from <Error as Any>::downcast_ref

view this post on Zulip Alex Crichton (May 01 2020 at 17:01):

the former recursively checks the whole chain while the latter only checks the top-most error

view this post on Zulip Alex Crichton (May 01 2020 at 17:02):

it looks like Error::is isn't defined, it's <Error as Any>::is, which I think means it's not checking the whole chain

view this post on Zulip Alex Crichton (May 01 2020 at 17:02):

so it may be that a Trap has some extra context attached to it

view this post on Zulip Alex Crichton (May 01 2020 at 17:02):

so the underlying trap is still there but the top-level type isn't Trap

view this post on Zulip Alex Crichton (May 01 2020 at 17:03):

e.downcast_ref::<Trap>().is_some() in theory should be true

view this post on Zulip Alex Crichton (May 01 2020 at 17:03):

er I could also be totally wrong, just misread the docs

view this post on Zulip Alex Crichton (May 01 2020 at 17:03):

Error::is is indeed a thing...

view this post on Zulip Alex Crichton (May 01 2020 at 17:04):

hm got some code to poke around in?

view this post on Zulip Dan Gohman (May 01 2020 at 17:38):

@Alex Crichton I have a branch here: https://github.com/sunfishcode/wasmtime/tree/sunfishcode/exit-unwind

Standalone JIT-style runtime for WebAsssembly, using Cranelift - sunfishcode/wasmtime

view this post on Zulip Dan Gohman (May 01 2020 at 17:38):

it's wip, and not complete, but cargo test test_trap_return fails and I'm trying to determine why

view this post on Zulip Dan Gohman (May 01 2020 at 17:39):

See the dbg's in crates/api/src/error.rs

view this post on Zulip Alex Crichton (May 01 2020 at 17:40):

oh Trap::User isn't an anyhow::Error

view this post on Zulip Alex Crichton (May 01 2020 at 17:40):

it's a dyn std::error::Error

view this post on Zulip Dan Gohman (May 01 2020 at 17:41):

yeah, so we downcast the std::error::Error to get the Trap out, and then stick it in a anyhow::Error

view this post on Zulip Dan Gohman (May 01 2020 at 17:42):

and that Error no longer claims to be is::<Trap>()

view this post on Zulip Alex Crichton (May 01 2020 at 17:42):

poking around, where's this error constructed?

view this post on Zulip Alex Crichton (May 01 2020 at 17:43):

oh

view this post on Zulip Dan Gohman (May 01 2020 at 17:43):

I believe it ultimately comes from catch_traps

view this post on Zulip Alex Crichton (May 01 2020 at 17:44):

this is b/c the return type of closuers changed

view this post on Zulip Alex Crichton (May 01 2020 at 17:44):

from Trap to anyhow::Error

view this post on Zulip Alex Crichton (May 01 2020 at 17:45):

so the Trap::User(..) now is always anyhow::Error under the hood

view this post on Zulip Alex Crichton (May 01 2020 at 17:45):

so to downcast that you'd have to downcast to anyhow::Error first

view this post on Zulip Alex Crichton (May 01 2020 at 17:45):

for a patch like this you'd probably want to replace Trap::User's payload with anyhow::Error

view this post on Zulip Alex Crichton (May 01 2020 at 17:45):

not really much reason in trying to be so general

view this post on Zulip Alex Crichton (May 01 2020 at 17:45):

with std::error::Error

view this post on Zulip Dan Gohman (May 01 2020 at 17:46):

Ok, makes sense


Last updated: Dec 23 2024 at 13:07 UTC