Skip to content

Commit 6e424fe

Browse files
committed
c++: fix ICE with constexpr ARRAY_REF [PR110382]
This code in cxx_eval_array_reference has been hard to get right. In r12-2304 I added some code; in r13-5693 I removed some of it. Here the problematic line is "S s = arr[0];" which causes a crash on the assert in verify_ctor_sanity: gcc_assert (!ctx->object || !DECL_P (ctx->object) || ctx->global->get_value (ctx->object) == ctx->ctor); ctx->object is the VAR_DECL 's', which is correct here. The second line points to the problem: we replaced ctx->ctor in cxx_eval_array_reference: new_ctx.ctor = build_constructor (elem_type, NULL); // #1 which I think we shouldn't have; the CONSTRUCTOR we created in cxx_eval_constant_expression/DECL_EXPR new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL); had the right type. We still need #1 though. E.g., in constexpr-96241.C, we never set ctx.ctor/object before calling cxx_eval_array_reference, so we have to build a CONSTRUCTOR there. And in constexpr-101371-2.C we have a ctx.ctor, but it has the wrong type, so we need a new one. We can fix the problem by always clearing the object, and, as an optimization, only create/free a new ctor when actually needed. PR c++/110382 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_array_reference): Create a new constructor only when we don't already have a matching one. Clear the object when the type is non-scalar. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/constexpr-110382.C: New test.
1 parent 09dda27 commit 6e424fe

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

gcc/cp/constexpr.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4291,15 +4291,24 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
42914291
else
42924292
val = build_value_init (elem_type, tf_warning_or_error);
42934293

4294-
if (!SCALAR_TYPE_P (elem_type))
4294+
/* Create a new constructor only if we don't already have a suitable one. */
4295+
const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
4296+
&& (!ctx->ctor
4297+
|| !same_type_ignoring_top_level_qualifiers_p
4298+
(elem_type, TREE_TYPE (ctx->ctor))));
4299+
if (new_ctor)
42954300
{
42964301
new_ctx = *ctx;
4302+
/* We clear the object here. We used to replace it with T, but that
4303+
caused problems (101371, 108158); and anyway, T is the initializer,
4304+
not the target object. */
4305+
new_ctx.object = NULL_TREE;
42974306
new_ctx.ctor = build_constructor (elem_type, NULL);
42984307
ctx = &new_ctx;
42994308
}
43004309
t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
43014310
overflow_p);
4302-
if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor)
4311+
if (new_ctor && t != ctx->ctor)
43034312
free_constructor (ctx->ctor);
43044313
return t;
43054314
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// PR c++/110382
2+
// { dg-do compile { target c++14 } }
3+
4+
struct S {
5+
double a = 0;
6+
};
7+
8+
constexpr double
9+
g ()
10+
{
11+
S arr[1];
12+
S s = arr[0];
13+
(void) arr[0];
14+
return s.a;
15+
}
16+
17+
int main() { return g (); }

0 commit comments

Comments
 (0)