* [PATCH] c++: constexpr empty subobject confusion [PR110197]
@ 2023-07-26 16:57 Patrick Palka
2023-07-26 17:14 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2023-07-26 16:57 UTC (permalink / raw)
To: gcc-patches; +Cc: jason, Patrick Palka
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk/13 (later)?
-- >8 --
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 cxx_eval_bare_aggregate
and cxx_eval_vec_init_1 consistently omit entries for such subobjects in
the parent ctx->ctor. We also need to allow cxx_eval_array_reference
to synthesize an empty element object even if the array CONSTRUCTOR
has CONSTRUCTOR_NO_CLEARING set.
PR c++/110197
gcc/cp/ChangeLog:
* constexpr.cc (cxx_eval_array_reference): Return a synthesized
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 empty, i.e. for any 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 +++++++++++++------
.../g++.dg/cpp0x/constexpr-empty18.C | 7 ++++++
.../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.41.0.450.ga80be15292
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: constexpr empty subobject confusion [PR110197]
2023-07-26 16:57 [PATCH] c++: constexpr empty subobject confusion [PR110197] Patrick Palka
@ 2023-07-26 17:14 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-07-26 17:14 UTC (permalink / raw)
To: Patrick Palka, gcc-patches
On 7/26/23 12:57, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk/13 (later)?
OK.
> -- >8 --
>
> 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 cxx_eval_bare_aggregate
> and cxx_eval_vec_init_1 consistently omit entries for such subobjects in
> the parent ctx->ctor. We also need to allow cxx_eval_array_reference
> to synthesize an empty element object even if the array CONSTRUCTOR
> has CONSTRUCTOR_NO_CLEARING set.
>
> PR c++/110197
>
> gcc/cp/ChangeLog:
>
> * constexpr.cc (cxx_eval_array_reference): Return a synthesized
> 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 empty, i.e. for any 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 +++++++++++++------
> .../g++.dg/cpp0x/constexpr-empty18.C | 7 ++++++
> .../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] = {};
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-07-26 17:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-26 16:57 [PATCH] c++: constexpr empty subobject confusion [PR110197] Patrick Palka
2023-07-26 17:14 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).