https://gcc.gnu.org/g:131ba6137bca263d7381ad3331840ee05e306fc9
commit r16-2147-g131ba6137bca263d7381ad3331840ee05e306fc9 Author: Jason Merrill <ja...@redhat.com> Date: Wed Jul 9 10:38:20 2025 -0400 c++: 'this' in lambda in noexcept-spec [PR121008] 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. Diff: --- gcc/cp/semantics.cc | 8 +++++--- gcc/testsuite/g++.dg/cpp2a/lambda-uneval28.C | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 28baf7b3172b..b57547cb8afa 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -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 index 000000000000..4b1bfc8f0268 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval28.C @@ -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() {}