From: Marek Polacek Date: Mon, 29 Oct 2018 19:40:18 +0000 (+0000) Subject: PR c++/87594 - constexpr rejects-valid with range-based for. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a385474c79d28d89861d25720715ed361e912b1a;p=gcc.git PR c++/87594 - constexpr rejects-valid with range-based for. * constexpr.c (potential_constant_expression_1): If the condition can't be evaluated, return true. * g++.dg/cpp1y/constexpr-loop8.C: New test. From-SVN: r265600 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9130b8663d3..6b9574a6527 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2018-10-29 Marek Polacek + + PR c++/87594 - constexpr rejects-valid with range-based for. + * constexpr.c (potential_constant_expression_1): If the condition + can't be evaluated, return true. + 2018-10-29 Joseph Myers Julian Brown diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 4fa8c965a9d..7692b1727da 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5825,7 +5825,9 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, { if (!processing_template_decl) tmp = cxx_eval_outermost_constant_expr (tmp, true); - if (integer_zerop (tmp)) + /* If we couldn't evaluate the condition, it might not ever be + true. */ + if (!integer_onep (tmp)) return true; } if (!RECUR (FOR_EXPR (t), any)) @@ -5853,7 +5855,8 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, return false; if (!processing_template_decl) tmp = cxx_eval_outermost_constant_expr (tmp, true); - if (integer_zerop (tmp)) + /* If we couldn't evaluate the condition, it might not ever be true. */ + if (!integer_onep (tmp)) return true; if (!RECUR (WHILE_BODY (t), any)) return false; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6cc3e1bb86a..bfccc6038ca 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-10-29 Marek Polacek + + PR c++/87594 - constexpr rejects-valid with range-based for. + * g++.dg/cpp1y/constexpr-loop8.C: New test. + 2018-10-29 Richard Biener PR tree-optimization/87785 diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-loop8.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-loop8.C new file mode 100644 index 00000000000..bf132f2484e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-loop8.C @@ -0,0 +1,44 @@ +// PR c++/87594 +// { dg-do compile { target c++14 } } + +constexpr bool always_false() { return false; } +int f() { return 1; } + +constexpr int +fn1() +{ + struct empty_range { + constexpr int* begin() { return 0; } + constexpr int* end() { return 0; } + } e; + for (auto x : e) + f(); + return 0; +} + +constexpr int +fn2 () +{ + int a[] = { 1, 2, 3 }; + for (auto x : a) + f(); // { dg-error "call to non-.constexpr. function" } + return 0; +} + +constexpr int +fn3 () +{ + __extension__ int a[] = { }; + for (auto x : a) + f(); + return 0; +} + + +void +bar () +{ + constexpr int i1 = fn1 (); + constexpr int i2 = fn2 (); // { dg-message "in .constexpr. expansion of " } + constexpr int i3 = fn3 (); +}