From efcd26051fe5353c970968c9db73d4c1420a514a Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Fri, 28 Feb 2025 09:59:51 -0800 Subject: [PATCH 1/2] wasmtime-wit-bindgen: emit a definition for all types in a wit The calculation of TypeInfo only reaches types which are passed to or from a function. For types which are not reachable, default to the defining them according to the ownership setting given to bindgen. I have my doubts that `with`-reuse of bindgen types actually works properly when bindgen is set to Ownership::Borrowing but thats out of scope for this PR, which is to fix #10090 --- crates/wit-bindgen/src/rust.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/wit-bindgen/src/rust.rs b/crates/wit-bindgen/src/rust.rs index d5bf7d063873..7e7a67fecfdc 100644 --- a/crates/wit-bindgen/src/rust.rs +++ b/crates/wit-bindgen/src/rust.rs @@ -293,8 +293,16 @@ pub trait RustGenerator<'a> { fn modes_of(&self, ty: TypeId) -> Vec<(String, TypeMode)> { let info = self.info(ty); + // Info only populated for types that are passed to and from functions. For + // types which are not, default to the ownership setting. if !info.owned && !info.borrowed { - return Vec::new(); + return vec![( + self.param_name(ty), + match self.ownership() { + Ownership::Owning => TypeMode::Owned, + Ownership::Borrowing { .. } => TypeMode::AllBorrowed("'a"), + }, + )]; } let mut result = Vec::new(); let first_mode = From 7cd4a027c2a54a8d0568ffe39112c35ba1e1159d Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Fri, 28 Feb 2025 10:51:48 -0800 Subject: [PATCH 2/2] component-macro: bless bindgen test output --- .../tests/expanded/dead-code.rs | 50 +++++++++++++++++++ .../tests/expanded/dead-code_async.rs | 50 +++++++++++++++++++ .../tests/expanded/dead-code_concurrent.rs | 50 +++++++++++++++++++ .../tests/expanded/dead-code_tracing_async.rs | 50 +++++++++++++++++++ .../component-macro/tests/expanded/records.rs | 20 ++++++++ .../tests/expanded/records_async.rs | 20 ++++++++ .../tests/expanded/records_concurrent.rs | 20 ++++++++ .../tests/expanded/records_tracing_async.rs | 20 ++++++++ .../tests/expanded/simple-wasi.rs | 19 +++++++ .../tests/expanded/simple-wasi_async.rs | 19 +++++++ .../tests/expanded/simple-wasi_concurrent.rs | 19 +++++++ .../expanded/simple-wasi_tracing_async.rs | 19 +++++++ 12 files changed, 356 insertions(+) diff --git a/crates/component-macro/tests/expanded/dead-code.rs b/crates/component-macro/tests/expanded/dead-code.rs index 7c230eb56aad..02180de797f9 100644 --- a/crates/component-macro/tests/expanded/dead-code.rs +++ b/crates/component-macro/tests/expanded/dead-code.rs @@ -248,6 +248,56 @@ pub mod a { pub mod interface_with_dead_type { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; + pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType; + const _: () = { + assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); + assert!( + 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 + ); + }; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(record)] + #[derive(Clone, Copy)] + pub struct DeadType { + #[component(name = "a")] + pub a: u32, + } + impl core::fmt::Debug for DeadType { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("DeadType").field("a", &self.a).finish() + } + } + const _: () = { + assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32); + assert!( + 4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32 + ); + }; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(variant)] + #[derive(Clone, Copy)] + pub enum V { + #[component(name = "a")] + A(LiveType), + #[component(name = "b")] + B(DeadType), + } + impl core::fmt::Debug for V { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + V::A(e) => f.debug_tuple("V::A").field(e).finish(), + V::B(e) => f.debug_tuple("V::B").field(e).finish(), + } + } + } + const _: () = { + assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32); + assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32); + }; pub trait Host {} pub trait GetHost< T, diff --git a/crates/component-macro/tests/expanded/dead-code_async.rs b/crates/component-macro/tests/expanded/dead-code_async.rs index 5ae89b9a6196..83045e481901 100644 --- a/crates/component-macro/tests/expanded/dead-code_async.rs +++ b/crates/component-macro/tests/expanded/dead-code_async.rs @@ -262,6 +262,56 @@ pub mod a { pub mod interface_with_dead_type { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; + pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType; + const _: () = { + assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); + assert!( + 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 + ); + }; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(record)] + #[derive(Clone, Copy)] + pub struct DeadType { + #[component(name = "a")] + pub a: u32, + } + impl core::fmt::Debug for DeadType { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("DeadType").field("a", &self.a).finish() + } + } + const _: () = { + assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32); + assert!( + 4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32 + ); + }; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(variant)] + #[derive(Clone, Copy)] + pub enum V { + #[component(name = "a")] + A(LiveType), + #[component(name = "b")] + B(DeadType), + } + impl core::fmt::Debug for V { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + V::A(e) => f.debug_tuple("V::A").field(e).finish(), + V::B(e) => f.debug_tuple("V::B").field(e).finish(), + } + } + } + const _: () = { + assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32); + assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32); + }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} pub trait GetHost< diff --git a/crates/component-macro/tests/expanded/dead-code_concurrent.rs b/crates/component-macro/tests/expanded/dead-code_concurrent.rs index 5a26662f08b9..a5c1352ab196 100644 --- a/crates/component-macro/tests/expanded/dead-code_concurrent.rs +++ b/crates/component-macro/tests/expanded/dead-code_concurrent.rs @@ -301,6 +301,56 @@ pub mod a { pub mod interface_with_dead_type { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; + pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType; + const _: () = { + assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); + assert!( + 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 + ); + }; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(record)] + #[derive(Clone, Copy)] + pub struct DeadType { + #[component(name = "a")] + pub a: u32, + } + impl core::fmt::Debug for DeadType { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("DeadType").field("a", &self.a).finish() + } + } + const _: () = { + assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32); + assert!( + 4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32 + ); + }; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(variant)] + #[derive(Clone, Copy)] + pub enum V { + #[component(name = "a")] + A(LiveType), + #[component(name = "b")] + B(DeadType), + } + impl core::fmt::Debug for V { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + V::A(e) => f.debug_tuple("V::A").field(e).finish(), + V::B(e) => f.debug_tuple("V::B").field(e).finish(), + } + } + } + const _: () = { + assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32); + assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32); + }; pub trait Host {} pub trait GetHost< T, diff --git a/crates/component-macro/tests/expanded/dead-code_tracing_async.rs b/crates/component-macro/tests/expanded/dead-code_tracing_async.rs index 5261c71d5d8d..023fe0456d64 100644 --- a/crates/component-macro/tests/expanded/dead-code_tracing_async.rs +++ b/crates/component-macro/tests/expanded/dead-code_tracing_async.rs @@ -275,6 +275,56 @@ pub mod a { pub mod interface_with_dead_type { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; + pub type LiveType = super::super::super::a::b::interface_with_live_type::LiveType; + const _: () = { + assert!(4 == < LiveType as wasmtime::component::ComponentType >::SIZE32); + assert!( + 4 == < LiveType as wasmtime::component::ComponentType >::ALIGN32 + ); + }; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(record)] + #[derive(Clone, Copy)] + pub struct DeadType { + #[component(name = "a")] + pub a: u32, + } + impl core::fmt::Debug for DeadType { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("DeadType").field("a", &self.a).finish() + } + } + const _: () = { + assert!(4 == < DeadType as wasmtime::component::ComponentType >::SIZE32); + assert!( + 4 == < DeadType as wasmtime::component::ComponentType >::ALIGN32 + ); + }; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(variant)] + #[derive(Clone, Copy)] + pub enum V { + #[component(name = "a")] + A(LiveType), + #[component(name = "b")] + B(DeadType), + } + impl core::fmt::Debug for V { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + V::A(e) => f.debug_tuple("V::A").field(e).finish(), + V::B(e) => f.debug_tuple("V::B").field(e).finish(), + } + } + } + const _: () = { + assert!(8 == < V as wasmtime::component::ComponentType >::SIZE32); + assert!(4 == < V as wasmtime::component::ComponentType >::ALIGN32); + }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} pub trait GetHost< diff --git a/crates/component-macro/tests/expanded/records.rs b/crates/component-macro/tests/expanded/records.rs index 516c0a43a54a..ed71c3d8ecaf 100644 --- a/crates/component-macro/tests/expanded/records.rs +++ b/crates/component-macro/tests/expanded/records.rs @@ -314,6 +314,15 @@ pub mod foo { 4 == < Aggregates as wasmtime::component::ComponentType >::ALIGN32 ); }; + pub type TupleTypedef = (i32,); + const _: () = { + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType >::SIZE32 + ); + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType >::ALIGN32 + ); + }; pub type IntTypedef = i32; const _: () = { assert!( @@ -680,6 +689,17 @@ pub mod exports { >::ALIGN32 ); }; + pub type TupleTypedef = (i32,); + const _: () = { + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType + >::SIZE32 + ); + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType + >::ALIGN32 + ); + }; pub type IntTypedef = i32; const _: () = { assert!( diff --git a/crates/component-macro/tests/expanded/records_async.rs b/crates/component-macro/tests/expanded/records_async.rs index baa76ecd663d..7ab89a501132 100644 --- a/crates/component-macro/tests/expanded/records_async.rs +++ b/crates/component-macro/tests/expanded/records_async.rs @@ -321,6 +321,15 @@ pub mod foo { 4 == < Aggregates as wasmtime::component::ComponentType >::ALIGN32 ); }; + pub type TupleTypedef = (i32,); + const _: () = { + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType >::SIZE32 + ); + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType >::ALIGN32 + ); + }; pub type IntTypedef = i32; const _: () = { assert!( @@ -714,6 +723,17 @@ pub mod exports { >::ALIGN32 ); }; + pub type TupleTypedef = (i32,); + const _: () = { + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType + >::SIZE32 + ); + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType + >::ALIGN32 + ); + }; pub type IntTypedef = i32; const _: () = { assert!( diff --git a/crates/component-macro/tests/expanded/records_concurrent.rs b/crates/component-macro/tests/expanded/records_concurrent.rs index 1597e0e7b222..c76b62956a48 100644 --- a/crates/component-macro/tests/expanded/records_concurrent.rs +++ b/crates/component-macro/tests/expanded/records_concurrent.rs @@ -321,6 +321,15 @@ pub mod foo { 4 == < Aggregates as wasmtime::component::ComponentType >::ALIGN32 ); }; + pub type TupleTypedef = (i32,); + const _: () = { + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType >::SIZE32 + ); + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType >::ALIGN32 + ); + }; pub type IntTypedef = i32; const _: () = { assert!( @@ -1134,6 +1143,17 @@ pub mod exports { >::ALIGN32 ); }; + pub type TupleTypedef = (i32,); + const _: () = { + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType + >::SIZE32 + ); + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType + >::ALIGN32 + ); + }; pub type IntTypedef = i32; const _: () = { assert!( diff --git a/crates/component-macro/tests/expanded/records_tracing_async.rs b/crates/component-macro/tests/expanded/records_tracing_async.rs index 160bd8cf7b25..2ff2183f7c1a 100644 --- a/crates/component-macro/tests/expanded/records_tracing_async.rs +++ b/crates/component-macro/tests/expanded/records_tracing_async.rs @@ -321,6 +321,15 @@ pub mod foo { 4 == < Aggregates as wasmtime::component::ComponentType >::ALIGN32 ); }; + pub type TupleTypedef = (i32,); + const _: () = { + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType >::SIZE32 + ); + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType >::ALIGN32 + ); + }; pub type IntTypedef = i32; const _: () = { assert!( @@ -875,6 +884,17 @@ pub mod exports { >::ALIGN32 ); }; + pub type TupleTypedef = (i32,); + const _: () = { + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType + >::SIZE32 + ); + assert!( + 4 == < TupleTypedef as wasmtime::component::ComponentType + >::ALIGN32 + ); + }; pub type IntTypedef = i32; const _: () = { assert!( diff --git a/crates/component-macro/tests/expanded/simple-wasi.rs b/crates/component-macro/tests/expanded/simple-wasi.rs index 09952eaa3c72..78d7e3dd7c83 100644 --- a/crates/component-macro/tests/expanded/simple-wasi.rs +++ b/crates/component-macro/tests/expanded/simple-wasi.rs @@ -300,6 +300,25 @@ pub mod foo { pub mod wall_clock { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(record)] + #[derive(Clone, Copy)] + pub struct WallClock {} + impl core::fmt::Debug for WallClock { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("WallClock").finish() + } + } + const _: () = { + assert!( + 0 == < WallClock as wasmtime::component::ComponentType >::SIZE32 + ); + assert!( + 1 == < WallClock as wasmtime::component::ComponentType >::ALIGN32 + ); + }; pub trait Host {} pub trait GetHost< T, diff --git a/crates/component-macro/tests/expanded/simple-wasi_async.rs b/crates/component-macro/tests/expanded/simple-wasi_async.rs index 1dcc718e5f81..353b89135549 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_async.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_async.rs @@ -316,6 +316,25 @@ pub mod foo { pub mod wall_clock { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(record)] + #[derive(Clone, Copy)] + pub struct WallClock {} + impl core::fmt::Debug for WallClock { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("WallClock").finish() + } + } + const _: () = { + assert!( + 0 == < WallClock as wasmtime::component::ComponentType >::SIZE32 + ); + assert!( + 1 == < WallClock as wasmtime::component::ComponentType >::ALIGN32 + ); + }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} pub trait GetHost< diff --git a/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs b/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs index 48c11ed67a4b..795efdc7e066 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_concurrent.rs @@ -397,6 +397,25 @@ pub mod foo { pub mod wall_clock { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(record)] + #[derive(Clone, Copy)] + pub struct WallClock {} + impl core::fmt::Debug for WallClock { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("WallClock").finish() + } + } + const _: () = { + assert!( + 0 == < WallClock as wasmtime::component::ComponentType >::SIZE32 + ); + assert!( + 1 == < WallClock as wasmtime::component::ComponentType >::ALIGN32 + ); + }; pub trait Host {} pub trait GetHost< T, diff --git a/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs b/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs index 621e50e3e092..06901e1fc71e 100644 --- a/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs +++ b/crates/component-macro/tests/expanded/simple-wasi_tracing_async.rs @@ -342,6 +342,25 @@ pub mod foo { pub mod wall_clock { #[allow(unused_imports)] use wasmtime::component::__internal::{anyhow, Box}; + #[derive(wasmtime::component::ComponentType)] + #[derive(wasmtime::component::Lift)] + #[derive(wasmtime::component::Lower)] + #[component(record)] + #[derive(Clone, Copy)] + pub struct WallClock {} + impl core::fmt::Debug for WallClock { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("WallClock").finish() + } + } + const _: () = { + assert!( + 0 == < WallClock as wasmtime::component::ComponentType >::SIZE32 + ); + assert!( + 1 == < WallClock as wasmtime::component::ComponentType >::ALIGN32 + ); + }; #[wasmtime::component::__internal::trait_variant_make(::core::marker::Send)] pub trait Host: Send {} pub trait GetHost<