https://gcc.gnu.org/g:7c5a9bf1d206fe20cb050200d4a30f11c76b1b19

commit r15-2210-g7c5a9bf1d206fe20cb050200d4a30f11c76b1b19
Author: Patrick Palka <ppa...@redhat.com>
Date:   Mon Jul 22 21:30:49 2024 -0400

    c++/coroutines: correct passing *this to promise type [PR104981]
    
    When passing *this to the promise type ctor (or to its operator new)
    (as per [dcl.fct.def.coroutine]/4), we add an explicit cast to lvalue
    reference.  But this is unnecessary since *this is already always an
    lvalue.  And doing so means we need to call convert_from_reference
    afterward to lower the reference expression to an implicit dereference,
    which we're currently neglecting to do and which causes overload
    resolution to get confused when computing argument conversions.
    
    So this patch removes this unneeded reference cast when passing *this
    to the promise ctor, and removes both the cast and implicit deref when
    passing *this to operator new, for consistency.  While we're here, use
    cp_build_fold_indirect_ref instead of directly building INDIRECT_REF.
    
            PR c++/104981
            PR c++/115550
    
    gcc/cp/ChangeLog:
    
            * coroutines.cc (morph_fn_to_coro): Remove unneeded calls
            to convert_to_reference and convert_from_reference when
            passing *this.  Use cp_build_fold_indirect_ref instead
            of directly building INDIRECT_REF.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/coroutines/pr104981-preview-this.C: New test.
            * g++.dg/coroutines/pr115550-preview-this.C: New test.
    
    Reviewed-by: Iain Sandoe <i...@sandoe.co.uk>
    Reviewed-by: Jason Merrill <ja...@redhat.com>

Diff:
---
 gcc/cp/coroutines.cc                               | 18 ++-------
 .../g++.dg/coroutines/pr104981-preview-this.C      | 34 ++++++++++++++++
 .../g++.dg/coroutines/pr115550-preview-this.C      | 47 ++++++++++++++++++++++
 3 files changed, 84 insertions(+), 15 deletions(-)

diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc
index f350fc33e9b4..e8f028df3adb 100644
--- a/gcc/cp/coroutines.cc
+++ b/gcc/cp/coroutines.cc
@@ -4620,13 +4620,8 @@ morph_fn_to_coro (tree orig, tree *resumer, tree 
*destroyer)
          if (parm_i->this_ptr || parm_i->lambda_cobj)
            {
              /* We pass a reference to *this to the allocator lookup.  */
-             tree tt = TREE_TYPE (TREE_TYPE (arg));
-             tree this_ref = build1 (INDIRECT_REF, tt, arg);
-             tt = cp_build_reference_type (tt, false);
-             this_ref = convert_to_reference (tt, this_ref, CONV_STATIC,
-                                              LOOKUP_NORMAL , NULL_TREE,
-                                              tf_warning_or_error);
-             vec_safe_push (args, convert_from_reference (this_ref));
+             tree this_ref = cp_build_fold_indirect_ref (arg);
+             vec_safe_push (args, this_ref);
            }
          else
            vec_safe_push (args, convert_from_reference (arg));
@@ -4845,14 +4840,7 @@ morph_fn_to_coro (tree orig, tree *resumer, tree 
*destroyer)
          if (parm.this_ptr || parm.lambda_cobj)
            {
              /* We pass a reference to *this to the param preview.  */
-             tree tt = TREE_TYPE (arg);
-             gcc_checking_assert (POINTER_TYPE_P (tt));
-             tree ct = TREE_TYPE (tt);
-             tree this_ref = build1 (INDIRECT_REF, ct, arg);
-             tree rt = cp_build_reference_type (ct, false);
-             this_ref = convert_to_reference (rt, this_ref, CONV_STATIC,
-                                              LOOKUP_NORMAL, NULL_TREE,
-                                              tf_warning_or_error);
+             tree this_ref = cp_build_fold_indirect_ref (arg);
              vec_safe_push (promise_args, this_ref);
            }
          else if (parm.rv_ref)
diff --git a/gcc/testsuite/g++.dg/coroutines/pr104981-preview-this.C 
b/gcc/testsuite/g++.dg/coroutines/pr104981-preview-this.C
new file mode 100644
index 000000000000..81eb963db4a5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/coroutines/pr104981-preview-this.C
@@ -0,0 +1,34 @@
+// PR c++/104981 - ICE from convert_to_base when passing *this to promise ctor
+
+#include <coroutine>
+
+class Base {};
+
+struct PromiseType;
+
+struct Result {
+ using promise_type = PromiseType;
+};
+
+struct PromiseType {
+  PromiseType(const Base& parser, auto&&...) {}
+
+  Result get_return_object() { return {}; }
+
+  static std::suspend_never initial_suspend() { return {}; }
+  static std::suspend_always final_suspend() noexcept { return {}; }
+  [[noreturn]] static void unhandled_exception() { throw; }
+
+  void return_value(int) {}
+};
+
+struct Derived : Base {
+  Result f() {
+   co_return 42;
+  }
+};
+
+int main() {
+  Derived d;
+  d.f();
+}
diff --git a/gcc/testsuite/g++.dg/coroutines/pr115550-preview-this.C 
b/gcc/testsuite/g++.dg/coroutines/pr115550-preview-this.C
new file mode 100644
index 000000000000..f62c07096b61
--- /dev/null
+++ b/gcc/testsuite/g++.dg/coroutines/pr115550-preview-this.C
@@ -0,0 +1,47 @@
+// PR c++/115550 - wrong deduction when passing *this to promise ctor template
+
+#include <coroutine>
+
+template <typename T> struct remove_reference { using type = T; };
+template <typename T> struct remove_reference<T&> { using type = T; };
+template <typename T> struct remove_reference<T&&> { using type = T; };
+template <typename T> using remove_reference_t = remove_reference<T>::type;
+
+template <typename, typename>
+struct is_same { static inline constexpr bool value = false; };
+template <typename T>
+struct is_same<T, T> { static inline constexpr bool value = true; };
+
+template <typename T, typename U>
+concept same_as = is_same<T, U>::value;
+
+struct coroutine
+{
+    struct promise_type
+    {
+        template <typename Arg>
+        explicit promise_type(Arg&&)
+        {
+            static_assert(same_as<
+                remove_reference_t<remove_reference_t<Arg>>,
+                remove_reference_t<Arg>
+            >);
+        }
+
+        coroutine get_return_object() { return {}; }
+
+        std::suspend_never initial_suspend() noexcept { return {}; }
+        std::suspend_never final_suspend() noexcept { return {}; }
+
+        void return_void() {}
+        void unhandled_exception() {throw;}
+    };
+};
+
+struct x
+{
+    coroutine f()
+    {
+        co_return;
+    }
+};

Reply via email to