Very strange trait bounds error

Ok, I've spent the last 3 hours debugging a trait bound problem that I encountered in an older project that I have returned to recently. So far, I have 3 version of the code: the original project, a new, black project for a minimal reproducible example, and a rust playground version.

Here is the playground link. The two highlighted lines worked in the playground and in the new (fresh) project, but not in the main project.

The error message something like the trait ReturnWasmAbi is not implemented for Result<T, wasm_bindgen::JsValue>, but there is a blanket implementation for that.

This is strange, because:

  1. The main project used to work a few months ago, I have not changed the version of the wasm_bindgen crate which is where these traits are coming from
  2. The exact same code worked just fine in the new project that used the same version of wasm_bindgen.
  3. The code in the playground works, so Rust can clearly use blanket implementations to figure this out.

So I decided to delete my Cargo.lock and target forders in both project, and rebuild it from scratch. Now it fails in both of them, but the "fix" with the extra trait bound also works in both of them.

At this point, I think the version of the wasm_bindgen dependent crate might be the problem, because the blanket implementation is a bit different in the newest version, but I'm not sure how is rust selecting which version to use, or how I verify which version is being used.

I have simply wasm-bindgen = "0.2.87" in both my Cargo.toml files. You can have a look at the original project or the reproducible example.

I removed your trait bounds in the playground, and it still compiles.

Did you run cargo clean in your main project? Usually weird compilation errors go away after you clean it.

Which ones? The ones where I explicitly said that the playground works without them, and are there to "fix" the main project? You don't say.

I tried it, still the same error.

Sorry, I meant these bounds:

    Result<T, JsValue>: ReturnWasmAbi

Didn't see that you mentioned in point #3 that it works in the playground without them (I inferred you were talking about these, based on the comments you added in the playground).

You can either manually check the Cargo.lock files or you can use cargo tree - The Cargo Book to inspect your dependency tree.

You can also pinpoint an exact version, like this:

wasm-bindgen = "=0.2.87"

You can read more about specifying dependencies' versions here:

1 Like

Thanks. cargo tree confirmed that it was using the newest version (0.2.89) instead of the specified one (0.2.87). "Locking" the version to =0.2.87 made it work again. I assume that means it is a breaking change and therefore shouldn't be a minor patch, but oh well.

You could create an issue in the wasm-bindgen repo to bring awareness to it: GitHub - rustwasm/wasm-bindgen: Facilitating high-level interactions between Wasm modules and JavaScript

1 Like