public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-8166] c++: lambda in non-type template parm type [PR99478]
@ 2021-04-14  4:34 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2021-04-14  4:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:e1666ebd9ad31dbd8b9b933c883bdd882cfd1522

commit r11-8166-ge1666ebd9ad31dbd8b9b933c883bdd882cfd1522
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Apr 13 22:28:32 2021 -0400

    c++: lambda in non-type template parm type [PR99478]
    
    In this testcase, the non-type template parameter has the type of a
    lambda-expression.  This makes no sense because a lambda in template context
    is specified to be distinct between different specializations of the
    template, even if the lambda is non-dependent, but here which specialization
    we are dealing with depends on which lambda we have, and vice versa.
    
    gcc/cp/ChangeLog:
    
            PR c++/99478
            * parser.c (cp_parser_lambda_expression): Reject lambda
            in template parameter type.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/99478
            * g++.dg/cpp2a/lambda-uneval14.C: New test.

Diff:
---
 gcc/cp/parser.c                              | 16 +++++++++++++++-
 gcc/testsuite/g++.dg/cpp2a/lambda-uneval14.C |  6 ++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index aec3aa3587f..3a107206318 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -10796,7 +10796,21 @@ cp_parser_lambda_expression (cp_parser* parser)
   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
 
   if (cxx_dialect >= cxx20)
-    /* C++20 allows lambdas in unevaluated context.  */;
+    {
+      /* C++20 allows lambdas in unevaluated context, but one in the type of a
+	 non-type parameter is nonsensical.
+
+	 Distinguish a lambda in the parameter type from a lambda in the
+	 default argument by looking at local_variables_forbidden_p, which is
+	 only set in default arguments.  */
+      if (processing_template_parmlist && !parser->local_variables_forbidden_p)
+	{
+	  error_at (token->location,
+		    "lambda-expression in template parameter type");
+	  token->error_reported = true;
+	  ok = false;
+	}
+    }
   else if (cp_unevaluated_operand)
     {
       if (!token->error_reported)
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-uneval14.C b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval14.C
new file mode 100644
index 00000000000..a18035954e1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval14.C
@@ -0,0 +1,6 @@
+// PR c++/99478
+// { dg-do compile { target c++20 } }
+
+template <decltype ([] {})> auto f() {} // { dg-error "lambda" }
+
+int main() { f<{}>(); }		// { dg-prune-output "no match" }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-14  4:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-14  4:34 [gcc r11-8166] c++: lambda in non-type template parm type [PR99478] Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).