E4227

E4227#

编译器诊断名称:cancel_not_allowed_in_context

在声明为 nocancel 的异步上下文中调用了可取消的异步函数。

nocancel 承诺该函数不会引入取消。这样的函数可以调用同步函数和其他 nocancel 异步函数,但不能调用可取消的异步函数。

错误示例#

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

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

建议#

如果调用方需要执行可取消操作,请移除其 nocancel。如果被调用方确实不会被取消,也请将它声明为 nocancel

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

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