[Bug fortran/35959] New: Recursive function with allocatable array
Problem with gfortran while developing a recursive function which allocates an array stored in a derived-type. This is the sample test : module testmod #define _DYNAMIC_TYPE allocatable !#define _DYNAMIC_TYPE pointer type, public :: t_type integer, dimension(:), _DYNAMIC_TYPE :: chars end type t_type integer, save :: callnb = 0 contains recursive function recursivefunc ( this ) result ( match ) type(t_type), intent(in) :: this type(t_type) :: subpattern logical :: thisalloc integer :: thislength logical :: match write ( * , * ) "recursivefunc [" , callnb , "]" thislength = size ( this % chars ) write ( * , * ) "length :", thislength callnb = callnb + 1 thisalloc = allocated ( this % chars ) !thisalloc = associated ( this % chars ) if ( .NOT. thisalloc ) then write ( 6 , * ) "STOP with error !" stop endif if ( thislength == 0 ) then match = .true. return endif allocate ( subpattern % chars ( thislength - 1 ) ) match = recursivefunc ( subpattern ) end function recursivefunc end module testmod program testprog use testmod implicit none type(t_type) :: this logical :: match allocate ( this % chars ( 10 )) match = recursivefunc ( this ) print * , "match :", match end program testprog Compile it with : gfortran -x f95-cpp-input testZeroSizeRecursive2.f90 -o testZeroSizeRecursive2.exe (flibs-workbench) 60 % testZeroSizeRecursive2.exe recursivefunc [ 0 ] length : 10 recursivefunc [ 1 ] length : 9 STOP with error ! This is because the allocated statement return .false., even if the array is really allocated. Trying the "pointer" version by modifying the #define preprocessing statement and the "allocated" to associated" make the problem disappear : recursivefunc [ 0 ] length : 10 recursivefunc [ 1 ] length : 9 recursivefunc [ 2 ] length : 8 recursivefunc [ 3 ] length : 7 recursivefunc [ 4 ] length : 6 recursivefunc [ 5 ] length : 5 recursivefunc [ 6 ] length : 4 recursivefunc [ 7 ] length : 3 recursivefunc [ 8 ] length : 2 recursivefunc [ 9 ] length : 1 recursivefunc [ 10 ] length : 0 match : T This is the version of gfortran I currently use ( I downloaded it today and I think that it is up-todate ) : (flibs-workbench) 61 % gfortran -v Using built-in specs. Target: i586-pc-mingw32 Configured with: ../trunk/configure --prefix=/mingw --enable-languages=c,fortran --with-gmp=/home/FX/local --with-ld=/mingw/bin/ld --with-as=/mingw/bin/as --disable-werror --enable-bootstrap --enable-threads --disable-nls --build=i586-pc-mingw32 --enable-libgomp --disable-shared Thread model: win32 gcc version 4.4.0 20080312 (experimental) [trunk revision 133139] (GCC) Regards, Michaël Baudin -- Summary: Recursive function with allocatable array Product: gcc Version: 4.4.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: michael dot baudin at gmail dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #3 from michael dot baudin at gmail dot com 2008-04-18 08:13 --- Subject: Re: Recursive function with allocatable array Hi Paul, The generic source code that you sent to me is interesting and you analysis is very clear. Thank you for taking the time to fix this. But I wonder how you produced this source (which have many in common with C), because I looked in the gfortran manual, and I obviously did not find any option to generate the generic source code, which is an internal step. Is that available only with the middle-end of gfortran and not available for end-users ? Best regards, Michaël On Thu, Apr 17, 2008 at 10:37 PM, pault at gcc dot gnu dot org <[EMAIL PROTECTED]> wrote: > > > --- Comment #2 from pault at gcc dot gnu dot org 2008-04-17 20:37 > --- > This is indeed a bug. A fix will be posted in half an hour or so. > > > Paul > > > -- > > pault at gcc dot gnu dot org changed: > >What|Removed |Added > > AssignedTo|unassigned at gcc dot gnu |pault at gcc dot gnu dot org >|dot org | > Status|NEW |ASSIGNED >Last reconfirmed|2008-04-17 05:59:41 |2008-04-17 20:37:53 > > >date|| > > > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959 > > --- You are receiving this mail because: --- > You reported the bug, or are watching the reporter. > -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #5 from michael dot baudin at gmail dot com 2008-04-18 14:11 --- Subject: Re: Recursive function with allocatable array Thank you very much. I will try immediately. On Fri, Apr 18, 2008 at 4:04 PM, jvdelisle at gcc dot gnu dot org <[EMAIL PROTECTED]> wrote: > --- Comment #4 from jvdelisle at gcc dot gnu dot org 2008-04-18 14:04 > --- > Reply to: But I wonder how you produced this source (which have many in > common > with C), > > The intermediate code can be listed using the -fdump-tree-original compiler > flag. It will be placed in a file with the word original appended to the > name. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #6 from michael dot baudin at gmail dot com 2008-04-18 15:07 --- Subject: Re: Recursive function with allocatable array I used the -fdump-tree-original option to compare the version produced on this source code with allocatable and pointer. Only the allocatable contains the following statements : static struct t_type subpattern = {}; subpattern.chars.data = 0B; I understand that the statement " subpattern.chars.data = 0B;" allows to free the array when it goes out of scope, as the standard says about allocatable arrays (which explains why it is not in the pointer version) : if (subpattern.chars.data != 0B) { __builtin_free (subpattern.chars.data); } subpattern.chars.data = 0B; The statement "static struct t_type subpattern = {};", which is not in the pointer version, is the cause of the problem so that the solution is to remove it : at second call, the array has been initialiazed to null, and because it is static, it memory is still deallocated on recursive calls. An allocatable array in a recursive subroutine should not be static (in fortran, that means having the SAVE attribute), isn't it ? Am I right ? Best regards, Michaël -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959
[Bug fortran/35959] Recursive function with allocatable array
--- Comment #10 from michael dot baudin at gmail dot com 2008-04-24 19:55 --- Subject: Re: Recursive function with allocatable array Thank you for fixing the bug. Michaël On Sun, Apr 20, 2008 at 12:32 AM, pault at gcc dot gnu dot org <[EMAIL PROTECTED]> wrote: > > > --- Comment #9 from pault at gcc dot gnu dot org 2008-04-19 22:32 > --- > Fixed on trunk and 4.3. > > Thanks for the report. > > Paul -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35959