diff mbox

C++ PATCH for c++/69842 (wrong error with generic lambda)

Message ID 56CF1C47.4030508@redhat.com
State New
Headers show

Commit Message

Jason Merrill Feb. 25, 2016, 3:22 p.m. UTC
On 02/17/2016 03:44 PM, Jason Merrill wrote:
> The problem here was that the call from the stub returned by the
> conversion function to the op() was changing an xvalue to an lvalue,
> leading to a parameter of the wrong type in the op().

...and now, handling variadic templates as well.

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

Patch

commit 7788d30617c68e660a73288a0c8adbe7d2d0a714
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Feb 25 09:56:42 2016 -0500

    	PR c++/69842
    	* method.c (forward_parm): Handle parameter packs.
    	* lambda.c (maybe_add_lambda_conv_op): Use it for them.

diff --git a/gcc/cp/lambda.c b/gcc/cp/lambda.c
index 296c6f7..cdc11fe 100644
--- a/gcc/cp/lambda.c
+++ b/gcc/cp/lambda.c
@@ -29,6 +29,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "cgraph.h"
 #include "tree-iterator.h"
 #include "toplev.h"
+#include "gimplify.h"
 
 /* Constructor for a lambda expression.  */
 
@@ -952,23 +953,18 @@  maybe_add_lambda_conv_op (tree type)
 
 	if (generic_lambda_p)
 	  {
-	    if (DECL_PACK_P (tgt))
-	      {
-		tree a = make_pack_expansion (tgt);
-		if (decltype_call)
-		  CALL_EXPR_ARG (decltype_call, ix) = copy_node (a);
-		PACK_EXPANSION_LOCAL_P (a) = true;
-		CALL_EXPR_ARG (call, ix) = a;
-	      }
-	    else
-	      {
-		++processing_template_decl;
-		tree a = forward_parm (tgt);
-		--processing_template_decl;
-		CALL_EXPR_ARG (call, ix) = a;
-		if (decltype_call)
-		  CALL_EXPR_ARG (decltype_call, ix) = copy_node (a);
-	      }
+	    ++processing_template_decl;
+	    tree a = forward_parm (tgt);
+	    --processing_template_decl;
+
+	    CALL_EXPR_ARG (call, ix) = a;
+	    if (decltype_call)
+	      CALL_EXPR_ARG (decltype_call, ix) = unshare_expr (a);
+
+	    if (PACK_EXPANSION_P (a))
+	      /* Set this after unsharing so it's not in decltype_call.  */
+	      PACK_EXPANSION_LOCAL_P (a) = true;
+
 	    ++ix;
 	  }
 	else
diff --git a/gcc/cp/method.c b/gcc/cp/method.c
index f455b32..0235e6a 100644
--- a/gcc/cp/method.c
+++ b/gcc/cp/method.c
@@ -481,9 +481,12 @@  tree
 forward_parm (tree parm)
 {
   tree exp = convert_from_reference (parm);
-  if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
-      || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
-    exp = move (exp);
+  tree type = TREE_TYPE (parm);
+  if (DECL_PACK_P (parm))
+    type = PACK_EXPANSION_PATTERN (type);
+  exp = build_static_cast (type, exp, tf_warning_or_error);
+  if (DECL_PACK_P (parm))
+    exp = make_pack_expansion (exp);
   return exp;
 }
 
diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic4.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic4.C
new file mode 100644
index 0000000..0b65f56
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic4.C
@@ -0,0 +1,20 @@ 
+// PR c++/69842
+// { dg-do compile { target c++14 } }
+
+template <class T, class U> struct assert_same;
+template <class T> struct assert_same<T,T> {};
+
+template<typename T>
+void sink(T &&)
+{
+  assert_same<int,T> a;
+}
+
+int main()
+{
+  auto const g([](auto &&...  _var) {
+      sink(static_cast<decltype(_var)>(_var)...);
+    });
+
+  g(0);
+}