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
1212namespace 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
2426struct 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};
5255struct task { using promise_type = promise; };
5356
5457task 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
5965bool c;
6066task 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 ();
6673task 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
7078template <typename T>
7179task 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}
7787template task dependent<S>();
0 commit comments