Repository navigation
Description
Summary
Make async trait methods work through dyn Trait dispatch. The language model says async methods are ordinary trait methods returning Task[T]; dynamic dispatch should therefore return a Task[T] handle that can be awaited.
Spec
§11.5 says async fn fetch(...) -> T is equivalent to fn fetch(...) -> Task[T]. Trait objects with async methods should work because Task[T] is a concrete fixed-size handle and the vtable entry returns it like any other concrete return value.
Current state
Dynamic dispatch exists: MirBuilder.lower_method_call detects dyn receivers and emits MirIntrinsic.DYN_CALL at src/MirLower.w:6155-6174; codegen lowers that through a vtable slot in Codegen.mir_emit_dyn_trait_call at src/CodegenDispatch.w:4427-4505. Trait metadata stores method flags, params, and return nodes (src/SemaDecl.w:1419-1424, src/CodegenTraits.w:48-70).
The requirements campaign verified the broken user symptom: calling an async method through &dyn Trait fails with “await requires a Task value.” The likely gap is that sema resolves the dyn method return from the raw trait return node instead of the async-desugared Task[T] type. Sema.collect_trait_decl records async method flags and requires async runtime at src/SemaDecl.w:1461-1462, but Sema.check_dyn_trait_method_call resolves info.ret_node directly at src/SemaCheck.w:10144-10153. Codegen also builds dyn method function types from raw return nodes in Codegen.dyn_trait_method_fn_type at src/CodegenTraits.w:160-172.
Implementation plan
- Normalize async trait method signatures at sema collection time: store return type as
Task[T]when the method flag includesFnFlags.ASYNC. - Update
Sema.find_dyn_trait_method_info/check_dyn_trait_method_callso dyn calls to async trait methods type asTask[T], not the innerT. - Update
Codegen.dyn_trait_method_fn_typeand wrapper generation so vtable entries use the actual lowered async function ABI returningTask[T]. - Verify wrapper generation in
Codegen.create_dyn_wrapperhandles the async method's real function symbol and does not accidentally await or unwrap the task. - Keep object-safety rules unchanged: §11.5 says async adds no extra dyn restrictions beyond concrete return type.
Test plan
Add test/spec/spec_ss11_5_dyn_async_trait_methods.w:
- trait with
async fn fetch(self: &Self, id: i32) -> i32 - concrete impl
let svc: &dyn Trait = &impllet task = svc.fetch(42)andtask.await == expected
Add a compile-error test proving a non-Task value still fails .await, so this does not weaken await checking.
Acceptance criteria
- Dyn calls to async trait methods type-check as
Task[T]. - The returned task can be awaited and produces the concrete impl's value.
- Vtable wrappers for async and sync methods coexist in the same trait.
- No boxing/future trait/GAT machinery is introduced.
with build && with build :fixpoint && with build :testpass.
Dependencies
#432 overlaps object-safety/vtable filtering. #480 may later enforce Task sendability but does not block same-fiber dyn await.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 11.5.1.1, 11.5.1.6, 11.5.1.9.
Activity
ehartford commentedon Jun 19, 2026
Implemented in 0fcce79. Dyn trait method lookup now carries async method flags, dyn call typing lowers async returns to Task[T], and async dyn vtable wrappers spawn the concrete async body and return the Task handle without awaiting. Added spec coverage in test/spec/spec_ss11_5_dyn_async_trait_methods.w and retained the existing non-Task await negative test. Verification passed with project seed/release commands: build, fixpoint, full test suite, and explicit test-green.