c++: Further tweaks for new-expression and paren-init [PR77841]
authorMarek Polacek <polacek@redhat.com>
Sun, 6 Sep 2020 00:50:32 +0000 (20:50 -0400)
committerMarek Polacek <polacek@redhat.com>
Wed, 9 Sep 2020 21:46:04 +0000 (17:46 -0400)
commit81de459ec7ccf46c3e5a776b0c13f01e989d9593
tree8ebc8c775d581e334adb900d038370649a4606b7
parentacbe30bbc884899da72df47d023ebde89f8f47f1
c++: Further tweaks for new-expression and paren-init [PR77841]

This patch corrects our handling of array new-expression with ()-init:

  new int[4](1, 2, 3, 4);

should work even with the explicit array bound, and

  new char[3]("so_sad");

should cause an error, but we weren't giving any.

Fixed by handling array new-expressions with ()-init in the same spot
where we deduce the array bound in array new-expression.  I'm now
always passing STRING_CSTs to build_new_1 wrapped in { } which allowed
me to remove the special handling of STRING_CSTs in build_new_1.  And
since the DIRECT_LIST_INIT_P block in build_new_1 calls digest_init, we
report errors about too short arrays. reshape_init now does the {"foo"}
-> "foo" transformation even for CONSTRUCTOR_IS_PAREN_INIT, so no need
to do it in build_new.

I took a stab at cp_complete_array_type's "FIXME: this code is duplicated
from reshape_init", but calling reshape_init there, I ran into issues
with has_designator_problem: when we reshape an already reshaped
CONSTRUCTOR again, d.cur.index has been filled, so we think that we
have a user-provided designator (though there was no designator in the
source code), and report an error.

gcc/cp/ChangeLog:

PR c++/77841
* decl.c (reshape_init): If we're initializing a char array from
a string-literal that is enclosed in braces, unwrap it.
* init.c (build_new_1): Don't handle string-initializers here.
(build_new): Handle new-expression with paren-init when the
array bound is known.  Always pass string constants to build_new_1
enclosed in braces.  Don't handle string-initializers in any
special way.

gcc/testsuite/ChangeLog:

PR c++/77841
* g++.old-deja/g++.ext/arrnew2.C: Expect the error only in C++17
and less.
* g++.old-deja/g++.robertl/eb58.C: Adjust dg-error.
* g++.old-deja/g++.robertl/eb63.C: Expect the error only in C++17
and less.
* g++.dg/cpp2a/new-array5.C: New test.
* g++.dg/cpp2a/paren-init36.C: New test.
* g++.dg/cpp2a/paren-init37.C: New test.
* g++.dg/pr84729.C: Adjust dg-error.
gcc/cp/decl.c
gcc/cp/init.c
gcc/testsuite/g++.dg/cpp2a/new-array5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/paren-init36.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/paren-init37.C [new file with mode: 0644]
gcc/testsuite/g++.dg/pr84729.C
gcc/testsuite/g++.old-deja/g++.ext/arrnew2.C
gcc/testsuite/g++.old-deja/g++.robertl/eb58.C
gcc/testsuite/g++.old-deja/g++.robertl/eb63.C