From: Marek Polacek Date: Wed, 10 Oct 2018 21:11:18 +0000 (+0000) Subject: PR c++/87567 - constexpr rejects call to non-constexpr function. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=8e9558f029123e00008d3ef464922128d018f0fc;p=gcc.git PR c++/87567 - constexpr rejects call to non-constexpr function. * constexpr.c (potential_constant_expression_1) : Return true if the condition is always false. : Likewise. * g++.dg/cpp1y/constexpr-loop7.C: New test. From-SVN: r265027 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 975f866c0b7..af8cb764139 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2018-10-10 Marek Polacek + + PR c++/87567 - constexpr rejects call to non-constexpr function. + * constexpr.c (potential_constant_expression_1) : Return + true if the condition is always false. + : Likewise. + 2018-10-09 Paolo Carlini PR c++/84423 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 403edda8c47..4fa8c965a9d 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -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)) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index bbb47d379c0..5c751749491 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-10-10 Marek Polacek + + PR c++/87567 - constexpr rejects call to non-constexpr function. + * g++.dg/cpp1y/constexpr-loop7.C: New test. + 2018-10-10 Paul A. Clarke 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 index 00000000000..6733820fee2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-loop7.C @@ -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; +}