[clang] [clang][Sema] Fix initialization of GRO when GRO-return type mismatches (CWG2563) (PR #179156)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 1 18:29:10 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-coroutines
Author: Weibo He (NewSigma)
<details>
<summary>Changes</summary>
This patch implements one piece of proposed solution to [CWG2563](https://cplusplus.github.io/CWG/issues/2563.html):
> get-return-object-invocation is as follows:
> ...
> otherwise, get-return-object-invocation initializes a variable with the exposition-only name gro as if by
> decltype(auto) gro = promise.get_return_object();
Close #<!-- -->98744
---
Full diff: https://github.com/llvm/llvm-project/pull/179156.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+2)
- (modified) clang/lib/Sema/SemaCoroutine.cpp (+2-1)
- (added) clang/test/CodeGenCoroutines/coro-gro3.cpp (+53)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 100474a5a1777..8c23db752287f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -136,6 +136,8 @@ Bug Fixes to Attribute Support
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
+- Fix initialization of GRO when GRO-return type mismatches, as part of CWG2563. (#GH98744)
+
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaCoroutine.cpp b/clang/lib/Sema/SemaCoroutine.cpp
index c0aba832dba94..065566ae02cf3 100644
--- a/clang/lib/Sema/SemaCoroutine.cpp
+++ b/clang/lib/Sema/SemaCoroutine.cpp
@@ -1865,7 +1865,8 @@ bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
} else {
GroDecl = VarDecl::Create(
S.Context, &FD, FD.getLocation(), FD.getLocation(),
- &S.PP.getIdentifierTable().get("__coro_gro"), GroType,
+ &S.PP.getIdentifierTable().get("__coro_gro"),
+ S.BuildDecltypeType(ReturnValue),
S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None);
GroDecl->setImplicit();
diff --git a/clang/test/CodeGenCoroutines/coro-gro3.cpp b/clang/test/CodeGenCoroutines/coro-gro3.cpp
new file mode 100644
index 0000000000000..8c37b1e9838d3
--- /dev/null
+++ b/clang/test/CodeGenCoroutines/coro-gro3.cpp
@@ -0,0 +1,53 @@
+// Tests defination of get-return-object-invocation [dcl.fct.def.coroutine] (and CWG2563)
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -emit-llvm %s -o - -disable-llvm-passes | FileCheck %s
+
+#include "Inputs/coroutine.h"
+
+using namespace std;
+
+extern "C" {
+void wrong();
+}
+
+template<bool LValue>
+struct Promise {
+ Promise() = default;
+ Promise(const Promise&) { wrong(); }
+ Promise(Promise&&) { wrong(); }
+
+ // Tests: decltype(auto) gro = promise.get_return_object();
+ auto&& get_return_object() {
+ if constexpr (LValue)
+ return *this;
+ else
+ return static_cast<Promise&&>(*this);
+ }
+ std::suspend_never initial_suspend() const { return {}; }
+ std::suspend_never final_suspend() const noexcept { return {}; }
+ void return_void() const {}
+ void unhandled_exception() const noexcept {}
+};
+
+template<bool LValue>
+struct Handle {
+ using promise_type = Promise<LValue>;
+
+ Handle(promise_type& p) {
+ if constexpr (!LValue)
+ wrong();
+ }
+ Handle(promise_type&& p) {
+ if constexpr (LValue)
+ wrong();
+ }
+};
+
+Handle<true> lvalue() {
+ co_return;
+}
+
+Handle<false> rvalue() {
+ co_return;
+}
+
+// CHECK-NOT: call void @wrong
``````````
</details>
https://github.com/llvm/llvm-project/pull/179156
More information about the cfe-commits
mailing list