]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: assignment to temporary [PR59950]
authorJason Merrill <jason@redhat.com>
Mon, 24 Jan 2022 05:01:40 +0000 (00:01 -0500)
committerJason Merrill <jason@redhat.com>
Thu, 12 May 2022 21:15:34 +0000 (17:15 -0400)
Given build_this of a TARGET_EXPR, cp_build_fold_indirect_ref returns the
TARGET_EXPR.  But that's the wrong value category for the result of the
defaulted class assignment operator, which returns an lvalue, so we need to
actually build the INDIRECT_REF.

PR c++/59950

gcc/cp/ChangeLog:

* call.c (build_over_call): Use cp_build_indirect_ref.

gcc/testsuite/ChangeLog:

* g++.dg/init/assign2.C: New test.

gcc/cp/call.c
gcc/testsuite/g++.dg/init/assign2.C [new file with mode: 0644]

index 49eb673ebccf6aedcb30bbd1d7b94c2e5f40bac9..5a4f93527bd6e1119ebc772f51474f28090a2a62 100644 (file)
@@ -9081,8 +9081,11 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
           && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
           && trivial_fn_p (fn))
     {
-      tree to = cp_stabilize_reference
-       (cp_build_fold_indirect_ref (argarray[0]));
+      /* Don't use cp_build_fold_indirect_ref, op= returns an lvalue even if
+        the object argument isn't one.  */
+      tree to = cp_build_indirect_ref (input_location, argarray[0],
+                                      RO_ARROW, complain);
+      to = cp_stabilize_reference (to);
       tree type = TREE_TYPE (to);
       tree as_base = CLASSTYPE_AS_BASE (type);
       tree arg = argarray[1];
diff --git a/gcc/testsuite/g++.dg/init/assign2.C b/gcc/testsuite/g++.dg/init/assign2.C
new file mode 100644 (file)
index 0000000..72d1264
--- /dev/null
@@ -0,0 +1,6 @@
+// PR c++/59950
+
+ struct Foo {};
+
+ int f(Foo *p);
+ int n = f(&(Foo() = Foo()));