diff mbox

[C++] PR 43453

Message ID 53834B3C.5060706@oracle.com
State New
Headers show

Commit Message

Paolo Carlini May 26, 2014, 2:10 p.m. UTC
Hi,

according to the analysis, we should not reject these initializations. 
Thus I added some code following closely 8.5/17. I also enlarged the 
testcase a bit to make sure that, for example, we still reject too long 
initializer-strings (a preliminary draft didn't call digest_init)

Tested x86_64-linux.

///////////////////
/cp
2014-05-26  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/43453
	* typeck.c (cp_build_modify_expr): Handle array of characters
	initialized by a string literal.
	* typeck2.c (store_init_value): Likewise.

/testsuite
2014-05-26  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/43453
	* g++.dg/init/pr43453.C: New.
diff mbox

Patch

Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 210928)
+++ cp/typeck.c	(working copy)
@@ -7502,6 +7502,18 @@  cp_build_modify_expr (tree lhs, enum tree_code mod
 	    return error_mark_node;
 	}
 
+      /* C++11 8.5/17: "If the destination type is an array of characters,
+	 an array of char16_t, an array of char32_t, or an array of wchar_t,
+	 and the initializer is a string literal...".  */
+      else if (TREE_CODE (newrhs) == STRING_CST
+	       && char_type_p (TREE_TYPE (lhstype))
+	       && modifycode == INIT_EXPR)
+	{
+	  newrhs = digest_init (lhstype, newrhs, complain);
+	  if (newrhs == error_mark_node)
+	    return error_mark_node;
+	}
+
       else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
 				     TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
 	{
Index: cp/typeck2.c
===================================================================
--- cp/typeck2.c	(revision 210928)
+++ cp/typeck2.c	(working copy)
@@ -786,7 +786,12 @@  store_init_value (tree decl, tree init, vec<tree,
       gcc_assert (TREE_CODE (decl) != RESULT_DECL);
 
       if (TREE_CODE (init) == TREE_LIST
-	       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
+	  && TREE_CODE (type) == ARRAY_TYPE
+	  /* C++11 8.5/17: "If the destination type is an array of characters,
+	     an array of char16_t, an array of char32_t, or an array of wchar_t,
+	     and the initializer is a string literal...".  */
+	  && !(char_type_p (TREE_TYPE (type))
+	       && TREE_CODE (TREE_VALUE (init)) == STRING_CST))
 	{
 	  error ("cannot initialize arrays using this syntax");
 	  return NULL_TREE;
Index: testsuite/g++.dg/init/pr43453.C
===================================================================
--- testsuite/g++.dg/init/pr43453.C	(revision 0)
+++ testsuite/g++.dg/init/pr43453.C	(working copy)
@@ -0,0 +1,22 @@ 
+// PR c++/43453
+
+struct A {
+  char x[4]; 
+  A() : x("bug") { };
+};
+
+char x [4] ("bug");
+
+struct B {
+  char y[4]; 
+  B() : y("bu") { };
+};
+
+char y [4] ("bu");
+
+struct C {
+  char z[4]; 
+  C() : z("bugs") { };  // { dg-error "too long" }
+};
+
+char z [4] ("bugs");    // { dg-error "too long" }