c++: lambdas inside constraints [PR92652]
authorPatrick Palka <ppalka@redhat.com>
Fri, 29 May 2020 13:40:40 +0000 (09:40 -0400)
committerPatrick Palka <ppalka@redhat.com>
Fri, 29 May 2020 13:44:13 +0000 (09:44 -0400)
When parsing a constraint-expression, a requires-clause or a
requires-expression, we temporarily increment processing_template_decl
so that we always obtain template trees which we could later reduce via
substitution even when not inside a template.

But incrementing processing_template_decl when we're already inside a
template has the unintended side effect of shifting up the template
parameter levels of a lambda defined inside one of these constructs,
which leads to confusion later during substitution into the lambda.

This patch fixes this issue by incrementing processing_template_decl
during parsing of these constructs only if it is 0.

Passes 'make check-c++', and also tested by building cmcstl2, does this
look OK to commit after a full bootstrap/regtest?

gcc/cp/ChangeLog:

PR c++/92652
PR c++/93698
PR c++/94128
* parser.c (cp_parser_requires_clause_expression): Temporarily
increment processing_template_decl only if it is 0.
(cp_parser_constraint_expression): Likewise.
(cp_parser_requires_expression): Likewise.

gcc/testsuite/ChangeLog:

PR c++/92652
PR c++/93698
PR c++/94128
* g++.dg/cpp2a/concepts-lambda8.C: New test.
* g++.dg/cpp2a/concepts-lambda9.C: New test.
* g++.dg/cpp2a/concepts-lambda10.C: New test.

gcc/cp/parser.c
gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C [new file with mode: 0644]

index 54ca875ce54c3673b3b02bfcb65c2afdfd944a54..74c40efd936c940ea46c0c30c62f411e38908a8e 100644 (file)
@@ -27663,11 +27663,16 @@ static tree
 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
 {
   processing_constraint_expression_sentinel parsing_constraint;
-  ++processing_template_decl;
+  temp_override<int> ovr (processing_template_decl);
+  if (!processing_template_decl)
+    /* Adjust processing_template_decl so that we always obtain template
+       trees here.  We don't do the usual ++processing_template_decl
+       because that would skew the template parameter depth of a lambda
+       within if we're already inside a template.  */
+    processing_template_decl = 1;
   cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
   if (check_for_bare_parameter_packs (expr))
     expr = error_mark_node;
-  --processing_template_decl;
   return expr;
 }
 
@@ -27684,12 +27689,14 @@ static tree
 cp_parser_constraint_expression (cp_parser *parser)
 {
   processing_constraint_expression_sentinel parsing_constraint;
-  ++processing_template_decl;
+  temp_override<int> ovr (processing_template_decl);
+  if (!processing_template_decl)
+    /* As in cp_parser_requires_clause_expression.  */
+    processing_template_decl = 1;
   cp_expr expr = cp_parser_binary_expression (parser, false, true,
                                              PREC_NOT_OPERATOR, NULL);
   if (check_for_bare_parameter_packs (expr))
     expr = error_mark_node;
-  --processing_template_decl;
   expr.maybe_add_location_wrapper ();
   return expr;
 }
@@ -27798,9 +27805,11 @@ cp_parser_requires_expression (cp_parser *parser)
       parms = NULL_TREE;
 
     /* Parse the requirement body. */
-    ++processing_template_decl;
+    temp_override<int> ovr (processing_template_decl);
+    if (!processing_template_decl)
+      /* As in cp_parser_requires_clause_expression.  */
+      processing_template_decl = 1;
     reqs = cp_parser_requirement_body (parser);
-    --processing_template_decl;
     if (reqs == error_mark_node)
       return error_mark_node;
   }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C
new file mode 100644 (file)
index 0000000..392da31
--- /dev/null
@@ -0,0 +1,7 @@
+// PR c++/94128
+// { dg-do compile { target c++20 } }
+
+void test(auto param)
+requires requires{ { [](auto p){return p;}(param) }; };
+
+void test2() { test(1); }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C
new file mode 100644 (file)
index 0000000..c1c9be6
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/92652
+// { dg-do compile { target concepts } }
+
+template < typename T >
+    requires ([]{return true ;}())
+void h() { }
+
+int main()
+{
+    h<int>();
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C
new file mode 100644 (file)
index 0000000..6b81ba0
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/93698
+// { dg-do compile { target concepts } }
+
+#include <utility>
+
+template <int N>
+concept foo = []<std::size_t... Is>(std::index_sequence<Is...>) constexpr {
+  return (Is + ...) > 10;
+}(std::make_index_sequence<N>());
+
+bool a = foo<7>;