public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9520] c++: when delegating constructor throws [PR103711]
@ 2022-01-28  4:38 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2022-01-28  4:38 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:2aa4400cb9830da0a335a8cbb75183706714b28d

commit r11-9520-g2aa4400cb9830da0a335a8cbb75183706714b28d
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jan 5 19:39:48 2022 -0500

    c++: when delegating constructor throws [PR103711]
    
    We were always calling the complete destructor if the target constructor
    throws, even if we were calling the base constructor.
    
            PR c++/103711
    
    gcc/cp/ChangeLog:
    
            * init.c (perform_target_ctor): Select destructor by in_chrg.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/eh/delegating1.C: New test.

Diff:
---
 gcc/cp/init.c                         | 10 ++++++++++
 gcc/testsuite/g++.dg/eh/delegating1.C | 28 ++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 3c4aa5846d6..c73afc08f04 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -541,6 +541,16 @@ perform_target_ctor (tree init)
 				|LOOKUP_NONVIRTUAL
 				|LOOKUP_DESTRUCTOR,
 				0, tf_warning_or_error);
+      if (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl))
+	{
+	  tree base = build_delete (input_location,
+				    type, decl, sfk_base_destructor,
+				    LOOKUP_NORMAL
+				    |LOOKUP_NONVIRTUAL
+				    |LOOKUP_DESTRUCTOR,
+				    0, tf_warning_or_error);
+	  expr = build_if_in_charge (expr, base);
+	}
       if (expr != error_mark_node
 	  && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
 	finish_eh_cleanup (expr);
diff --git a/gcc/testsuite/g++.dg/eh/delegating1.C b/gcc/testsuite/g++.dg/eh/delegating1.C
new file mode 100644
index 00000000000..c33374a3b6b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/delegating1.C
@@ -0,0 +1,28 @@
+// PR c++/103711
+// { dg-do run { target c++11 } }
+
+int constructions = 0;
+int destructions = 0;
+
+struct A
+{
+  A() { constructions++; }
+  virtual ~A() { destructions++; }
+};
+
+struct B : public virtual A
+{
+  B(int) { }
+  B() : B(1) { throw -1; }
+  virtual ~B() = default;
+};
+
+struct C : public B { };
+
+int main() {
+  try {
+    C c;
+  }
+  catch (int) {}
+  return (constructions - destructions);
+}


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

only message in thread, other threads:[~2022-01-28  4:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-28  4:38 [gcc r11-9520] c++: when delegating constructor throws [PR103711] 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).