]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: throwing dtor and empty try [PR113088]
authorJason Merrill <jason@redhat.com>
Wed, 20 Dec 2023 16:06:27 +0000 (11:06 -0500)
committerJason Merrill <jason@redhat.com>
Wed, 24 Jan 2024 10:18:29 +0000 (05:18 -0500)
maybe_splice_retval_cleanup assumed that the function body can't be empty if
there's a throwing cleanup, but when I added cleanups to try blocks in
r12-6333-gb10e031458d541 I didn't adjust that assumption.

PR c++/113088
PR c++/33799

gcc/cp/ChangeLog:

* except.cc (maybe_splice_retval_cleanup): Handle an empty block.

gcc/testsuite/ChangeLog:

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

(cherry picked from commit 02c0b49798228d777610f898cd9d63ebec43656d)

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

index b9f49d1bfc6bee01a941af0f780f1196cc70c964..96e19b18a716cffd11e23fd723955db3fe311235 100644 (file)
@@ -1345,9 +1345,13 @@ maybe_splice_retval_cleanup (tree compound_stmt, bool is_try)
        }
 
       /* Skip past other decls, they can't contain a return.  */
-      while (TREE_CODE (tsi_stmt (iter)) == DECL_EXPR)
+      while (!tsi_end_p (iter)
+            && TREE_CODE (tsi_stmt (iter)) == DECL_EXPR)
        tsi_next (&iter);
-      gcc_assert (!tsi_end_p (iter));
+
+      if (tsi_end_p (iter))
+       /* Nothing to wrap.  */
+       return;
 
       /* Wrap the rest of the STATEMENT_LIST in a CLEANUP_STMT.  */
       tree stmts = NULL_TREE;
diff --git a/gcc/testsuite/g++.dg/eh/return2.C b/gcc/testsuite/g++.dg/eh/return2.C
new file mode 100644 (file)
index 0000000..b467d74
--- /dev/null
@@ -0,0 +1,26 @@
+// PR c++/113088
+
+#if __cplusplus >= 201103L
+#define THROWS noexcept(false)
+#else
+#define THROWS
+#endif
+
+struct X {
+  ~X() {}
+};
+
+struct Y {
+  ~Y() THROWS {}
+};
+
+X foo() {
+  try {
+    return X();
+  } catch (...) {
+    Y();
+    return X();
+  }
+
+  try { } catch (...) { }
+}