Skip to content

Async trait methods through dyn dispatch (§11.5) #431

Closed

Description

@ehartford

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

  1. Normalize async trait method signatures at sema collection time: store return type as Task[T] when the method flag includes FnFlags.ASYNC.
  2. Update Sema.find_dyn_trait_method_info/check_dyn_trait_method_call so dyn calls to async trait methods type as Task[T], not the inner T.
  3. Update Codegen.dyn_trait_method_fn_type and wrapper generation so vtable entries use the actual lowered async function ABI returning Task[T].
  4. Verify wrapper generation in Codegen.create_dyn_wrapper handles the async method's real function symbol and does not accidentally await or unwrap the task.
  5. 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 = &impl
  • let task = svc.fetch(42) and task.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 :test pass.

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

  1. ehartford commented on Jun 19, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions