Repository navigation
Description
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
- Add
was_cancelledtoSemaBuiltinSymbolsinsrc/Sema.wnext tocancelandis_done, then intern it inSema.init_intrinsic_symbols. - 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 returnsbool. - Add a method intrinsic classification for
Task.was_cancelledinsrc/MirLower.w, mapping it toMirIntrinsic.FIBER_WAS_CANCELLED_RETURN. - In MIR lowering, extract the task's
fiber_idfield the same way the cancellation cleanup path does (src/MirLower.w:4886) and emit the intrinsic call into ani32/boolresult. - In codegen, reuse the existing
with_fiber_was_cancelled_returnruntime 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. - Keep the method non-suspending: do not route it through
.awaitor 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 returnsfalseor 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()andScopedTask[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
Resulterror payload. with build && with build :fixpoint && with build :testpass.
Dependencies
None.
Activity
ehartford commentedon Jun 16, 2026
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.