From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 3028A385DC2D; Sat, 12 Mar 2022 20:02:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3028A385DC2D MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-7630] c++: ICE with non-constant satisfaction value [PR98644] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 28750ac2765e808aa4f45a73c630e53575834efd X-Git-Newrev: ab71d3fe4b23af4c29a8d6fcf1e914fed4393e3b Message-Id: <20220312200231.3028A385DC2D@sourceware.org> Date: Sat, 12 Mar 2022 20:02:31 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 12 Mar 2022 20:02:31 -0000 https://gcc.gnu.org/g:ab71d3fe4b23af4c29a8d6fcf1e914fed4393e3b commit r12-7630-gab71d3fe4b23af4c29a8d6fcf1e914fed4393e3b Author: Patrick Palka Date: Sat Mar 12 14:57:59 2022 -0500 c++: ICE with non-constant satisfaction value [PR98644] Here during satisfaction, the expression of the atomic constraint after substitution is (int *) NON_LVALUE_EXPR <1> != 0B, which is not a C++ constant expression due to the reinterpret_cast, but TREE_CONSTANT is set since its value is otherwise effectively constant. We then call maybe_constant_value on it, which proceeds via its fail-fast path to exit early without clearing TREE_CONSTANT. But satisfy_atom relies on checking TREE_CONSTANT of the result of maybe_constant_value in order to detect non-constant satisfaction. This patch fixes this by making the fail-fast path of maybe_constant_value clear TREE_CONSTANT in this case, like cxx_eval_outermost_constant_expr in the normal path would have done. PR c++/98644 gcc/cp/ChangeLog: * constexpr.cc (mark_non_constant): Define, split out from ... (cxx_eval_outermost_constant_expr): ... here. (maybe_constant_value): Use it. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-pr98644.C: New test. * g++.dg/parse/array-size2.C: Remove expected diagnostic about a narrowing conversion. Co-authored-by: Jason Merrill Diff: --- gcc/cp/constexpr.cc | 46 ++++++++++++++++----------- gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C | 7 ++++ gcc/testsuite/g++.dg/parse/array-size2.C | 2 -- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 388957913be..bbb6cd99b3c 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7579,6 +7579,29 @@ find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/) return NULL_TREE; } +/* T has TREE_CONSTANT set but has been deemed not a valid C++ constant + expression. Return a version of T that has TREE_CONSTANT cleared. */ + +static tree +mark_non_constant (tree t) +{ + gcc_checking_assert (TREE_CONSTANT (t)); + + /* This isn't actually constant, so unset TREE_CONSTANT. + Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires + it to be set if it is invariant address, even when it is not + a valid C++ constant expression. Wrap it with a NOP_EXPR + instead. */ + if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR) + t = copy_node (t); + else if (TREE_CODE (t) == CONSTRUCTOR) + t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t); + else + t = build_nop (TREE_TYPE (t), t); + TREE_CONSTANT (t) = false; + return t; +} + /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression. STRICT has the same sense as for constant_value_1: true if we only allow conforming C++ constant expressions, or false if we want a constant value @@ -7801,20 +7824,7 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, else if (constexpr_dtor) return r; else if (non_constant_p && TREE_CONSTANT (r)) - { - /* This isn't actually constant, so unset TREE_CONSTANT. - Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires - it to be set if it is invariant address, even when it is not - a valid C++ constant expression. Wrap it with a NOP_EXPR - instead. */ - if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR) - r = copy_node (r); - else if (TREE_CODE (r) == CONSTRUCTOR) - r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r); - else - r = build_nop (TREE_TYPE (r), r); - TREE_CONSTANT (r) = false; - } + r = mark_non_constant (r); else if (non_constant_p) return t; @@ -7965,11 +7975,9 @@ maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) if (!is_nondependent_constant_expression (t)) { - if (TREE_OVERFLOW_P (t)) - { - t = build_nop (TREE_TYPE (t), t); - TREE_CONSTANT (t) = false; - } + if (TREE_OVERFLOW_P (t) + || (!processing_template_decl && TREE_CONSTANT (t))) + t = mark_non_constant (t); return t; } else if (CONSTANT_CLASS_P (t)) diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C b/gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C new file mode 100644 index 00000000000..6772f72a3ce --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-pr98644.C @@ -0,0 +1,7 @@ +// PR c++/98644 +// { dg-do compile { target c++20 } } + +template concept Signed = bool(T(1)); // { dg-error "reinterpret_cast" } +static_assert(Signed); // { dg-error "non-constant" } + +constexpr bool B = requires { requires bool((char *)1); }; // { dg-error "reinterpret_cast" } diff --git a/gcc/testsuite/g++.dg/parse/array-size2.C b/gcc/testsuite/g++.dg/parse/array-size2.C index c4a69df3b01..e58fe266e77 100644 --- a/gcc/testsuite/g++.dg/parse/array-size2.C +++ b/gcc/testsuite/g++.dg/parse/array-size2.C @@ -15,8 +15,6 @@ void foo (void) { char g[(char *) &((struct S *) 0)->b - (char *) 0]; // { dg-error "40:size of array .g. is not an integral constant-expression" } - // { dg-error "narrowing conversion" "" { target c++11 } .-1 } - // { dg-message "expression has a constant value but is not a C.. constant-expression" "" { target c++11 } .-2 } char h[(__SIZE_TYPE__) &((struct S *) 8)->b]; // { dg-error "10:size of array .h. is not an integral constant-expression" } bar (g, h); }