[clang] Forbid co_await and co_yield in invalid expr contexts (PR #130455)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 9 04:36:15 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/2] 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/2] 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}}
+}
More information about the cfe-commits
mailing list