https://github.com/NewSigma created 
https://github.com/llvm/llvm-project/pull/179156

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

>From 804a2eea1baa4839dbbac8ac58dbfb82d245316a Mon Sep 17 00:00:00 2001
From: NewSigma <[email protected]>
Date: Mon, 2 Feb 2026 10:27:07 +0800
Subject: [PATCH] [clang][Sema] Fix initialization of GRO when GRO-return type
 mismatches (CWG2563)

---
 clang/docs/ReleaseNotes.rst                |  2 +
 clang/lib/Sema/SemaCoroutine.cpp           |  3 +-
 clang/test/CodeGenCoroutines/coro-gro3.cpp | 53 ++++++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CodeGenCoroutines/coro-gro3.cpp

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

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to