From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id DE95B3992003; Fri, 9 Jul 2021 20:14:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE95B3992003 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-8715] c++: missing dtor with -fno-elide-constructors [PR100838] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 542d90bfd802274f38637aaaffc2dd1de62c9ec6 X-Git-Newrev: 5830fffacd05463c20b592bb6ed20e333d7d272b Message-Id: <20210709201426.DE95B3992003@sourceware.org> Date: Fri, 9 Jul 2021 20:14:26 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 09 Jul 2021 20:14:27 -0000 https://gcc.gnu.org/g:5830fffacd05463c20b592bb6ed20e333d7d272b commit r11-8715-g5830fffacd05463c20b592bb6ed20e333d7d272b Author: Jason Merrill 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. For GCC 11, limit the changes to -fno-elide-constructors. PR c++/100838 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. Diff: --- gcc/cp/call.c | 8 ++++++-- gcc/testsuite/g++.dg/init/no-elide2.C | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/gcc/cp/call.c b/gcc/cp/call.c index 4f1565dc68f..41e13cae0cc 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -4108,7 +4108,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." @@ -4117,6 +4117,7 @@ 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) + && (flag_elide_constructors || (flags & LOOKUP_ONLYCONVERTING)) && !(convflags & LOOKUP_NO_TEMP_BIND)) cand->second_conv = build_conv (ck_rvalue, totype, cand->second_conv); @@ -7804,10 +7805,13 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum, break; }; + tsubst_flags_t sub_complain = complain; + if (!flag_elide_constructors) + sub_complain &= ~tf_no_cleanup; expr = convert_like (next_conversion (convs), expr, fn, argnum, convs->kind == ck_ref_bind ? issue_conversion_warnings : false, - c_cast_p, complain); + c_cast_p, sub_complain); if (expr == error_mark_node) return error_mark_node; 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(); +}