From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 40C943858D37; Fri, 28 Apr 2023 22:13:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 40C943858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682720005; bh=d9CUOsEwkRQEqGpADdEE4+Z397fF6kOnkg1Ms07guSM=; h=From:To:Subject:Date:From; b=DEYkdrEo0KrR8afM2jamGshUKwAIIctXMWeUr2ngndVj4Wrwk6/mXSzf2iC1BQIvr dz4pSYj2C7Nu5jIjzwQgXruMlFaMj/hPcWT+SfY2vefzy1TU0mhy5gdx3+nfI6Mp8y AdvT06nF1vyY+tAihz4GQBTcUIXu83h+XdZdiaKA= 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-9492] c++: unevaluated array new-expr size constantness [PR108219] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: dbdcc2017689805a8118c8c959e07817f025f83a X-Git-Newrev: 73e86b6766cc92aa8c18cc987bf95929c4ea0672 Message-Id: <20230428221325.40C943858D37@sourceware.org> Date: Fri, 28 Apr 2023 22:13:25 +0000 (GMT) List-Id: https://gcc.gnu.org/g:73e86b6766cc92aa8c18cc987bf95929c4ea0672 commit r12-9492-g73e86b6766cc92aa8c18cc987bf95929c4ea0672 Author: Patrick Palka Date: Wed Mar 1 14:09:37 2023 -0500 c++: unevaluated array new-expr size constantness [PR108219] Here we're mishandling the unevaluated array new-expressions due to a supposed non-constant array size ever since r12-5253-g4df7f8c79835d569 made us no longer perform constant evaluation of non-manifestly-constant expressions within unevaluated contexts. This shouldn't make a difference here since the array sizes are constant literals, except they're expressed as NON_LVALUE_EXPR location wrappers around INTEGER_CST, wrappers which used to get stripped as part of constant evaluation and now no longer do. Moreover it means build_vec_init can't constant fold the MINUS_EXPR 'maxindex' passed from build_new_1 when in an unevaluated context (since it tries reducing it via maybe_constant_value called with mce_unknown). This patch fixes these issues by making maybe_constant_value (and fold_non_dependent_expr) try folding an unevaluated non-manifestly-constant operand via fold(), as long as it simplifies to a simple constant, rather than doing no simplification at all. This covers e.g. simple arithmetic and casts including stripping of location wrappers around INTEGER_CST. In passing, this patch also fixes maybe_constant_value to avoid constant evaluating an unevaluated operand when called with mce_false, by adjusting the early exit test appropriately. Co-authored-by: Jason Merrill PR c++/108219 PR c++/108218 gcc/cp/ChangeLog: * constexpr.cc (fold_to_constant): Define. (maybe_constant_value): Move up early exit test for unevaluated operands. Try reducing an unevaluated operand to a constant via fold_to_constant. (fold_non_dependent_expr_template): Add early exit test for CONSTANT_CLASS_P nodes. Try reducing an unevaluated operand to a constant via fold_to_constant. * cp-tree.h (fold_to_constant): Declare. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/new6.C: New test. * g++.dg/cpp2a/concepts-new1.C: New test. (cherry picked from commit 096f034a8f5df41f610e62c1592fb90a3f551cd5) Diff: --- gcc/cp/constexpr.cc | 25 ++++++++++++++++++++++--- gcc/cp/cp-tree.h | 1 + gcc/testsuite/g++.dg/cpp0x/new6.C | 13 +++++++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-new1.C | 13 +++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index b4e2898d90b..46b2b0296c3 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -8144,6 +8144,19 @@ fold_simple (tree t) return t; } +/* Try folding the expression T to a simple constant. + Returns that constant, otherwise returns T. */ + +tree +fold_to_constant (tree t) +{ + tree r = fold (t); + if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r)) + return r; + else + return t; +} + /* If T is a constant expression, returns its reduced value. Otherwise, if T does not have TREE_CONSTANT set, returns T. Otherwise, returns a version of T without TREE_CONSTANT. @@ -8188,9 +8201,10 @@ maybe_constant_value (tree t, tree decl, bool manifestly_const_eval) return r; } - /* Don't evaluate an unevaluated operand. */ + /* Don't constant evaluate an unevaluated non-manifestly-constant operand, + but at least try folding it to a simple constant. */ if (cp_unevaluated_operand) - return t; + return fold_to_constant (t); uid_sensitive_constexpr_evaluation_checker c; r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl); @@ -8254,9 +8268,14 @@ fold_non_dependent_expr_template (tree t, tsubst_flags_t complain, } return t; } + else if (CONSTANT_CLASS_P (t)) + /* No evaluation needed. */ + return t; + /* Don't constant evaluate an unevaluated non-manifestly-constant operand, + but at least try folding it to a simple constant. */ if (cp_unevaluated_operand && !manifestly_const_eval) - return t; + return fold_to_constant (t); tree r = cxx_eval_outermost_constant_expr (t, true, true, manifestly_const_eval, diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 8228a7b43f1..64b3196d1e9 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8438,6 +8438,7 @@ extern tree fold_non_dependent_init (tree, tsubst_flags_t = tf_warning_or_error, bool = false, tree = NULL_TREE); extern tree fold_simple (tree); +extern tree fold_to_constant (tree); extern bool reduced_constant_expression_p (tree); extern bool is_instantiation_of_constexpr (tree); extern bool var_in_constexpr_fn (tree); diff --git a/gcc/testsuite/g++.dg/cpp0x/new6.C b/gcc/testsuite/g++.dg/cpp0x/new6.C new file mode 100644 index 00000000000..d8f11441423 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/new6.C @@ -0,0 +1,13 @@ +// PR c++/108218 +// { dg-do compile { target c++11 } } + +template +void f() { + decltype(new int[-1]) p; // { dg-error "negative" } + decltype(new int[0-1]) q; // { dg-error "negative" } + decltype(new int[1*-1]) r; // { dg-error "negative" } +} + +decltype(new int[-1]) p; // { dg-error "negative" } +decltype(new int[0-1]) q; // { dg-error "negative" } +decltype(new int[1*-1]) r; // { dg-error "negative" } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-new1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-new1.C new file mode 100644 index 00000000000..62007205108 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-new1.C @@ -0,0 +1,13 @@ +// PR c++/108219 +// { dg-do compile { target c++20 } } + +template +concept C = requires { new T[1]{{ 42 }}; }; + +template +concept D = requires { new T[2][1]{{{ 42 }}, {{ 42 }}}; }; + +struct A { A(int); }; + +static_assert(C); +static_assert(D);