From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 09706382F0B9; Wed, 25 May 2022 16:59:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 09706382F0B9 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-10760] c++: constexpr ref to array of array [PR102307] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: bea4429004b02b834c6a15dcc3bfbd953b230585 X-Git-Newrev: 5022f4af0d1e2f3fdf2bb159d4372ee3cf34b052 Message-Id: <20220525165924.09706382F0B9@sourceware.org> Date: Wed, 25 May 2022 16:59:24 +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: Wed, 25 May 2022 16:59:24 -0000 https://gcc.gnu.org/g:5022f4af0d1e2f3fdf2bb159d4372ee3cf34b052 commit r10-10760-g5022f4af0d1e2f3fdf2bb159d4372ee3cf34b052 Author: Jason Merrill Date: Tue Apr 26 11:15:04 2022 -0400 c++: constexpr ref to array of array [PR102307] The problem here is that first check_initializer calls build_aggr_init_full_exprs, which does overload resolution, but then in the case of failed constexpr throws away the result and does it again in build_functional_cast. But in the first overload resolution, reshape_init_array_1 decided to reuse the inner CONSTRUCTORs because tf_error is set, so we know we're committed. But the second pass gets confused by the CONSTRUCTORs with non-init-list types. Fixed by avoiding a second pass: instead, pass the call from build_aggr_init to build_cplus_new, which will turn it into a TARGET_EXPR. I don't bother to change the object argument because it will be replaced later in simplify_aggr_init_expr. PR c++/102307 gcc/cp/ChangeLog: * decl.c (check_initializer): Use build_cplus_new in case of constexpr failure. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/constexpr-array2.C: New test. Diff: --- gcc/cp/decl.c | 17 ++++++++++++----- gcc/testsuite/g++.dg/cpp1z/constexpr-array2.C | 12 ++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 6089cd7e52d..38003a5a70f 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6876,12 +6876,19 @@ check_initializer (tree decl, tree init, int flags, vec **cleanups) /* Declared constexpr or constinit, but no suitable initializer; massage init appropriately so we can pass it into store_init_value for the error. */ - if (CLASS_TYPE_P (type) - && (!init || TREE_CODE (init) == TREE_LIST)) + tree new_init = NULL_TREE; + if (!processing_template_decl + && TREE_CODE (init_code) == CALL_EXPR) + new_init = build_cplus_new (type, init_code, tf_none); + else if (CLASS_TYPE_P (type) + && (!init || TREE_CODE (init) == TREE_LIST)) + new_init = build_functional_cast (input_location, type, + init, tf_none); + if (new_init) { - init = build_functional_cast (input_location, type, - init, tf_none); - if (TREE_CODE (init) == TARGET_EXPR) + init = new_init; + if (TREE_CODE (init) == TARGET_EXPR + && !(flags & LOOKUP_ONLYCONVERTING)) TARGET_EXPR_DIRECT_INIT_P (init) = true; } init_code = NULL_TREE; diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-array2.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-array2.C new file mode 100644 index 00000000000..c30e3f2361d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-array2.C @@ -0,0 +1,12 @@ +// PR c++/102307 +// { dg-do compile { target c++11 } } + +#include +template struct Matrix { + constexpr Matrix(double const (&arr)[N][M]); // { dg-warning "never defined" } + constexpr Matrix(std::array, N> const &arr); +}; +int main() { + constexpr Matrix<2, 3> + mat {{ {1.0, 2.0, 3.0}, {4.0, 5.0, 6.0} }}; // { dg-error "before its definition" } +}