Here, in template substitution we were calling process_outer_var_ref for both references to the outer variable without doing a name lookup to see if there is a local binding. If we add the local capture to local_specializations, we will find that when we come to the second reference.

Tested x86_64-pc-linux-gnu, applying to trunk and 5.
commit 3d5c19d59298d326eec727209f1bb28570c4a1cf
Author: Jason Merrill <ja...@redhat.com>
Date:   Fri Jun 19 10:34:04 2015 -0400

    	PR c++/65843
    	* pt.c (tsubst_copy_and_build): Register a capture proxy in
    	local_specializations.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 8377d21..5179827 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -15684,7 +15684,11 @@ tsubst_copy_and_build (tree t,
 	      r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
 	  }
 	else if (outer_automatic_var_p (r))
-	  r = process_outer_var_ref (r, complain);
+	  {
+	    r = process_outer_var_ref (r, complain);
+	    if (is_capture_proxy (r))
+	      register_local_specialization (r, t);
+	  }
 
 	if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
 	  /* If the original type was a reference, we'll be wrapped in
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-rep1.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-rep1.C
new file mode 100644
index 0000000..a35060b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-rep1.C
@@ -0,0 +1,14 @@
+// PR c++/65843
+// { dg-do compile { target c++11 } }
+
+template<class T>
+void test(T b)
+{
+    const int a = b;
+    [&] () { return a, a; }();
+}
+
+int main() {
+    test(1);
+ return 0;
+}

Reply via email to