]> gcc.gnu.org Git - gcc.git/commitdiff
re PR fortran/34402 (Diagnose illegal initialization of derived type containing alloc...
authorDaniel Franke <franke.daniel@gmail.com>
Thu, 10 Dec 2009 19:57:16 +0000 (14:57 -0500)
committerDaniel Franke <dfranke@gcc.gnu.org>
Thu, 10 Dec 2009 19:57:16 +0000 (14:57 -0500)
gcc/fortran/:
2009-12-10  Daniel Franke  <franke.daniel@gmail.com>

        PR fortran/34402
        * expr.c (check_alloc_comp_init): New.
        (check_init_expr): Verify that allocatable components
        are not data-initalized.

gcc/testsuite/:
2009-12-10  Daniel Franke  <franke.daniel@gmail.com>

        PR fortran/34402
        * gfortran.dg/alloc_comp_init_expr.f03: New.

From-SVN: r155138

gcc/fortran/ChangeLog
gcc/fortran/expr.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/alloc_comp_init_expr.f03 [new file with mode: 0644]

index 2eaf6d00f54f2dc1a459c9430cd4ae2bcfc9417a..7280dd70e37b7a129fca14fd30844aa97e8fe299 100644 (file)
@@ -1,3 +1,10 @@
+2009-12-10  Daniel Franke  <franke.daniel@gmail.com>
+
+       PR fortran/34402
+       * expr.c (check_alloc_comp_init): New.
+       (check_init_expr): Verify that allocatable components
+       are not data-initalized.
+
 2008-12-08  Daniel Kraft  <d@domob.eu>
 
        PR fortran/41177
index c693773ebf20a97d9d84d8eecaac1a7eca7cee0f..f0cfd1896288d595629c48e3dcb79186e494d327 100644 (file)
@@ -2034,6 +2034,32 @@ not_numeric:
   return FAILURE;
 }
 
+/* F2003, 7.1.7 (3): In init expression, allocatable components
+   must not be data-initialized.  */
+static gfc_try
+check_alloc_comp_init (gfc_expr *e)
+{
+  gfc_component *c;
+  gfc_constructor *ctor;
+
+  gcc_assert (e->expr_type == EXPR_STRUCTURE);
+  gcc_assert (e->ts.type == BT_DERIVED);
+
+  for (c = e->ts.u.derived->components, ctor = e->value.constructor;
+       c; c = c->next, ctor = ctor->next)
+    {
+      if (c->attr.allocatable
+          && ctor->expr->expr_type != EXPR_NULL)
+        {
+         gfc_error("Invalid initialization expression for ALLOCATABLE "
+                   "component '%s' in structure constructor at %L",
+                   c->name, &ctor->expr->where);
+         return FAILURE;
+       }
+    }
+
+  return SUCCESS;
+}
 
 static match
 check_init_expr_arguments (gfc_expr *e)
@@ -2383,10 +2409,18 @@ check_init_expr (gfc_expr *e)
       break;
 
     case EXPR_STRUCTURE:
-      if (e->ts.is_iso_c)
-       t = SUCCESS;
-      else
-       t = gfc_check_constructor (e, check_init_expr);
+      t = e->ts.is_iso_c ? SUCCESS : FAILURE;
+      if (t == SUCCESS)
+       break;
+
+      t = check_alloc_comp_init (e);
+      if (t == FAILURE)
+       break;
+
+      t = gfc_check_constructor (e, check_init_expr);
+      if (t == FAILURE)
+       break;
+
       break;
 
     case EXPR_ARRAY:
index c7ea91a34467fe0dd87df3579abb01525e43b1bb..b28f1da27679604d9670c597ad824fab01e78f74 100644 (file)
@@ -1,3 +1,8 @@
+2009-12-10  Daniel Franke  <franke.daniel@gmail.com>
+
+        PR fortran/34402
+       * gfortran.dg/alloc_comp_init_expr.f03: New.
+
 2009-12-09  David Edelsohn  <edelsohn@gnu.org>
 
        * gcc.target/powerpc/bswap64-4.c: Disable on AIX.
diff --git a/gcc/testsuite/gfortran.dg/alloc_comp_init_expr.f03 b/gcc/testsuite/gfortran.dg/alloc_comp_init_expr.f03
new file mode 100644 (file)
index 0000000..5e399ac
--- /dev/null
@@ -0,0 +1,14 @@
+! { dg-do "compile" }
+! PR fortran/34402 - allocatable components shall not be
+! data-initialized in init expr
+
+  type t
+    real, allocatable :: x(:)
+  end type
+
+  ! The following is illegal!
+  type (t) :: bad = t ( (/ 1., 3., 5., 7., 9. /) )     ! { dg-error "Invalid initialization expression" }
+
+  ! This is ok
+  type (t) :: ok = t ( NULL() )
+end
This page took 0.087309 seconds and 5 git commands to generate.