diff mbox series

c++: requires-expression folding [PR101182]

Message ID 20210623233727.2288297-1-ppalka@redhat.com
State New
Headers show
Series c++: requires-expression folding [PR101182] | expand

Commit Message

Patrick Palka June 23, 2021, 11:37 p.m. UTC
Here we're crashing because cp_fold_function walks into the (templated)
requirements of a requires-expression outside a template, but the
folding routines aren't prepared to handle templated trees.  This patch
fixes this by making cp_fold use evaluate_requires_expr to fold a
requires-expression as a whole, which also means we no longer need to
explicitly do so during gimplification.  (We don't immediately fold
non-dependent requires-expressions at parse time for sake of better
diagnostics in case one appears as the condition of a failed
static_assert.)

Bootstrapped and regtested on x86_64-pc-linux-gnu, also tested on
cmcstl2 and range-v3, does this look OK for trunk/11?

	PR c++/101182

gcc/cp/ChangeLog:

	* constraint.cc (evaluate_requires_expr): Adjust function comment.
	* cp-gimplify.c (cp_genericize_r) <case REQUIRES_EXPR>: Move to ...
	(cp_fold) <case REQUIRES_EXPR>: ... here.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-requires27.C: New test.
---
 gcc/cp/constraint.cc                             |  2 +-
 gcc/cp/cp-gimplify.c                             | 10 ++++------
 gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C | 10 ++++++++++
 3 files changed, 15 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C

Comments

Jason Merrill June 24, 2021, 2:42 p.m. UTC | #1
On 6/23/21 7:37 PM, Patrick Palka wrote:
> Here we're crashing because cp_fold_function walks into the (templated)
> requirements of a requires-expression outside a template, but the
> folding routines aren't prepared to handle templated trees.  This patch
> fixes this by making cp_fold use evaluate_requires_expr to fold a
> requires-expression as a whole, which also means we no longer need to
> explicitly do so during gimplification.  (We don't immediately fold
> non-dependent requires-expressions at parse time for sake of better
> diagnostics in case one appears as the condition of a failed
> static_assert.)
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, also tested on
> cmcstl2 and range-v3, does this look OK for trunk/11?

OK.

> 	PR c++/101182
> 
> gcc/cp/ChangeLog:
> 
> 	* constraint.cc (evaluate_requires_expr): Adjust function comment.
> 	* cp-gimplify.c (cp_genericize_r) <case REQUIRES_EXPR>: Move to ...
> 	(cp_fold) <case REQUIRES_EXPR>: ... here.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-requires27.C: New test.
> ---
>   gcc/cp/constraint.cc                             |  2 +-
>   gcc/cp/cp-gimplify.c                             | 10 ++++------
>   gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C | 10 ++++++++++
>   3 files changed, 15 insertions(+), 7 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C
> 
> diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
> index 74b16d27101..286332ba2a7 100644
> --- a/gcc/cp/constraint.cc
> +++ b/gcc/cp/constraint.cc
> @@ -3340,7 +3340,7 @@ evaluate_concept_check (tree check)
>   }
>   
>   /* Evaluate the requires-expression T, returning either boolean_true_node
> -   or boolean_false_node.  This is used during gimplification and constexpr
> +   or boolean_false_node.  This is used during folding and constexpr
>      evaluation.  */
>   
>   tree
> diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
> index 96d91b6ef2f..33e10556e32 100644
> --- a/gcc/cp/cp-gimplify.c
> +++ b/gcc/cp/cp-gimplify.c
> @@ -1465,12 +1465,6 @@ cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
>   	TARGET_EXPR_NO_ELIDE (stmt) = 1;
>         break;
>   
> -    case REQUIRES_EXPR:
> -      /* Emit the value of the requires-expression.  */
> -      *stmt_p = evaluate_requires_expr (stmt);
> -      *walk_subtrees = 0;
> -      break;
> -
>       case TEMPLATE_ID_EXPR:
>         gcc_assert (concept_check_p (stmt));
>         /* Emit the value of the concept check.  */
> @@ -2708,6 +2702,10 @@ cp_fold (tree x)
>   	x = r;
>         break;
>   
> +    case REQUIRES_EXPR:
> +      x = evaluate_requires_expr (x);
> +      break;
> +
>       default:
>         return org_x;
>       }
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C
> new file mode 100644
> index 00000000000..87a759a689a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C
> @@ -0,0 +1,10 @@
> +// PR c++/101182
> +// { dg-do compile { target concepts } }
> +
> +int a;
> +int g(bool);
> +
> +int f() {
> +  g(requires { a++; });
> +  return requires { a++; };
> +}
>
diff mbox series

Patch

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 74b16d27101..286332ba2a7 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3340,7 +3340,7 @@  evaluate_concept_check (tree check)
 }
 
 /* Evaluate the requires-expression T, returning either boolean_true_node
-   or boolean_false_node.  This is used during gimplification and constexpr
+   or boolean_false_node.  This is used during folding and constexpr
    evaluation.  */
 
 tree
diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 96d91b6ef2f..33e10556e32 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -1465,12 +1465,6 @@  cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data)
 	TARGET_EXPR_NO_ELIDE (stmt) = 1;
       break;
 
-    case REQUIRES_EXPR:
-      /* Emit the value of the requires-expression.  */
-      *stmt_p = evaluate_requires_expr (stmt);
-      *walk_subtrees = 0;
-      break;
-
     case TEMPLATE_ID_EXPR:
       gcc_assert (concept_check_p (stmt));
       /* Emit the value of the concept check.  */
@@ -2708,6 +2702,10 @@  cp_fold (tree x)
 	x = r;
       break;
 
+    case REQUIRES_EXPR:
+      x = evaluate_requires_expr (x);
+      break;
+
     default:
       return org_x;
     }
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C
new file mode 100644
index 00000000000..87a759a689a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-requires27.C
@@ -0,0 +1,10 @@ 
+// PR c++/101182
+// { dg-do compile { target concepts } }
+
+int a;
+int g(bool);
+
+int f() {
+  g(requires { a++; });
+  return requires { a++; };
+}