8sa1-gcc/gcc/testsuite/g++.dg/cpp2a/new-array5.C
Marek Polacek 81de459ec7 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.
2020-09-09 17:46:04 -04:00

16 lines
460 B
C

// PR c++/77841
// { dg-do compile { target c++11 } }
auto p1 = new int[][1]();
auto p2 = new int[1][1]();
#if __cpp_aggregate_paren_init
auto p3 = new int[][4]({1, 2}, {3, 4});
auto p4 = new int[2][4]({1, 2}, {3, 4});
auto p5 = new int[2][1]({1, 2}, {3}); // { dg-error "too many initializers" "" { target c++20 } }
#endif
auto b1 = new int[][1]{};
auto b2 = new int[1][1]{};
auto b3 = new int[][4]{{1, 2}, {3, 4}};
auto b4 = new int[2][4]{{1, 2}, {3, 4}};