On 1/29/21 12:28 PM, Patrick Palka wrote:
In this testcase we're crash during constexpr evaluation of the
ARRAY_REF b[0] as part of folding the lambda's by-copy capture of b
(which is encoded as a VEC_INIT_EXPR<b>).  Since A's default constructor
is not yet defined, b's initializer is not actually constant, but
because A is an empty type, evaluation of the referent b from
cxx_eval_array_ref yields an empty CONSTRUCTOR.  From there we proceed
to {}-initialize the missing array element at index 0.  We crash from
verify_ctor_sanity during evaluation of this initializer because we
updated constexpr_ctx::ctor without updating ::object; the former has
type A[3] and the latter is the target of the TARGET_EXPR for b[0][0]
created from cxx_eval_vec_init_1 (and so has type A).

This patch conservatively fixes this issue by clearing new_ctx.object at
the same time that we set new_ctx.ctor.  Strictly speaking, the object
under construction should perhaps be the ARRAY_REF itself

Yes.

but I haven't
been able to come up with a testcase for which this difference matters.

I suspect that any case where it would matter wouldn't get to this point, because we would have already built up the initializer under digest_init.

But I also think it shouldn't hurt to use 't' as new_ctx.object, so let's do that.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk and the 10 branch?

gcc/cp/ChangeLog:

        PR c++/98295
        * constexpr.c (cxx_eval_array_reference): Clear new_ctx.object
        when setting new_ctx.ctor.

gcc/testsuite/ChangeLog:

        PR c++/98295
        * g++.dg/cpp0x/constexpr-98295.C: New test.
---
  gcc/cp/constexpr.c                           |  1 +
  gcc/testsuite/g++.dg/cpp0x/constexpr-98295.C | 11 +++++++++++
  2 files changed, 12 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-98295.C

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index baa97a0ef17..0606b12e3d6 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -3760,6 +3760,7 @@ 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);
        new_ctx = *ctx;
+      new_ctx.object = NULL_TREE;
        new_ctx.ctor = build_constructor (elem_type, NULL);
        ctx = &new_ctx;
      }
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-98295.C 
b/gcc/testsuite/g++.dg/cpp0x/constexpr-98295.C
new file mode 100644
index 00000000000..935a6cb5314
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-98295.C
@@ -0,0 +1,11 @@
+// PR c++/98295
+// { dg-do compile { target c++11 } }
+
+struct A { constexpr A(); };
+
+void f() {
+  const A b[2][3];
+  [b] { };
+}
+
+constexpr A::A() {}


Reply via email to