public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: throwing dtor and empty try [PR113088]
@ 2023-12-20 17:31 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2023-12-20 17:31 UTC (permalink / raw)
  To: gcc-patches

Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

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.
---
 gcc/cp/except.cc                  |  8 ++++++--
 gcc/testsuite/g++.dg/eh/return2.C | 26 ++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/eh/return2.C

diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc
index 7208c084edf..e4ed2771778 100644
--- a/gcc/cp/except.cc
+++ b/gcc/cp/except.cc
@@ -1357,9 +1357,13 @@ maybe_splice_retval_cleanup (tree compound_stmt, bool is_try)
 	return;
 
       /* 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
index 00000000000..b467d7494af
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/return2.C
@@ -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 (...) { }
+}

base-commit: a3ff76278efe006dc0b50249c8e5baf565bff56b
-- 
2.39.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-12-20 17:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-20 17:31 [pushed] c++: throwing dtor and empty try [PR113088] Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).