c++: Allow template lambdas without lambda-declarator [PR97839]
authorMarek Polacek <polacek@redhat.com>
Tue, 17 Nov 2020 16:38:25 +0000 (11:38 -0500)
committerMarek Polacek <polacek@redhat.com>
Sat, 21 Nov 2020 20:35:14 +0000 (15:35 -0500)
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.

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
gcc/testsuite/g++.dg/cpp2a/lambda-generic8.C [new file with mode: 0644]

index cf4e4aa1b75318d20c25fc466b65ab477c60dda3..ea5af7094874cdef317fd7d822b27b63485e538c 100644 (file)
@@ -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 (file)
index 0000000..f3c3809
--- /dev/null
@@ -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>();
+}