c++: Handle RANGE_EXPR index in init_subob_ctx [PR97328]

In the testcase below, we're ICEing during constexpr evaluation of the
CONSTRUCTOR {.data={{}, [1 ... 7]={}}} of type 'vector'.  The interesting
thing about this CONSTRUCTOR is that it has a RANGE_EXPR index for an
element initializer which doesn't satisfy reduced_constant_expression_p
(because the field 't' is uninitialized).

This is a problem because init_subob_ctx currently punts on setting up a
sub-aggregate initialization context when given a RANGE_EXPR index, so
we later trip over the asserts in verify_ctor_sanity when recursing into
cxx_eval_bare_aggregate on this element initializer.

Fix this by making init_subob_ctx set up an appropriate initialization
context when supplied a RANGE_EXPR index.

gcc/cp/ChangeLog:

	PR c++/97328
	* constexpr.c (init_subob_ctx): Don't punt on RANGE_EXPR
	indexes, instead build a sub-aggregate initialization context
	with no subobject.

gcc/testsuite/ChangeLog:

	PR c++/97328
	* g++.dg/cpp2a/constexpr-init19.C: New test.
	* g++.dg/cpp2a/constexpr-init20.C: New test.
This commit is contained in:
Patrick Palka 2020-10-22 07:40:40 -04:00
parent 46fdced6a9
commit 3d423c6f6a
3 changed files with 39 additions and 2 deletions

View File

@ -3954,7 +3954,8 @@ init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
new_ctx = *ctx;
if (index && TREE_CODE (index) != INTEGER_CST
&& TREE_CODE (index) != FIELD_DECL)
&& TREE_CODE (index) != FIELD_DECL
&& TREE_CODE (index) != RANGE_EXPR)
/* This won't have an element in the new CONSTRUCTOR. */
return;
@ -3967,7 +3968,13 @@ init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
update object to refer to the subobject and ctor to refer to
the (newly created) sub-initializer. */
if (ctx->object)
new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
{
if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
/* There's no well-defined subobject for this index. */
new_ctx.object = NULL_TREE;
else
new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
}
tree elt = build_constructor (type, NULL);
CONSTRUCTOR_NO_CLEARING (elt) = true;
new_ctx.ctor = elt;

View File

@ -0,0 +1,15 @@
// PR c++/97328
// { dg-do compile { target c++20 } }
struct vector {
struct storage {
int t;
constexpr storage() {}
} data[8];
};
constexpr auto foo() {
vector i;
return i;
}
auto val = foo();

View File

@ -0,0 +1,15 @@
// PR c++/97328
// { dg-do compile { target c++20 } }
struct vector {
union storage {
int t;
constexpr storage() {}
} data[8];
};
constexpr auto foo() {
vector i;
return i;
}
auto val = foo();