re PR fortran/89174 (Allocation segfault with CLASS(*) MOLD)
authorThomas Koenig <tkoenig@gcc.gnu.org>
Sun, 24 Feb 2019 22:49:47 +0000 (22:49 +0000)
committerThomas Koenig <tkoenig@gcc.gnu.org>
Sun, 24 Feb 2019 22:49:47 +0000 (22:49 +0000)
2019-02-24  Thomas Koenig  <tkoenig@gcc.gnu.org>

PR fortran/89174
* trans-expr.c (gfc_find_and_cut_at_last_class_ref): Add is_mold
to garguments. If we are dealing with a MOLD, call
gfc_expr_to_initialize().
* trans-stmt.c (gfc_trans_allocate): For MOLD, pass is_mold=true
to gfc_find_and_cut_at_last_class_ref.
* trans.h (gfc_find_and_cut_at_last_class_ref): Add optional
argument is_mold with default false.

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

PR fortran/89174
* gfortran.dg/allocate_with_mold_3.f90: New test.

From-SVN: r269179

gcc/fortran/ChangeLog
gcc/fortran/trans-expr.c
gcc/fortran/trans-stmt.c
gcc/fortran/trans.h
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/allocate_with_mold_3.f90 [new file with mode: 0644]

index db151a8..16b8eb0 100644 (file)
@@ -1,3 +1,14 @@
+2019-02-24  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       PR fortran/89174
+       * trans-expr.c (gfc_find_and_cut_at_last_class_ref): Add is_mold
+       to garguments. If we are dealing with a MOLD, call
+       gfc_expr_to_initialize().
+       * trans-stmt.c (gfc_trans_allocate): For MOLD, pass is_mold=true
+       to gfc_find_and_cut_at_last_class_ref.
+       * trans.h (gfc_find_and_cut_at_last_class_ref): Add optional
+       argument is_mold with default false.
+
 2019-02-24  Harald Anlauf  <anlauf@gmx.de>
 
        PR fortran/89266
index a865cd6..0702713 100644 (file)
@@ -352,7 +352,7 @@ gfc_vptr_size_get (tree vptr)
    of refs following.  */
 
 gfc_expr *
-gfc_find_and_cut_at_last_class_ref (gfc_expr *e)
+gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold)
 {
   gfc_expr *base_expr;
   gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
@@ -394,7 +394,10 @@ gfc_find_and_cut_at_last_class_ref (gfc_expr *e)
       e->ref = NULL;
     }
 
-  base_expr = gfc_copy_expr (e);
+  if (is_mold)
+    base_expr = gfc_expr_to_initialize (e);
+  else
+    base_expr = gfc_copy_expr (e);
 
   /* Restore the original tail expression.  */
   if (class_ref)
index 6b785a6..5b6625f 100644 (file)
@@ -6641,7 +6641,7 @@ gfc_trans_allocate (gfc_code * code)
          /* Use class_init_assign to initialize expr.  */
          gfc_code *ini;
          ini = gfc_get_code (EXEC_INIT_ASSIGN);
-         ini->expr1 = gfc_find_and_cut_at_last_class_ref (expr);
+         ini->expr1 = gfc_find_and_cut_at_last_class_ref (expr, true);
          tmp = gfc_trans_class_init_assign (ini);
          gfc_free_statements (ini);
          gfc_add_expr_to_block (&block, tmp);
index 7d46684..9d9ac22 100644 (file)
@@ -412,7 +412,7 @@ tree gfc_class_data_get (tree);
 tree gfc_class_vptr_get (tree);
 tree gfc_class_len_get (tree);
 tree gfc_class_len_or_zero_get (tree);
-gfc_expr * gfc_find_and_cut_at_last_class_ref (gfc_expr *);
+gfc_expr * gfc_find_and_cut_at_last_class_ref (gfc_expr *, bool is_mold = false);
 /* Get an accessor to the class' vtab's * field, when a class handle is
    available.  */
 tree gfc_class_vtab_hash_get (tree);
index 39d7ab5..624382c 100644 (file)
@@ -1,3 +1,8 @@
+2019-02-24  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       PR fortran/89174
+       * gfortran.dg/allocate_with_mold_3.f90: New test.
+
 2019-02-24  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR target/87007
diff --git a/gcc/testsuite/gfortran.dg/allocate_with_mold_3.f90 b/gcc/testsuite/gfortran.dg/allocate_with_mold_3.f90
new file mode 100644 (file)
index 0000000..797edbe
--- /dev/null
@@ -0,0 +1,21 @@
+! { dg-do  run }
+! PR fortran/89174 - this used to segfault on execution.
+! Test case by Neil Carlson.
+module mod
+  type :: array_data
+    class(*), allocatable :: mold
+  contains
+    procedure :: push
+  end type
+contains
+  subroutine push(this, value)
+    class(array_data), intent(inout) :: this
+    class(*), intent(in) :: value
+    allocate(this%mold, mold=value) ! <== SEGFAULTS HERE
+  end subroutine
+end module
+
+use mod
+type(array_data) :: foo
+call foo%push(42)
+end