8sa1-gcc/gcc/testsuite/g++.dg/cpp2a/new-array1.C
Marek Polacek 73a2b8dd17 c++: Implement P1009: Array size deduction in new-expressions.
This patch implements C++20 P1009, allowing code like

  new double[]{1,2,3}; // array bound will be deduced

Since this proposal makes the initialization rules more consistent, it is
applied to all previous versions of C++ (thus, effectively, all the way back
to C++11).

My patch is based on Jason's patch that handled the basic case.  I've
extended it to work with ()-init and also the string literal case.
Further testing revealed that to handle stuff like

  new int[]{t...};

in a template, we have to consider such a NEW_EXPR type-dependent.
Obviously, we first have to expand the pack to be able to deduce the
number of elements in the array.

Curiously, while implementing this proposal, I noticed that we fail
to accept

  new char[4]{"abc"};

so I've assigned 77841 to self.  I think the fix will depend on the
build_new_1 hunk in this patch.

The new tree.c function build_constructor_from_vec helps us morph
a vector into a CONSTRUCTOR more efficiently.

gcc/cp/ChangeLog:

	PR c++/93529
	* call.c (build_new_method_call_1): Use build_constructor_from_vec
	instead of build_tree_list_vec + build_constructor_from_list.
	* init.c (build_new_1): Handle new char[]{"foo"}.  Use
	build_constructor_from_vec instead of build_tree_list_vec +
	build_constructor_from_list.
	(build_new): Deduce the array size in new-expression if not
	present.  Handle ()-init.  Handle initializing an array from
	a string literal.
	* parser.c (cp_parser_new_type_id): Leave [] alone.
	(cp_parser_direct_new_declarator): Allow [].
	* pt.c (type_dependent_expression_p): In a NEW_EXPR, consider
	array types whose dimension has to be deduced type-dependent.

gcc/ChangeLog:

	PR c++/93529
	* tree.c (build_constructor_from_vec): New.
	* tree.h (build_constructor_from_vec): Declare.

gcc/testsuite/ChangeLog:

	PR c++/93529
	* g++.dg/cpp0x/sfinae4.C: Adjust expected result after P1009.
	* g++.dg/cpp2a/new-array1.C: New test.
	* g++.dg/cpp2a/new-array2.C: New test.
	* g++.dg/cpp2a/new-array3.C: New test.
	* g++.dg/cpp2a/new-array4.C: New test.

Co-authored-by: Jason Merrill <jason@redhat.com>
2020-08-31 16:09:10 -04:00

71 lines
2.1 KiB
C

// PR c++/93529
// P1009: Array size deduction in new-expressions
// { dg-do run { target c++11 } }
// When the array bound is deduced to 0, malloc(0) returns
// a non-dereferenceable pointer.
int *p0 = new int[]{};
int *p1 = new int[]{ 1 };
int *p2 = new int[]{ 1, 2, 3 };
char *c1 = new char[]{"foo"};
#if __cpp_aggregate_paren_init
int *q0 = new int[]();
int *q1 = new int[](1);
int *q2 = new int[](1, 2, 3);
char *d1 = new char[]("foo");
char *d2 = new char[4]("foo");
char *d3 = new char[]((("foo")));
#endif
struct Aggr { int a; int b; int c; };
Aggr *a1 = new Aggr[]{};
Aggr *a2 = new Aggr[]{ 1, 2, 3 };
Aggr *a3 = new Aggr[]{ 1, 2, 3, 4 };
Aggr *a4 = new Aggr[]{ { 1, 2, 3 } };
Aggr *a5 = new Aggr[]{ { 1 }, { 6, 7 } };
#if __cpp_designated_initializers
Aggr *a9 = new Aggr[]{ { .a = 1, .b = 2, .c = 3 } };
#endif
#if __cpp_aggregate_paren_init
Aggr *a6 = new Aggr[]();
Aggr *a7 = new Aggr[]({ 1, 2, 3 });
Aggr *a8 = new Aggr[]({ 1 }, { 6, 7 });
#endif
int
main ()
{
if (p1[0] != 1 || p2[0] != 1 || p2[1] != 2 || p2[2] != 3)
__builtin_abort ();
if (__builtin_strcmp (c1, "foo"))
__builtin_abort ();
if (a2->a != 1 || a2->b != 2 || a2->c != 3)
__builtin_abort ();
if (a3[0].a != 1 || a3[0].b != 2 || a3[0].c != 3
|| a3[1].a != 4 || a3[1].b != 0 || a3[1].c != 0)
__builtin_abort ();
if (a4->a != 1 || a4->b != 2 || a4->c != 3)
__builtin_abort ();
if (a5[0].a != 1 || a5[0].b != 0 || a5[0].c != 0
|| a5[1].a != 6 || a5[1].b != 7 || a5[1].c != 0)
__builtin_abort ();
#if __cpp_designated_initializers
if (a9->a != 1 || a9->b != 2 || a9->c != 3)
__builtin_abort ();
#endif
#if __cpp_aggregate_paren_init
if (q1[0] != 1)
__builtin_abort ();
if (q2[0] != 1 || q2[1] != 2 || q2[2] != 3)
__builtin_abort ();
if (__builtin_strcmp (d1, "foo") || __builtin_strcmp (d2, "foo")
|| __builtin_strcmp (d3, "foo"))
__builtin_abort ();
if (a7[0].a != 1 || a7[0].b != 2 || a7[0].c != 3)
__builtin_abort ();
if (a8[0].a != 1 || a8[0].b != 0 || a8[0].c != 0
|| a8[1].a != 6 || a8[1].b != 7 || a8[1].c != 0)
__builtin_abort ();
#endif
}