PR c++/87567 - constexpr rejects call to non-constexpr function.
authorMarek Polacek <polacek@redhat.com>
Wed, 10 Oct 2018 21:11:18 +0000 (21:11 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 10 Oct 2018 21:11:18 +0000 (21:11 +0000)
* constexpr.c (potential_constant_expression_1) <case FOR_STMT>: Return
true if the condition is always false.
<case WHILE_STMT>: Likewise.

* g++.dg/cpp1y/constexpr-loop7.C: New test.

From-SVN: r265027

gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1y/constexpr-loop7.C [new file with mode: 0644]

index 975f866c0b7aaf9ff6b0229a6328aed9b32277bb..af8cb764139e7150cf3550cd68f85ca92afc347c 100644 (file)
@@ -1,3 +1,10 @@
+2018-10-10  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/87567 - constexpr rejects call to non-constexpr function.
+       * constexpr.c (potential_constant_expression_1) <case FOR_STMT>: Return
+       true if the condition is always false.
+       <case WHILE_STMT>: Likewise.
+
 2018-10-09  Paolo Carlini  <paolo.carlini@oracle.com>
 
        PR c++/84423
index 403edda8c47b796412888892bc3a0df8f35b62b3..4fa8c965a9d56a458077825072f005e2d5669bca 100644 (file)
@@ -5818,8 +5818,16 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
     case FOR_STMT:
       if (!RECUR (FOR_INIT_STMT (t), any))
        return false;
-      if (!RECUR (FOR_COND (t), rval))
+      tmp = FOR_COND (t);
+      if (!RECUR (tmp, rval))
        return false;
+      if (tmp)
+       {
+         if (!processing_template_decl)
+           tmp = cxx_eval_outermost_constant_expr (tmp, true);
+         if (integer_zerop (tmp))
+           return true;
+       }
       if (!RECUR (FOR_EXPR (t), any))
        return false;
       if (!RECUR (FOR_BODY (t), any))
@@ -5840,8 +5848,13 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
       return true;
 
     case WHILE_STMT:
-      if (!RECUR (WHILE_COND (t), rval))
+      tmp = WHILE_COND (t);
+      if (!RECUR (tmp, rval))
        return false;
+      if (!processing_template_decl)
+       tmp = cxx_eval_outermost_constant_expr (tmp, true);
+      if (integer_zerop (tmp))
+       return true;
       if (!RECUR (WHILE_BODY (t), any))
        return false;
       if (breaks (jump_target) || continues (jump_target))
index bbb47d379c05af1f53129f74757112c27b678264..5c751749491b3e2a3321051f2d12cba3c2c1e2a2 100644 (file)
@@ -1,3 +1,8 @@
+2018-10-10  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/87567 - constexpr rejects call to non-constexpr function.
+       * g++.dg/cpp1y/constexpr-loop7.C: New test.
+
 2018-10-10  Paul A. Clarke  <pc@us.ibm.com>
 
        PR target/87579
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-loop7.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-loop7.C
new file mode 100644 (file)
index 0000000..6733820
--- /dev/null
@@ -0,0 +1,21 @@
+// PR c++/87567
+// { dg-do compile { target c++14 } }
+
+constexpr bool always_false() { return false; }
+int f() { return 1; }
+
+constexpr int
+fn1 ()
+{
+  while (always_false ())
+    return f();
+  return 0;
+}
+
+constexpr int
+fn2 ()
+{
+  for (;always_false();)
+    return f();
+  return 0;
+}