diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 0eb3e8531c58ee15e0f74585017ee2d40306e09c..5e8aefc12893cb8ea87ef781e63a42225de95d30 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8097,7 +8097,7 @@ extern void maybe_generic_this_capture (tree, tree); extern tree maybe_resolve_dummy (tree, bool); extern tree current_nonlambda_function (void); extern tree nonlambda_method_basetype (void); -extern tree current_nonlambda_scope (void); +extern tree current_nonlambda_scope (bool = false); extern tree current_lambda_expr (void); extern bool generic_lambda_fn_p (tree); extern tree do_dependent_capture (tree, bool = false); diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc index acb7208fd18555cc5c988bca2564d97ee5815796..5593636eaf8edc9b5ac8efda8c964c0214e383a9 100644 --- a/gcc/cp/lambda.cc +++ b/gcc/cp/lambda.cc @@ -1045,15 +1045,17 @@ nonlambda_method_basetype (void) } } -/* Like current_scope, but looking through lambdas. */ +/* Like current_scope, but looking through lambdas. If ONLY_SKIP_CLOSURES_P, + only look through closure types. */ tree -current_nonlambda_scope (void) +current_nonlambda_scope (bool only_skip_closures_p/*=false*/) { tree scope = current_scope (); for (;;) { - if (TREE_CODE (scope) == FUNCTION_DECL + if (!only_skip_closures_p + && TREE_CODE (scope) == FUNCTION_DECL && LAMBDA_FUNCTION_P (scope)) { scope = CP_TYPE_CONTEXT (DECL_CONTEXT (scope)); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 24322817f3ed4ed2c3b32e965cf18f8c4d5d415d..03010089c3c625a0f625c83e0dc7ce8901c19a1f 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -11877,7 +11877,8 @@ 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 ())) + tree scope = current_nonlambda_scope (/*only_skip_closures_p=*/true); + if (TREE_CODE (scope) != FUNCTION_DECL && !parsing_nsdmi ()) error ("non-local lambda expression cannot have a capture-default"); } diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-uneval21.C b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval21.C new file mode 100644 index 0000000000000000000000000000000000000000..d3ea3acf52d42b9c8cda47a923d0cf5440d4b256 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval21.C @@ -0,0 +1,32 @@ +// PR c++/117602 +// { dg-do compile { target c++20 } } + +auto x1 = [](decltype([&]{})){}; // { dg-error "non-local lambda" } + +auto x2 = [&]() { // { dg-error "non-local lambda" } + [&]() { }; +}; + +auto x3 = []() { + [&]() { }; +}; + +auto x4 = []() { + []() { }; +}; + +auto x5 = [&]() { // { dg-error "non-local lambda" } + []() { }; +}; + +void +g () +{ + [&](decltype([&](decltype([=](decltype([&](decltype([&]() {})) {})) {})) {})){}; + [&](decltype([&](decltype([&](decltype([&](decltype([&]() {})) {})) {})) {})){}; + [=](decltype([=](decltype([=](decltype([=](decltype([&]() {})) {})) {})) {})){}; + + [=]() { + [&]() { }; + }; +}