[PATCH, PR fortran/45170] -- properly translates substring reference

Steve Kargl sgk@troutmask.apl.washington.edu
Sat Aug 27 00:34:00 GMT 2011


The attached patch allows gfortran to compile the
attached testcase.  The resulting executable runs
as expected.

Short story:  

  character(len=20) :: string = 'some text here'
  character(len=:), allocatable :: s
  n = 5
  allocate(s, source=string(:n))

The length of s is determined from the expression in
the source= argument.  If this expression is a lonely
substring reference as in the above, then gfortran 
does set the correct length.  This patch fixes this.

OK for trunk?

2011-08-26  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/45170
	* trans-stmt.c (gfc_trans_allocate): Evaluate the substring.

2011-08-26  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/45170
	* gfortran.dg/allocate_with_source_2.f90: New test
-- 
Steve
-------------- next part --------------
A non-text attachment was scrubbed...
Name: trans-stmt.c.diff
Type: text/x-diff
Size: 592 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20110827/05a241ea/attachment.bin>
-------------- next part --------------
! { dg-do run }
! PR 45170
! A variation of a theme for deferred type parameters.  The
! substring reference in the source= portion of the allocate
! was not probably resolved.  Testcase is a modified version
! of a program due to Hans-Werner Boschmann <boschmann at tp1
! dot physik dot uni-siegen dot de>
!
program helloworld
  character(:),allocatable::string
  real::rnd
  call hello(5, string)
  if (string /= 'hello' .or. len(string) /= 5) call abort
contains
  subroutine hello (n,string)
    character(:),allocatable,intent(out)::string
    integer,intent(in)::n
    character(20)::helloworld="hello world"
   allocate(string, source=helloworld(:n))
  end subroutine hello
end program helloworld


More information about the Fortran mailing list