From: Jakub Jelinek Date: Wed, 15 Jan 2025 17:27:07 +0000 (+0100) Subject: c++: Handle RAW_DATA_CST in unify [PR118390] X-Git-Tag: basepoints/gcc-16~2629 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2619413a5e032e2f2c3d4a2202ba8bf324cf08ab;p=thirdparty%2Fgcc.git c++: Handle RAW_DATA_CST in unify [PR118390] This patch uses the count_ctor_elements function to fix up unify deduction of array sizes. 2025-01-15 Jakub Jelinek PR c++/118390 * cp-tree.h (count_ctor_elements): Declare. * call.cc (count_ctor_elements): No longer static. * pt.cc (unify): Use count_ctor_elements instead of CONSTRUCTOR_NELTS. * g++.dg/cpp/embed-20.C: New test. * g++.dg/cpp0x/pr118390.C: New test. --- diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 63be8b58906..9e57261cf17 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -4333,7 +4333,7 @@ has_non_trivial_temporaries (tree expr) /* Return number of initialized elements in CTOR. */ -static unsigned HOST_WIDE_INT +unsigned HOST_WIDE_INT count_ctor_elements (tree ctor) { unsigned HOST_WIDE_INT len = 0; diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 2d17c4c77b5..bb10c7ca053 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -6815,6 +6815,7 @@ extern tree type_decays_to (tree); extern tree extract_call_expr (tree); extern tree build_trivial_dtor_call (tree, bool = false); extern tristate ref_conv_binds_to_temporary (tree, tree, bool = false); +extern unsigned HOST_WIDE_INT count_ctor_elements (tree); extern tree build_user_type_conversion (tree, tree, int, tsubst_flags_t); extern tree build_new_function_call (tree, vec **, diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index ff0a3a4f7d8..3b6743f2e3a 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -25072,7 +25072,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict, && deducible_array_bound (TYPE_DOMAIN (parm))) { /* Also deduce from the length of the initializer list. */ - tree max = size_int (CONSTRUCTOR_NELTS (arg)); + tree max = size_int (count_ctor_elements (arg)); tree idx = compute_array_index_type (NULL_TREE, max, tf_none); if (idx == error_mark_node) return unify_invalid (explain_p); diff --git a/gcc/testsuite/g++.dg/cpp/embed-20.C b/gcc/testsuite/g++.dg/cpp/embed-20.C new file mode 100644 index 00000000000..ad708406f3d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp/embed-20.C @@ -0,0 +1,14 @@ +// PR c++/118390 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +template +constexpr int +foo (const T (&x)[N]) +{ + return N; +} + +static_assert (foo ({ + #embed __FILE__ limit (64) +}) == 64, ""); diff --git a/gcc/testsuite/g++.dg/cpp0x/pr118390.C b/gcc/testsuite/g++.dg/cpp0x/pr118390.C new file mode 100644 index 00000000000..e5017b017d7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/pr118390.C @@ -0,0 +1,23 @@ +// PR c++/118390 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +template +constexpr int +foo (const T (&x)[N]) +{ + return N; +} + +static_assert (foo ({ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, +}) == 160, "");