aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2021-05-31 12:36:25 -0400
committerJason Merrill <jason@redhat.com>2021-06-02 14:40:20 -0400
commit63d182b29306e582bfb151cf762820211ea1cc7e (patch)
tree920f37acf7dc517abab88adcdb035293a121d340
parent659cc7d6320aae7ab390b5886f0efed22f78e244 (diff)
downloadgcc-63d182b29306e582bfb151cf762820211ea1cc7e.zip
gcc-63d182b29306e582bfb151cf762820211ea1cc7e.tar.gz
gcc-63d182b29306e582bfb151cf762820211ea1cc7e.tar.bz2
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 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.
-rw-r--r--gcc/cp/call.c6
-rw-r--r--gcc/testsuite/g++.dg/init/no-elide2.C32
2 files changed, 36 insertions, 2 deletions
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 90192b1..17fc60c 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -4110,7 +4110,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."
@@ -4119,6 +4119,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);
@@ -7800,7 +7802,7 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
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, complain & ~tf_no_cleanup);
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 0000000..9a0ba19
--- /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();
+}