]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: 'this' in lambda in noexcept-spec [PR121008]
authorJason Merrill <jason@redhat.com>
Wed, 9 Jul 2025 14:38:20 +0000 (10:38 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 9 Jul 2025 15:49:24 +0000 (11:49 -0400)
In r16-970 I changed finish_this_expr to look at current_class_type rather
than current_class_ptr to accommodate explicit object lambdas.  But here in
a lambda in the noexcept-spec, the closure type doesn't yet have the
function as its context, so lambda_expr_this_capture can't find the function
and gives up.  But in this context current_class_ptr refers to the
function's 'this', so let's go back to using it in that case.

PR c++/121008
PR c++/113563

gcc/cp/ChangeLog:

* semantics.cc (finish_this_expr): Do check current_class_ref for
non-lambda.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/lambda-uneval28.C: New test.

gcc/cp/semantics.cc
gcc/testsuite/g++.dg/cpp2a/lambda-uneval28.C [new file with mode: 0644]

index 28baf7b3172bef8031c5b66893380ddf4cc9bb7e..b57547cb8afa980a71596f3dd98ca2f341409a8d 100644 (file)
@@ -3605,11 +3605,13 @@ finish_this_expr (void)
 {
   tree result = NULL_TREE;
 
-  if (current_class_type && LAMBDA_TYPE_P (current_class_type))
+  if (current_class_ref && !LAMBDA_TYPE_P (TREE_TYPE (current_class_ref)))
+    result = current_class_ptr;
+  else if (current_class_type && LAMBDA_TYPE_P (current_class_type))
     result = (lambda_expr_this_capture
              (CLASSTYPE_LAMBDA_EXPR (current_class_type), /*add*/true));
-  else if (current_class_ptr)
-    result = current_class_ptr;
+  else
+    gcc_checking_assert (!current_class_ptr);
 
   if (result)
     /* The keyword 'this' is a prvalue expression.  */
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-uneval28.C b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval28.C
new file mode 100644 (file)
index 0000000..4b1bfc8
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/121008
+// { dg-do compile { target c++20 } }
+
+struct A {
+  void f()
+    noexcept(noexcept([this]() noexcept(noexcept(this)) {}))
+  {}
+};
+
+int main() {}