Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- The error here should also check that we aren't nested in another lambda; in it, at_function_scope_p() will be false.
PR c++/117602 gcc/cp/ChangeLog: * parser.cc (cp_parser_lambda_introducer): Check if we're in a lambda before emitting the error about a non-local lambda with a capture-default. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-uneval19.C: New test. --- gcc/cp/parser.cc | 5 ++++- gcc/testsuite/g++.dg/cpp2a/lambda-uneval19.C | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-uneval19.C diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 07b12224615..dc79ff42a3b 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -11611,7 +11611,10 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr) cp_lexer_consume_token (parser->lexer); first = false; - if (!(at_function_scope_p () || parsing_nsdmi ())) + if (!(at_function_scope_p () + || parsing_nsdmi () + || (current_class_type + && LAMBDA_TYPE_P (current_class_type)))) error ("non-local lambda expression cannot have a capture-default"); } diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-uneval19.C b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval19.C new file mode 100644 index 00000000000..79390180811 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval19.C @@ -0,0 +1,14 @@ +// PR c++/117602 +// { dg-do compile { target c++20 } } + +void +g () +{ + [&](decltype([&](decltype([=](decltype([&](decltype([&]() {})) {})) {})) {})){}; + [&](decltype([&](decltype([&](decltype([&](decltype([&]() {})) {})) {})) {})){}; + [=](decltype([=](decltype([=](decltype([=](decltype([&]() {})) {})) {})) {})){}; + + [=]() { + [&]() { }; + }; +} base-commit: 96a468842ef8b5d9b971428c7ba4e14fdab5ea94 -- 2.47.0