diff options
author | Marek Polacek <polacek@redhat.com> | 2025-02-27 17:42:49 -0500 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2025-03-04 09:25:54 -0500 |
commit | 173cf7c9b8c0d61bb2cb0bd3a9e3150b393ab59a (patch) | |
tree | 3eced7ff97a6dd41df66a2ba544a9fe503e74e41 | |
parent | d883f3233c4b7e0dce52539a12dfcccc8aff43e4 (diff) | |
download | gcc-173cf7c9b8c0d61bb2cb0bd3a9e3150b393ab59a.zip gcc-173cf7c9b8c0d61bb2cb0bd3a9e3150b393ab59a.tar.gz gcc-173cf7c9b8c0d61bb2cb0bd3a9e3150b393ab59a.tar.bz2 |
c++: ICE with RANGE_EXPR and array init [PR109431]
We crash because we generate
{[0 ... 1]={.low=0, .high=1}, [1]={.low=0, .high=1}}
which output_constructor_regular_field doesn't want to see. This
happens since r9-1483: process_init_constructor_array can now create
a RANGE_EXPR. But the bug isn't in that patch; the problem is that
build_vec_init doesn't handle RANGE_EXPRs.
build_vec_init has a FOR_EACH_CONSTRUCTOR_ELT loop which populates
const_vec. In this case it loops over the elements of
{[0 ... 1]={.low=0, .high=1}}
but assumes that each element initializes one element. So after the
loop num_initialized_elts was 1, and then below:
HOST_WIDE_INT last = tree_to_shwi (maxindex);
if (num_initialized_elts <= last)
{
tree field = size_int (num_initialized_elts);
if (num_initialized_elts != last)
field = build2 (RANGE_EXPR, sizetype, field,
size_int (last));
CONSTRUCTOR_APPEND_ELT (const_vec, field, e);
}
we added the extra initializer.
It seemed convenient to use range_expr_nelts like below.
PR c++/109431
gcc/cp/ChangeLog:
* cp-tree.h (range_expr_nelts): Declare.
* init.cc (build_vec_init): If the CONSTRUCTOR's index is a
RANGE_EXPR, use range_expr_nelts to count how many elements
were initialized.
gcc/testsuite/ChangeLog:
* g++.dg/init/array67.C: New test.
Reviewed-by: Jason Merrill <jason@redhat.com>
-rw-r--r-- | gcc/cp/cp-tree.h | 1 | ||||
-rw-r--r-- | gcc/cp/init.cc | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/init/array67.C | 29 |
3 files changed, 34 insertions, 1 deletions
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index c76a92d..583d049 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8621,6 +8621,7 @@ extern tree mangle_decomp (tree, vec<tree> &); extern void mangle_module_substitution (int); extern int mangle_module_component (tree id, bool partition); extern tree mangle_module_global_init (int); +extern unsigned HOST_WIDE_INT range_expr_nelts (tree); /* in dump.cc */ extern bool cp_dump_tree (void *, tree); diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index f77da42..ce6e58e 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -4802,7 +4802,10 @@ build_vec_init (tree base, tree maxindex, tree init, tree baseref = build1 (INDIRECT_REF, type, base); tree one_init; - num_initialized_elts++; + if (field && TREE_CODE (field) == RANGE_EXPR) + num_initialized_elts += range_expr_nelts (field); + else + num_initialized_elts++; /* We need to see sub-array TARGET_EXPR before cp_fold_r so we can handle cleanup flags properly. */ diff --git a/gcc/testsuite/g++.dg/init/array67.C b/gcc/testsuite/g++.dg/init/array67.C new file mode 100644 index 0000000..8aee8fd --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array67.C @@ -0,0 +1,29 @@ +// PR c++/109431 +// { dg-do run { target c++11 } } + +struct RangeLimits +{ + int low = 0; + int high = 1; + constexpr RangeLimits() { } +}; + +template <int> +int parameterLimits(void) +{ + static RangeLimits constexpr param_limits[2] = {}; + if (param_limits[0].low != 0 + || param_limits[0].high != 1 + || param_limits[1].low != 0 + || param_limits[1].high != 1) + __builtin_abort (); + auto const &limits = param_limits[1]; + return 0; +} + +auto s = parameterLimits<1>(); + +int +main () +{ +} |