[PATCH] c++: bogus error with nested lambdas [PR117602]

Jason Merrill jason@redhat.com
Tue Jan 21 22:09:32 GMT 2025


On 1/16/25 5:42 PM, Marek Polacek wrote:
> On Wed, Jan 15, 2025 at 04:18:36PM -0500, Jason Merrill wrote:
>> On 1/15/25 12:55 PM, Marek Polacek wrote:
>>> On Wed, Jan 15, 2025 at 09:39:41AM -0500, Jason Merrill wrote:
>>>> On 11/15/24 9:08 AM, Marek Polacek wrote:
>>>>> 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))))
>>>>
>>>> How about using current_nonlambda_scope () instead of at_function_scope_p
>>>> ()?
>>>
>>> I think I remember not using that because current_nonlambda_scope() will
>>> give us a namespace_decl :: for non-local stuff so it won't be null.  Do
>>> you still prefer that (checking the result of current_nonlambda_scope())
>>> to what I did in my patch?
>>
>> I think so, your change looks to be true for lambdas outside function scope
>> as well.
> 
> I think it works correctly for both
> 
>    auto x = [&]() { // error
>        [&]() { };
>    };
>    auto x2 = []() {
>        [&]() { };
>    };
> 
> but current_nonlambda_scope () will return '::' for the nested lambdas too.
> Am I missing something?

Ah, good point.  But it doesn't work correctly for an adjustment to the 
PR testcase; with your patch the following is wrongly accepted:

auto x = [](decltype([&]{})){};

Perhaps current_scope should look through closure types, so 
at_function_scope_p gives the right answer?

Jason



More information about the Gcc-patches mailing list