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
Open
mohitmishra786
wants to merge
2
commits into
edgcpp:main
Choose a base branch
from
mohitmishra786:gh-447-co-await-lvalue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| [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; | ||
| } | ||
|
|
||
| Also, the C++-generating back end now puts parentheses around a co_await | ||
| or co_yield expression that is an operand of another operator, so that | ||
| "(co_await x).m" is no longer generated as "co_await x.m". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++23 Test_name.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| //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. The generated C++ also needs parentheses around | ||
| // co_await and co_yield when they are operands (e.g., of "." or "="). | ||
|
|
||
| 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; | ||
| }; | ||
|
|
||
| struct S { int m; int get(); }; | ||
|
|
||
| task g() { | ||
| int i = (co_await awaiter<S>{}).m; | ||
| int j = (co_await awaiter<S>{}).get(); | ||
| int k = (co_await awaiter<S &>{}).m = 1; | ||
| } | ||
|
|
||
| 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); | ||
| (co_yield 1) = 2; | ||
| decltype(auto) d = co_yield 1; | ||
| static_assert(std::is_same<decltype(d), int &>::value); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Parenthesize the await operand when its precedence requires it.
With
PARENS_IN_ILFALSE, the parser does not keep parentheses in the IL. Theenk_awaitbranch then emits its operand without parentheses. For a parenthesized operation such as(co_await (a + b)), the output isco_await a + b, which awaits onlya. The same loss occurs for other lower-precedence operands, such asco_await (c ? x : y).Request parentheses for the operand in the
enk_awaitbranch when the operand's precedence is lower thanco_await. Use the same precedence check that the binary-operator path uses inparens_may_be_needed(src/cp_gen_be.c:18317). Theyieldbranch has the same gap and should be handled in the same change.