c++: More self-modifying constexpr init [PR94470]
authorPatrick Palka <ppalka@redhat.com>
Mon, 13 Apr 2020 20:53:02 +0000 (16:53 -0400)
committerPatrick Palka <ppalka@redhat.com>
Mon, 13 Apr 2020 20:53:02 +0000 (16:53 -0400)
commit077dd9b3f17710da8af6adce816754ddceb57b5a
treeb0d7554dab038a86c0cb058116998372543639da
parentee26baf4a8162a309fbcba591377b9e0c362630d
c++: More self-modifying constexpr init [PR94470]

In this PR we're incorrectly rejecting a self-modifying constexpr initializer as
a consequence of the fix for PR78572.

It looks like however that the fix for PR78572 is obsoleted by the fix for
PR89336: the testcase from the former PR successfully compiles even with its fix
reverted.

But then further testing showed that the analogous testcase of PR78572 where the
array has an aggregate element type is still problematic (i.e. we ICE) even with
the fix for PR78572 applied.  The reason is that in cxx_eval_bare_aggregate we
attach a constructor_elt of aggregate type always to the end of the new
CONSTRUCTOR, but that's not necessarily correct if the CONSTRUCTOR is
self-modifying.  We should instead be using get_or_insert_ctor_field to insert
the constructor_elt in the right place.

So this patch reverts the PR78572 fix and makes the appropriate changes to
cxx_eval_bare_aggregate.  This fixes PR94470, and we now are also able to fully
reduce the initializers of 'arr' and 'arr2' in the new test array57.C to
constant initializers.

gcc/cp/ChangeLog:

PR c++/94470
* constexpr.c (get_or_insert_ctor_field): Set default value of parameter
'pos_hint' to -1.
(cxx_eval_bare_aggregate): Use get_or_insert_ctor_field instead of
assuming the the next index belongs at the end of the new CONSTRUCTOR.
(cxx_eval_store_expression): Revert PR c++/78572 fix.

gcc/testsuite/ChangeLog:

PR c++/94470
* g++.dg/cpp1y/constexpr-nsdmi8.C: New test.
* g++.dg/cpp1y/constexpr-nsdmi9.C: New test.
* g++.dg/init/array57.C: New test.
gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1y/constexpr-nsdmi8.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-nsdmi9.C [new file with mode: 0644]
gcc/testsuite/g++.dg/init/array57.C [new file with mode: 0644]