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.
This commit is contained in:
Patrick Palka 2023-07-27 09:10:07 -04:00
parent 33b153ff52
commit a426b91b27
3 changed files with 35 additions and 7 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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] = {};