Repository navigation
Keep needed parentheses around co_await and co_yield in generated C++. [GH #546] #547
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
Merged
nv-cmeerw
merged 4 commits into
edgcpp:main
from
mohitmishra786:gh-546-coroutine-parens
Nov 20, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f55ae7e
Keep needed parentheses around co_await and co_yield in generated C++…
mohitmishra786 7e016cd
Add dependent co_yield cases to the GH546 test. [GH #546]
mohitmishra786 b7e729a
Use type checks in the GH546 test; reword the Changes entry. [GH #546]
mohitmishra786 81023c1
Expand the GH546 test and use the usual Changes phrasing. [GH #546]
mohitmishra786 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| [GH #546] | ||
| C++-generating back end: dropped parentheses around co_await and co_yield | ||
|
|
||
| In configurations with PARENS_IN_IL set to FALSE, the C++-generating back | ||
| end could drop parentheses that are needed to keep the meaning of co_await | ||
| and co_yield expressions, so the generated code was rejected or had a | ||
| different meaning. For example, with --c++20: | ||
|
|
||
| #include <coroutine> | ||
| struct S { int m; }; | ||
| struct A { | ||
| bool await_ready(); | ||
| void await_suspend(std::coroutine_handle<>); | ||
| S await_resume(); | ||
| }; | ||
| int f(); | ||
| std::task<int> g(bool c, A x, A y) { | ||
| int i = (co_await x).m; // Was generated as "co_await x.m". | ||
| co_await (c ? x : y); // Was generated as "co_await c ? x : y". | ||
| co_yield (f(), i); // Was generated as "co_yield f(), i". | ||
| co_return i; | ||
| } | ||
|
|
||
| Additionally, in configurations with ALL_TEMPLATE_INFO_IN_IL set to TRUE, a | ||
| dependent co_yield expression used as an operand, as in "(co_yield T{}) + 1", | ||
| had the same problem. Parentheses are now added where they are needed. |
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 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++20 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 @@ | ||
| fe_only -DTEST_NUMBER=2 --c++20 --gnu_version 150200 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 @@ | ||
| fe_only -DTEST_NUMBER=3 --c++20 --clang_version 230100 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 @@ | ||
| fe_only -DTEST_NUMBER=4 --ms_c++20 --microsoft_version 1950 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,225 @@ | ||
| //type:fp | ||
| //options:--c++20:--c++20 --gnu_version 150200:--c++20 --clang_version 230100:--ms_c++20 --microsoft_version 1950 | ||
| //remark:[GH #546] Parentheses around co_await and co_yield in generated C++ | ||
| // | ||
| // In configurations with PARENS_IN_IL set to FALSE, the C++-generating back | ||
| // end dropped parentheses that are needed around a co_await or co_yield | ||
| // expression used as an operand, and around some operands of co_await and | ||
| // co_yield. The static assertions fail if the generated code groups an | ||
| // expression differently. The dependent cases go through the IL (and so | ||
| // need the fix) only in configurations with ALL_TEMPLATE_INFO_IN_IL set to | ||
| // TRUE. | ||
|
|
||
| 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<typename T, typename U> | ||
| constexpr bool is_same_v = false; | ||
| template<typename T> | ||
| constexpr bool is_same_v<T, T> = true; | ||
|
|
||
| struct suspend { | ||
| bool await_ready() noexcept; | ||
| void await_suspend(std::coroutine_handle<>) noexcept; | ||
| void await_resume() noexcept; | ||
| }; | ||
|
|
||
| template<typename T> | ||
| struct awaiter { | ||
| bool await_ready() noexcept; | ||
| void await_suspend(std::coroutine_handle<>) noexcept; | ||
| T await_resume() noexcept; | ||
| }; | ||
|
|
||
| struct S { | ||
| int m; | ||
| void operator=(int); | ||
| }; | ||
|
|
||
| struct task; | ||
| struct promise { | ||
| suspend initial_suspend() noexcept; | ||
| suspend final_suspend() noexcept; | ||
| void return_void(); | ||
| void unhandled_exception(); | ||
| task get_return_object(); | ||
| awaiter<S> yield_value(S); | ||
| awaiter<int> yield_value(int); | ||
| }; | ||
| struct task { using promise_type = promise; }; | ||
|
|
||
| namespace await_expression { | ||
| /* The awaited temporary is not parenthesized for any other reason, so | ||
| this checks the co_await expression as the object of ".". */ | ||
| task f() { | ||
| auto i = (co_await awaiter<S>{}).m; | ||
| static_assert(is_same_v<decltype(i), int>); | ||
| co_return; | ||
| } | ||
| } | ||
|
|
||
| namespace await_operand { | ||
| /* The operand of co_await is a cast-expression, so a comma or an | ||
| assignment operand needs parentheses. */ | ||
| awaiter<int> x, y; | ||
| task comma() { | ||
| auto r = co_await (void(), awaiter<int>{}); | ||
| static_assert(is_same_v<decltype(r), int>); | ||
| co_return; | ||
| } | ||
| task assignment() { | ||
| auto r = co_await (x = y); | ||
| static_assert(is_same_v<decltype(r), int>); | ||
| co_return; | ||
| } | ||
| } | ||
|
|
||
| namespace yield_expression { | ||
| /* A yield-expression is an assignment-expression, so it needs | ||
| parentheses as the operand of an assignment, conditional, or comma | ||
| operation. */ | ||
| task assignment() { | ||
| (co_yield S{}) = 1; | ||
| co_return; | ||
| } | ||
| task operand() { | ||
| auto a = (co_yield 1) + 2L; | ||
| auto b = co_yield 1 + 2L; | ||
| auto c = co_yield (1 + 2L); | ||
| auto d = (co_yield 1 + 2L); | ||
| static_assert(is_same_v<decltype(a), long>); | ||
| static_assert(is_same_v<decltype(b), int>); | ||
| static_assert(is_same_v<decltype(c), int>); | ||
| static_assert(is_same_v<decltype(d), int>); | ||
| co_return; | ||
| } | ||
| task conditional() { | ||
| auto a = (co_yield 1) ? 2L : 3L; | ||
| auto b = co_yield 1 ? 2L : 3L; | ||
| auto c = co_yield (1 ? 2L : 3L); | ||
| auto d = (co_yield 1 ? 2L : 3L); | ||
| static_assert(is_same_v<decltype(a), long>); | ||
| static_assert(is_same_v<decltype(b), int>); | ||
| static_assert(is_same_v<decltype(c), int>); | ||
| static_assert(is_same_v<decltype(d), int>); | ||
| co_return; | ||
| } | ||
| task comma() { | ||
| int i = 0; | ||
| auto a = ((co_yield 1), 2L); | ||
| auto b = (++i, (co_yield 1)); | ||
| static_assert(is_same_v<decltype(a), long>); | ||
| static_assert(is_same_v<decltype(b), int>); | ||
| co_return; | ||
| } | ||
| } | ||
|
|
||
| namespace yield_operand { | ||
| /* The operand of co_yield is an assignment-expression, so only a comma | ||
| expression needs parentheses. void() cannot be passed to | ||
| yield_value, so a lost parenthesis makes the generated code | ||
| ill-formed. */ | ||
| task comma() { | ||
| co_yield (void(), 1); | ||
| co_return; | ||
| } | ||
| } | ||
|
|
||
| namespace dependent { | ||
| /* These mirror the non-dependent tests, with both literal and dependent | ||
| T operands. */ | ||
| template<typename T> | ||
| task assignment() { | ||
| (co_yield S{}) = 1; | ||
| (co_yield T{}) = 1; | ||
| co_return; | ||
| } | ||
| template task assignment<S>(); | ||
|
|
||
| template<typename T> | ||
| task operand() { | ||
| auto a = (co_yield 1) + 2L; | ||
| auto b = (co_yield T{}) + 2L; | ||
| auto c = co_yield 1 + 2L; | ||
| auto d = co_yield T{} + 2L; | ||
| auto e = co_yield (T{} + 2L); | ||
| auto f = (co_yield T{} + 2L); | ||
| static_assert(is_same_v<decltype(a), long>); | ||
| static_assert(is_same_v<decltype(b), long>); | ||
| static_assert(is_same_v<decltype(c), int>); | ||
| static_assert(is_same_v<decltype(d), int>); | ||
| static_assert(is_same_v<decltype(e), int>); | ||
| static_assert(is_same_v<decltype(f), int>); | ||
| co_return; | ||
| } | ||
| template task operand<int>(); | ||
|
|
||
| template<typename T> | ||
| task conditional() { | ||
| auto a = (co_yield 1) ? 2L : 3L; | ||
| auto b = (co_yield T{}) ? 2L : 3L; | ||
| auto c = co_yield T{} ? 2L : 3L; | ||
| auto d = co_yield (T{} ? 2L : 3L); | ||
| auto e = (co_yield T{} ? 2L : 3L); | ||
| static_assert(is_same_v<decltype(a), long>); | ||
| static_assert(is_same_v<decltype(b), long>); | ||
| static_assert(is_same_v<decltype(c), int>); | ||
| static_assert(is_same_v<decltype(d), int>); | ||
| static_assert(is_same_v<decltype(e), int>); | ||
| co_return; | ||
| } | ||
| template task conditional<int>(); | ||
|
|
||
| template<typename T> | ||
| task comma() { | ||
| T i{}; | ||
| auto a = ((co_yield 1), 2L); | ||
| auto b = ((co_yield T{}), 2L); | ||
| auto c = (++i, (co_yield T{})); | ||
| static_assert(is_same_v<decltype(a), long>); | ||
| static_assert(is_same_v<decltype(b), long>); | ||
| static_assert(is_same_v<decltype(c), int>); | ||
| co_return; | ||
| } | ||
| template task comma<int>(); | ||
|
|
||
| template<typename T> | ||
| task yield_operand() { | ||
| co_yield (void(), 1); | ||
| co_yield (void(), T{}); | ||
| co_return; | ||
| } | ||
| template task yield_operand<S>(); | ||
|
|
||
| template<typename T> | ||
| task await_expression() { | ||
| auto a = (co_await awaiter<S>{}).m; | ||
| auto b = (co_await awaiter<T>{}).m; | ||
| static_assert(is_same_v<decltype(a), int>); | ||
| static_assert(is_same_v<decltype(b), int>); | ||
| co_return; | ||
| } | ||
| template task await_expression<S>(); | ||
|
|
||
| template<typename T> | ||
| task await_operand() { | ||
| awaiter<T> x, y; | ||
| auto a = co_await (void(), awaiter<T>{}); | ||
| auto b = co_await (x = y); | ||
| auto c = co_await (void(), awaiter<int>{}); | ||
| static_assert(is_same_v<decltype(a), T>); | ||
| static_assert(is_same_v<decltype(b), T>); | ||
| static_assert(is_same_v<decltype(c), int>); | ||
| co_return; | ||
| } | ||
| template task await_operand<int>(); | ||
| } |
6 changes: 0 additions & 6 deletions
6
tests/tests/imported/clang/cpp/SemaCXX/.warn-unsequenced-coro.rto/edg_x86_32_cp.1.1.txt
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
tests/tests/imported/clang/cpp/SemaCXX/.warn-unsequenced-coro.rto/edg_x86_64_cp.1.1.txt
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.