]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: throwing cleanup after return [PR113347]
authorJason Merrill <jason@redhat.com>
Tue, 23 Jan 2024 20:41:09 +0000 (15:41 -0500)
committerJason Merrill <jason@redhat.com>
Wed, 24 Jan 2024 19:39:50 +0000 (14:39 -0500)
Here we were assuming that current_retval_sentinel would be set if we have
seen a throwing cleanup, but that's not the case if the cleanup is after all
returns.

This change isn't needed on trunk, where current_retval_sentinel is set for
all NRV functions.

PR c++/113347

gcc/cp/ChangeLog:

* semantics.cc (finalize_nrv_r): Handle null
current_retval_sentinel.

gcc/testsuite/ChangeLog:

* g++.dg/eh/return3.C: New test.

(cherry picked from commit 713fbaff8b17a01d3b72110f89112851ed43a90a)

gcc/cp/semantics.cc
gcc/testsuite/g++.dg/eh/return3.C [new file with mode: 0644]

index c42402c7c141056b91a83dc1e39954fa09a452b9..e5db5189f33d6b9db284c45aa98282112cfa9755 100644 (file)
@@ -4887,7 +4887,8 @@ finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
       /* If a cleanup might throw, we need to clear current_retval_sentinel on
         the exception path so an outer cleanup added by
         maybe_splice_retval_cleanup doesn't run.  */
-      if (cp_function_chain->throwing_cleanup)
+      if (current_retval_sentinel
+         && cp_function_chain->throwing_cleanup)
        {
          tree clear = build2 (MODIFY_EXPR, boolean_type_node,
                               current_retval_sentinel,
diff --git a/gcc/testsuite/g++.dg/eh/return3.C b/gcc/testsuite/g++.dg/eh/return3.C
new file mode 100644 (file)
index 0000000..76aa50d
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/113347
+
+#if __cplusplus < 201103L
+#define THROWS
+#else
+#define THROWS noexcept(false)
+#endif
+
+struct A { ~A(); };
+struct B { ~B() THROWS; };
+
+A f()
+{
+  A a;
+  return a;
+  B();
+}