From: Jason Merrill <jason@redhat.com>
To: gcc-patches@gcc.gnu.org
Subject: [pushed 11/11] c++: when delegating constructor throws [PR103711]
Date: Thu, 6 Jan 2022 19:21:56 -0500 [thread overview]
Message-ID: <20220107002156.2992278-11-jason@redhat.com> (raw)
In-Reply-To: <20220107002156.2992278-1-jason@redhat.com>
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 | 10 ++++++++++
gcc/testsuite/g++.dg/eh/delegating1.C | 28 +++++++++++++++++++++++++++
2 files changed, 38 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/eh/delegating1.C
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index bfe4ad464bf..c932699ffa6 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -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
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);
+}
--
2.27.0
prev parent reply other threads:[~2022-01-07 0:22 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-07 0:21 [pushed 01/11] c++: don't preevaluate new-initializer Jason Merrill
2022-01-07 0:21 ` [pushed 02/11] c++: loop over array elts w/o explicit init [PR92385] Jason Merrill
2022-01-07 0:21 ` [pushed 03/11] c++: temporary lifetime with aggregate init [PR94041] Jason Merrill
2022-01-07 0:21 ` [pushed 04/11] c++: temporary lifetime with array aggr " Jason Merrill
2022-01-07 0:21 ` [pushed 05/11] c++: EH and partially constructed aggr temp [PR66139] Jason Merrill
2022-01-07 0:21 ` [pushed 06/11] c++: don't cleanup the last aggregate elt Jason Merrill
2022-01-07 0:21 ` [pushed 07/11] c++: keep destroying array after one dtor throws [PR66451] Jason Merrill
2022-01-07 0:21 ` [pushed 08/11] c++: clean up ref-extended temp on throwing dtor [PR53868] Jason Merrill
2022-01-07 0:21 ` [pushed 09/11] c++: destroy retval on throwing cleanup in try [PR33799] Jason Merrill
2022-01-07 0:21 ` [pushed 10/11] c++: nested catch in ctor fn-try-block [PR61611] Jason Merrill
2022-01-07 0:21 ` Jason Merrill [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220107002156.2992278-11-jason@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).