[clang] Forbid co_await and co_yield in invalid expr contexts (PR #130455)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 10 04:25:47 PDT 2025


https://github.com/NewSigma updated https://github.com/llvm/llvm-project/pull/130455

>From 85a40587375533ac8fcd842395c923895c5debaf Mon Sep 17 00:00:00 2001
From: NewSigma <NewSigma at 163.com>
Date: Sun, 9 Mar 2025 10:27:24 +0800
Subject: [PATCH 1/3] Fobbid co_await in invalid expr context

---
 clang/lib/Sema/SemaCoroutine.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaCoroutine.cpp b/clang/lib/Sema/SemaCoroutine.cpp
index 0e4f3b20c78cd..53536b0d14037 100644
--- a/clang/lib/Sema/SemaCoroutine.cpp
+++ b/clang/lib/Sema/SemaCoroutine.cpp
@@ -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;
   }
@@ -798,7 +802,6 @@ static bool checkSuspensionContext(Sema &S, SourceLocation Loc,
     S.Diag(Loc, diag::err_coroutine_within_handler) << Keyword;
     return false;
   }
-
   return true;
 }
 

>From eeb51f2775739390596b69a33eeae17ecf069148 Mon Sep 17 00:00:00 2001
From: NewSigma <NewSigma at 163.com>
Date: Sun, 9 Mar 2025 19:35:16 +0800
Subject: [PATCH 2/3] Add test for unevaluate context

---
 .../{coroutine-decltype.cpp => coroutine-unevaluate.cpp}  | 8 ++++++++
 1 file changed, 8 insertions(+)
 rename clang/test/SemaCXX/{coroutine-decltype.cpp => coroutine-unevaluate.cpp} (78%)

diff --git a/clang/test/SemaCXX/coroutine-decltype.cpp b/clang/test/SemaCXX/coroutine-unevaluate.cpp
similarity index 78%
rename from clang/test/SemaCXX/coroutine-decltype.cpp
rename to clang/test/SemaCXX/coroutine-unevaluate.cpp
index 7cbe6688d4155..164caed2836a1 100644
--- a/clang/test/SemaCXX/coroutine-decltype.cpp
+++ b/clang/test/SemaCXX/coroutine-unevaluate.cpp
@@ -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}}
+}

>From 5d124731e21ff4be9fa0a091c3e3e5a8b3d31782 Mon Sep 17 00:00:00 2001
From: NewSigma <NewSigma at 163.com>
Date: Mon, 10 Mar 2025 19:20:20 +0800
Subject: [PATCH 3/3] Add release note for pr130455

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7859871b0493a..cd3f5344f2d9a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -294,6 +294,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)
 
 Improvements to C++ diagnostics
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



More information about the cfe-commits mailing list