diff mbox

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

Message ID 20110827003424.GA55073@troutmask.apl.washington.edu
State New
Headers show

Commit Message

Steve Kargl Aug. 27, 2011, 12:34 a.m. UTC
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

Comments

Mikael Morin Aug. 28, 2011, 9:36 p.m. UTC | #1
On Saturday 27 August 2011 02:34:24 Steve Kargl wrote:
> 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. 
... does *not* set the correct length, I suppose?

> OK for trunk?
It is OK. Rather obvious. Thanks

Mikael
Steve Kargl Aug. 30, 2011, 3:35 p.m. UTC | #2
On Sun, Aug 28, 2011 at 11:36:42PM +0200, Mikael Morin wrote:
> On Saturday 27 August 2011 02:34:24 Steve Kargl wrote:
> > 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. 
> ... does *not* set the correct length, I suppose?
> 
> > OK for trunk?
> It is OK. Rather obvious. Thanks
> 

Thanks.

Committed revision 178329.
diff mbox

Patch

Index: trans-stmt.c
===================================================================
--- trans-stmt.c	(revision 177772)
+++ trans-stmt.c	(working copy)
@@ -4783,6 +4783,10 @@  gfc_trans_allocate (gfc_code * code)
 			|| code->expr3->expr_type == EXPR_CONSTANT)
 		    {
 		      gfc_conv_expr (&se_sz, code->expr3);
+		      gfc_add_block_to_block (&se.pre, &se_sz.pre);
+		      se_sz.string_length
+			= gfc_evaluate_now (se_sz.string_length, &se.pre);
+		      gfc_add_block_to_block (&se.pre, &se_sz.post);
 		      memsz = se_sz.string_length;
 		    }
 		  else if (code->expr3->mold