]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: requires-expression folding [PR101182]
authorPatrick Palka <ppalka@redhat.com>
Thu, 24 Jun 2021 15:29:02 +0000 (11:29 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 13 Jul 2021 13:53:27 +0000 (09:53 -0400)
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.  (Note that we delay folding
of such requires-expressions for sake of better diagnostics when one is
used as the condition of a failed static_assert.)

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-requires25.C: New test.

(cherry picked from commit c06493dc30afbf65b14d783c7cd53f20877ef577)

gcc/cp/constraint.cc
gcc/cp/cp-gimplify.c
gcc/testsuite/g++.dg/cpp2a/concepts-requires25.C [new file with mode: 0644]

index 6baaa0650b03873b85de25a75a52c8060dbd5e2e..3e320afd8409f98fdb2673e81cdd786ae6df39e6 100644 (file)
@@ -3363,7 +3363,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
index 9079a5b90ca0610143735cd9f76c39a5cd1e423a..f6febaece5ae32a3fea05657c02216eceb5e14a1 100644 (file)
@@ -1459,12 +1459,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.  */
@@ -2702,6 +2696,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-requires25.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires25.C
new file mode 100644 (file)
index 0000000..90cbedb
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/101182
+// { dg-do compile { target concepts } }
+
+int a;
+void g(bool);
+
+bool f() {
+  g(requires { a++; });
+  return requires { a++; };
+}