Skip to content

Commit 2c877c2

Browse files
mohitmishra786nv-cmeerw
authored andcommitted
Use type checks in the GH546 test; reword the Changes entry. [GH #546]
1 parent 6a2cd7e commit 2c877c2

2 files changed

Lines changed: 45 additions & 22 deletions

File tree

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
[GH #546]
2-
Parentheses around co_await and co_yield in generated C++
2+
C++-generating back end: dropped parentheses around co_await and co_yield
33

4-
The C++-generating back end could drop parentheses that are needed to keep
5-
the meaning of co_await and co_yield expressions, so the generated code was
6-
rejected or had a different meaning. For example:
4+
With PARENS_IN_IL=0, the C++-generating back end could drop parentheses that
5+
are needed to keep the meaning of co_await and co_yield expressions, so the
6+
generated code was rejected or had a different meaning. For example, with
7+
--c++20:
78

8-
int i = (co_await a).m; // Was generated as "co_await a.m".
9-
co_await (c ? x : y); // Was generated as "co_await c ? x : y".
10-
co_yield (first(), second()); // Was generated as
11-
// "co_yield first(), second()".
9+
#include <coroutine>
10+
struct S { int m; };
11+
struct A {
12+
bool await_ready();
13+
void await_suspend(std::coroutine_handle<>);
14+
S await_resume();
15+
};
16+
int f();
17+
std::task<int> g(bool c, A x, A y) {
18+
int i = (co_await x).m; // Was generated as "co_await x.m".
19+
co_await (c ? x : y); // Was generated as "co_await c ? x : y".
20+
co_yield (f(), i); // Was generated as "co_yield f(), i".
21+
co_return i;
22+
}
1223

13-
Parentheses are now added where they are needed.
24+
A dependent co_yield expression used as an operand, as in
25+
"(co_yield T{}) + 1", had the same problem when ALL_TEMPLATE_INFO_IN_IL is
26+
also set. Parentheses are now added where they are needed.

‎tests/tests/changes/GH546.sft.cpp‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
//options:--c++20
33
//remark:[GH #546] Parentheses around co_await and co_yield in generated C++
44
//
5-
// Without parentheses in the IL, the C++-generating back end dropped
6-
// parentheses that are needed around a co_await or co_yield expression used
7-
// as an operand, around a co_await operand with lower precedence, and around
8-
// a comma operand of co_yield. The dependent cases go through the IL (and
9-
// so need the fix) only when templates are kept in the IL
10-
// (ALL_TEMPLATE_INFO_IN_IL).
5+
// With PARENS_IN_IL=0, the C++-generating back end dropped parentheses that
6+
// are needed around a co_await or co_yield expression used as an operand,
7+
// around a co_await operand with lower precedence, and around a comma
8+
// operand of co_yield. The static assertions fail if the generated code
9+
// groups an expression differently. The dependent cases go through the IL
10+
// (and so need the fix) only with ALL_TEMPLATE_INFO_IN_IL=1.
1111

1212
namespace std {
1313
template<typename = void> struct coroutine_handle;
@@ -19,6 +19,8 @@ namespace std {
1919
template<typename R, typename...> struct coroutine_traits {
2020
using promise_type = typename R::promise_type;
2121
};
22+
template<class T, class U> struct is_same { static constexpr bool value = false; };
23+
template<class T> struct is_same<T, T> { static constexpr bool value = true; };
2224
}
2325

2426
struct suspend {
@@ -48,30 +50,38 @@ struct promise {
4850
task get_return_object();
4951
awaiter<S> yield_value(S);
5052
awaiter<int> yield_value(int);
53+
awaiter<short> yield_value(long); // A different type, to catch "co_yield (x + 2L)".
5154
};
5255
struct task { using promise_type = promise; };
5356

5457
task outer() {
55-
int i = (co_await awaiter<S>{}).m;
58+
auto i = (co_await awaiter<S>{}).m;
59+
static_assert(std::is_same<decltype(i), int>::value);
5660
(co_yield S{}) = 1;
61+
auto v = (co_yield 1) + 2L;
62+
static_assert(std::is_same<decltype(v), long>::value);
5763
}
5864

5965
bool c;
6066
task await_operand() {
61-
co_await (c ? awaiter<S>{} : awaiter<S>{});
67+
auto r = co_await (c ? awaiter<int>{} : awaiter<int>{});
68+
static_assert(std::is_same<decltype(r), int>::value);
6269
}
6370

64-
int first();
65-
S second();
71+
long first();
72+
int second();
6673
task yield_operand() {
67-
co_yield (first(), second());
74+
auto v = co_yield (first(), second());
75+
static_assert(std::is_same<decltype(v), int>::value);
6876
}
6977

7078
template<typename T>
7179
task dependent() {
7280
(co_yield T{}) = 1;
73-
int a = (co_yield 1) + 1;
74-
int b = (co_yield T{}.m) + 1;
81+
auto a = (co_yield 1) + 2L;
82+
static_assert(std::is_same<decltype(a), long>::value);
83+
auto b = (co_yield T{}.m) + 2L;
84+
static_assert(std::is_same<decltype(b), long>::value);
7585
co_return;
7686
}
7787
template task dependent<S>();

0 commit comments

Comments
 (0)