public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-10727] c++: missing dtor with -fno-elide-constructors [PR100838] Date: Thu, 12 May 2022 21:24:35 +0000 (GMT) [thread overview] Message-ID: <20220512212435.7DC61385043C@sourceware.org> (raw) https://gcc.gnu.org/g:67f742536cbb60d5f8b7bba2cac0a141191e29af commit r10-10727-g67f742536cbb60d5f8b7bba2cac0a141191e29af Author: Jason Merrill <jason@redhat.com> Date: Mon May 31 12:36:25 2021 -0400 c++: missing dtor with -fno-elide-constructors [PR100838] tf_no_cleanup only applies to the outermost TARGET_EXPR, and we already clear it for nested calls in build_over_call, but in this case both constructor calls came from convert_like, so we need to clear it in the recursive call as well. This revealed that we were adding an extra ck_rvalue in direct-initialization cases where it was wrong. PR c++/100838 PR c++/105265 gcc/cp/ChangeLog: * call.c (convert_like_internal): Clear tf_no_cleanup when recursing. (build_user_type_conversion_1): Only add ck_rvalue if LOOKUP_ONLYCONVERTING. gcc/testsuite/ChangeLog: * g++.dg/init/no-elide2.C: New test. * g++.dg/cpp0x/initlist-new6.C: New test. Diff: --- gcc/cp/call.c | 6 +++-- gcc/testsuite/g++.dg/cpp0x/initlist-new6.C | 39 ++++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/init/no-elide2.C | 32 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 5a4f93527bd..21ded9b3938 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -4063,7 +4063,7 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags, { cand->second_conv = build_identity_conv (totype, NULL_TREE); - /* If totype isn't a reference, and LOOKUP_NO_TEMP_BIND isn't + /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is set, then this is copy-initialization. In that case, "The result of the call is then used to direct-initialize the object that is the destination of the copy-initialization." @@ -4072,6 +4072,8 @@ build_user_type_conversion_1 (tree totype, tree expr, int flags, We represent this in the conversion sequence with an rvalue conversion, which means a constructor call. */ if (!TYPE_REF_P (totype) + && cxx_dialect < cxx17 + && (flags & LOOKUP_ONLYCONVERTING) && !(convflags & LOOKUP_NO_TEMP_BIND)) cand->second_conv = build_conv (ck_rvalue, totype, cand->second_conv); @@ -7678,7 +7680,7 @@ convert_like_real_1 (conversion *convs, tree expr, tree fn, int argnum, expr = convert_like_real (next_conversion (convs), expr, fn, argnum, convs->kind == ck_ref_bind ? issue_conversion_warnings : false, - c_cast_p, complain); + c_cast_p, complain & ~tf_no_cleanup); if (expr == error_mark_node) return error_mark_node; diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-new6.C b/gcc/testsuite/g++.dg/cpp0x/initlist-new6.C new file mode 100644 index 00000000000..0ef27806acf --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-new6.C @@ -0,0 +1,39 @@ +// PR c++/105265 +// { dg-do run { target c++11 } } + +int c; + +class Block +{ +public: + Block(int n) : data{new char[n]}, size{n} + { + ++c; + } + + ~Block() + { + --c; + delete[] data; + } + +private: + char* data; + int size; +}; + +struct Cargo +{ + Block const& block; +}; + +int main() +{ + { + Cargo* c = new Cargo{{4000}}; + delete c; + } + if (c != 0) + __builtin_abort (); + return 0; +} diff --git a/gcc/testsuite/g++.dg/init/no-elide2.C b/gcc/testsuite/g++.dg/init/no-elide2.C new file mode 100644 index 00000000000..9a0ba1936ab --- /dev/null +++ b/gcc/testsuite/g++.dg/init/no-elide2.C @@ -0,0 +1,32 @@ +// PR c++/100838 +// { dg-do run } +// { dg-additional-options -fno-elide-constructors } + +extern "C" int puts (const char *); + +int c,d; +class MyString { +public: + MyString(const char* s = "") { + puts ("ctor"); + ++c; + } + ~MyString() { + puts ("dtor"); + ++d; + } + MyString(const MyString& s) { + puts ("copy ctor"); + ++c; + } + MyString& operator=(const MyString& s); +}; + +int main() { + { + MyString s1 = "Hello"; + puts ("main"); + } + if (c != d) + __builtin_abort(); +}
reply other threads:[~2022-05-12 21:24 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20220512212435.7DC61385043C@sourceware.org \ --to=jason@gcc.gnu.org \ --cc=gcc-cvs@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: linkBe 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).