C++ PATCH for c++/91705 - constexpr evaluation rejects ++/-- on floats
diff mbox series

Message ID 20190910161856.GJ14737@redhat.com
State New
Headers show
Series
  • C++ PATCH for c++/91705 - constexpr evaluation rejects ++/-- on floats
Related show

Commit Message

Marek Polacek Sept. 10, 2019, 4:18 p.m. UTC
We started to reject this testcase with r269078, whereby we perform constexpr
evaluation on pre-cp_fold_function bodies.  In this test that causes a problem
because the offset argument of a PREINCREMENT_EXPR was a FLOAT_EXPR, and then
the call to fold_build2 doesn't produce a REAL_CST and VERIFY_CONSTANT triggers.

So we need to fold the offset.  But we can't use cxx_eval_constant_expression
because offset can be stuff like "(int*) 4" which is invalid in constexpr.

But we have fold_simple which uses const_unop, and that is precisely the
function that can bash those FLOAT_EXPRs down to REAL_CSTs.

Bootstrapped/regtested on x86_64-linux, ok for trunk/9?

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.

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

Comments

Jason Merrill Sept. 10, 2019, 8:59 p.m. UTC | #1
On 9/10/19 11:18 AM, Marek Polacek wrote:
> We started to reject this testcase with r269078, whereby we perform constexpr
> evaluation on pre-cp_fold_function bodies.  In this test that causes a problem
> because the offset argument of a PREINCREMENT_EXPR was a FLOAT_EXPR, and then
> the call to fold_build2 doesn't produce a REAL_CST and VERIFY_CONSTANT triggers.
> 
> So we need to fold the offset.  But we can't use cxx_eval_constant_expression
> because offset can be stuff like "(int*) 4" which is invalid in constexpr.
> 
> But we have fold_simple which uses const_unop, and that is precisely the
> function that can bash those FLOAT_EXPRs down to REAL_CSTs.
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk/9?
> 
> 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.

OK.

Jason

Patch
diff mbox series

diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c
index 6c547d6d179..58db691e134 100644
--- gcc/cp/constexpr.c
+++ gcc/cp/constexpr.c
@@ -4164,6 +4164,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 gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C
new file mode 100644
index 00000000000..0d22851e4b2
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C
@@ -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);