Repository: bytecodealliance/wasm-tools (rust)

Issue:
Internal error involving task.return + stream type
The following wast:
```wast
(component
  (canon task.return (result (stream u8)) (core func))
)
```
fails with the following error:
```
internal error: entered unreachable code: should be expanded by now
```
This following wast succeeds, so the issue seems to be related to the `stream` in the type:
```wast
(component
  (canon task.return (result u8) (core func))
)
```


Pull request:
Fix expanding `task.return` types in text parsing
Closes #2188

Patch:
diff --git a/crates/wast/src/component/expand.rs b/crates/wast/src/component/expand.rs
index 0887fc42..3143c65d 100644
--- a/crates/wast/src/component/expand.rs
+++ b/crates/wast/src/component/expand.rs
@@ -264,11 +264,14 @@ impl<'a> Expander<'a> {
             CanonicalFuncKind::Lift { ty, .. } => {
                 self.expand_component_type_use(ty);
             }
-            CanonicalFuncKind::Core(_) => {}
+            CanonicalFuncKind::Core(func) => {
+                self.expand_core_func_kind(func);
+            }
         }
     }
 
-    fn expand_core_func(&mut self, func: CoreFunc<'a>) -> ComponentField<'a> {
+    fn expand_core_func(&mut self, mut func: CoreFunc<'a>) -> ComponentField<'a> {
+        self.expand_core_func_kind(&mut func.kind);
         match func.kind {
             CoreFuncKind::Alias(a) => ComponentField::Alias(Alias {
                 span: func.span,
@@ -289,6 +292,19 @@ impl<'a> Expander<'a> {
         }
     }
 
+    fn expand_core_func_kind(&mut self, func: &mut CoreFuncKind<'a>) {
+        match func {
+            CoreFuncKind::TaskReturn(f) => {
+                if let Some(ty) = &mut f.result {
+                    self.expand_component_val_ty(ty);
+                }
+            }
+
+            // Other core funcs don't need expansion at this time
+            _ => {}
+        }
+    }
+
     fn expand_func(&mut self, func: &mut Func<'a>) -> Option<ComponentField<'a>> {
         for name in func.exports.names.drain(..) {
             let id = gensym::fill(func.span, &mut func.id);


Test patch:
diff --git a/tests/cli/component-model-async/task-builtins.wast b/tests/cli/component-model-async/task-builtins.wast
index 1e0d3c5d..53998623 100644
--- a/tests/cli/component-model-async/task-builtins.wast
+++ b/tests/cli/component-model-async/task-builtins.wast
@@ -385,3 +385,7 @@
   (core func (canon context.set i32 0))
   (canon context.set i32 0 (core func))
 )
+
+(component
+  (canon task.return (result (stream u8)) (core func))
+)
diff --git a/tests/snapshots/cli/component-model-async/task-builtins.wast.json b/tests/snapshots/cli/component-model-async/task-builtins.wast.json
index 442ca64b..50464cc6 100644
--- a/tests/snapshots/cli/component-model-async/task-builtins.wast.json
+++ b/tests/snapshots/cli/component-model-async/task-builtins.wast.json
@@ -245,6 +245,12 @@
       "line": 318,
       "filename": "task-builtins.36.wasm",
       "module_type": "binary"
+    },
+    {
+      "type": "module",
+      "line": 389,
+      "filename": "task-builtins.37.wasm",
+      "module_type": "binary"
     }
   ]
 }
\ No newline at end of file
diff --git a/tests/snapshots/cli/component-model-async/task-builtins.wast/37.print b/tests/snapshots/cli/component-model-async/task-builtins.wast/37.print
new file mode 100644
index 00000000..28dfb3b0
--- /dev/null
+++ b/tests/snapshots/cli/component-model-async/task-builtins.wast/37.print
@@ -0,0 +1,4 @@
+(component
+  (type (;0;) (stream u8))
+  (core func (;0;) (canon task.return (result 0)))
+)