From 0712ea6313bc44f9ae8feb235c1b02c92cdd0527 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Tue, 4 Feb 2020 15:54:17 -0500 Subject: [PATCH] c++: Fix constexpr vs. omitted aggregate init. Value-initialization is importantly different from {}-initialization for this testcase, where the former calls the deleted S constructor and the latter initializes S happily. PR c++/90951 * constexpr.c (cxx_eval_array_reference): {}-initialize missing elements instead of value-initializing them. --- gcc/cp/ChangeLog | 4 ++++ gcc/cp/constexpr.c | 12 ++++++++++-- gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C | 10 ++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 53f414f..956e94b 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,9 @@ 2020-02-04 Jason Merrill + PR c++/90951 + * constexpr.c (cxx_eval_array_reference): {}-initialize missing + elements instead of value-initializing them. + PR c++/86917 * init.c (perform_member_init): Simplify. * constexpr.c (cx_check_missing_mem_inits): Allow uninitialized diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index c35ec5a..8a02c6e 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -3324,8 +3324,16 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, } /* If it's within the array bounds but doesn't have an explicit - initializer, it's value-initialized. */ - tree val = build_value_init (elem_type, tf_warning_or_error); + initializer, it's initialized from {}. But use build_value_init + directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */ + tree val; + if (CP_AGGREGATE_TYPE_P (elem_type)) + { + tree empty_ctor = build_constructor (init_list_type_node, NULL); + val = digest_init (elem_type, empty_ctor, tf_warning_or_error); + } + else + val = build_value_init (elem_type, tf_warning_or_error); return cxx_eval_constant_expression (ctx, val, lval, non_constant_p, overflow_p); } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C new file mode 100644 index 0000000..5389698 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array24.C @@ -0,0 +1,10 @@ +// PR c++/90951 +// { dg-do compile { target c++11 } } + +#define assert(expr) static_assert (expr, #expr) + +struct S { const char a[2]; }; + +constexpr struct S a[1][1][1] = { }; + +assert ('\0' == *a[0][0][0].a); -- 2.7.4