2019-02-17  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/71066
	* trans-decl.c (generate_coarray_sym_init):  For an array
	constructor in a DATA statement of a coarray variable, set the
	rank to 1 to avoid confusion later on.  If the constructor
	contains only one value, use that for initiailizig.

2019-02-17  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/71066
	* gfortran.dg/coarray_data_1.f90: New test.



git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@268960 138bc75d-0d04-0410-961f-82ee72b054a4
diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog
index 3a8c00d..ceef926 100644
--- a/gcc/fortran/ChangeLog
+++ b/gcc/fortran/ChangeLog
@@ -1,3 +1,11 @@
+2019-02-17  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+	PR fortran/71066
+	* trans-decl.c (generate_coarray_sym_init):  For an array
+	constructor in a DATA statement of a coarray variable, set the
+	rank to 1 to avoid confusion later on.  If the constructor
+	contains only one value, use that for initiailizig.
+
 2019-02-14  Janne Blomqvist  <jb@gcc.gnu.org>
 
 	PR fortran/81552
diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index b8f50f7..9a8f2d3 100644
--- a/gcc/fortran/trans-decl.c
+++ b/gcc/fortran/trans-decl.c
@@ -5399,6 +5399,33 @@
   /* Handle "static" initializer.  */
   if (sym->value)
     {
+      if (sym->value->expr_type == EXPR_ARRAY)
+	{
+	  gfc_constructor *c, *cnext;
+
+	  /* Test if the array has more than one element.  */
+	  c = gfc_constructor_first (sym->value->value.constructor);
+	  gcc_assert (c);  /* Empty constructor should not happen here.  */
+	  cnext = gfc_constructor_next (c);
+
+	  if (cnext)
+	    {
+	      /* An EXPR_ARRAY with a rank > 1 here has to come from a
+		 DATA statement.  Set its rank here as not to confuse
+		 the following steps.   */
+	      sym->value->rank = 1;
+	    }
+	  else
+	    {
+	      /* There is only a single value in the constructor, use
+		 it directly for the assignment.  */
+	      gfc_expr *new_expr;
+	      new_expr = gfc_copy_expr (c->expr);
+	      gfc_free_expr (sym->value);
+	      sym->value = new_expr;
+	    }
+	}
+
       sym->attr.pointer = 1;
       tmp = gfc_trans_assignment (gfc_lval_expr_from_sym (sym), sym->value,
 				  true, false);
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index d6a90ef..e68f969 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2019-02-17  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+	PR fortran/71066
+	* gfortran.dg/coarray_data_1.f90: New test.
+
 2019-02-16  Eric Botcazou  <ebotcazou@adacore.com>
 
 	* c-c++-common/patchable_function_entry-decl.c: Add -fno-pie on SPARC.
diff --git a/gcc/testsuite/gfortran.dg/coarray_data_1.f90 b/gcc/testsuite/gfortran.dg/coarray_data_1.f90
new file mode 100644
index 0000000..94ab4c2
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray_data_1.f90
@@ -0,0 +1,11 @@
+! { dg-do  run }
+! { dg-options "-fcoarray=lib -lcaf_single " }
+! PR 71066 - this used to ICE
+program p
+   real :: a(2,2)[*]
+   integer :: b(2,2)[*]
+   data a /4*0.0/
+   data b /1234, 2345, 3456, 4567/
+   if (any (a /= 0.0)) stop 1
+   if (any (b /= reshape([1234, 2345, 3456, 4567],[2,2]))) stop 2
+end