diff --git a/src/cp_gen_be.c b/src/cp_gen_be.c index fdc3911326..16c3d988c3 100644 --- a/src/cp_gen_be.c +++ b/src/cp_gen_be.c @@ -986,7 +986,7 @@ static constexpr a_byte generated_precedence[(int)eok_last+1] = { PREC_POSTFIX, /* eok_va_start_single_operand */ PREC_LOWEST, /* eok_lvalue */ PREC_PREFIX, /* eok_await */ - PREC_PREFIX, /* eok_yield */ + PREC_ASSIGNMENT, /* eok_yield */ PREC_PRIMARY, /* eok_splice */ PREC_LOWEST, /* eok_error */ PREC_LOWEST /* eok_last */ @@ -19959,6 +19959,7 @@ gen_expr that might end up generating this expr as a temporary. #endif /* BUILTIN_FUNCTIONS_ENABLED */ case enk_yield: /* yield expression: generate "co_yield ". */ + if (need_parens) write_tok_ch('('); write_tok_str("co_yield "); expr = expr->variant.await_info.operand; /* Skip nodes implementing the binding of the operand to the call. */ @@ -19989,13 +19990,26 @@ gen_expr that might end up generating this expr as a temporary. operand (which corresponds to the actual operand of the yield statement). */ expr = expr->next->next; - gen_expression(expr); + /* The operand of co_yield is an assignment-expression, so it needs + parentheses only if it is a comma operation, e.g., + "co_yield (f(), g())". */ + gen_expr(expr, parens_may_be_needed(PREC_COMMA, expr), + /*obj_expr_of_mfunc_operator=*/FALSE); + if (need_parens) write_tok_ch(')'); break; case enk_await: + if (need_parens) write_tok_ch('('); write_tok_str((msvc_is_generated_code_target && msvc_target_version_number < 1900) ? "__await " : "co_await "); - gen_expression(expr->variant.await_info.operand); + /* The operand of co_await is a cast-expression, like the operand of + a unary operator, so it may need parentheses, e.g., + "co_await (c ? x : y)". */ + gen_expr(expr->variant.await_info.operand, + parens_may_be_needed(PREC_PREFIX, + expr->variant.await_info.operand), + /*obj_expr_of_mfunc_operator=*/FALSE); + if (need_parens) write_tok_ch(')'); break; case enk_fold: gen_fold_expression(expr); diff --git a/src/pending_changes/gh-546-coroutine-parens.txt b/src/pending_changes/gh-546-coroutine-parens.txt new file mode 100644 index 0000000000..b059d86895 --- /dev/null +++ b/src/pending_changes/gh-546-coroutine-parens.txt @@ -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 + struct S { int m; }; + struct A { + bool await_ready(); + void await_suspend(std::coroutine_handle<>); + S await_resume(); + }; + int f(); + std::task 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. diff --git a/tests/expectations/edg_x86_32_cp/imported.log b/tests/expectations/edg_x86_32_cp/imported.log index 80dd7714e7..68a94244ff 100644 --- a/tests/expectations/edg_x86_32_cp/imported.log +++ b/tests/expectations/edg_x86_32_cp/imported.log @@ -332,7 +332,6 @@ clang/cpp/SemaCXX/warn-new-overaligned-3.sft.cpp:fp:-DTEST_NUMBER=1:FAIL: clang/cpp/SemaCXX/warn-new-overaligned.sft.cpp:fp:-DTEST_NUMBER=1 --c++03:FAIL: clang/cpp/SemaCXX/warn-overaligned-type-thrown.sft.cpp:fp:-DTEST_NUMBER=18 --c++11 --exceptions --exceptions -DNODIAG --target linux_aarch64:REQUIREMENTS NOT MET: clang/cpp/SemaCXX/warn-reorder-ctor-initialization.sft.cpp:fp:-DTEST_NUMBER=1:FAIL: -clang/cpp/SemaCXX/warn-unsequenced-coro.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: clang/cpp/SemaCXX/windows-arm-valist.sft.cpp:fp:-DTEST_NUMBER=1 -w --c++11:PASS: clang/cpp/SemaTemplate/atomics.sft.cpp:fp:-DTEST_NUMBER=1:PASS: clang/cpp/SemaTemplate/dependent-expr.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:ABORT: diff --git a/tests/expectations/edg_x86_64_cp/imported.log b/tests/expectations/edg_x86_64_cp/imported.log index f6dd23f368..ff1491cf6a 100644 --- a/tests/expectations/edg_x86_64_cp/imported.log +++ b/tests/expectations/edg_x86_64_cp/imported.log @@ -60,7 +60,6 @@ clang/cpp/SemaCXX/ms-exception-spec.sft.cpp:fn:-DTEST_NUMBER=1 --c++11 --ms_comp clang/cpp/SemaCXX/pr61460.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:PASS: clang/cpp/SemaCXX/type-dependent-exprs.sft.cpp:fn:-DTEST_NUMBER=1:PASS: clang/cpp/SemaCXX/warn-reorder-ctor-initialization.sft.cpp:fp:-DTEST_NUMBER=1:FAIL: -clang/cpp/SemaCXX/warn-unsequenced-coro.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: clang/cpp/SemaTemplate/dependent-expr.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:ABORT: clang/cpp/SemaTemplate/find-unexpanded-packs.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:PASS: clang/cpp/SemaTemplate/fun-template-def.sft.cpp:fn:-DTEST_NUMBER=1 --c++03:PASS: diff --git a/tests/tests/changes/.GH546.rto/default.1.1.txt b/tests/tests/changes/.GH546.rto/default.1.1.txt new file mode 100644 index 0000000000..32fc82e916 --- /dev/null +++ b/tests/tests/changes/.GH546.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --c++20 Test_name.c diff --git a/tests/tests/changes/.GH546.rto/default.2.1.txt b/tests/tests/changes/.GH546.rto/default.2.1.txt new file mode 100644 index 0000000000..32ab43e04f --- /dev/null +++ b/tests/tests/changes/.GH546.rto/default.2.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=2 --c++20 --gnu_version 150200 Test_name.c diff --git a/tests/tests/changes/.GH546.rto/default.3.1.txt b/tests/tests/changes/.GH546.rto/default.3.1.txt new file mode 100644 index 0000000000..a0f1c6652b --- /dev/null +++ b/tests/tests/changes/.GH546.rto/default.3.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=3 --c++20 --clang_version 230100 Test_name.c diff --git a/tests/tests/changes/.GH546.rto/default.4.1.txt b/tests/tests/changes/.GH546.rto/default.4.1.txt new file mode 100644 index 0000000000..1bbe45a65c --- /dev/null +++ b/tests/tests/changes/.GH546.rto/default.4.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=4 --ms_c++20 --microsoft_version 1950 Test_name.c diff --git a/tests/tests/changes/GH546.sft.cpp b/tests/tests/changes/GH546.sft.cpp new file mode 100644 index 0000000000..7b3db29523 --- /dev/null +++ b/tests/tests/changes/GH546.sft.cpp @@ -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 struct coroutine_handle; + template<> struct coroutine_handle<> { void *address(); }; + template struct coroutine_handle { + static coroutine_handle from_address(void *); + operator coroutine_handle<>() noexcept; + }; + template struct coroutine_traits { + using promise_type = typename R::promise_type; + }; +} + +template +constexpr bool is_same_v = false; +template +constexpr bool is_same_v = true; + +struct suspend { + bool await_ready() noexcept; + void await_suspend(std::coroutine_handle<>) noexcept; + void await_resume() noexcept; +}; + +template +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 yield_value(S); + awaiter 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{}).m; + static_assert(is_same_v); + co_return; + } +} + +namespace await_operand { + /* The operand of co_await is a cast-expression, so a comma or an + assignment operand needs parentheses. */ + awaiter x, y; + task comma() { + auto r = co_await (void(), awaiter{}); + static_assert(is_same_v); + co_return; + } + task assignment() { + auto r = co_await (x = y); + static_assert(is_same_v); + 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); + static_assert(is_same_v); + static_assert(is_same_v); + static_assert(is_same_v); + 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); + static_assert(is_same_v); + static_assert(is_same_v); + static_assert(is_same_v); + co_return; + } + task comma() { + int i = 0; + auto a = ((co_yield 1), 2L); + auto b = (++i, (co_yield 1)); + static_assert(is_same_v); + static_assert(is_same_v); + 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 + task assignment() { + (co_yield S{}) = 1; + (co_yield T{}) = 1; + co_return; + } + template task assignment(); + + template + 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); + static_assert(is_same_v); + static_assert(is_same_v); + static_assert(is_same_v); + static_assert(is_same_v); + static_assert(is_same_v); + co_return; + } + template task operand(); + + template + 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); + static_assert(is_same_v); + static_assert(is_same_v); + static_assert(is_same_v); + static_assert(is_same_v); + co_return; + } + template task conditional(); + + template + 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); + static_assert(is_same_v); + static_assert(is_same_v); + co_return; + } + template task comma(); + + template + task yield_operand() { + co_yield (void(), 1); + co_yield (void(), T{}); + co_return; + } + template task yield_operand(); + + template + task await_expression() { + auto a = (co_await awaiter{}).m; + auto b = (co_await awaiter{}).m; + static_assert(is_same_v); + static_assert(is_same_v); + co_return; + } + template task await_expression(); + + template + task await_operand() { + awaiter x, y; + auto a = co_await (void(), awaiter{}); + auto b = co_await (x = y); + auto c = co_await (void(), awaiter{}); + static_assert(is_same_v); + static_assert(is_same_v); + static_assert(is_same_v); + co_return; + } + template task await_operand(); +} diff --git a/tests/tests/imported/clang/cpp/SemaCXX/.warn-unsequenced-coro.rto/edg_x86_32_cp.1.1.txt b/tests/tests/imported/clang/cpp/SemaCXX/.warn-unsequenced-coro.rto/edg_x86_32_cp.1.1.txt deleted file mode 100644 index 0461e619b0..0000000000 --- a/tests/tests/imported/clang/cpp/SemaCXX/.warn-unsequenced-coro.rto/edg_x86_32_cp.1.1.txt +++ /dev/null @@ -1,6 +0,0 @@ -fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_version 999999 --c++20 Test_name.c -"SemaCXX/warn-unsequenced-coro.cpp", line 93: error: the operand to this co_await expression resolves to non-class "int" - co_await (i = 0), (std::suspend_always{}); - ^ - -1 error detected in the compilation of "Test_name.c". diff --git a/tests/tests/imported/clang/cpp/SemaCXX/.warn-unsequenced-coro.rto/edg_x86_64_cp.1.1.txt b/tests/tests/imported/clang/cpp/SemaCXX/.warn-unsequenced-coro.rto/edg_x86_64_cp.1.1.txt deleted file mode 100644 index 0461e619b0..0000000000 --- a/tests/tests/imported/clang/cpp/SemaCXX/.warn-unsequenced-coro.rto/edg_x86_64_cp.1.1.txt +++ /dev/null @@ -1,6 +0,0 @@ -fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_version 999999 --c++20 Test_name.c -"SemaCXX/warn-unsequenced-coro.cpp", line 93: error: the operand to this co_await expression resolves to non-class "int" - co_await (i = 0), (std::suspend_always{}); - ^ - -1 error detected in the compilation of "Test_name.c".