]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: TARGET_EXPR_ELIDING_P and std::move [PR107267]
authorJason Merrill <jason@redhat.com>
Mon, 23 Jan 2023 22:14:11 +0000 (17:14 -0500)
committerJason Merrill <jason@redhat.com>
Mon, 23 Jan 2023 23:35:11 +0000 (18:35 -0500)
With -ffold-simple-inlines, we turn calls to std::move into the static_cast
equivalent.  In this testcase, this exposes the FindResult temporary to copy
elision which is not specified by the standard, through an optimization in
gimplify_modify_expr_rhs.  Since the type is not TREE_ADDRESSABLE, this is
not detectable by the user, so we just need to soften the assert.

PR c++/107267

gcc/cp/ChangeLog:

* cp-gimplify.cc (cp_gimplify_init_expr): Allow unexpected elision
of trivial types.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/move2.C: New test.

gcc/cp/cp-gimplify.cc
gcc/testsuite/g++.dg/cpp0x/move2.C [new file with mode: 0644]

index 340b464104632937d140780dbc069f369bc63cb7..83ba1285bfd1bf83b81fd44d0e36ea305cfdc5bd 100644 (file)
@@ -250,7 +250,10 @@ cp_gimplify_init_expr (tree *expr_p)
   if (TREE_CODE (from) == TARGET_EXPR)
     if (tree init = TARGET_EXPR_INITIAL (from))
       {
-       gcc_checking_assert (TARGET_EXPR_ELIDING_P (from));
+       /* Make sure that we expected to elide this temporary.  But also allow
+          gimplify_modify_expr_rhs to elide temporaries of trivial type.  */
+       gcc_checking_assert (TARGET_EXPR_ELIDING_P (from)
+                            || !TREE_ADDRESSABLE (TREE_TYPE (from)));
        if (target_expr_needs_replace (from))
          {
            /* If this was changed by cp_genericize_target_expr, we need to
diff --git a/gcc/testsuite/g++.dg/cpp0x/move2.C b/gcc/testsuite/g++.dg/cpp0x/move2.C
new file mode 100644 (file)
index 0000000..b8c8683
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/107267
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -ffold-simple-inlines }
+
+namespace std {
+  template<typename _Tp> _Tp &&move(_Tp &&);
+}
+
+struct FindResult {
+  FindResult();
+  int result;
+};
+
+FindResult pop_ret = std::move(FindResult());