scylladb/seastar

Clang introduced [[clang::coro_lifetimebound]] annotation for lifetime analysis in coroutines

usx95 opened this issue · 1 comments

Hello folks,

This is more of an FYI that we introduced the [[clang::coro_lifetimebound]] annotation in clang for lifetime bound analysis of reference parameters of a coroutine. (docs. RFC)

Example lifetime findings:

template <typename T> struct [[clang::coro_return_type, clang::coro_lifetimebound]] Task {
  using promise_type = some_promise_type;
};

Task<int> coro(const int& a) { co_return a + 1; }
Task<int> [[clang::coro_wrapper]] coro_wrapper(const int& a, const int& b) {
  return a > b ? coro(a) : coro(b);
}
Task<int> temporary_reference() {
  auto foo = coro(1); // warning: capturing reference to a temporary that would die after the expression.

  int a = 1;
  auto bar = coro_wrapper(a, 0); // warning: `b` captures a reference to a temporary.

  co_return co_await coro(1); // fine.
}
[[clang::coro_wrapper]] Task<int> stack_reference(int a) {
  return coro(a); // warning: returning address of stack variable `a`.
}

Feel free to try it out. I am happy to incorporate any feedback and answer any questions.

Thanks. We have an ongoing discussion (you were mentioned) here: https://groups.google.com/g/seastar-dev/c/CpUOtC2NJLM