Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/cp_gen_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 <expr>". */
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. */
Expand Down Expand Up @@ -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),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
/*obj_expr_of_mfunc_operator=*/FALSE);
if (need_parens) write_tok_ch(')');
break;
case enk_fold:
gen_fold_expression(expr);
Expand Down
26 changes: 26 additions & 0 deletions src/pending_changes/gh-546-coroutine-parens.txt
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.
1 change: 0 additions & 1 deletion tests/expectations/edg_x86_32_cp/imported.log
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion tests/expectations/edg_x86_64_cp/imported.log
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/tests/changes/.GH546.rto/default.1.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --c++20 Test_name.c
1 change: 1 addition & 0 deletions tests/tests/changes/.GH546.rto/default.2.1.txt
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
1 change: 1 addition & 0 deletions tests/tests/changes/.GH546.rto/default.3.1.txt
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
1 change: 1 addition & 0 deletions tests/tests/changes/.GH546.rto/default.4.1.txt
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
225 changes: 225 additions & 0 deletions tests/tests/changes/GH546.sft.cpp
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>();
}

This file was deleted.

This file was deleted.

Loading