c++: lambdas inside constraints [PR92652]

Message ID 20200528150316.2078692-1-ppalka@redhat.com
State New
Headers show
Series
  • c++: lambdas inside constraints [PR92652]
Related show

Commit Message

Kewen.Lin via Gcc-patches May 28, 2020, 3:03 p.m.
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 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                                | 15 +++++++++------
 gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C |  7 +++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C  | 11 +++++++++++
 gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C  | 11 +++++++++++
 4 files changed, 38 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C

-- 
2.27.0.rc1.5.gae92ac8ae3

Comments

Kewen.Lin via Gcc-patches May 28, 2020, 8:40 p.m. | #1
On 5/28/20 11:03 AM, Patrick Palka wrote:
> 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 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                                | 15 +++++++++------

>   gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C |  7 +++++++

>   gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C  | 11 +++++++++++

>   gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C  | 11 +++++++++++

>   4 files changed, 38 insertions(+), 6 deletions(-)

>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C

>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C

>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C

> 

> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c

> index 54ca875ce54..3bca1f3770a 100644

> --- a/gcc/cp/parser.c

> +++ b/gcc/cp/parser.c

> @@ -27663,11 +27663,12 @@ 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)

> +    processing_template_decl = 1;


This needs a comment about why you're doing it this way instead of the 
usual ++.  OK with that change.

>     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 +27685,13 @@ 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)

> +    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 +27800,10 @@ 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)

> +      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

> index 00000000000..392da312b28

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C

> @@ -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

> index 00000000000..c1c9be682d0

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C

> @@ -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

> index 00000000000..6b81ba0adac

> --- /dev/null

> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C

> @@ -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>;

>

Patch

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 54ca875ce54..3bca1f3770a 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -27663,11 +27663,12 @@  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)
+    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 +27685,13 @@  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)
+    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 +27800,10 @@  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)
+      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
index 00000000000..392da312b28
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda10.C
@@ -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
index 00000000000..c1c9be682d0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda8.C
@@ -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
index 00000000000..6b81ba0adac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-lambda9.C
@@ -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>;