E4227

E4227#

Compiler diagnostic name: cancel_not_allowed_in_context.

A cancellable async function is called from an async context declared with nocancel.

nocancel promises that cancellation cannot be introduced by the function. Such a function may call synchronous functions and other nocancel async functions, but not cancellable async functions.

Erroneous example#

///|
async fn cancellable_operation() -> Unit {
  ()
}

///|
pub async fn run_without_cancellation() -> Unit nocancel {
  cancellable_operation()
}

Suggestion#

Remove nocancel from the caller when it needs to perform a cancellable operation. If the callee genuinely cannot be cancelled, declare the callee with nocancel as well.

///|
async fn noncancellable_operation() -> Unit nocancel {
  ()
}

///|
pub async fn run_without_cancellation() -> Unit nocancel {
  noncancellable_operation()
}