Skip to content

Commit 0cc8a63

Browse files
NewSigmacor3ntin
andauthored
Forbid co_await and co_yield in invalid expr contexts (#130455)
Fix #78426 C++26 introduced braced initializer lists as template arguments. However, such contexts should be considered invalid for co_await and co_yield. This commit explicitly rules out the possibility of using these exprs in template arguments. --------- Co-authored-by: cor3ntin <[email protected]>
1 parent 257e483 commit 0cc8a63

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ Bug Fixes to C++ Support
295295
direct-list-initialized from an array is corrected to direct-initialization.
296296
- Clang no longer crashes when a coroutine is declared ``[[noreturn]]``. (#GH127327)
297297
- Clang now uses the parameter location for abbreviated function templates in ``extern "C"``. (#GH46386)
298+
- Clang will emit an error instead of crash when use co_await or co_yield in
299+
C++26 braced-init-list template parameter initialization. (#GH78426)
298300
- Fixes matching of nested template template parameters. (#GH130362)
299301
- Correctly diagnoses template template paramters which have a pack parameter
300302
not in the last position.

clang/lib/Sema/SemaCoroutine.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,11 @@ static bool checkSuspensionContext(Sema &S, SourceLocation Loc,
788788
// First emphasis of [expr.await]p2: must be a potentially evaluated context.
789789
// That is, 'co_await' and 'co_yield' cannot appear in subexpressions of
790790
// \c sizeof.
791-
if (S.isUnevaluatedContext()) {
791+
const auto ExprContext = S.currentEvaluationContext().ExprContext;
792+
const bool BadContext =
793+
S.isUnevaluatedContext() ||
794+
ExprContext != Sema::ExpressionEvaluationContextRecord::EK_Other;
795+
if (BadContext) {
792796
S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword;
793797
return false;
794798
}
@@ -798,7 +802,6 @@ static bool checkSuspensionContext(Sema &S, SourceLocation Loc,
798802
S.Diag(Loc, diag::err_coroutine_within_handler) << Keyword;
799803
return false;
800804
}
801-
802805
return true;
803806
}
804807

clang/test/SemaCXX/coroutine-decltype.cpp renamed to clang/test/SemaCXX/coroutine-unevaluate.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ MyTask DoAnotherthing() {
3232
static_assert(__is_same(void, decltype(co_yield 0))); // expected-error {{'co_yield' cannot be used in an unevaluated context}}
3333
co_return;
3434
}
35+
36+
template<class>
37+
struct Task {};
38+
39+
void BracedInitListCXX26() {
40+
[]() -> Task<{ co_await 1 }> {}; // expected-error {{'co_await' cannot be used in an unevaluated context}}
41+
[]() -> Task<{ co_yield 1 }> {}; // expected-error {{'co_yield' cannot be used in an unevaluated context}}
42+
}

0 commit comments

Comments
 (0)