Skip to content

Forbid co_await and co_yield in invalid expr contexts #130455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 10, 2025
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ Bug Fixes to C++ Support
direct-list-initialized from an array is corrected to direct-initialization.
- Clang no longer crashes when a coroutine is declared ``[[noreturn]]``. (#GH127327)
- Clang now uses the parameter location for abbreviated function templates in ``extern "C"``. (#GH46386)
- Clang will emit an error instead of crash when use co_await or co_yield in
C++26 braced-init-list template parameter initialization. (#GH78426)
- Fixes matching of nested template template parameters. (#GH130362)
- Correctly diagnoses template template paramters which have a pack parameter
not in the last position.
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/Sema/SemaCoroutine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,11 @@ static bool checkSuspensionContext(Sema &S, SourceLocation Loc,
// First emphasis of [expr.await]p2: must be a potentially evaluated context.
// That is, 'co_await' and 'co_yield' cannot appear in subexpressions of
// \c sizeof.
if (S.isUnevaluatedContext()) {
const auto ExprContext = S.currentEvaluationContext().ExprContext;
const bool BadContext =
S.isUnevaluatedContext() ||
ExprContext != Sema::ExpressionEvaluationContextRecord::EK_Other;
if (BadContext) {
S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword;
return false;
}
Expand All @@ -798,7 +802,6 @@ static bool checkSuspensionContext(Sema &S, SourceLocation Loc,
S.Diag(Loc, diag::err_coroutine_within_handler) << Keyword;
return false;
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ MyTask DoAnotherthing() {
static_assert(__is_same(void, decltype(co_yield 0))); // expected-error {{'co_yield' cannot be used in an unevaluated context}}
co_return;
}

template<class>
struct Task {};

void BracedInitListCXX26() {
[]() -> Task<{ co_await 1 }> {}; // expected-error {{'co_await' cannot be used in an unevaluated context}}
[]() -> Task<{ co_yield 1 }> {}; // expected-error {{'co_yield' cannot be used in an unevaluated context}}
}