From d85569f63db86e656ecb79b81c74a906f27bf509 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 10 Sep 2019 21:04:33 +0000 Subject: [PATCH] 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. From-SVN: r275613 --- gcc/cp/ChangeLog | 6 ++ gcc/cp/constexpr.c | 4 ++ gcc/testsuite/ChangeLog | 5 ++ gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C | 66 ++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 2c488297f99..15b1fbfcd0d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2019-09-10 Marek Polacek + + PR c++/91705 - constexpr evaluation rejects ++/-- on floats. + * constexpr.c (cxx_eval_increment_expression): Call fold_simple on + the offset. + 2019-09-10 Paolo Carlini * decl.c (has_designator_problem): Use cp_expr_loc_or_input_loc diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 6c547d6d179..58db691e134 100644 --- a/gcc/cp/constexpr.c +++ b/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 a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c8779bb34e5..464765d5293 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2019-09-10 Marek Polacek + + PR c++/91705 - constexpr evaluation rejects ++/-- on floats. + * g++.dg/cpp1y/constexpr-incr2.C: New test. + 2019-09-10 David Edelsohn * gfortran.dg/default_format_1.f90: Remove XFAIL AIX. diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-incr2.C new file mode 100644 index 00000000000..0d22851e4b2 --- /dev/null +++ b/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 +constexpr T fn1(T t) +{ + return ++t; +} + +constexpr float fn2(float t) +{ + return ++t; +} + +template +constexpr T fn3(T t) +{ + return --t; +} + +constexpr float fn4(float t) +{ + return --t; +} + +template +constexpr T fn5(T t) +{ + return t++; +} + +constexpr float fn6(float t) +{ + return t++; +} + +template +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); -- 2.30.2