diff mbox

C++ PATCH for c++/49105 (rejects-valid with cast to const int&&)

Message ID 4DDAE413.9000707@redhat.com
State New
Headers show

Commit Message

Jason Merrill May 23, 2011, 10:47 p.m. UTC
The main problem in this PR was that build_const_cast_1 hadn't been 
updated to support rvalue references, so the first patch does that.  The 
second patch fixes a couple of minor issues that I noticed while 
investigating this; neither of them should affect observable semantics, 
but they match the standard better.

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

Patch

commit 93e0f8336fe098431665731a91c23647fcd2a29e
Author: Jason Merrill <jason@redhat.com>
Date:   Mon May 23 13:21:52 2011 -0400

    	PR c++/49105
    	* typeck.c (build_const_cast_1): Handle rvalue references.

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 81ee63d..13b919c 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -6241,14 +6241,29 @@  build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
 
   /* [expr.const.cast]
 
-     An lvalue of type T1 can be explicitly converted to an lvalue of
-     type T2 using the cast const_cast<T2&> (where T1 and T2 are object
-     types) if a pointer to T1 can be explicitly converted to the type
-     pointer to T2 using a const_cast.  */
+     For two object types T1 and T2, if a pointer to T1 can be explicitly
+     converted to the type "pointer to T2" using a const_cast, then the
+     following conversions can also be made:
+
+     -- an lvalue of type T1 can be explicitly converted to an lvalue of
+     type T2 using the cast const_cast<T2&>;
+
+     -- a glvalue of type T1 can be explicitly converted to an xvalue of
+     type T2 using the cast const_cast<T2&&>; and
+
+     -- if T1 is a class type, a prvalue of type T1 can be explicitly
+     converted to an xvalue of type T2 using the cast const_cast<T2&&>.  */
+
   if (TREE_CODE (dst_type) == REFERENCE_TYPE)
     {
       reference_type = dst_type;
-      if (! real_lvalue_p (expr))
+      if (!TYPE_REF_IS_RVALUE (dst_type)
+	  ? real_lvalue_p (expr)
+	  : (CLASS_TYPE_P (TREE_TYPE (dst_type))
+	     ? lvalue_p (expr)
+	     : lvalue_or_rvalue_with_address_p (expr)))
+	/* OK.  */;
+      else
 	{
 	  if (complain & tf_error)
 	    error ("invalid const_cast of an rvalue of type %qT to type %qT",
diff --git a/gcc/testsuite/g++.dg/cpp0x/rv-cast2.C b/gcc/testsuite/g++.dg/cpp0x/rv-cast2.C
new file mode 100644
index 0000000..94ee4ca
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/rv-cast2.C
@@ -0,0 +1,21 @@ 
+// Test for const_cast to reference (5.2.11/4).
+// { dg-options -std=c++0x }
+
+template <class T> T&& xval();
+template <class T> T& lval();
+template <class T> T prval();
+
+struct A { };
+
+int main()
+{
+  const_cast<int&>(lval<int>());
+  const_cast<int&>(xval<int>());   // { dg-error "" }
+  const_cast<int&>(prval<int>());  // { dg-error "" }
+  const_cast<int&&>(lval<int>());
+  const_cast<int&&>(xval<int>());
+  const_cast<int&&>(prval<int>()); // { dg-error "" }
+  const_cast<A&&>(lval<A>());
+  const_cast<A&&>(xval<A>());
+  const_cast<A&&>(prval<A>());
+}

commit 120577c5010612c5f7cf2828c165997b72990ddd
Author: Jason Merrill <jason@redhat.com>
Date:   Mon May 23 13:31:50 2011 -0400

    	PR c++/49105
    	* typeck.c (cp_build_c_cast): Don't strip cv-quals when
    	converting to reference.
    	(build_static_cast_1): Update for glvalues.

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 13b919c..69b25d3 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -5768,11 +5768,11 @@  build_static_cast_1 (tree type, tree expr, bool c_cast_p,
       return convert_from_reference (rvalue (cp_fold_convert (type, expr)));
     }
 
-  /* "An lvalue of type cv1 T1 can be cast to type rvalue reference to
+  /* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
      cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)."  */
   if (TREE_CODE (type) == REFERENCE_TYPE
       && TYPE_REF_IS_RVALUE (type)
-      && real_lvalue_p (expr)
+      && lvalue_or_rvalue_with_address_p (expr)
       && reference_related_p (TREE_TYPE (type), intype)
       && (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
     {
@@ -6448,7 +6448,7 @@  cp_build_c_cast (tree type, tree expr, tsubst_flags_t complain)
       if (!CLASS_TYPE_P (type))
 	type = TYPE_MAIN_VARIANT (type);
       result_type = TREE_TYPE (result);
-      if (!CLASS_TYPE_P (result_type))
+      if (!CLASS_TYPE_P (result_type) && TREE_CODE (type) != REFERENCE_TYPE)
 	result_type = TYPE_MAIN_VARIANT (result_type);
       /* If the type of RESULT does not match TYPE, perform a
 	 const_cast to make it match.  If the static_cast or
diff --git a/gcc/testsuite/g++.dg/cpp0x/sfinae25.C b/gcc/testsuite/g++.dg/cpp0x/sfinae25.C
new file mode 100644
index 0000000..7bdc8f8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/sfinae25.C
@@ -0,0 +1,10 @@ 
+// PR c++/49105
+// { dg-options -std=c++0x }
+
+template<class T, class = decltype(T{})>
+char f(int);
+
+template<class T>
+auto f(...) -> char(&)[2];
+
+static_assert(sizeof(f<const int&&>(0)) == 1, "Error"); // #