Bug 34704

Summary: [4.3 Regression] Derived-type initialization ignored unless save attribute is present
Product: gcc Reporter: Paolo Giannozzi <p.giannozzi>
Component: fortranAssignee: Paul Thomas <pault>
Status: RESOLVED FIXED    
Severity: normal CC: gcc-bugs, ismail
Priority: P3 Keywords: wrong-code
Version: 4.3.0   
Target Milestone: 4.3.0   
Host: Target:
Build: Known to work: 4.2.2
Known to fail: 4.3.0 Last reconfirmed: 2008-01-07 16:44:11
Bug Depends on:    
Bug Blocks: 32834    

Description Paolo Giannozzi 2008-01-07 15:19:40 UTC
It looks like a derived-type variable, containing an allocatable array and 
a variable set to an initial value, picks some random number instead of the
initial value, unless it has the "save" attribute. The following piece of
code demonstrates the problem: the only difference between the first and the
second subroutine is the presence of the save attribute to t. No problem if 
"t%a" is a fixed-dimension array. "t%n" should be 1023. 

!
program boh
  !
  call mah0
  call mah1
  !
end program boh
!
subroutine mah0
  !
  type mix_type
     real(8), allocatable :: a(:)
     integer :: n=1023
  end type mix_type
  type(mix_type) :: t
  !
  allocate(t%a(1))
  t%a=3.1415926
  print *, 'n,a=',t%n,t%a 
  deallocate(t%a)
  !
end subroutine mah0
!
subroutine mah1
  !
  type mix_type
     real(8), allocatable :: a(:)
     integer :: n=1023
  end type mix_type
  type(mix_type), save :: t
  !
  allocate(t%a(1))
  t%a=3.1415926
  print *, 'n,a=',t%n,t%a 
  deallocate(t%a)
  !
end subroutine mah1

gfortran -v
Using built-in specs.
Target: i386-apple-darwin8.10.1
Configured with: /tmp/gfortran-20071231/ibin/../gcc/configure --prefix=/usr/local/gfortran --enable-languages=c,fortran --with-gmp=/tmp/gfortran-20071231/gfortran_libs --enable-bootstrap
Thread model: posix
gcc version 4.3.0 20071231 (experimental) [trunk revision 131236] (GCC) 

$ gfortran  boh.f90
$ ./a.out
 n,a=     2123354   3.1415925025939941
 n,a=        1023   3.1415925025939941

$ gfortran -O3  boh.f90
$ ./a.out
 n,a=          -1   3.1415925025939941
 n,a=        1023   3.1415925025939941
Comment 1 Paul Thomas 2008-01-07 16:44:11 UTC
I have a fix for this and PR34681.  I was hoping to submit tonight but it now looks like it will be tomorrow.

Paul
Comment 2 Tobias Burnus 2008-01-08 09:22:23 UTC
Patch by Paul: http://gcc.gnu.org/ml/gcc-patches/2008-01/msg00269.html
Comment 3 Paul Thomas 2008-01-08 15:13:22 UTC
Subject: Bug 34704

Author: pault
Date: Tue Jan  8 15:12:34 2008
New Revision: 131395

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=131395
Log:
2008-01-08  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/34681
	* trans_array.c (gfc_trans_deferred_array): Do not null the
	data pointer on entering scope, nor deallocate it on leaving
	scope, if the symbol has the 'save' attribute.

	PR fortran/34704
	* trans_decl.c (gfc_finish_var_decl): Derived types with
	allocatable components and an initializer must be TREE_STATIC.

2008-01-08  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/34681
	PR fortran/34704
	* gfortran.dg/alloc_comp_default_init_1.f90: New test.



Added:
    trunk/gcc/testsuite/gfortran.dg/alloc_comp_default_init_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-array.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog

Comment 4 Paul Thomas 2008-01-08 15:15:45 UTC
Fixed on trunk

Paul