Patchwork C++ PATCH for c++/56135 (wrong 'this' capture)

login
register
mail settings
Submitter Jason Merrill
Date Feb. 13, 2013, 2:20 p.m.
Message ID <511BA12A.6010407@redhat.com>
Download mbox | patch
Permalink /patch/220145/
State New
Headers show

Comments

Jason Merrill - Feb. 13, 2013, 2:20 p.m.
When the only use of 'this' is implicitly by a dependent name, we don't 
see the capture until instantiation time.  Don't clobber its capture 
list entry when we instantiate the ones seen at template definition time.

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

Patch

commit cdcb44a750af3921d6883832892dd8cd5e2d22b4
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Feb 12 21:47:56 2013 -0500

    	PR c++/56135
    	* pt.c (tsubst_copy_and_build): Don't forget any new
    	captures that arose from use of dependent names.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index a3359ad..2aadd4d 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -14457,9 +14457,11 @@  tsubst_copy_and_build (tree t,
 	complete_type (type);
 
 	/* The capture list refers to closure members, so this needs to
-	   wait until after we finish instantiating the type.  */
+	   wait until after we finish instantiating the type.  Also keep
+	   any captures that may have been added during instantiation.  */
 	LAMBDA_EXPR_CAPTURE_LIST (r)
-	  = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
+	  = chainon (RECUR (LAMBDA_EXPR_CAPTURE_LIST (t)),
+		     LAMBDA_EXPR_CAPTURE_LIST (r));
 	LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
 
 	RETURN (build_lambda_object (r));
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this8.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this8.C
new file mode 100644
index 0000000..9309a44
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this8.C
@@ -0,0 +1,39 @@ 
+// PR c++/56135
+// { dg-do run { target c++11 } }
+
+#include <functional>
+
+extern "C" void abort() throw();
+
+struct test {
+  template<typename T>
+  std::function<void()> broken(int x) {
+    return [=] { +x; print<T>(); };
+  }
+
+  std::function<void()> works0() {
+    return [=] { print<int>(); };
+  }
+
+  template<typename T>
+  std::function<void()> works1() {
+    return [=] { print<int>(); };
+  }
+
+  template<typename T>
+  std::function<void()> works2() {
+    return [=] { this->print<T>(); };
+  }
+
+  template<typename T>
+  void print() { if (this == NULL) abort (); }
+};
+
+int main(void) {
+  test().broken<int>(1)();
+  test().works0()();
+  test().works1<int>()();
+  test().works2<int>()();
+
+  return 0;
+}