@@ -11,8 +11,7 @@ use wit_bindgen_core::{
1111 AsyncFilterSet , Direction , Files , InterfaceGenerator as CoreInterfaceGenerator , Ns , Source ,
1212 WorldGenerator ,
1313 abi:: { self , AbiVariant , Bindgen , Bitcast , Instruction , LiftLower , WasmType } ,
14- dealias,
15- uwrite, uwriteln,
14+ dealias, uwrite, uwriteln,
1615 wit_parser:: {
1716 Alignment , ArchitectureSize , Docs , Enum , Flags , FlagsRepr , Function , Handle , Int ,
1817 InterfaceId , LiftLowerAbi , Mangling , ManglingAndAbi , Param , Record , Resolve ,
@@ -858,6 +857,9 @@ impl InterfaceGenerator<'_> {
858857 #doc(hidden)
859858 pub fn {func_name}({params}) -> {result_type} {{
860859 {async_pkg}with_waitableset(async fn() {{
860+ // Intentionally run export body in a task-group child task.
861+ // MoonBit's structured concurrency model uses the task group
862+ // as an umbrella for async work started by the export.
861863 {async_pkg}with_task_group(async fn(task_group) {{
862864 {cleanup_list}
863865 {src}
@@ -1076,7 +1078,12 @@ impl InterfaceGenerator<'_> {
10761078 } )
10771079 . collect :: < Vec < _ > > ( ) ;
10781080
1079- // For async exports, add taskgroup parameter (always Unit type)
1081+ // For async exports, add a task-group parameter.
1082+ //
1083+ // This is intentionally `TaskGroup[Unit]` even when the function result
1084+ // type is not `Unit`: this task group models the umbrella lifetime for
1085+ // export-side structured concurrency and cancellation, rather than the
1086+ // direct return payload type of the exported function.
10801087 if async_ && matches ! ( direction, Direction :: Export ) {
10811088 params. push ( "task_group : @async.TaskGroup[Unit]" . to_string ( ) ) ;
10821089 }
@@ -2945,11 +2952,11 @@ impl Bindgen for FunctionBindgen<'_, '_> {
29452952 match ty {
29462953 Type :: Id ( id) => match & self . interface_gen . resolve . types [ * id] . kind {
29472954 TypeDefKind :: Handle ( Handle :: Own ( _) ) => {
2948- let constructor =
2949- self . interface_gen . world_gen . pkg_resolver . type_constructor (
2950- self . interface_gen . name ,
2951- ty ,
2952- ) ;
2955+ let constructor = self
2956+ . interface_gen
2957+ . world_gen
2958+ . pkg_resolver
2959+ . type_constructor ( self . interface_gen . name , ty ) ;
29532960 uwriteln ! ( self . src, "let _ = {constructor}::drop({op});" ) ;
29542961 }
29552962 TypeDefKind :: Future ( _) | TypeDefKind :: Stream ( _) => {
0 commit comments