11// type:fp
2- // options:--c++20
2+ // options:--c++20:--c++20 --gnu_version 150200:--c++20 --clang_version 230100:--ms_c++20 --microsoft_version 1950
33// remark:[GH #546] Parentheses around co_await and co_yield in generated C++
44//
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.
5+ // In configurations with PARENS_IN_IL set to FALSE, the C++-generating back
6+ // end dropped parentheses that are needed around a co_await or co_yield
7+ // expression used as an operand, and around some operands of co_await and
8+ // co_yield. The static assertions fail if the generated code groups an
9+ // expression differently. The dependent cases go through the IL (and so
10+ // need the fix) only in configurations with ALL_TEMPLATE_INFO_IN_IL set to
11+ // TRUE.
1112
1213namespace std {
1314 template <typename = void > struct coroutine_handle ;
@@ -19,21 +20,24 @@ namespace std {
1920 template <typename R, typename ...> struct coroutine_traits {
2021 using promise_type = typename R::promise_type;
2122 };
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 ; };
2423}
2524
25+ template <typename T, typename U>
26+ constexpr bool is_same_v = false ;
27+ template <typename T>
28+ constexpr bool is_same_v<T, T> = true ;
29+
2630struct suspend {
2731 bool await_ready () noexcept ;
2832 void await_suspend (std::coroutine_handle<>) noexcept ;
2933 void await_resume () noexcept ;
3034};
3135
32- template <typename R >
36+ template <typename T >
3337struct awaiter {
3438 bool await_ready () noexcept ;
3539 void await_suspend (std::coroutine_handle<>) noexcept ;
36- R await_resume () noexcept ;
40+ T await_resume () noexcept ;
3741};
3842
3943struct S {
@@ -50,38 +54,172 @@ struct promise {
5054 task get_return_object ();
5155 awaiter<S> yield_value (S);
5256 awaiter<int > yield_value (int );
53- awaiter<short > yield_value (long ); // A different type, to catch "co_yield (x + 2L)".
5457};
5558struct task { using promise_type = promise; };
5659
57- task outer () {
58- auto i = (co_await awaiter<S>{}).m ;
59- static_assert (std::is_same<decltype (i), int >::value);
60- (co_yield S{}) = 1 ;
61- auto v = (co_yield 1 ) + 2L ;
62- static_assert (std::is_same<decltype (v), long >::value);
60+ namespace await_expression {
61+ /* The awaited temporary is not parenthesized for any other reason, so
62+ this checks the co_await expression as the object of ".". */
63+ task f () {
64+ auto i = (co_await awaiter<S>{}).m ;
65+ static_assert (is_same_v<decltype (i), int >);
66+ co_return ;
67+ }
6368}
6469
65- bool c;
66- task await_operand () {
67- auto r = co_await (c ? awaiter<int >{} : awaiter<int >{});
68- static_assert (std::is_same<decltype (r), int >::value);
70+ namespace await_operand {
71+ /* The operand of co_await is a cast-expression, so a comma or an
72+ assignment operand needs parentheses. */
73+ awaiter<int > x, y;
74+ task comma () {
75+ auto r = co_await (void (), awaiter<int >{});
76+ static_assert (is_same_v<decltype (r), int >);
77+ co_return ;
78+ }
79+ task assignment () {
80+ auto r = co_await (x = y);
81+ static_assert (is_same_v<decltype (r), int >);
82+ co_return ;
83+ }
6984}
7085
71- long first ();
72- int second ();
73- task yield_operand () {
74- auto v = co_yield (first (), second ());
75- static_assert (std::is_same<decltype (v), int >::value);
86+ namespace yield_expression {
87+ /* A yield-expression is an assignment-expression, so it needs
88+ parentheses as the operand of an assignment, conditional, or comma
89+ operation. */
90+ task assignment () {
91+ (co_yield S{}) = 1 ;
92+ co_return ;
93+ }
94+ task operand () {
95+ auto a = (co_yield 1 ) + 2L ;
96+ auto b = co_yield 1 + 2L ;
97+ auto c = co_yield (1 + 2L );
98+ auto d = (co_yield 1 + 2L );
99+ static_assert (is_same_v<decltype (a), long >);
100+ static_assert (is_same_v<decltype (b), int >);
101+ static_assert (is_same_v<decltype (c), int >);
102+ static_assert (is_same_v<decltype (d), int >);
103+ co_return ;
104+ }
105+ task conditional () {
106+ auto a = (co_yield 1 ) ? 2L : 3L ;
107+ auto b = co_yield 1 ? 2L : 3L ;
108+ auto c = co_yield (1 ? 2L : 3L );
109+ auto d = (co_yield 1 ? 2L : 3L );
110+ static_assert (is_same_v<decltype (a), long >);
111+ static_assert (is_same_v<decltype (b), int >);
112+ static_assert (is_same_v<decltype (c), int >);
113+ static_assert (is_same_v<decltype (d), int >);
114+ co_return ;
115+ }
116+ task comma () {
117+ int i = 0 ;
118+ auto a = ((co_yield 1 ), 2L );
119+ auto b = (++i, (co_yield 1 ));
120+ static_assert (is_same_v<decltype (a), long >);
121+ static_assert (is_same_v<decltype (b), int >);
122+ co_return ;
123+ }
76124}
77125
78- template <typename T>
79- task dependent () {
80- (co_yield T{}) = 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);
85- co_return ;
126+ namespace yield_operand {
127+ /* The operand of co_yield is an assignment-expression, so only a comma
128+ expression needs parentheses. void() cannot be passed to
129+ yield_value, so a lost parenthesis makes the generated code
130+ ill-formed. */
131+ task comma () {
132+ co_yield (void (), 1 );
133+ co_return ;
134+ }
135+ }
136+
137+ namespace dependent {
138+ /* These mirror the non-dependent tests, with both literal and dependent
139+ T operands. */
140+ template <typename T>
141+ task assignment () {
142+ (co_yield S{}) = 1 ;
143+ (co_yield T{}) = 1 ;
144+ co_return ;
145+ }
146+ template task assignment<S>();
147+
148+ template <typename T>
149+ task operand () {
150+ auto a = (co_yield 1 ) + 2L ;
151+ auto b = (co_yield T{}) + 2L ;
152+ auto c = co_yield 1 + 2L ;
153+ auto d = co_yield T{} + 2L ;
154+ auto e = co_yield (T{} + 2L );
155+ auto f = (co_yield T{} + 2L );
156+ static_assert (is_same_v<decltype (a), long >);
157+ static_assert (is_same_v<decltype (b), long >);
158+ static_assert (is_same_v<decltype (c), int >);
159+ static_assert (is_same_v<decltype (d), int >);
160+ static_assert (is_same_v<decltype (e), int >);
161+ static_assert (is_same_v<decltype (f), int >);
162+ co_return ;
163+ }
164+ template task operand<int >();
165+
166+ template <typename T>
167+ task conditional () {
168+ auto a = (co_yield 1 ) ? 2L : 3L ;
169+ auto b = (co_yield T{}) ? 2L : 3L ;
170+ auto c = co_yield T{} ? 2L : 3L ;
171+ auto d = co_yield (T{} ? 2L : 3L );
172+ auto e = (co_yield T{} ? 2L : 3L );
173+ static_assert (is_same_v<decltype (a), long >);
174+ static_assert (is_same_v<decltype (b), long >);
175+ static_assert (is_same_v<decltype (c), int >);
176+ static_assert (is_same_v<decltype (d), int >);
177+ static_assert (is_same_v<decltype (e), int >);
178+ co_return ;
179+ }
180+ template task conditional<int >();
181+
182+ template <typename T>
183+ task comma () {
184+ T i{};
185+ auto a = ((co_yield 1 ), 2L );
186+ auto b = ((co_yield T{}), 2L );
187+ auto c = (++i, (co_yield T{}));
188+ static_assert (is_same_v<decltype (a), long >);
189+ static_assert (is_same_v<decltype (b), long >);
190+ static_assert (is_same_v<decltype (c), int >);
191+ co_return ;
192+ }
193+ template task comma<int >();
194+
195+ template <typename T>
196+ task yield_operand () {
197+ co_yield (void (), 1 );
198+ co_yield (void (), T{});
199+ co_return ;
200+ }
201+ template task yield_operand<S>();
202+
203+ template <typename T>
204+ task await_expression () {
205+ auto a = (co_await awaiter<S>{}).m ;
206+ auto b = (co_await awaiter<T>{}).m ;
207+ static_assert (is_same_v<decltype (a), int >);
208+ static_assert (is_same_v<decltype (b), int >);
209+ co_return ;
210+ }
211+ template task await_expression<S>();
212+
213+ template <typename T>
214+ task await_operand () {
215+ awaiter<T> x, y;
216+ auto a = co_await (void (), awaiter<T>{});
217+ auto b = co_await (x = y);
218+ auto c = co_await (void (), awaiter<int >{});
219+ static_assert (is_same_v<decltype (a), T>);
220+ static_assert (is_same_v<decltype (b), T>);
221+ static_assert (is_same_v<decltype (c), int >);
222+ co_return ;
223+ }
224+ template task await_operand<int >();
86225}
87- template task dependent<S>();
0 commit comments