]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: fix missing lifetime extension [PR119383]
authorMarek Polacek <polacek@redhat.com>
Tue, 25 Mar 2025 17:36:24 +0000 (13:36 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 1 Apr 2025 17:43:40 +0000 (13:43 -0400)
Since r15-8011 cp_build_indirect_ref_1 won't do the *&TARGET_EXPR ->
TARGET_EXPR folding not to change its value category.  That fix seems
correct but it made us stop extending the lifetime in this testcase,
causing a wrong-code issue -- extend_ref_init_temps_1 did not see
through the extra *& because it doesn't use a tree walk.

This patch reverts r15-8011 and instead handles the problem in
build_over_call by calling force_lvalue in the is_really_empty_class
case as well as in the general case.

PR c++/119383

gcc/cp/ChangeLog:

* call.cc (build_over_call): Use force_lvalue to ensure op= returns
an lvalue.
* cp-tree.h (force_lvalue): Declare.
* cvt.cc (force_lvalue): New.
* typeck.cc (cp_build_indirect_ref_1): Revert r15-8011.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/temp-extend3.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
(cherry picked from commit e9803f10c9f376f6d091e7ef3ad6e1c92e7c8e8c)

gcc/cp/call.cc
gcc/cp/cp-tree.h
gcc/cp/cvt.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp0x/temp-extend3.C [new file with mode: 0644]

index 180afa6d053dffe733328bd7ccb3c35dfe14de6b..604b0b003a323e3a2043abdf97aa771c6638d31b 100644 (file)
@@ -10519,10 +10519,8 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       if (is_really_empty_class (type, /*ignore_vptr*/true))
        {
          /* Avoid copying empty classes, but ensure op= returns an lvalue even
-            if the object argument isn't one. This isn't needed in other cases
-            since MODIFY_EXPR is always considered an lvalue.  */
-         to = cp_build_addr_expr (to, tf_none);
-         to = cp_build_indirect_ref (input_location, to, RO_ARROW, complain);
+            if the object argument isn't one.  */
+         to = force_lvalue (to, complain);
          val = build2 (COMPOUND_EXPR, type, arg, to);
          suppress_warning (val, OPT_Wunused);
        }
@@ -10543,6 +10541,9 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
          tree array_type, alias_set;
 
          arg2 = TYPE_SIZE_UNIT (as_base);
+         /* Ensure op= returns an lvalue even if the object argument isn't
+            one.  */
+         to = force_lvalue (to, complain);
          to = cp_stabilize_reference (to);
          arg0 = cp_build_addr_expr (to, complain);
 
index a9a14d6267c8ce2f25e01b87582c369d7b69d747..cfdfc4f9abe75ff22b030c2adc8de3df41df8603 100644 (file)
@@ -6948,6 +6948,7 @@ extern tree convert_to_reference          (tree, tree, int, int, tree,
                                                 tsubst_flags_t);
 extern tree convert_from_reference             (tree);
 extern tree force_rvalue                       (tree, tsubst_flags_t);
+extern tree force_lvalue                       (tree, tsubst_flags_t);
 extern tree ocp_convert                                (tree, tree, int, int,
                                                 tsubst_flags_t);
 extern tree cp_convert                         (tree, tree, tsubst_flags_t);
index db086c017e893858ef5027c20c8a6357f9c79ac3..b046010e7ff8d821e96f827c16cda8b4249fe3c5 100644 (file)
@@ -574,6 +574,19 @@ force_rvalue (tree expr, tsubst_flags_t complain)
   return expr;
 }
 
+/* Force EXPR to be an lvalue, if it isn't already.  */
+
+tree
+force_lvalue (tree expr, tsubst_flags_t complain)
+{
+  if (!lvalue_p (expr))
+    {
+      expr = cp_build_addr_expr (expr, complain);
+      expr = cp_build_indirect_ref (input_location, expr, RO_ARROW, complain);
+    }
+  return expr;
+}
+
 \f
 /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
    TREE_OVERFLOW set only if it is set in ORIG.  Otherwise, return EXPR
index 6e938f168a4cbd117b8681fb8a88f24f69b8ccdf..7f97c9423f0a1ceca17e3155ca1fdf4a5c0e69b2 100644 (file)
@@ -3838,13 +3838,11 @@ cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
          return error_mark_node;
        }
       else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
-              && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0)))
-              /* Don't let this change the value category.  '*&TARGET_EXPR'
-                 is an lvalue, but folding it into 'TARGET_EXPR' would turn
-                 it into a prvalue of class type.  */
-              && lvalue_p (TREE_OPERAND (pointer, 0)))
+              && same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
        /* The POINTER was something like `&x'.  We simplify `*&x' to
-          `x'.  */
+          `x'.  This can change the value category: '*&TARGET_EXPR'
+          is an lvalue and folding it into 'TARGET_EXPR' turns it into
+          a prvalue of class type.  */
        return TREE_OPERAND (pointer, 0);
       else
        {
diff --git a/gcc/testsuite/g++.dg/cpp0x/temp-extend3.C b/gcc/testsuite/g++.dg/cpp0x/temp-extend3.C
new file mode 100644 (file)
index 0000000..3eab88d
--- /dev/null
@@ -0,0 +1,32 @@
+// PR c++/119383
+// { dg-do run { target c++11 } }
+
+int g;
+
+struct base {
+  virtual base *clone() const = 0;
+  ~base() { }
+};
+
+struct impl : virtual base {
+  base *clone() const { return new impl; }  // #1
+  impl() { ++g; }
+  ~impl() { --g; }
+};
+
+const base *
+make_a_clone ()
+{
+  const base &base = impl{}; // #2
+  return base.clone();
+}
+
+int
+main ()
+{
+  make_a_clone ();
+  // impl::impl() is called twice (#1 and #2), impl::~impl() once,
+  // at the end of make_a_clone.
+  if (g != 1)
+    __builtin_abort ();
+}