Skip to content

Commit f81c0dc

Browse files
authored
Add T: 'static to Store<T> (#10760)
* Add `T: 'static` to `Store<T> Since the beginning the `T` type parameter on `Store<T>` has had no bounds on it. This was intended for maximal flexibility in terms of what embedders place within a `Store<T>` and I've personally advocated that we need to keep it this way. In the development of the WASIp3 work, however, I've at least personally reached the conclusion that this is no longer tenable and proceeding will require adding a `'static` bound to data within a store. Wasmtime today [already] carries unsafe `transmute`s to work around this lack of `'static` bound, and while the number of `unsafe` parts is relatively small right now we're still fundamentally lying to the compiler about lifetime bounds internally. With the WASIp3 async work this degree of "lying" has become even worse. Joel has written up some examples [on Zulip] about how the Rust compiler is requiring `'static` bounds in surprising ways. These patterns are cropping up quite frequently in the WASIp3 work and it's becoming particularly onerous maintaining all of the `unsafe` and ensuring that everything is in sync. In the WASIp3 repository I've additionally [prototyped a change] which would additionally practically require `T: 'static` in more locations. This change is one I plan on landing in Wasmtime in the near future and while its main motivations are for enabling WASIp3 work it is also a much nicer system than what we have today, in my opinion. Overall the cost of not having `T: 'static` on `Store<T>` is effectively becoming quite costly, in particular with respect to WASIp3 work. This is coupled with all known embedders already using `T: 'static` data within a `Store<T>` so the expectation of the impact of this change is not large. The main downside of this change as a result is that when and where to place `'static` bounds is sort of a game of whack-a-mole with the compiler. For example I changed `Store<T>` to require `'static` here, but the rest of the change is basically "hit compile until rustc says it's ok". There's not necessarily a huge amount of rhyme-or-reason to where `'static` bounds crop up, which can be surprising or difficult to work with for users. In the end I feel that this change is necessary and one we can't shy away from. If problems crop up we'll need to figure out how to thread that needle at that time, but I'm coming around to thinking that `T: 'static` is just a fundamental constraint we'll have to take on at this time. Maybe a future version of Rust that fixes some of Joel's examples (if they can be fixed, we're not sure of that) we could consider relaxing this but that's left for future work. [already]: https://github.com/bytecodealliance/wasmtime/blob/35053d6d8d1a5d4692cf636cba0c920b4a79a44b/crates/wasmtime/src/runtime/store.rs#L602-L611 [on Zulip]: https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/.22type.20may.20not.20live.20long.20enough.22.20for.20generic.20closure/near/473862072 [prototyped a change]: bytecodealliance/wasip3-prototyping#158 * Remove a no-longer-necessary `unsafe` block * Update test expectations * Fix gc-disabled builds
1 parent 9d40c6e commit f81c0dc

File tree

171 files changed

+1118
-646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+1118
-646
lines changed

benches/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ fn wasm_to_host(c: &mut Criterion) {
360360
return;
361361
}
362362

363-
let mut typed = Linker::new(&engine);
363+
let mut typed = Linker::<()>::new(&engine);
364364
typed
365365
.func_wrap_async("", "nop", |caller, _: ()| {
366366
Box::new(async {

crates/component-macro/tests/expanded/char.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/// has been created through a [`Linker`](wasmtime::component::Linker).
77
///
88
/// For more information see [`TheWorld`] as well.
9-
pub struct TheWorldPre<T> {
9+
pub struct TheWorldPre<T: 'static> {
1010
instance_pre: wasmtime::component::InstancePre<T>,
1111
indices: TheWorldIndices,
1212
}
13-
impl<T> Clone for TheWorldPre<T> {
13+
impl<T: 'static> Clone for TheWorldPre<T> {
1414
fn clone(&self) -> Self {
1515
Self {
1616
instance_pre: self.instance_pre.clone(),
1717
indices: self.indices.clone(),
1818
}
1919
}
2020
}
21-
impl<_T> TheWorldPre<_T> {
21+
impl<_T: 'static> TheWorldPre<_T> {
2222
/// Creates a new copy of `TheWorldPre` bindings which can then
2323
/// be used to instantiate into a particular store.
2424
///
@@ -149,6 +149,7 @@ const _: () = {
149149
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
150150
) -> wasmtime::Result<()>
151151
where
152+
T: 'static,
152153
U: foo::foo::chars::Host,
153154
{
154155
foo::foo::chars::add_to_linker(linker, get)?;
@@ -176,6 +177,7 @@ pub mod foo {
176177
host_getter: G,
177178
) -> wasmtime::Result<()>
178179
where
180+
T: 'static,
179181
G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>,
180182
{
181183
let mut inst = linker.instance("foo:foo/chars")?;
@@ -205,6 +207,7 @@ pub mod foo {
205207
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
206208
) -> wasmtime::Result<()>
207209
where
210+
T: 'static,
208211
U: Host,
209212
{
210213
add_to_linker_get_host(linker, get)

crates/component-macro/tests/expanded/char_async.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/// has been created through a [`Linker`](wasmtime::component::Linker).
77
///
88
/// For more information see [`TheWorld`] as well.
9-
pub struct TheWorldPre<T> {
9+
pub struct TheWorldPre<T: 'static> {
1010
instance_pre: wasmtime::component::InstancePre<T>,
1111
indices: TheWorldIndices,
1212
}
13-
impl<T> Clone for TheWorldPre<T> {
13+
impl<T: 'static> Clone for TheWorldPre<T> {
1414
fn clone(&self) -> Self {
1515
Self {
1616
instance_pre: self.instance_pre.clone(),
1717
indices: self.indices.clone(),
1818
}
1919
}
2020
}
21-
impl<_T> TheWorldPre<_T> {
21+
impl<_T: 'static> TheWorldPre<_T> {
2222
/// Creates a new copy of `TheWorldPre` bindings which can then
2323
/// be used to instantiate into a particular store.
2424
///
@@ -155,6 +155,7 @@ const _: () = {
155155
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
156156
) -> wasmtime::Result<()>
157157
where
158+
T: 'static,
158159
T: Send,
159160
U: foo::foo::chars::Host + Send,
160161
{
@@ -184,6 +185,7 @@ pub mod foo {
184185
host_getter: G,
185186
) -> wasmtime::Result<()>
186187
where
188+
T: 'static,
187189
G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>,
188190
T: Send,
189191
{
@@ -218,6 +220,7 @@ pub mod foo {
218220
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
219221
) -> wasmtime::Result<()>
220222
where
223+
T: 'static,
221224
U: Host + Send,
222225
T: Send,
223226
{

crates/component-macro/tests/expanded/char_concurrent.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/// has been created through a [`Linker`](wasmtime::component::Linker).
77
///
88
/// For more information see [`TheWorld`] as well.
9-
pub struct TheWorldPre<T> {
9+
pub struct TheWorldPre<T: 'static> {
1010
instance_pre: wasmtime::component::InstancePre<T>,
1111
indices: TheWorldIndices,
1212
}
13-
impl<T> Clone for TheWorldPre<T> {
13+
impl<T: 'static> Clone for TheWorldPre<T> {
1414
fn clone(&self) -> Self {
1515
Self {
1616
instance_pre: self.instance_pre.clone(),
1717
indices: self.indices.clone(),
1818
}
1919
}
2020
}
21-
impl<_T> TheWorldPre<_T> {
21+
impl<_T: 'static> TheWorldPre<_T> {
2222
/// Creates a new copy of `TheWorldPre` bindings which can then
2323
/// be used to instantiate into a particular store.
2424
///
@@ -155,7 +155,8 @@ const _: () = {
155155
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
156156
) -> wasmtime::Result<()>
157157
where
158-
T: Send + foo::foo::chars::Host<Data = T> + 'static,
158+
T: 'static,
159+
T: Send + foo::foo::chars::Host<Data = T>,
159160
U: Send + foo::foo::chars::Host<Data = T>,
160161
{
161162
foo::foo::chars::add_to_linker(linker, get)?;
@@ -201,6 +202,7 @@ pub mod foo {
201202
host_getter: G,
202203
) -> wasmtime::Result<()>
203204
where
205+
T: 'static,
204206
G: for<'a> wasmtime::component::GetHost<
205207
&'a mut T,
206208
Host: Host<Data = T> + Send,
@@ -278,6 +280,7 @@ pub mod foo {
278280
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
279281
) -> wasmtime::Result<()>
280282
where
283+
T: 'static,
281284
U: Host<Data = T> + Send,
282285
T: Send + 'static,
283286
{

crates/component-macro/tests/expanded/char_tracing_async.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/// has been created through a [`Linker`](wasmtime::component::Linker).
77
///
88
/// For more information see [`TheWorld`] as well.
9-
pub struct TheWorldPre<T> {
9+
pub struct TheWorldPre<T: 'static> {
1010
instance_pre: wasmtime::component::InstancePre<T>,
1111
indices: TheWorldIndices,
1212
}
13-
impl<T> Clone for TheWorldPre<T> {
13+
impl<T: 'static> Clone for TheWorldPre<T> {
1414
fn clone(&self) -> Self {
1515
Self {
1616
instance_pre: self.instance_pre.clone(),
1717
indices: self.indices.clone(),
1818
}
1919
}
2020
}
21-
impl<_T> TheWorldPre<_T> {
21+
impl<_T: 'static> TheWorldPre<_T> {
2222
/// Creates a new copy of `TheWorldPre` bindings which can then
2323
/// be used to instantiate into a particular store.
2424
///
@@ -155,6 +155,7 @@ const _: () = {
155155
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
156156
) -> wasmtime::Result<()>
157157
where
158+
T: 'static,
158159
T: Send,
159160
U: foo::foo::chars::Host + Send,
160161
{
@@ -184,6 +185,7 @@ pub mod foo {
184185
host_getter: G,
185186
) -> wasmtime::Result<()>
186187
where
188+
T: 'static,
187189
G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>,
188190
T: Send,
189191
{
@@ -247,6 +249,7 @@ pub mod foo {
247249
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
248250
) -> wasmtime::Result<()>
249251
where
252+
T: 'static,
250253
U: Host + Send,
251254
T: Send,
252255
{

crates/component-macro/tests/expanded/conventions.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/// has been created through a [`Linker`](wasmtime::component::Linker).
77
///
88
/// For more information see [`TheWorld`] as well.
9-
pub struct TheWorldPre<T> {
9+
pub struct TheWorldPre<T: 'static> {
1010
instance_pre: wasmtime::component::InstancePre<T>,
1111
indices: TheWorldIndices,
1212
}
13-
impl<T> Clone for TheWorldPre<T> {
13+
impl<T: 'static> Clone for TheWorldPre<T> {
1414
fn clone(&self) -> Self {
1515
Self {
1616
instance_pre: self.instance_pre.clone(),
1717
indices: self.indices.clone(),
1818
}
1919
}
2020
}
21-
impl<_T> TheWorldPre<_T> {
21+
impl<_T: 'static> TheWorldPre<_T> {
2222
/// Creates a new copy of `TheWorldPre` bindings which can then
2323
/// be used to instantiate into a particular store.
2424
///
@@ -151,6 +151,7 @@ const _: () = {
151151
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
152152
) -> wasmtime::Result<()>
153153
where
154+
T: 'static,
154155
U: foo::foo::conventions::Host,
155156
{
156157
foo::foo::conventions::add_to_linker(linker, get)?;
@@ -224,6 +225,7 @@ pub mod foo {
224225
host_getter: G,
225226
) -> wasmtime::Result<()>
226227
where
228+
T: 'static,
227229
G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host>,
228230
{
229231
let mut inst = linker.instance("foo:foo/conventions")?;
@@ -333,6 +335,7 @@ pub mod foo {
333335
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
334336
) -> wasmtime::Result<()>
335337
where
338+
T: 'static,
336339
U: Host,
337340
{
338341
add_to_linker_get_host(linker, get)

crates/component-macro/tests/expanded/conventions_async.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/// has been created through a [`Linker`](wasmtime::component::Linker).
77
///
88
/// For more information see [`TheWorld`] as well.
9-
pub struct TheWorldPre<T> {
9+
pub struct TheWorldPre<T: 'static> {
1010
instance_pre: wasmtime::component::InstancePre<T>,
1111
indices: TheWorldIndices,
1212
}
13-
impl<T> Clone for TheWorldPre<T> {
13+
impl<T: 'static> Clone for TheWorldPre<T> {
1414
fn clone(&self) -> Self {
1515
Self {
1616
instance_pre: self.instance_pre.clone(),
1717
indices: self.indices.clone(),
1818
}
1919
}
2020
}
21-
impl<_T> TheWorldPre<_T> {
21+
impl<_T: 'static> TheWorldPre<_T> {
2222
/// Creates a new copy of `TheWorldPre` bindings which can then
2323
/// be used to instantiate into a particular store.
2424
///
@@ -157,6 +157,7 @@ const _: () = {
157157
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
158158
) -> wasmtime::Result<()>
159159
where
160+
T: 'static,
160161
T: Send,
161162
U: foo::foo::conventions::Host + Send,
162163
{
@@ -232,6 +233,7 @@ pub mod foo {
232233
host_getter: G,
233234
) -> wasmtime::Result<()>
234235
where
236+
T: 'static,
235237
G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>,
236238
T: Send,
237239
{
@@ -366,6 +368,7 @@ pub mod foo {
366368
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
367369
) -> wasmtime::Result<()>
368370
where
371+
T: 'static,
369372
U: Host + Send,
370373
T: Send,
371374
{

crates/component-macro/tests/expanded/conventions_concurrent.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/// has been created through a [`Linker`](wasmtime::component::Linker).
77
///
88
/// For more information see [`TheWorld`] as well.
9-
pub struct TheWorldPre<T> {
9+
pub struct TheWorldPre<T: 'static> {
1010
instance_pre: wasmtime::component::InstancePre<T>,
1111
indices: TheWorldIndices,
1212
}
13-
impl<T> Clone for TheWorldPre<T> {
13+
impl<T: 'static> Clone for TheWorldPre<T> {
1414
fn clone(&self) -> Self {
1515
Self {
1616
instance_pre: self.instance_pre.clone(),
1717
indices: self.indices.clone(),
1818
}
1919
}
2020
}
21-
impl<_T> TheWorldPre<_T> {
21+
impl<_T: 'static> TheWorldPre<_T> {
2222
/// Creates a new copy of `TheWorldPre` bindings which can then
2323
/// be used to instantiate into a particular store.
2424
///
@@ -157,7 +157,8 @@ const _: () = {
157157
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
158158
) -> wasmtime::Result<()>
159159
where
160-
T: Send + foo::foo::conventions::Host<Data = T> + 'static,
160+
T: 'static,
161+
T: Send + foo::foo::conventions::Host<Data = T>,
161162
U: Send + foo::foo::conventions::Host<Data = T>,
162163
{
163164
foo::foo::conventions::add_to_linker(linker, get)?;
@@ -329,6 +330,7 @@ pub mod foo {
329330
host_getter: G,
330331
) -> wasmtime::Result<()>
331332
where
333+
T: 'static,
332334
G: for<'a> wasmtime::component::GetHost<
333335
&'a mut T,
334336
Host: Host<Data = T> + Send,
@@ -708,6 +710,7 @@ pub mod foo {
708710
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
709711
) -> wasmtime::Result<()>
710712
where
713+
T: 'static,
711714
U: Host<Data = T> + Send,
712715
T: Send + 'static,
713716
{

crates/component-macro/tests/expanded/conventions_tracing_async.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/// has been created through a [`Linker`](wasmtime::component::Linker).
77
///
88
/// For more information see [`TheWorld`] as well.
9-
pub struct TheWorldPre<T> {
9+
pub struct TheWorldPre<T: 'static> {
1010
instance_pre: wasmtime::component::InstancePre<T>,
1111
indices: TheWorldIndices,
1212
}
13-
impl<T> Clone for TheWorldPre<T> {
13+
impl<T: 'static> Clone for TheWorldPre<T> {
1414
fn clone(&self) -> Self {
1515
Self {
1616
instance_pre: self.instance_pre.clone(),
1717
indices: self.indices.clone(),
1818
}
1919
}
2020
}
21-
impl<_T> TheWorldPre<_T> {
21+
impl<_T: 'static> TheWorldPre<_T> {
2222
/// Creates a new copy of `TheWorldPre` bindings which can then
2323
/// be used to instantiate into a particular store.
2424
///
@@ -157,6 +157,7 @@ const _: () = {
157157
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
158158
) -> wasmtime::Result<()>
159159
where
160+
T: 'static,
160161
T: Send,
161162
U: foo::foo::conventions::Host + Send,
162163
{
@@ -232,6 +233,7 @@ pub mod foo {
232233
host_getter: G,
233234
) -> wasmtime::Result<()>
234235
where
236+
T: 'static,
235237
G: for<'a> wasmtime::component::GetHost<&'a mut T, Host: Host + Send>,
236238
T: Send,
237239
{
@@ -526,6 +528,7 @@ pub mod foo {
526528
get: impl Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
527529
) -> wasmtime::Result<()>
528530
where
531+
T: 'static,
529532
U: Host + Send,
530533
T: Send,
531534
{

0 commit comments

Comments
 (0)