]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: implicit 'this' in generic lambda [PR122048]
authorJason Merrill <jason@redhat.com>
Wed, 24 Sep 2025 21:31:37 +0000 (22:31 +0100)
committerJason Merrill <jason@redhat.com>
Sat, 27 Sep 2025 08:04:58 +0000 (09:04 +0100)
Here template substitution was replacing a reference to the 'this' of X::f
with the implicit closure parameter of the operator(), which is wrong.  The
closure parameter is never a suitable replacement for a 'this' parameter.

PR c++/122048

gcc/cp/ChangeLog:

* pt.cc (tsubst_expr): Don't use a lambda current_class_ptr.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/lambda-generic-this6.C: New test.

gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp1y/lambda-generic-this6.C [new file with mode: 0644]

index 06287f0b04525e798a36b0ea64970497d7464c89..96ead4f1b554ff1df3d11d58353f62eb58d25633 100644 (file)
@@ -22330,7 +22330,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
          if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL)
            {
              /* We get here for a use of 'this' in an NSDMI.  */
-             if (DECL_NAME (t) == this_identifier && current_class_ptr)
+             if (DECL_NAME (t) == this_identifier && current_class_ptr
+                 && !LAMBDA_TYPE_P (TREE_TYPE (TREE_TYPE (current_class_ptr))))
                RETURN (current_class_ptr);
 
              /* Parameters of non-templates map to themselves (e.g. in
diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-this6.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-this6.C
new file mode 100644 (file)
index 0000000..8bbff1d
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/122048
+// { dg-do compile { target c++14 } }
+
+class X {
+  void f();
+  int i;
+};
+void X::f() {[&](auto) {sizeof i;}(1);}