diff mbox

C++ PATCH for c++/49216 (problems with new T[1]{})

Message ID 4DFFFEB9.6070901@redhat.com
State New
Headers show

Commit Message

Jason Merrill June 21, 2011, 2:15 a.m. UTC
The reported problem was due to creating a CONSTRUCTOR with pointer 
type, expecting it to be an array; now we only build a CONSTRUCTOR for 
array type.  While I was looking at this issue I noticed that the 
user-defined type case also had problems, because it was still using T() 
instead of {} to initialize an aggregate element without an explicit 
initializer.  And then after fixing that I needed to fix substitution 
failure recovery in convert_like_real.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit c4c9a3c2114d65faf23819ab9efdb9fdc4d650f1
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Jun 20 17:14:39 2011 -0400

    	PR c++/49216
    	* init.c (build_vec_init): Don't try to use a CONSTRUCTOR when
    	base is a pointer.
    	* typeck2.c (process_init_constructor_array): Use {} for classes,
    	too.
    	* call.c (convert_like_real): Handle substitution failure.

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index e9d6e7e..caf95b0 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -5621,7 +5621,7 @@  convert_like_real (conversion *convs, tree expr, tree fn, int argnum,
 	    expr = build_cplus_new (totype, expr, complain);
 
 	    /* Remember that this was list-initialization.  */
-	    if (convs->check_narrowing)
+	    if (convs->check_narrowing && expr != error_mark_node)
 	      TARGET_EXPR_LIST_INIT_P (expr) = true;
 	  }
 
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 140e064..3c347a4 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -3082,8 +3082,9 @@  build_vec_init (tree base, tree maxindex, tree init,
       unsigned HOST_WIDE_INT idx;
       tree field, elt;
       /* Should we try to create a constant initializer?  */
-      bool try_const = (literal_type_p (inner_elt_type)
-			|| TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type));
+      bool try_const = (TREE_CODE (atype) == ARRAY_TYPE
+			&& (literal_type_p (inner_elt_type)
+			    || TYPE_HAS_CONSTEXPR_CTOR (inner_elt_type)));
       bool saw_non_const = false;
       bool saw_const = false;
       /* If we're initializing a static array, we want to do static
diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 6d6267e..ff2949c 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -1051,14 +1051,9 @@  process_init_constructor_array (tree type, tree init,
 	if (type_build_ctor_call (TREE_TYPE (type)))
 	  {
 	    /* If this type needs constructors run for default-initialization,
-	      we can't rely on the back end to do it for us, so build up
-	      TARGET_EXPRs.  If the type in question is a class, just build
-	      one up; if it's an array, recurse.  */
-	    if (MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
-              next = build_functional_cast (TREE_TYPE (type), NULL_TREE,
-                                            complain);
-	    else
-	      next = build_constructor (init_list_type_node, NULL);
+	      we can't rely on the back end to do it for us, so make the
+	      initialization explicit by list-initializing from {}.  */
+	    next = build_constructor (init_list_type_node, NULL);
 	    next = digest_init (TREE_TYPE (type), next, complain);
 	  }
 	else if (!zero_init_p (TREE_TYPE (type)))
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist53.C b/gcc/testsuite/g++.dg/cpp0x/initlist53.C
new file mode 100644
index 0000000..750ebba
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist53.C
@@ -0,0 +1,20 @@ 
+// PR c++/49216
+// { dg-options -std=c++0x }
+// { dg-do run }
+
+#include <initializer_list>
+extern "C" void abort();
+
+bool constructed;
+
+struct A
+{
+  A(std::initializer_list<int>) { constructed = true; }
+};
+
+int main() {
+  new A[1]{};
+  int *p = new int[1]{};
+  if (p[0] != 0 || !constructed)
+    abort();
+}