llvm / llvm/llvm-project

[coroutines] Temporaries are not preserved in the symmetric `await-suspend` case

Open
#189,400 1 comment 0 reactions 0 assignees View on GitHub
coroutines
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/3cx1bd111

In this example, `await-suspend` is used in the form `await-suspend.resume()`, however the temporaries created during the evaluation of `await-suspend` are destroyed prior to the call to `resume()`, rather than at the end of the full-expression.

```cpp
#include
#include

struct Tag {
struct promise_type {
Tag get_return_object() { return {}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
};

struct S {
~S() { std::cout << "~S()" << std::endl; }
};

struct Awaiter {
auto await_ready() { return false; }
auto await_suspend(auto h, S && = {}) {
std::cout << "suspend" << std::endl;
return h;
}
auto await_resume() {
std::cout << "resume" << std::endl;
}
};

auto await_suspend(auto, S && = {}) {
struct Checker {
auto resume() {
std::cout << "resume" << std::endl;
}
};

std::cout << "suspend" << std::endl;
return Checker{};
}

Tag Coroutine() {
std::cout << "[co_await]" << std::endl;
co_await Awaiter{};

std::cout << "\n[check]" << std::endl;
await_suspend(nullptr).resume();
}

int main() {
Coroutine();
}
```

Output:

```cpp
[co_await]
suspend
~S()
resume

[check]
suspend
resume
~S()
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the coroutine behavior using the Godbolt example at https://godbolt.org/z/3cx1bd111, then trace the compiler's handling of the symmetric await-suspend expression and its temporary lifetime. Done means the temporary is destroyed only at the end of the full-expression, with the output ordering matching the issue's expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.