Skip to content

Add Task.was_cancelled() observation API #374

Closed

Description

@ehartford

Summary

Expose the spec'd Task.was_cancelled() API on the user-facing task surface. The runtime already records whether an awaited fiber completed by cancellation, but only MIR-internal cleanup paths can observe it. This gives users the cancellation observation API promised by §14.7 without changing cancellation into an error value.

Spec

§14.7 lists was_cancelled as (&Task[T]) -> bool: true if the task was cancelled before completing normally, and never suspends. §14.7 also says cancellation is a control transfer, not a Result/Err value; there is no TaskCancelled error type.

let t = long_running()
t.cancel()
let _ = t.await
assert(t.was_cancelled())

Current state

The state exists in the runtime. with_fiber_was_cancelled_return(fiber_id) checks the last awaited fiber and then falls back to completed-fiber state (rt/fiber_runtime.w:266). MIR lowering already emits MirIntrinsic.FIBER_WAS_CANCELLED_RETURN while handling cancellation cleanup (src/MirLower.w:4889, src/MirLower.w:4893).

The public sema surface only interns cancel and is_done (src/Sema.w:103, src/Sema.w:1428) and only recognizes those two methods on Task/ScopedTask (src/SemaCheck.w:10432). lib/std/task.w defines Task[T] and ScopedTask[T] but no ordinary method body for was_cancelled (lib/std/task.w:10, lib/std/task.w:18).

Implementation plan

  1. Add was_cancelled to SemaBuiltinSymbols in src/Sema.w next to cancel and is_done, then intern it in Sema.init_intrinsic_symbols.
  2. Extend the Task/ScopedTask method branch in Sema.check_method_call_parts (src/SemaCheck.w:10432) so .was_cancelled() is accepted on awaitable task values, takes zero arguments, and returns bool.
  3. Add a method intrinsic classification for Task.was_cancelled in src/MirLower.w, mapping it to MirIntrinsic.FIBER_WAS_CANCELLED_RETURN.
  4. In MIR lowering, extract the task's fiber_id field the same way the cancellation cleanup path does (src/MirLower.w:4886) and emit the intrinsic call into an i32/bool result.
  5. In codegen, reuse the existing with_fiber_was_cancelled_return runtime call used for the MIR intrinsic. If the intrinsic only exists in a cleanup-only path today, generalize that emission rather than duplicating runtime state.
  6. Keep the method non-suspending: do not route it through .await or scheduler operations.

Test plan

Add test/spec/spec_ss14_7_task_was_cancelled.w with:

  • cancel a running task, await/join cleanup, then assert t.was_cancelled();
  • a normally completed task returns false;
  • calling .was_cancelled() before completion returns false or the documented stable value without blocking.

Add test/compile_errors/err_task_was_cancelled_args.w with //! expect-error: task method expects zero arguments.

Add one no-suspend positive case showing no_suspend: t.was_cancelled() is accepted after #354's callable suspension model lands.

Acceptance criteria

  • Task[T].was_cancelled() and ScopedTask[T].was_cancelled() typecheck as zero-argument bool methods.
  • The method observes existing runtime cancellation-return state.
  • The method never suspends and is allowed in no_suspend.
  • Cancellation remains a control-transfer observation, not a Result error payload.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

None.

Activity

  1. ehartford commented on Jun 16, 2026

    @ehartford
    ContributorAuthor

    Implemented in f785b47. Added the public Task/ScopedTask was_cancelled() observation surface, lowered it to the existing with_fiber_was_cancelled_return runtime state, and kept it non-suspending/no_suspend-safe. While wiring the shared task observation path, also completed the missing is_done() MIR/codegen intrinsic so &Task[T] observation methods lower correctly instead of falling into generic-call codegen. Added positive spec coverage for normal, cancelled, scoped, pre-completion, and no_suspend observation, plus a negative zero-argument diagnostic. Verification passed: 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