This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch, fortran] PR28425 - Derived type initialization via a derived type is broken


:ADDPATCH fortran:

The title a bit inaccurate because derived type initialization via a derived type was never broken but it was just never done in the first place!

The problem is that derived type constructors of derived types that have derived type components will only accept missing or constructor arguments. This patch extends this to include variables, parameters and functions by adding a few, straightforward, absolutely standard lines of code to trans-expr.c (gfc_trans_subcomponent_assign). The testcase is an expansion of the example submitted to Bugzilla.

Regtested on FC5/Athlon. OK for trunk and 4.1?

Paul

2006-08-06 Paul Thomas <pault@gcc.gnu.org>

   PR fortran/28425
   * trans-expr.c (gfc_trans_subcomponent_assign): Translate
   derived type component expressions other than another derived
   type constructor.

2006-08-06 Paul Thomas <pault@gcc.gnu.org>

   PR fortran/28425
   * gfortran.dg/derived_constructor_comps_1.f90: New test.

Index: gcc/fortran/trans-expr.c
===================================================================
*** gcc/fortran/trans-expr.c	(revision 115965)
--- gcc/fortran/trans-expr.c	(working copy)
*************** gfc_trans_subcomponent_assign (tree dest
*** 2659,2667 ****
      }
    else if (expr->ts.type == BT_DERIVED)
      {
!       /* Nested derived type.  */
!       tmp = gfc_trans_structure_assign (dest, expr);
!       gfc_add_expr_to_block (&block, tmp);
      }
    else
      {
--- 2659,2677 ----
      }
    else if (expr->ts.type == BT_DERIVED)
      {
!       if (expr->expr_type != EXPR_STRUCTURE)
! 	{
! 	  gfc_init_se (&se, NULL);
! 	  gfc_conv_expr (&se, expr);
! 	  gfc_add_modify_expr (&block, dest,
! 			       fold_convert (TREE_TYPE (dest), se.expr));
! 	}
!       else
! 	{
! 	  /* Nested constructors.  */
! 	  tmp = gfc_trans_structure_assign (dest, expr);
! 	  gfc_add_expr_to_block (&block, tmp);
! 	}
      }
    else
      {
Index: gcc/testsuite/gfortran.dg/derived_constructor_comps_1.f90
===================================================================
*** gcc/testsuite/gfortran.dg/derived_constructor_comps_1.f90	(revision 0)
--- gcc/testsuite/gfortran.dg/derived_constructor_comps_1.f90	(revision 0)
***************
*** 0 ****
--- 1,50 ----
+ ! { dg-do run }
+ !
+ ! Tests fix for PR28425 in which anything other than a constructor would
+ ! not work for derived type components in a structure constructor.
+ !
+ ! Original version sent by Vivek Rao on 18 Jan 06
+ ! Modified by Steve Kargl to remove IO
+ !
+ module foo_mod
+ 
+   implicit none
+ 
+   type :: date_m
+      integer :: month
+   end type date_m
+ 
+   type :: file_info
+      type(date_m) :: date
+   end type file_info
+ 
+ end module foo_mod
+ 
+ program prog
+ 
+   use foo_mod
+ 
+   implicit none
+   type(date_m)  :: dat
+   type(file_info) :: xx
+ 
+   dat = date_m(1)
+ 
+   xx = file_info(date_m(-1))  ! This always worked - a constructor
+   if (xx%date%month /= -1) call abort
+ 
+   xx = file_info(dat)         ! This was the original PR - a variable
+   if (xx%date%month /= 1) call abort
+ 
+   xx = file_info(foo(2))      ! ...functions were also broken
+   if (xx%date%month /= 2) call abort
+ 
+ contains
+ 
+   function foo (i) result (ans)
+      integer :: i
+      type(date_m) :: ans
+      ans = date_m(2)
+   end function foo
+ 
+ end program prog
2006-08-06  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/28425
	* trans-expr.c (gfc_trans_subcomponent_assign): Translate
	derived type component expressions other than another derived
	type constructor.

2006-08-06  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/28425
	* gfortran.dg/derived_constructor_comps_1.f90: New test.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]