Patchwork C++ PATCH for c++/49924 (constexpr rejects-valid with array member)

login
register
mail settings
Submitter Jason Merrill
Date Aug. 1, 2011, 9 p.m.
Message ID <4E37140B.2080404@redhat.com>
Download mbox | patch
Permalink /patch/107821/
State New
Headers show

Comments

Jason Merrill - Aug. 1, 2011, 9 p.m.
cxx_eval_vec_init_1 was assuming that any CONSTRUCTOR would be for the 
element type, which is silly in this context because an array 
initializer is also a CONSTRUCTOR.  Oops.  While I was fixing this I 
also changed value-initialization to be pre-evaluated like 
default-initialization.

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

Patch

commit e421970c44311cf24389f8281f0e635523f9eea3
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Aug 1 14:44:01 2011 -0400

    	PR c++/49924
    	* semantics.c (cxx_eval_vec_init_1): Fix logic.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 47b714f..0b2a96f 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -6651,6 +6651,7 @@  cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
   tree elttype = TREE_TYPE (atype);
   int max = tree_low_cst (array_type_nelts (atype), 0);
   VEC(constructor_elt,gc) *n = VEC_alloc (constructor_elt, gc, max + 1);
+  bool pre_init = false;
   int i;
 
   /* For the default constructor, build up a call to the default
@@ -6658,8 +6659,15 @@  cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
      here, as for a constructor to be constexpr, all members must be
      initialized, which for a defaulted default constructor means they must
      be of a class type with a constexpr default constructor.  */
-  if (value_init)
-    gcc_assert (!init);
+  if (TREE_CODE (elttype) == ARRAY_TYPE)
+    /* We only do this at the lowest level.  */;
+  else if (value_init)
+    {
+      init = build_value_init (elttype, tf_warning_or_error);
+      init = cxx_eval_constant_expression
+	    (call, init, allow_non_constant, addr, non_constant_p);
+      pre_init = true;
+    }
   else if (!init)
     {
       VEC(tree,gc) *argvec = make_tree_vector ();
@@ -6669,6 +6677,7 @@  cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
       release_tree_vector (argvec);
       init = cxx_eval_constant_expression (call, init, allow_non_constant,
 					   addr, non_constant_p);
+      pre_init = true;
     }
 
   if (*non_constant_p && !allow_non_constant)
@@ -6690,17 +6699,14 @@  cxx_eval_vec_init_1 (const constexpr_call *call, tree atype, tree init,
 					 allow_non_constant, addr,
 					 non_constant_p);
 	}
-      else if (value_init)
+      else if (pre_init)
 	{
-	  eltinit = build_value_init (elttype, tf_warning_or_error);
-	  eltinit = cxx_eval_constant_expression
-	    (call, eltinit, allow_non_constant, addr, non_constant_p);
-	}
-      else if (TREE_CODE (init) == CONSTRUCTOR)
-	{
-	  /* Initializing an element using the call to the default
-	     constructor we just built above.  */
-	  eltinit = unshare_expr (init);
+	  /* Initializing an element using value or default initialization
+	     we just pre-built above.  */
+	  if (i == 0)
+	    eltinit = init;
+	  else
+	    eltinit = unshare_expr (init);
 	}
       else
 	{
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-array4.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-array4.C
new file mode 100644
index 0000000..9aeb75d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-array4.C
@@ -0,0 +1,14 @@ 
+// PR c++/49924
+// { dg-options -std=c++0x }
+
+struct A { constexpr A() { } };
+
+struct B {
+  A array[1]; //non-static member array of a literal type w constexpr ctor
+  constexpr B() : array{} { } // here is the problem
+};
+
+int main()
+{
+  constexpr B b{};  // won't compile
+}