This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ PATCH for c++/50261 (aggregate mem-initializers)


When we have a braced-init-list as an initializer for an aggregate type, we need to call reshape_init on it to put it into the appropriate form; we were forgetting this in the case of mem-initializers.

Tested x86_64-pc-linux-gnu, applying to trunk and 4.8.
commit de8c0876aa988ca01a3ec256eccd0a90ebb8d73d
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Apr 24 13:49:50 2013 -0400

    	PR c++/50261
    	* init.c (perform_member_init): Call reshape_init.

diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index 28e2555..3587b08 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -634,7 +634,12 @@ perform_member_init (tree member, tree init)
 	init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
 						tf_warning_or_error);
       if (TREE_TYPE (init) != type)
-	init = digest_init (type, init, tf_warning_or_error);
+	{
+	  if (BRACE_ENCLOSED_INITIALIZER_P (init)
+	      && CP_AGGREGATE_TYPE_P (type))
+	    init = reshape_init (type, init, tf_warning_or_error);
+	  init = digest_init (type, init, tf_warning_or_error);
+	}
       if (init == error_mark_node)
 	return;
       /* A FIELD_DECL doesn't really have a suitable lifetime, but
diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist69.C b/gcc/testsuite/g++.dg/cpp0x/initlist69.C
new file mode 100644
index 0000000..5d59dfe
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/initlist69.C
@@ -0,0 +1,29 @@
+// PR c++/50261
+// { dg-require-effective-target c++11 }
+
+template <typename T>
+struct ca {
+  T elem[1];
+
+  ca(const T (&s)[1]): elem{{s}} { }	   // { dg-error "braces" }
+  ca(const T (&s)[1],int): elem({{s}}) { } // { dg-error "paren|invalid" }
+  ca(const T (&s)[1],char): elem(s) { }	   // { dg-error "array" }
+  ca(const T (&s)[1],double): elem{s} { }  // { dg-error "invalid" }
+
+  ca(const T &v): elem{{v}} { }	      // { dg-error "braces" }
+  ca(const T &v,int): elem{{{v}}} { } // { dg-error "braces" }
+  ca(const T &v,char): elem{v} { }    // OK
+  ca(const T &v,double): elem({v}) { } // { dg-error "paren" }
+};
+
+int main() {
+  int a[1] = {0};
+  ca<int> d(a);
+  ca<int> d1(a,1);
+  ca<int> d2(a,'2');
+  ca<int> d3(a,3.0);
+  ca<int> e(a[0]);
+  ca<int> e1(a[0],1);
+  ca<int> e2(a[0],'2');
+  ca<int> e3(a[0],3.0);
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]