Skip to content

Commit 9449a9e

Browse files
committed
Require async types when lift/lower uses async
This is an adjustment to the validation of component-model-async functions and types to correspond to WebAssembly/component-model#646. Specifically when using the `async` option to lift or lower a function it's now required that the component model type is additionally `async`. This means, for example, that `canon lower` of a sync-typed function cannot use the `async` ABI option. Additionally `canon lift` cannot use `async` if the destination component function type is synchronous. This notably required a number of updates throughout tests. My personal judgement on the required updates here is that everything was originally written without `async` function types since that didn't exist, then most of these weren't updated, but should have been, once `async` was added as a function type. In that sense I don't feel these are reflective of real-world breakage, but instead I think it's reflective of the history of the implementation's development.
1 parent 1a2b915 commit 9449a9e

39 files changed

Lines changed: 213 additions & 139 deletions

File tree

crates/wasmparser/src/validator/component.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,18 @@ impl CanonicalOptions {
395395
Ok(self)
396396
}
397397

398+
fn check_asyncness(&self, ty: &ComponentFuncType, offset: usize) -> Result<()> {
399+
// The `async` canonical ABI option is only allowed with `async`-typed
400+
// functions.
401+
if self.concurrency.is_async() && !ty.async_ {
402+
bail!(
403+
offset,
404+
"the `async` canonical option requires an async function type",
405+
);
406+
}
407+
Ok(())
408+
}
409+
398410
pub(crate) fn check_core_type(
399411
&self,
400412
types: &mut TypeAlloc,
@@ -1303,6 +1315,7 @@ impl ComponentState {
13031315
// export signature
13041316
let mut options = self.check_options(types, options, offset)?;
13051317
options.check_lift(types, self, core_ty_id, offset)?;
1318+
options.check_asyncness(ty, offset)?;
13061319
let func_ty = ty.lower(types, &options, Abi::Lift, offset)?;
13071320
let lowered_core_ty_id = func_ty.intern(types, offset);
13081321

@@ -1359,6 +1372,8 @@ impl ComponentState {
13591372
// the expected canonical ABI import signature.
13601373
let options = self.check_options(types, options, offset)?;
13611374
options.check_lower(offset)?;
1375+
options.check_asyncness(ty, offset)?;
1376+
13621377
let func_ty = ty.lower(types, &options, Abi::Lower, offset)?;
13631378
let ty_id = func_ty.intern(types, offset);
13641379

crates/wit-component/src/dummy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ fn push_imported_func(
9090
func: &Function,
9191
mangling: ManglingAndAbi,
9292
) {
93+
let mangling = mangling.for_func(func);
9394
let sig = resolve.wasm_signature(mangling.import_variant(), func);
9495

9596
let (module, name) = resolve.wasm_import_name(mangling, WasmImport::Func { interface, func });
@@ -352,6 +353,7 @@ fn push_func_export(
352353
func: &Function,
353354
mangling: ManglingAndAbi,
354355
) {
356+
let mangling = mangling.for_func(func);
355357
let sig = resolve.wasm_signature(mangling.export_variant(), func);
356358
let name = resolve.wasm_export_name(
357359
mangling,

crates/wit-component/tests/components/async-builtins/component.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,17 @@
235235
(with "" (instance $fixup-args))
236236
)
237237
)
238-
(type (;0;) (func (param "s" string) (result string)))
238+
(type (;0;) (func async (param "s" string) (result string)))
239239
(alias core export $main "[async-lift-stackful]foo" (core func $"[async-lift-stackful]foo" (;33;)))
240240
(func $foo (;0;) (type 0) (canon lift (core func $"[async-lift-stackful]foo") (memory $memory) (realloc $realloc) string-encoding=utf8 async))
241241
(export $"#func1 foo" (@name "foo") (;1;) "foo" (func $foo))
242-
(type (;1;) (func (param "s" string) (result string)))
242+
(type (;1;) (func async (param "s" string) (result string)))
243243
(alias core export $main "[async-lift-stackful]foo:foo/bar#foo" (core func $"[async-lift-stackful]foo:foo/bar#foo" (;34;)))
244244
(func $"#func2 foo" (@name "foo") (;2;) (type 1) (canon lift (core func $"[async-lift-stackful]foo:foo/bar#foo") (memory $memory) (realloc $realloc) string-encoding=utf8 async))
245245
(component $foo:foo/bar-shim-component (;0;)
246-
(type (;0;) (func (param "s" string) (result string)))
246+
(type (;0;) (func async (param "s" string) (result string)))
247247
(import "import-func-foo" (func (;0;) (type 0)))
248-
(type (;1;) (func (param "s" string) (result string)))
248+
(type (;1;) (func async (param "s" string) (result string)))
249249
(export (;1;) "foo" (func 0) (func (type 1)))
250250
)
251251
(instance $foo:foo/bar-shim-instance (;0;) (instantiate $foo:foo/bar-shim-component
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package root:component;
22

33
world root {
4-
export foo: func(s: string) -> string;
4+
export foo: async func(s: string) -> string;
55
export foo:foo/bar;
66
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package foo:foo;
22

33
interface bar {
4-
foo: func(s: string) -> string;
4+
foo: async func(s: string) -> string;
55
}
66

77
world module {
88
export bar;
9-
export foo: func(s: string) -> string;
9+
export foo: async func(s: string) -> string;
1010
}

crates/wit-component/tests/components/async-export-with-callback/component.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@
3232
)
3333
(core instance $main (;0;) (instantiate $main))
3434
(alias core export $main "memory" (core memory $memory (;0;)))
35-
(type (;0;) (func (param "s" string) (result string)))
35+
(type (;0;) (func async (param "s" string) (result string)))
3636
(alias core export $main "[async-lift]foo" (core func $"[async-lift]foo" (;0;)))
3737
(alias core export $main "cabi_realloc" (core func $cabi_realloc (;1;)))
3838
(alias core export $main "[callback][async-lift]foo" (core func $"[callback][async-lift]foo" (;2;)))
3939
(func $foo (;0;) (type 0) (canon lift (core func $"[async-lift]foo") (memory $memory) (realloc $cabi_realloc) string-encoding=utf8 async (callback $"[callback][async-lift]foo")))
4040
(export $"#func1 foo" (@name "foo") (;1;) "foo" (func $foo))
41-
(type (;1;) (func (param "s" string) (result string)))
41+
(type (;1;) (func async (param "s" string) (result string)))
4242
(alias core export $main "[async-lift]foo:foo/bar#foo" (core func $"[async-lift]foo:foo/bar#foo" (;3;)))
4343
(alias core export $main "[callback][async-lift]foo:foo/bar#foo" (core func $"[callback][async-lift]foo:foo/bar#foo" (;4;)))
4444
(func $"#func2 foo" (@name "foo") (;2;) (type 1) (canon lift (core func $"[async-lift]foo:foo/bar#foo") (memory $memory) (realloc $cabi_realloc) string-encoding=utf8 async (callback $"[callback][async-lift]foo:foo/bar#foo")))
4545
(component $foo:foo/bar-shim-component (;0;)
46-
(type (;0;) (func (param "s" string) (result string)))
46+
(type (;0;) (func async (param "s" string) (result string)))
4747
(import "import-func-foo" (func (;0;) (type 0)))
48-
(type (;1;) (func (param "s" string) (result string)))
48+
(type (;1;) (func async (param "s" string) (result string)))
4949
(export (;1;) "foo" (func 0) (func (type 1)))
5050
)
5151
(instance $foo:foo/bar-shim-instance (;0;) (instantiate $foo:foo/bar-shim-component
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package root:component;
22

33
world root {
4-
export foo: func(s: string) -> string;
4+
export foo: async func(s: string) -> string;
55
export foo:foo/bar;
66
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package foo:foo;
22

33
interface bar {
4-
foo: func(s: string) -> string;
4+
foo: async func(s: string) -> string;
55
}
66

77
world module {
88
export bar;
9-
export foo: func(s: string) -> string;
9+
export foo: async func(s: string) -> string;
1010
}

crates/wit-component/tests/components/async-export/component.wat

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,44 @@
3535
(type (;2;) (record (field "a1" 1) (field "a2" 1) (field "a3" 1) (field "a4" 1)))
3636
(core instance $main (;0;) (instantiate $main))
3737
(alias core export $main "memory" (core memory $memory (;0;)))
38-
(type (;3;) (func (param "s" string) (result string)))
38+
(type (;3;) (func async (param "s" string) (result string)))
3939
(alias core export $main "[async-lift-stackful]foo" (core func $"[async-lift-stackful]foo" (;0;)))
4040
(alias core export $main "cabi_realloc" (core func $cabi_realloc (;1;)))
4141
(func $foo (;0;) (type 3) (canon lift (core func $"[async-lift-stackful]foo") (memory $memory) (realloc $cabi_realloc) string-encoding=utf8 async))
4242
(export $"#func1 foo" (@name "foo") (;1;) "foo" (func $foo))
43-
(type (;4;) (func (param "s" string) (result string)))
43+
(type (;4;) (func async (param "s" string) (result string)))
4444
(alias core export $main "[async-lift-stackful]foo:foo/bar#foo" (core func $"[async-lift-stackful]foo:foo/bar#foo" (;2;)))
4545
(func $"#func2 foo" (@name "foo") (;2;) (type 4) (canon lift (core func $"[async-lift-stackful]foo:foo/bar#foo") (memory $memory) (realloc $cabi_realloc) string-encoding=utf8 async))
46-
(type (;5;) (func (result string)))
46+
(type (;5;) (func async (result string)))
4747
(alias core export $main "[async-lift-stackful]foo:foo/bar#get-string" (core func $"[async-lift-stackful]foo:foo/bar#get-string" (;3;)))
4848
(func $get-string (;3;) (type 5) (canon lift (core func $"[async-lift-stackful]foo:foo/bar#get-string") (memory $memory) string-encoding=utf8 async))
49-
(type (;6;) (func (result 2)))
49+
(type (;6;) (func async (result 2)))
5050
(alias core export $main "[async-lift-stackful]foo:foo/bar#get-big" (core func $"[async-lift-stackful]foo:foo/bar#get-big" (;4;)))
5151
(func $get-big (;4;) (type 6) (canon lift (core func $"[async-lift-stackful]foo:foo/bar#get-big") (memory $memory) async))
5252
(component $foo:foo/bar-shim-component (;0;)
53-
(type (;0;) (func (param "s" string) (result string)))
53+
(type (;0;) (func async (param "s" string) (result string)))
5454
(import "import-func-foo" (func (;0;) (type 0)))
55-
(type (;1;) (func (result string)))
55+
(type (;1;) (func async (result string)))
5656
(import "import-func-get-string" (func (;1;) (type 1)))
5757
(type (;2;) (record (field "a1" u8) (field "a2" u8) (field "a3" u8) (field "a4" u8)))
5858
(import "import-type-big3" (type (;3;) (eq 2)))
5959
(type (;4;) (record (field "a1" 3) (field "a2" 3) (field "a3" 3) (field "a4" 3)))
6060
(import "import-type-big2" (type (;5;) (eq 4)))
6161
(type (;6;) (record (field "a1" 5) (field "a2" 5) (field "a3" 5) (field "a4" 5)))
6262
(import "import-type-big" (type (;7;) (eq 6)))
63-
(type (;8;) (func (result 7)))
63+
(type (;8;) (func async (result 7)))
6464
(import "import-func-get-big" (func (;2;) (type 8)))
6565
(type (;9;) (record (field "a1" u8) (field "a2" u8) (field "a3" u8) (field "a4" u8)))
6666
(export (;10;) "big3" (type 9))
6767
(type (;11;) (record (field "a1" 10) (field "a2" 10) (field "a3" 10) (field "a4" 10)))
6868
(export (;12;) "big2" (type 11))
6969
(type (;13;) (record (field "a1" 12) (field "a2" 12) (field "a3" 12) (field "a4" 12)))
7070
(export (;14;) "big" (type 13))
71-
(type (;15;) (func (param "s" string) (result string)))
71+
(type (;15;) (func async (param "s" string) (result string)))
7272
(export (;3;) "foo" (func 0) (func (type 15)))
73-
(type (;16;) (func (result string)))
73+
(type (;16;) (func async (result string)))
7474
(export (;4;) "get-string" (func 1) (func (type 16)))
75-
(type (;17;) (func (result 14)))
75+
(type (;17;) (func async (result 14)))
7676
(export (;5;) "get-big" (func 2) (func (type 17)))
7777
)
7878
(instance $foo:foo/bar-shim-instance (;0;) (instantiate $foo:foo/bar-shim-component
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package root:component;
22

33
world root {
4-
export foo: func(s: string) -> string;
4+
export foo: async func(s: string) -> string;
55
export foo:foo/bar;
66
}

0 commit comments

Comments
 (0)