public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ PATCH] Don't allow taking the address of an rvalue reference (PR c++/35153)
@ 2008-02-15 21:01 Doug Gregor
  2008-02-19  1:09 ` Mark Mitchell
  0 siblings, 1 reply; 4+ messages in thread
From: Doug Gregor @ 2008-02-15 21:01 UTC (permalink / raw)
  To: GCC Patches

[-- Attachment #1: Type: text/plain, Size: 932 bytes --]

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.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: address-of-rref.patch --]
[-- Type: text/x-patch; name=address-of-rref.patch, Size: 867 bytes --]

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" }

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-02-19  1:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-15 21:01 [C++ PATCH] Don't allow taking the address of an rvalue reference (PR c++/35153) Doug Gregor
2008-02-19  1:09 ` Mark Mitchell
2008-02-19  1:17   ` Doug Gregor
2008-02-19  1:24     ` Mark Mitchell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).