aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPatrick Palka <ppalka@redhat.com>2021-06-30 20:44:52 -0400
committerPatrick Palka <ppalka@redhat.com>2021-06-30 20:44:52 -0400
commita688c284dd3848b6c4ea553035f0f9769fb4fbc9 (patch)
treeb1f5e3e737b4d9af203fe878a0f6c7652e29e775 /gcc
parent613497aa6e28ca009d8498002424019d2a8a9ca5 (diff)
downloadgcc-a688c284dd3848b6c4ea553035f0f9769fb4fbc9.zip
gcc-a688c284dd3848b6c4ea553035f0f9769fb4fbc9.tar.gz
gcc-a688c284dd3848b6c4ea553035f0f9769fb4fbc9.tar.bz2
c++: cxx_eval_array_reference and empty elem type [PR101194]
Here the initializer for x is represented as an empty CONSTRUCTOR due to its empty element type. So during constexpr evaluation of the ARRAY_REF x[0], we end up trying to value initialize the omitted element at index 0, which fails because the element type is not default constructible. This patch makes cxx_eval_array_reference specifically handle the case where the element type is an empty type. PR c++/101194 gcc/cp/ChangeLog: * constexpr.c (cxx_eval_array_reference): When the element type is an empty type and the corresponding element is omitted, just return an empty CONSTRUCTOR instead of attempting value initialization. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-empty16.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/constexpr.c4
-rw-r--r--gcc/testsuite/g++.dg/cpp0x/constexpr-empty16.C10
2 files changed, 13 insertions, 1 deletions
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 4cd9db3..39787f3 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -3845,7 +3845,9 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
tree val;
constexpr_ctx new_ctx;
- if (CP_AGGREGATE_TYPE_P (elem_type))
+ if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
+ return build_constructor (elem_type, NULL);
+ else 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);
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-empty16.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty16.C
new file mode 100644
index 0000000..79be244
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty16.C
@@ -0,0 +1,10 @@
+// PR c++/101194
+// { dg-do compile { target c++11 } }
+
+struct nodefault {
+ constexpr nodefault(int) { }
+};
+
+constexpr nodefault x[1] = { nodefault{0} };
+
+constexpr nodefault y = x[0];