public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9710] c++: constexpr array reference and value-initialization [PR101371]
@ 2022-03-29 1:44 Marek Polacek
0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2022-03-29 1:44 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:da47a84e277c7fbeebbf9c1e7dc1e8ba3277fe53
commit r11-9710-gda47a84e277c7fbeebbf9c1e7dc1e8ba3277fe53
Author: Marek Polacek <polacek@redhat.com>
Date: Tue Jul 13 17:16:54 2021 -0400
c++: constexpr array reference and value-initialization [PR101371]
This PR gave me a hard time: I saw multiple issues starting with
different revisions. But ultimately the root cause seems to be
the following, and the attached patch fixes all issues I've found
here.
In cxx_eval_array_reference we create a new constexpr context for the
CP_AGGREGATE_TYPE_P case, but we also have to create it for the
non-aggregate case. In this test, we are evaluating
((B *)this)->a = rhs->a
which means that we set ctx.object to ((B *)this)->a. Then we proceed
to evaluate the initializer, rhs->a. For *rhs, we eval rhs, a PARM_DECL,
for which we have (const B &) &c.arr[0] in the hash table. Then
cxx_fold_indirect_ref gives us c.arr[0]. c is evaluated to {.arr={}} so
c.arr is {}. Now we want c.arr[0], so we end up in cxx_eval_array_reference
and since we're initializing from {}, we call build_value_init which
gives us an AGGR_INIT_EXPR that calls 'constexpr B::B()'. Then we
evaluate this AGGR_INIT_EXPR and since its first argument is dummy,
we take ctx.object instead. But that is the wrong object, we're not
initializing ((B *)this)->a here. And so we wound up with an
initializer for A, and then crash in cxx_eval_component_reference:
gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
where DECL_CONTEXT (part) is B (as it should be) but the type of whole
was A.
So create a new object, if there already was one, and the element type
is not a scalar.
PR c++/101371
gcc/cp/ChangeLog:
* constexpr.c (cxx_eval_array_reference): Create a new .object
and .ctor for the non-aggregate non-scalar case too when
value-initializing.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1y/constexpr-101371-2.C: New test.
* g++.dg/cpp1y/constexpr-101371.C: New test.
(cherry picked from commit a42f8120442cf3ba25d621bed857b5be19019d0c)
Diff:
---
gcc/cp/constexpr.c | 15 +++++++++----
gcc/testsuite/g++.dg/cpp1y/constexpr-101371-2.C | 23 ++++++++++++++++++++
gcc/testsuite/g++.dg/cpp1y/constexpr-101371.C | 29 +++++++++++++++++++++++++
3 files changed, 63 insertions(+), 4 deletions(-)
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 4d86442ca11..f528bfd38ab 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -3859,16 +3859,23 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
{
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);
+
+ if (!SCALAR_TYPE_P (elem_type))
+ {
new_ctx = *ctx;
- new_ctx.object = t;
+ if (ctx->object)
+ /* If there was no object, don't add one: it could confuse us
+ into thinking we're modifying a const object. */
+ new_ctx.object = t;
new_ctx.ctor = build_constructor (elem_type, NULL);
ctx = &new_ctx;
}
- else
- val = build_value_init (elem_type, tf_warning_or_error);
t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
overflow_p);
- if (CP_AGGREGATE_TYPE_P (elem_type) && t != ctx->ctor)
+ if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor)
free_constructor (ctx->ctor);
return t;
}
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-101371-2.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-101371-2.C
new file mode 100644
index 00000000000..fb67b67c265
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-101371-2.C
@@ -0,0 +1,23 @@
+// PR c++/101371
+// { dg-do compile { target c++14 } }
+
+struct A {
+ int i;
+};
+struct B {
+ A a{};
+ constexpr B() : a() {}
+ constexpr B(const B &rhs) : a(rhs.a) {}
+};
+struct C {
+ B arr[1];
+};
+
+constexpr C
+fn ()
+{
+ C c{};
+ return c;
+}
+
+constexpr C c = fn();
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-101371.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-101371.C
new file mode 100644
index 00000000000..b6351b806b9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-101371.C
@@ -0,0 +1,29 @@
+// PR c++/101371
+// { dg-do compile { target c++14 } }
+
+struct A {
+ int i;
+};
+struct B {
+ A a{};
+ constexpr B() : a() {}
+ constexpr B(const B &rhs) : a(rhs.a) {}
+};
+struct C {
+ B arr[1];
+};
+
+struct X {
+ constexpr C fn () const
+ {
+ C c{};
+ return c;
+ }
+};
+
+void
+g ()
+{
+ X x;
+ constexpr auto z = x.fn();
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-03-29 1:44 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-29 1:44 [gcc r11-9710] c++: constexpr array reference and value-initialization [PR101371] Marek Polacek
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).