]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/91705 - constexpr evaluation rejects ++/-- on floats.
authorMarek Polacek <polacek@redhat.com>
Tue, 10 Sep 2019 22:39:46 +0000 (22:39 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Tue, 10 Sep 2019 22:39:46 +0000 (22:39 +0000)
* constexpr.c (cxx_eval_increment_expression): Call fold_simple on
the offset.

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

From-SVN: r275615

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

index fc78b3f24b4053e9d6eab4c7c7306362164c37e8..8bd42b839731e798f866f0298b2696183f061b8a 100644 (file)
@@ -1,3 +1,12 @@
+2019-09-10  Marek Polacek  <polacek@redhat.com>
+
+       Backported from mainline
+       2019-09-10  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/91705 - constexpr evaluation rejects ++/-- on floats.
+       * constexpr.c (cxx_eval_increment_expression): Call fold_simple on
+       the offset.
+
 2019-09-01  Marek Polacek  <polacek@redhat.com>
 
        Backported from mainline
index f6f21b33396c9886ebf9ccf092114d1aa29356bc..a229b48d746811c604c47f92b496de2768802f46 100644 (file)
@@ -3973,6 +3973,10 @@ cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
   tree offset = TREE_OPERAND (t, 1);
   gcc_assert (TREE_CONSTANT (offset));
 
+  /* OFFSET is constant, but perhaps not constant enough.  We need to
+     e.g. bash FLOAT_EXPRs to REAL_CSTs.  */
+  offset = fold_simple (offset);
+
   /* The operand as an lvalue.  */
   op = cxx_eval_constant_expression (ctx, op, true,
                                     non_constant_p, overflow_p);
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C
new file mode 100644 (file)
index 0000000..0d22851
--- /dev/null
@@ -0,0 +1,66 @@
+// PR c++/91705 - constexpr evaluation rejects ++/-- on floats.
+// { dg-do compile { target c++14 } }
+
+#define SA(X) static_assert((X),#X)
+
+template <class T>
+constexpr T fn1(T t)
+{
+  return ++t;
+}
+
+constexpr float fn2(float t)
+{
+  return ++t;
+}
+
+template <class T>
+constexpr T fn3(T t)
+{
+  return --t;
+}
+
+constexpr float fn4(float t)
+{
+  return --t;
+}
+
+template <class T>
+constexpr T fn5(T t)
+{
+  return t++;
+}
+
+constexpr float fn6(float t)
+{
+  return t++;
+}
+
+template <class T>
+constexpr T fn7(T t)
+{
+  return t--;
+}
+
+constexpr float fn8(float t)
+{
+  return t--;
+}
+
+constexpr double r1 = fn1(2.0f);
+SA(r1 == 3);
+constexpr double r2 = fn2(2.0f);
+SA(r2 == 3);
+constexpr double r3 = fn3(2.0f);
+SA(r3 == 1);
+constexpr double r4 = fn4(2.0f);
+SA(r4 == 1);
+
+constexpr double r5 = fn5(2.0f);
+SA(r5 == 2);
+constexpr double r6 = fn6(2.0f);
+SA(r6 == 2);
+constexpr double r7 = fn7(2.0f);
+SA(r7 == 2);
+constexpr double r8 = fn8(2.0f);
+SA(r8 == 2);