diff mbox

[Fortran,66035,v2,5/6,Regression] gfortran ICE segfault

Message ID 20150706135457.429f0e35@vepi2
State New
Headers show

Commit Message

Andre Vehreschild July 6, 2015, 11:54 a.m. UTC
Hi all,

please find attached the next version of the patch for pr66035 fixing an ICE.
Scope (copied from first submit):

An ICE occurred when in a structure constructor an allocatable component of
type class was initialized with an existing class object. This was caused by 

- the size of the memory to allocate for the component was miscalculated,
- the vptr was not set correctly, and
- when the class object to be used for init was allocatable already, it was
  copied wasting some memory instead of a view_convert inserted.

Bootstraps and regtests fine on x86_64-linux-gnu/f21.

Ok for trunk?

Regards,
	Andre

Comments

Mikael Morin July 10, 2015, 4:47 p.m. UTC | #1
hello Andre.

Le 06/07/2015 13:54, Andre Vehreschild a écrit :
> Hi all,
> 
> please find attached the next version of the patch for pr66035 fixing an ICE.
> Scope (copied from first submit):
> 
> An ICE occurred when in a structure constructor an allocatable component of
> type class was initialized with an existing class object. This was caused by 
> 
> - the size of the memory to allocate for the component was miscalculated,
> - the vptr was not set correctly, and
> - when the class object to be used for init was allocatable already, it was
>   copied wasting some memory instead of a view_convert inserted.
> 
> Bootstraps and regtests fine on x86_64-linux-gnu/f21.
> 
> Ok for trunk?
> 
> Regards,
> 	Andre
> 
> 
> pr66035_2.patch
> 
> diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
> index 195f7a4..74af725 100644
> --- a/gcc/fortran/trans-expr.c
> +++ b/gcc/fortran/trans-expr.c
> @@ -6903,6 +6903,29 @@ alloc_scalar_allocatable_for_subcomponent_assignment (stmtblock_t *block,
>  				       TREE_TYPE (tmp), tmp,
>  				       fold_convert (TREE_TYPE (tmp), size));
>      }
> +  else if (cm->ts.type == BT_CLASS)
> +    {
> +      gcc_assert (expr2->ts.type == BT_CLASS || expr2->ts.type == BT_DERIVED);
> +      if (expr2->ts.type == BT_DERIVED)
> +	{
> +	  tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
> +	  size = TYPE_SIZE_UNIT (tmp);
> +	}
> +      else
> +	{
> +	  gfc_expr *e2vtab;
> +	  gfc_se se;
> +	  e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
> +	  gfc_add_vptr_component (e2vtab);
> +	  gfc_add_size_component (e2vtab);
> +	  gfc_init_se (&se, NULL);
> +	  gfc_conv_expr (&se, e2vtab);
> +	  gfc_add_block_to_block (block, &se.pre);
> +	  size = fold_convert (size_type_node, se.expr);
> +	  gfc_free_expr (e2vtab);
> +	}
> +      size_in_bytes = size;
> +    }
>    else
>      {
>        /* Otherwise use the length in bytes of the rhs.  */
That part is OK.

> @@ -7030,7 +7053,8 @@ gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
>        gfc_add_expr_to_block (&block, tmp);
>      }
>    else if (init && (cm->attr.allocatable
> -	   || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable)))
> +	   || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
> +	       && expr->ts.type != BT_CLASS)))
>      {
>        /* Take care about non-array allocatable components here.  The alloc_*
>  	 routine below is motivated by the alloc_scalar_allocatable_for_
> @@ -7074,6 +7098,14 @@ gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
>  	  tmp = gfc_build_memcpy_call (tmp, se.expr, size);
>  	  gfc_add_expr_to_block (&block, tmp);
>  	}
> +      else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_CLASS)
> +	{
> +	  tmp = gfc_copy_class_to_class (se.expr, dest, integer_one_node,
> +				   CLASS_DATA (cm)->attr.unlimited_polymorphic);
> +	  gfc_add_expr_to_block (&block, tmp);
> +	  gfc_add_modify (&block, gfc_class_vptr_get (dest),
> +			  gfc_class_vptr_get (se.expr));
> +	}
>        else
>  	gfc_add_modify (&block, tmp,
>  			fold_convert (TREE_TYPE (tmp), se.expr));
But this hunk is canceled by the one before, isn't it?
I mean, If the condition here is true, the condition before was false?

Mikael
diff mbox

Patch

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 195f7a4..74af725 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -6903,6 +6903,29 @@  alloc_scalar_allocatable_for_subcomponent_assignment (stmtblock_t *block,
 				       TREE_TYPE (tmp), tmp,
 				       fold_convert (TREE_TYPE (tmp), size));
     }
+  else if (cm->ts.type == BT_CLASS)
+    {
+      gcc_assert (expr2->ts.type == BT_CLASS || expr2->ts.type == BT_DERIVED);
+      if (expr2->ts.type == BT_DERIVED)
+	{
+	  tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
+	  size = TYPE_SIZE_UNIT (tmp);
+	}
+      else
+	{
+	  gfc_expr *e2vtab;
+	  gfc_se se;
+	  e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
+	  gfc_add_vptr_component (e2vtab);
+	  gfc_add_size_component (e2vtab);
+	  gfc_init_se (&se, NULL);
+	  gfc_conv_expr (&se, e2vtab);
+	  gfc_add_block_to_block (block, &se.pre);
+	  size = fold_convert (size_type_node, se.expr);
+	  gfc_free_expr (e2vtab);
+	}
+      size_in_bytes = size;
+    }
   else
     {
       /* Otherwise use the length in bytes of the rhs.  */
@@ -7030,7 +7053,8 @@  gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
       gfc_add_expr_to_block (&block, tmp);
     }
   else if (init && (cm->attr.allocatable
-	   || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable)))
+	   || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
+	       && expr->ts.type != BT_CLASS)))
     {
       /* Take care about non-array allocatable components here.  The alloc_*
 	 routine below is motivated by the alloc_scalar_allocatable_for_
@@ -7074,6 +7098,14 @@  gfc_trans_subcomponent_assign (tree dest, gfc_component * cm, gfc_expr * expr,
 	  tmp = gfc_build_memcpy_call (tmp, se.expr, size);
 	  gfc_add_expr_to_block (&block, tmp);
 	}
+      else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_CLASS)
+	{
+	  tmp = gfc_copy_class_to_class (se.expr, dest, integer_one_node,
+				   CLASS_DATA (cm)->attr.unlimited_polymorphic);
+	  gfc_add_expr_to_block (&block, tmp);
+	  gfc_add_modify (&block, gfc_class_vptr_get (dest),
+			  gfc_class_vptr_get (se.expr));
+	}
       else
 	gfc_add_modify (&block, tmp,
 			fold_convert (TREE_TYPE (tmp), se.expr));
diff --git a/gcc/testsuite/gfortran.dg/structure_constructor_13.f03 b/gcc/testsuite/gfortran.dg/structure_constructor_13.f03
new file mode 100644
index 0000000..c74e325
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/structure_constructor_13.f03
@@ -0,0 +1,28 @@ 
+! { dg-do run }
+!
+! Contributed by Melven Roehrig-Zoellner  <Melven.Roehrig-Zoellner@DLR.de>
+! PR fortran/66035
+
+program test_pr66035
+  type t
+  end type t
+  type w
+    class(t), allocatable :: c
+  end type w
+
+  type(t) :: o
+
+  call test(o)
+contains
+  subroutine test(o)
+    class(t), intent(inout) :: o
+    type(w), dimension(:), allocatable :: list
+
+    select type (o)
+      class is (t)
+        list = [w(o)] ! This caused an ICE
+      class default
+        call abort()
+    end select
+  end subroutine
+end program