I have no idea why the existing code tried to deduce the desired quals
from current_class_ref rather than just look at the object it has. That
gives the wrong answer if current_class_ref is for a lambda.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 6a9b91f58b18503ae7a4a55fe494c0336df10484
Author: Jason Merrill <ja...@redhat.com>
Date: Fri Feb 15 14:14:12 2013 -0500
PR c++/54277
* semantics.c (finish_non_static_data_member): Get the quals from
the object.
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index efe09bb..d0cdf8b 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -1574,9 +1574,7 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
else
{
/* Set the cv qualifiers. */
- int quals = (current_class_ref
- ? cp_type_quals (TREE_TYPE (current_class_ref))
- : TYPE_UNQUALIFIED);
+ int quals = cp_type_quals (TREE_TYPE (object));
if (DECL_MUTABLE_P (decl))
quals &= ~TYPE_QUAL_CONST;
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this9.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this9.C
new file mode 100644
index 0000000..07ddd08
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-this9.C
@@ -0,0 +1,19 @@
+// PR c++/54277
+// { dg-do compile { target c++11 } }
+
+struct Used
+{
+ void foo() { }
+};
+
+template <typename>
+struct S
+{
+ Used x;
+
+ void bar()
+ {
+ auto f = [this] { x.foo(); };
+ f();
+ }
+};