Repository navigation
Give co_await the value category of await_resume(). [GH #447] #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f0d886c
fe59fc9
0c66807
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [GH #447] | ||
| Value category of co_await and co_yield expressions | ||
|
|
||
| A co_await or co_yield expression was always treated as a prvalue, even | ||
| when the await_resume call returns a reference. It now has the value | ||
| category of the await_resume call, so code like this is accepted: | ||
|
|
||
| struct bool_ref_awaiter { | ||
| bool await_ready() noexcept; | ||
| void await_suspend(std::coroutine_handle<>) noexcept; | ||
| bool &await_resume() noexcept; | ||
| }; | ||
| task f() { | ||
| (co_await bool_ref_awaiter{}) = false; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making bool b;
struct bool_ref_awaiter {
constexpr bool await_ready() noexcept { return true; }
void await_suspend(std::coroutine_handle<>) noexcept;
constexpr bool &await_resume() noexcept { return b; }
};
task f() {
(co_await bool_ref_awaiter{}) = false;
}
Seems like there are more fixes needed in that area.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mohitmishra786 ideally, we should address this one as well in this PR. Although we could defer it to a separate PR for later, it would be good to have a rough understanding what it would involve, or if it could be a quick fix.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in a78bd3f, in this PR rather than as a follow-up. The crash itself isn't new: on I added your case to |
||
|
|
||
| Also, an internal error in lowering is fixed for a co_await whose | ||
| await_ready and await_resume are both constexpr when await_resume returns a | ||
| reference (both calls are then folded, and the awaiter was wrongly cloned | ||
| for the await_suspend call). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++23 Test_name.c |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| //type:fp | ||
| //options:--c++23 | ||
| //remark:[GH #447] co_await has the value category of await_resume() | ||
| // | ||
| // A co_await or co_yield expression was always treated as a prvalue, so | ||
| // assigning to the result of an await_resume() that returns an lvalue | ||
| // reference was rejected. Also covers awaiters whose await_ready and | ||
| // await_resume calls are folded. | ||
|
|
||
| namespace std { | ||
| template <typename = void> struct coroutine_handle; | ||
| template <> struct coroutine_handle<> { void *address(); }; | ||
| template <typename> struct coroutine_handle { | ||
| static coroutine_handle from_address(void *); | ||
| operator coroutine_handle<>() noexcept; | ||
| }; | ||
| template <typename R, typename...> struct coroutine_traits { | ||
| using promise_type = typename R::promise_type; | ||
| }; | ||
| template <class T, class U> struct is_same { static constexpr bool value = false; }; | ||
| template <class T> struct is_same<T, T> { static constexpr bool value = true; }; | ||
| } // namespace std | ||
|
|
||
| struct suspend { | ||
| bool await_ready() noexcept; | ||
| void await_suspend(std::coroutine_handle<>) noexcept; | ||
| void await_resume() noexcept; | ||
| }; | ||
|
|
||
| template <class R> struct awaiter { | ||
| bool await_ready() noexcept; | ||
| void await_suspend(std::coroutine_handle<>) noexcept; | ||
| R await_resume() noexcept; | ||
| }; | ||
|
|
||
| struct task; | ||
|
|
||
| struct promise { | ||
| suspend initial_suspend() noexcept; | ||
| suspend final_suspend() noexcept; | ||
| void return_void(); | ||
| void unhandled_exception(); | ||
| task get_return_object(); | ||
| awaiter<int &> yield_value(int); | ||
| }; | ||
|
|
||
| struct task { | ||
| using promise_type = promise; | ||
| }; | ||
|
|
||
| task f() { | ||
| (co_await awaiter<bool &>{}) = false; | ||
| bool *p = &co_await awaiter<bool &>{}; | ||
| decltype(auto) a = co_await awaiter<bool &>{}; | ||
| decltype(auto) b = co_await awaiter<bool &&>{}; | ||
| decltype(auto) c = co_await awaiter<bool>{}; | ||
| static_assert(std::is_same<decltype(a), bool &>::value); | ||
| static_assert(std::is_same<decltype(b), bool &&>::value); | ||
| static_assert(std::is_same<decltype(c), bool>::value); | ||
| int &r = co_yield 1; | ||
| decltype(auto) d = co_yield 1; | ||
| static_assert(std::is_same<decltype(d), int &>::value); | ||
| } | ||
|
|
||
| // When await_ready and await_resume are both constexpr and await_resume | ||
| // returns a reference, both calls are folded. The awaiter must then not be | ||
| // cloned for await_suspend (lowering used to fail with an internal error). | ||
| bool flag; | ||
| template <bool Ready> struct constexpr_ref_awaiter { | ||
| constexpr bool await_ready() noexcept { return Ready; } | ||
| void await_suspend(std::coroutine_handle<>) noexcept; | ||
| constexpr bool &await_resume() noexcept { return flag; } | ||
| }; | ||
|
|
||
| task g() { | ||
| (co_await constexpr_ref_awaiter<true>{}) = false; | ||
| (co_await constexpr_ref_awaiter<false>{}) = false; | ||
| bool x = co_await constexpr_ref_awaiter<true>{}; | ||
| (void)x; | ||
| } | ||
|
|
||
| // A reference to a function gives an lvalue, even for an rvalue reference. | ||
| void fn(); | ||
| task h() { | ||
| void (&l)() = co_await awaiter<void (&)()>{}; | ||
| void (&r)() = co_await awaiter<void (&&)()>{}; | ||
| decltype(auto) d = co_await awaiter<void (&&)()>{}; | ||
| static_assert(std::is_same<decltype(d), void (&)()>::value); | ||
| l(); | ||
| r(); | ||
| d(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| cpfe -DTEST_NUMBER=1 --set_flag coroutines --no_ms_perm --microsoft_v 1923 Test_name.c | ||
| "Test_name.c", line 51: error: initial value of reference to non-const must be an lvalue | ||
| Immovable &r1 = co_await Aw{}; | ||
| ^ | ||
|
|
||
| 1 error detected in the compilation of "Test_name.c". | ||
| eccp: diagnostics generated from compilation of Test_name.int.c: | ||
| Test_name.c: In function '_Z1fv': | ||
| Test_name.c:51:14: error: 'co_await__T9' undeclared (first use in this function) | ||
| Test_name.c:51:14: note: each undeclared identifier is reported only once for each function it appears in | ||
| Test_name.c:51:172: error: '__T18' undeclared (first use in this function); did you mean '__T17'? | ||
| eccp: end of diagnostics from compilation of Test_name.int.c | ||
| eccp: gcc compilation of Test_name.int.c returned an exit status of 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve the reference category when a folded
await_resume()names a variable.When
await_resume()is constexpr and returns a reference to a variable, folding can replace the call with a plain lvalue of the variable's type.strip_ref_indirectthen returns a node that is not reference-typed. The check at line 49267 fails, so theco_awaitexpression stays a prvalue. An assignment such as(co_await awaiter{}) = 1can then be rejected. The non-folded form and the folded non-variable form both produce an lvalue.The co_await result must take the category of
await_resume(): an lvalue forT&and an xvalue forT&&, in this folded case as well. The inspected code does not establish which representation should carry that category. Choose the representation that matches the existing lvalue and xvalue handling.