[PATCH] c++: Allow template lambdas without lambda-declarator [PR97839]

Jason Merrill jason@redhat.com
Fri Nov 20 22:14:41 GMT 2020


On 11/17/20 1:05 PM, Marek Polacek wrote:
> Our implementation of template lambdas incorrectly requires the optional
> lambda-declarator.  This was probably required by an early draft of
> generic lambdas, but now the production is [expr.prim.lambda.general]:
> 
>   lambda-expression:
>      lambda-introducer lambda-declarator [opt] compound-statement
>      lambda-introducer < template-parameter-list > requires-clause [opt]
> 	  lambda-declarator [opt] compound-statement
> 
> Therefore, we should accept the following test.
> 
> Incidentally, I noticed we give a terrible diagnostic when the user uses
> 'mutable', but forgets to type '()' before it, which sounds like a common
> mistake.  So it seems to me we should handle that specifically, rather
> than to emit this:
> 
> lambda-generic8.C: In lambda function:
> lambda-generic8.C:8:18: error: expected '{' before 'mutable'
>      8 |   []<typename T> mutable {}.operator()<int>();
>        |                  ^~~~~~~
> lambda-generic8.C: In function 'int main()':
> lambda-generic8.C:8:17: error: expected ';' before 'mutable'
>      8 |   []<typename T> mutable {}.operator()<int>();
>        |                 ^~~~~~~~
>        |                 ;
> lambda-generic8.C:8:28: error: expected primary-expression before '.' token
>      8 |   []<typename T> mutable {}.operator()<int>();
>        |                            ^
> lambda-generic8.C:8:40: error: expected primary-expression before 'int'
>      8 |   []<typename T> mutable {}.operator()<int>();
>        |                                        ^~~
> 
> Is it okay to fix this in stage3?

Yes: this is a bugfix, not new functionality.

> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> gcc/cp/ChangeLog:
> 
> 	PR c++/97839
> 	* parser.c (cp_parser_lambda_declarator_opt): Don't require ().
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c++/97839
> 	* g++.dg/cpp2a/lambda-generic8.C: New test.
> ---
>   gcc/cp/parser.c                              | 14 ++++++--------
>   gcc/testsuite/g++.dg/cpp2a/lambda-generic8.C |  9 +++++++++
>   2 files changed, 15 insertions(+), 8 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-generic8.C
> 
> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
> index 42f705266bb..9f09c778c29 100644
> --- a/gcc/cp/parser.c
> +++ b/gcc/cp/parser.c
> @@ -10604,6 +10604,8 @@ cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
>   
>      lambda-expression:
>        lambda-introducer lambda-declarator [opt] compound-statement
> +     lambda-introducer < template-parameter-list > requires-clause [opt]
> +       lambda-declarator [opt] compound-statement
>   
>      Returns a representation of the expression.  */
>   
> @@ -11061,13 +11063,11 @@ cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
>   /* Parse the (optional) middle of a lambda expression.
>   
>      lambda-declarator:
> -     < template-parameter-list [opt] >
> -       requires-clause [opt]
> -     ( parameter-declaration-clause [opt] )
> -       attribute-specifier [opt]
> +     ( parameter-declaration-clause )
>          decl-specifier-seq [opt]
> -       exception-specification [opt]
> -       lambda-return-type-clause [opt]
> +       noexcept-specifier [opt]
> +       attribute-specifier-seq [opt]
> +       trailing-return-type [opt]
>          requires-clause [opt]
>   
>      LAMBDA_EXPR is the current representation of the lambda expression.  */
> @@ -11217,8 +11217,6 @@ cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
>            trailing-return-type in case of decltype.  */
>         pop_bindings_and_leave_scope ();
>       }
> -  else if (template_param_list != NULL_TREE) // generate diagnostic
> -    cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
>   
>     /* Create the function call operator.
>   
> diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-generic8.C b/gcc/testsuite/g++.dg/cpp2a/lambda-generic8.C
> new file mode 100644
> index 00000000000..f3c3809b36d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-generic8.C
> @@ -0,0 +1,9 @@
> +// PR c++/97839
> +// { dg-do compile { target c++20 } }
> +// Test that a lambda with <template-param-list> doesn't require
> +// a lambda-declarator.
> +
> +int main()
> +{
> +  []<typename T>{}.operator()<int>();
> +}
> 
> base-commit: 8661f4faa875f361cd22a197774c1fa04cd0580b
> 



More information about the Gcc-patches mailing list