Skip to content

Implement compiler-visible current-fiber suspension model #354

Closed

Description

@ehartford

Summary

Implement may_suspend as compiler-visible current-fiber effect metadata, not as a syntactic .await search. The compiler already rejects several direct suspension forms, but indirect callable calls and some runtime-yielding primitives are not represented in callable type information. This closes the safety gap for no_suspend, @[no_await_guard], FFI callbacks, and no-runtime builds without adding user-facing call-site ceremony.

Spec

§14.3 Invariant 5 says the compiler computes a may_suspend property for every function and callable type information. A function is may_suspend if it directly performs current-fiber suspension or makes a same-fiber call through a callable whose type is may_suspend; calling an async fn alone is a fiber-creation firewall and does not suspend the caller.

§14.3.1 requires no_suspend blocks to reject direct .await, collection/select await, same-fiber calls through may_suspend callables, structured-concurrency joins, implicit cleanup await, and direct fiber-aware runtime operations. §14.17 adds that fiber-aware synchronization participates in this analysis.

async fn f -> i32: 1
let cb = () => f().await

no_suspend:
    cb()        // error: same-fiber callable may suspend

no_suspend:
    f()         // ok: creates Task, does not await

Current state

Sema.expr_may_suspend is an AST walk and direct-function recursion (src/SemaCheck.w:2058, src/SemaCheck.w:2072). It recognizes NK_AWAIT, NK_SELECT_AWAIT, and NK_ASYNC_SCOPE, and treats NK_ASYNC_BLOCK/NK_SPAWN as non-suspending (src/SemaCheck.w:2076, src/SemaCheck.w:2080). It does not store the result on TY_FN/TY_EXTERN_FN callable types.

Sema.check_callable_value_call validates argument and capture effects for callable values but has no may_suspend check on the callable type (src/SemaCheck.w:7699, src/SemaCheck.w:7728). MIR has a stronger pass, but suspend_is_scheduler_yield_intrinsic only recognizes await/select/scope-await intrinsics (src/MirSuspendCheck.w:221), and suspend_callee_sym only extracts direct constant function callees (src/MirSuspendCheck.w:234). That leaves arbitrary closure/function-pointer values outside the closed effect model.

Channel send/recv already block by yielding or running scheduler steps when full/empty (rt/channel_runtime.w:88, rt/channel_runtime.w:121, rt/channel_runtime.w:148), but those intrinsics are not in the MIR suspension set. This is the root cause of examples like no_suspend: tx.send(1) being accepted even though a bounded channel can yield the current fiber.

Implementation plan

  1. Extend callable type storage in src/Sema.w / type construction helpers so TY_FN, closure function types, function pointers, and future dyn-callable surfaces carry a may_suspend bit. Keep the bit internal; users do not annotate it.
  2. When resolving function declarations, closures, and callable values in src/SemaCheck.w, compute may_suspend from body analysis and store it on the callable type. Preserve the existing firewall: async function calls produce Task[T] and only the body/awaiting callable is may_suspend.
  3. Update check_callable_value_call (src/SemaCheck.w:7699) to consult the callable type bit and feed the existing no_suspend/@[no_await_guard] diagnostics. The diagnostic should name the callable value when possible, e.g. same-fiber callable may suspend inside no_suspend block.
  4. Replace direct AST-only checks for FFI callback and OS-thread callback safety (src/SemaCheck.w:6995, src/SemaCheck.w:8060) with the callable bit so indirect callbacks are covered.
  5. Extend suspend_is_scheduler_yield_intrinsic in src/MirSuspendCheck.w:221 to include CHAN_SEND, CHAN_RECV, future lock-acquire intrinsics from Real generic Mutex[T]/RwLock[T] with fiber-aware blocking (§14.17) #471, timer/sleep, and any I/O primitive that is modeled as a direct current-fiber wait rather than Task-returning.
  6. Teach MIR call analysis to recognize callable-value calls whose lowered callee has a may_suspend type, not just CK_FN constants. Keep diagnostics anchored to the user call site via body.call_ast_node.
  7. Add a small design note or comments at the primitive-set helper explaining that this list is closed and must be updated when a new fiber-yielding intrinsic is introduced.

Test plan

Add test/compile_errors/err_no_suspend_may_suspend_callable_value.w for let cb = () => task.await; no_suspend: cb() with //! expect-error: E0702.

Add test/compile_errors/err_no_await_guard_callable_value.w with a live @[no_await_guard] guard and an indirect callable that awaits.

Add test/compile_errors/err_ffi_callback_callable_may_suspend.w for passing a callable value that may suspend to an extern "C" fn callback parameter.

Add test/compile_errors/err_no_suspend_channel_send_recv.w with bounded send and empty recv inside no_suspend.

Extend test/spec/spec_ss14_3_async_calling_is_unrestricted.w with positive coverage that calling an async fn without awaiting remains allowed in no_suspend.

Acceptance criteria

  • Callable type information carries a compiler-only may_suspend bit.
  • Same-fiber indirect calls through may_suspend callable values are rejected in no_suspend, @[no_await_guard], FFI callback, and no-runtime contexts.
  • Async task creation remains allowed where only suspension is forbidden.
  • The MIR primitive suspension set includes all direct runtime operations that can yield the current fiber.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Overlaps #419 for channel send/recv as suspension primitives and #471 for lock-acquire primitives. Future dyn-callable coverage may depend on the generics/type identity work in #391.

Activity

  1. ehartford commented on Jun 16, 2026

    @ehartford
    ContributorAuthor

    Implemented in 47d45bf. The checker now follows closure-backed callable values in may-suspend analysis, rejects indirect same-fiber suspension in no_suspend and live no_await_guard contexts, applies the same check for extern C callback callable values, and treats channel send/recv as current-fiber yielding operations in both sema no_suspend checks and MIR suspension classification. Added positive and negative coverage for callable values, no_await_guard, extern callbacks, channel send/recv in no_suspend, and task creation without suspension. Verification passed after rebasing onto current main: 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