diff mbox

C++ PATCH for c++/52014 (decltype and members of the enclosing class of a lambda)

Message ID 5150B4E9.9090908@redhat.com
State New
Headers show

Commit Message

Jason Merrill March 25, 2013, 8:34 p.m. UTC
We were getting confused trying to capture this for this->foo_ in the 
decltype.  But we shouldn't capture anything just because it's mentioned 
in decltype.

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

Patch

commit 580a948e95a571add5ea02b18f996cac227dfa77
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Mar 24 06:15:22 2013 -0400

    	PR c++/52014
    	* semantics.c (lambda_expr_this_capture): Don't capture 'this' in
    	unevaluated context.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index e3aeb81..fb38e8d 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -9454,6 +9454,11 @@  lambda_expr_this_capture (tree lambda)
 
   tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda);
 
+  /* In unevaluated context this isn't an odr-use, so just return the
+     nearest 'this'.  */
+  if (cp_unevaluated_operand)
+    return lookup_name (this_identifier);
+
   /* Try to default capture 'this' if we can.  */
   if (!this_capture
       && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE)
@@ -9523,11 +9528,6 @@  lambda_expr_this_capture (tree lambda)
 
   if (!this_capture)
     {
-      /* In unevaluated context this isn't an odr-use, so just return the
-	 nearest 'this'.  */
-      if (cp_unevaluated_operand)
-	return lookup_name (this_identifier);
-
       error ("%<this%> was not captured for this lambda function");
       result = error_mark_node;
     }
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this14.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this14.C
new file mode 100644
index 0000000..9834bfd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this14.C
@@ -0,0 +1,49 @@ 
+// PR c++/52014
+// { dg-require-effective-target c++11 }
+
+template <class Iterator, class Func>
+void for_each(const Iterator first, const Iterator last, Func func)
+{
+  for (Iterator it = first; it != last; ++it) {
+    func(*it);
+  }
+}
+
+template <class T>
+struct helper
+{
+  typedef typename T::size_type type;
+};
+
+template <class T>
+struct helper<T&>
+{
+  typedef typename T::size_type type;
+};
+
+template <class T>
+struct helper<T*>
+{
+  typedef typename T::size_type type;
+};
+
+struct bar
+{
+  struct foo
+  {
+    typedef int size_type;
+  } foo_;
+
+  void test()
+  {
+    int arr[] = { 1, 2, 3 };
+    for_each(arr, arr + 3, [&](helper<foo>::type i) {
+	for_each(arr, arr + 3, [&](helper<decltype(foo_)>::type j) { });
+      });
+  }
+};
+
+int main()
+{
+  return 0;
+}