anlavandier opened PR #11761 from anlavandier:fix/bindgen-send to bytecodealliance:main:
<!--
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
-->
Fixes #11752.This change will ensure that
bindgen!invocation withimports/exports: { default: async }will require generateT: Sendbounds inWorldName::add_to_linker<T, D>even when the world doesn't import functions directly and only imports interfaces defined in another module.An example of code that currently doesn't (but in my opinion) compile and will now compile is in the linked issue. It is reproduced below with minor touch-ups for convenience.
package example:example; interface api { foo: func(); } world bar { import api; export baz: func(); } world api-impl { import api; }use wasmtime::component::bindgen; bindgen!({ world: "bar", path: "wit/example.wit", with: { "example:example/api": async_impl, }, imports: { default: async } }); pub mod async_impl { use wasmtime::component::bindgen; bindgen!({ world: "api-impl", path: "wit/example.wit", imports: { default: async, } }); pub struct MyHost; pub use example::example::api::{Host, HostWithStore, add_to_linker}; impl Host for MyHost { async fn foo(&mut self) { let _ = 3 + 4; } } } fn main() {}The compile error is the following:
error[E0277]: `T` cannot be sent between threads safely --> src/main.rs:3:1 | 3 | / bindgen!({ 4 | | world: "bar", 5 | | path: "wit/example.wit", 6 | | with: { ... | 12 | | }); | |__^ `T` cannot be sent between threads safely | note: required by a bound in `add_to_linker` --> src/main.rs:16:5 | 16 | / bindgen!({ 17 | | world: "api-impl", 18 | | path: "wit/example.wit", 19 | | imports: { ... | 22 | | }); | |______^ required by this bound in `add_to_linker` = note: this error originates in the macro `bindgen` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider further restricting type parameter `T` with trait `Send` | 12 | }), T: std::marker::Send; | ++++++++++++++++++++++ For more information about this error, try `rustc --explain E0277`. error: could not compile `component-model-test` (bin "component-model-test") due to 1 previous error
anlavandier requested alexcrichton for a review on PR #11761.
anlavandier requested wasmtime-core-reviewers for a review on PR #11761.
anlavandier commented on PR #11761:
This PR makes the CI fail when comparing against previous expansions. If this contribution is deemed useful then changing the relevant files in
crates/component-macro/tests/expanded/is easy to do. I didn't do it at first because it would add a lot of changes that aren't related to the main point of this PR.
anlavandier edited a comment on PR #11761:
This PR makes the CI fail when comparing against previous expansions. If this contribution is deemed useful then changing the relevant files in
crates/component-macro/tests/expanded/is easy to do. I didn't do it for now because it would add a lot of changes that aren't related to the main point of this PR.
alexcrichton commented on PR #11761:
Seems reasonable enough to me, thanks! Would you be up for adding a test for this as well? (e.g. writing down your example in this PR in
tests/codegen.rs). Then including the test updates in theexpandeddirectory should be fine as well.
anlavandier updated PR #11761.
anlavandier commented on PR #11761:
Seems reasonable enough to me, thanks! Would you be up for adding a test for this as well? (e.g. writing down your example in this PR in
tests/codegen.rs). Then including the test updates in theexpandeddirectory should be fine as well.@alexcrichton Done in the latest commits. If you have better ideas for names in the example added in
codegen.rsI'm happy to oblige.
anlavandier updated PR #11761.
anlavandier updated PR #11761.
alexcrichton submitted PR review.
alexcrichton merged PR #11761.
Last updated: Dec 06 2025 at 07:03 UTC