diff mbox

C++ PATCH for c++/48289 (-pedantic breaks std::move)

Message ID 4D8CBE75.4050700@redhat.com
State New
Headers show

Commit Message

Jason Merrill March 25, 2011, 4:10 p.m. UTC
The problem here was that we were improperly treating 
static_cast<std::remove_reference<T>::type&&>(x) as an lvalue because 
the dereference was inside a NON_DEPENDENT_EXPR, which we always treat 
as an lvalue.  That seems like a bad assumption, but we can fix this bug 
simply by moving the dereference outside.

Tested x86_64-pc-linux-gnu, applied to trunk and 4.5, will apply to 4.6 
after 4.6.0.
commit 91300f260ac2bb684e62e28a1e63b8646d1d49cc
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Mar 25 11:16:54 2011 +0100

    	PR c++/48289
    	* pt.c (build_non_dependent_expr): Keep dereferences outside the
    	NON_DEPENDENT_EXPR.

Comments

Eric Botcazou March 28, 2011, 12:17 a.m. UTC | #1
> Tested x86_64-pc-linux-gnu, applied to trunk and 4.5, will apply to 4.6
> after 4.6.0.

The test doesn't pass on the 4.5 branch:
  FAIL: g++.dg/cpp0x/move1.C (test for excess errors)
Jason Merrill March 28, 2011, 3:18 p.m. UTC | #2
On 03/27/2011 08:17 PM, Eric Botcazou wrote:
>> Tested x86_64-pc-linux-gnu, applied to trunk and 4.5, will apply to 4.6
>> after 4.6.0.
>
> The test doesn't pass on the 4.5 branch:
>    FAIL: g++.dg/cpp0x/move1.C (test for excess errors)

Good point.  I guess I won't bother trying to fix this in 4.5.  Reverted.

Jason
diff mbox

Patch

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index c8c1010..9032dd9 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -18846,24 +18846,17 @@  build_non_dependent_expr (tree expr)
 		   TREE_OPERAND (expr, 0),
 		   build_non_dependent_expr (TREE_OPERAND (expr, 1)));
 
+  /* Keep dereferences outside the NON_DEPENDENT_EXPR so lvalue_kind
+     doesn't need to look inside.  */
+  if (TREE_CODE (expr) == INDIRECT_REF && REFERENCE_REF_P (expr))
+    return convert_from_reference (build_non_dependent_expr
+				   (TREE_OPERAND (expr, 0)));
+
   /* If the type is unknown, it can't really be non-dependent */
   gcc_assert (TREE_TYPE (expr) != unknown_type_node);
 
-  /* Otherwise, build a NON_DEPENDENT_EXPR.
-
-     REFERENCE_TYPEs are not stripped for expressions in templates
-     because doing so would play havoc with mangling.  Consider, for
-     example:
-
-       template <typename T> void f<T& g>() { g(); }
-
-     In the body of "f", the expression for "g" will have
-     REFERENCE_TYPE, even though the standard says that it should
-     not.  The reason is that we must preserve the syntactic form of
-     the expression so that mangling (say) "f<g>" inside the body of
-     "f" works out correctly.  Therefore, the REFERENCE_TYPE is
-     stripped here.  */
-  return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
+  /* Otherwise, build a NON_DEPENDENT_EXPR.  */
+  return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
 }
 
 /* ARGS is a vector of expressions as arguments to a function call.
diff --git a/gcc/testsuite/g++.dg/cpp0x/move1.C b/gcc/testsuite/g++.dg/cpp0x/move1.C
new file mode 100644
index 0000000..12e363a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/move1.C
@@ -0,0 +1,15 @@ 
+// { dg-options "-std=c++0x -pedantic-errors" }
+
+#include <utility>
+
+class A { };
+
+static void g ( A && ) { }
+
+template < class T > class B {
+public:
+ void f ( ) {
+  A a;
+  g ( std :: move ( a ) );
+ }
+};