From a426b91b27e28985f47d16827a532fbc28c09bd7 Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Thu, 27 Jul 2023 09:10:07 -0400 Subject: [PATCH] c++: constexpr empty subobject elision [PR110197] Now that init_subob_ctx no longer sets new_ctx.ctor for a subobject of empty type, it seems we need to ensure its callers also consistently omit entries in the parent ctx->ctor for such subobjects. We also need to allow cxx_eval_array_reference to synthesize an empty subobject even if the array CONSTRUCTOR has CONSTRUCTOR_NO_CLEARING set. PR c++/110197 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_array_reference): Allow synthesizing an empty subobject even if CONSTRUCTOR_NO_CLEARING is set. (cxx_eval_bare_aggregate): Set 'no_slot' to true more generally whenever new_ctx.ctor is set to NULL_TREE by init_subob_ctx, i.e. whenever initializing an subobject of empty type. (cxx_eval_vec_init_1): Define 'no_slot' as above and use it accordingly. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-empty18.C: New test. * g++.dg/cpp0x/constexpr-empty19.C: New test. --- gcc/cp/constexpr.cc | 23 ++++++++++++++++------- gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C | 7 +++++++ gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C | 12 ++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index f2fcb54626d..da2c3116810 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -4297,6 +4297,9 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, /* Not found. */ + if (is_really_empty_class (elem_type, /*ignore_vptr*/false)) + return build_constructor (elem_type, NULL); + if (TREE_CODE (ary) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (ary)) { @@ -4314,9 +4317,7 @@ 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 (is_really_empty_class (elem_type, /*ignore_vptr*/false)) - return build_constructor (elem_type, NULL); - else if (CP_AGGREGATE_TYPE_P (elem_type)) + 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); @@ -5095,9 +5096,9 @@ cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t, FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value) { tree orig_value = value; - /* Like in cxx_eval_store_expression, omit entries for empty fields. */ - bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index); init_subob_ctx (ctx, new_ctx, index, value); + /* Like in cxx_eval_store_expression, omit entries for empty fields. */ + bool no_slot = new_ctx.ctor == NULL_TREE; int pos_hint = -1; if (new_ctx.ctor != ctx->ctor && !no_slot) { @@ -5261,7 +5262,8 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, bool reuse = false; constexpr_ctx new_ctx; init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype); - if (new_ctx.ctor != ctx->ctor) + bool no_slot = new_ctx.ctor == NULL_TREE; + if (new_ctx.ctor != ctx->ctor && !no_slot) { if (zeroed_out) CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false; @@ -5306,7 +5308,14 @@ cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init, } if (*non_constant_p) break; - if (new_ctx.ctor != ctx->ctor) + if (no_slot) + { + /* This is an initializer for an empty subobject; now that we've + checked that it's constant, we can ignore it. */ + gcc_checking_assert (i == 0); + break; + } + else if (new_ctx.ctor != ctx->ctor) { /* We appended this element above; update the value. */ gcc_assert ((*p)->last().index == idx); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C new file mode 100644 index 00000000000..4bb9e3dcb64 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty18.C @@ -0,0 +1,7 @@ +// PR c++/110197 +// { dg-do compile { target c++11 } } + +struct A { constexpr A(int) { } }; +struct B { A a; }; +constexpr B f(int n) { return B{A{n}}; } +constexpr B b = f(1); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C new file mode 100644 index 00000000000..5ad67682c5b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-empty19.C @@ -0,0 +1,12 @@ +// PR c++/110197 +// { dg-do compile { target c++11 } } + +struct A { + constexpr A() : A(__builtin_is_constant_evaluated()) { } + constexpr A(int) { } +}; +constexpr A a1[1] = {{}}; +constexpr A a2[2] = {{}, {}}; +constexpr A a3[3] = {{}, {}, {}}; +constexpr A a4[4] = {}; +constexpr A a5[5] = {}; -- 2.11.4.GIT