]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/coroutines: fix passing *this to promise type, again [PR116327]
authorPatrick Palka <ppalka@redhat.com>
Thu, 15 Aug 2024 14:20:18 +0000 (10:20 -0400)
committerPatrick Palka <ppalka@redhat.com>
Thu, 15 Aug 2024 14:20:18 +0000 (10:20 -0400)
In r15-2210 we got rid of the unnecessary cast to lvalue reference when
passing *this to the promise type ctor, and as a drive-by change we also
simplified the code to use cp_build_fold_indirect_ref.

But it turns out cp_build_fold_indirect_ref does too much here, namely
it has a shortcut for returning current_class_ref if the operand is
current_class_ptr.  The problem with that shortcut is current_class_ref
might have gotten clobbered earlier if it appeared in the function body,
since rewrite_param_uses walks and rewrites in-place all local variable
uses to their corresponding frame copy.

So later cp_build_fold_indirect_ref for *this will instead return the
clobbered current_class_ref i.e. *frame_ptr->this, which doesn't make
sense here since we're in the ramp function and not the actor function
where frame_ptr is in scope.

This patch fixes this by using the build_fold_indirect_ref instead of
cp_build_fold_indirect_ref.

PR c++/116327
PR c++/104981
PR c++/115550

gcc/cp/ChangeLog:

* coroutines.cc (morph_fn_to_coro): Use build_fold_indirect_ref
instead of cp_build_fold_indirect_ref.

gcc/testsuite/ChangeLog:

* g++.dg/coroutines/pr104981-preview-this.C: Improve coverage by
adding a non-static data member use within the coroutine member
function.
* g++.dg/coroutines/pr116327-preview-this.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/coroutines.cc
gcc/testsuite/g++.dg/coroutines/pr104981-preview-this.C
gcc/testsuite/g++.dg/coroutines/pr116327-preview-this.C [new file with mode: 0644]

index 145ec4b1d16bafb2f7860318d00dc29a9e6bf0f1..f7791cbfb9a682b093954fd7dde5d0c863574bbe 100644 (file)
@@ -4850,7 +4850,9 @@ 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 this_ref = cp_build_fold_indirect_ref (arg);
+             /* It's unsafe to use the cp_ version here since current_class_ref
+                might've gotten clobbered earlier during rewrite_param_uses.  */
+             tree this_ref = build_fold_indirect_ref (arg);
              vec_safe_push (args, this_ref);
            }
          else
@@ -5070,7 +5072,9 @@ 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 this_ref = cp_build_fold_indirect_ref (arg);
+             /* It's unsafe to use the cp_ version here since current_class_ref
+                might've gotten clobbered earlier during rewrite_param_uses.  */
+             tree this_ref = build_fold_indirect_ref (arg);
              vec_safe_push (promise_args, this_ref);
            }
          else if (parm.rv_ref)
index 81eb963db4a59808e101f09a8c6c5bcbf82b94e5..9f1e3970ce35b0c239f326c5df57e82a97f7829c 100644 (file)
@@ -23,8 +23,10 @@ struct PromiseType {
 };
 
 struct Derived : Base {
+  int m = 41;
   Result f() {
-   co_return 42;
+   ++m;
+   co_return m;
   }
 };
 
diff --git a/gcc/testsuite/g++.dg/coroutines/pr116327-preview-this.C b/gcc/testsuite/g++.dg/coroutines/pr116327-preview-this.C
new file mode 100644 (file)
index 0000000..27b69a4
--- /dev/null
@@ -0,0 +1,22 @@
+// PR c++/116327 - ICE in coroutine with parameter preview on lambda with captures
+
+#include <coroutine>
+
+struct coroutine{
+  struct promise_type{
+    promise_type(const auto &...){}
+    std::suspend_never initial_suspend(){ return {}; }
+    std::suspend_always final_suspend()noexcept{ return {}; }
+    void unhandled_exception(){}
+    coroutine get_return_object(){ return {}; }
+    void return_value(int)noexcept{}
+  };
+};
+
+int main(){
+  auto f = [a=0](auto) -> coroutine {
+    co_return 2;
+  };
+  f(0);
+  return 0;
+}