c++: when delegating constructor throws [PR103711]
authorJason Merrill <jason@redhat.com>
Thu, 6 Jan 2022 00:39:48 +0000 (19:39 -0500)
committerJason Merrill <jason@redhat.com>
Fri, 7 Jan 2022 00:25:43 +0000 (19:25 -0500)
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.

gcc/cp/init.c
gcc/testsuite/g++.dg/eh/delegating1.C [new file with mode: 0644]

index bfe4ad4..c932699 100644 (file)
@@ -545,6 +545,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 (file)
index 0000000..c33374a
--- /dev/null
@@ -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);
+}