This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] Don't allow taking the address of an rvalue reference (PR c++/35153)


This tiny little patch makes sure that we don't try to take the
address of an rvalue. Before C++0x rvalue references, we had all of
the cases covered; now we can end up with a function call that returns
an rvalue reference. In this case, we'll wrap an INDIRECT_REF around
the call... and not diagnose when we've taken the address of that
rvalue. I think this bug is technically a regression, because we used
to reject the invalid code in the test case but now we accept it. This
patch fixes that problem.

Tested i686-pc-linux-gnu; okay for 4.3?

  - Doug

2008-02-15  Douglas Gregor  <doug.gregor@gmail.com>

	PR c++/35153
	* typeck.c (build_x_unary_op): Don't allow us to take the address
	of an rvalue that's hiding being an INDIRECT_REF. We can only end
	up triggering that error when we have some kind of rvalue
	reference.

2008-02-15  Douglas Gregor  <doug.gregor@gmail.com>

	PR c++/35153
	* g++.dg/cpp0x/rref-35153.C: New.
Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 132347)
+++ cp/typeck.c	(working copy)
@@ -4040,6 +4040,10 @@ build_x_unary_op (enum tree_code code, t
 	}
       else if (TREE_CODE (xarg) == TARGET_EXPR)
 	warning (0, "taking address of temporary");
+      else if (TREE_CODE (xarg) == INDIRECT_REF
+	       && real_lvalue_p (xarg) == clk_none)
+	error ("cannot take the address of rvalue %<%E%>", xarg);
+
       exp = build_unary_op (ADDR_EXPR, xarg, 0);
     }
 
Index: testsuite/g++.dg/cpp0x/rref-35153.C
===================================================================
--- testsuite/g++.dg/cpp0x/rref-35153.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/rref-35153.C	(revision 0)
@@ -0,0 +1,3 @@
+// { dg-options "-std=c++0x" }
+int && f();
+void g() { &(f()); } // { dg-error "address of rvalue" }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]