[pushed] c++: assignment to temporary [PR59950]

Message ID 20220125192941.885833-1-jason@redhat.com
State Committed
Commit fe5cee6f62a0b229d9d51616b7490331d39b5ddd
Headers
Series [pushed] c++: assignment to temporary [PR59950] |

Commit Message

Jason Merrill Jan. 25, 2022, 7:29 p.m. UTC
  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.

Tested x86_64-pc-linux-gnu, applying to trunk.

	PR c++/59950

gcc/cp/ChangeLog:

	* call.cc (build_over_call): Use cp_build_indirect_ref.

gcc/testsuite/ChangeLog:

	* g++.dg/init/assign2.C: New test.
---
 gcc/cp/call.cc                      | 5 ++++-
 gcc/testsuite/g++.dg/init/assign2.C | 6 ++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/init/assign2.C


base-commit: aeac414923aa1e87986c7fc6f9b921d89a9b86cf
  

Patch

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index f7f861cd16e..bc157cdd1fb 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -9789,7 +9789,10 @@  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_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);
       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
index 00000000000..72d1264f3c9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/assign2.C
@@ -0,0 +1,6 @@ 
+// PR c++/59950
+
+ struct Foo {};
+
+ int f(Foo *p);
+ int n = f(&(Foo() = Foo()));