public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/47592] New: Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar()))
@ 2011-02-02 22:38 tkoenig at gcc dot gnu.org
  2011-02-03  7:43 ` [Bug fortran/47592] " paul.richard.thomas at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2011-02-02 22:38 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47592

           Summary: Multiple function invocation with ALLOCATE
                    (SOURCE=REPEAT('x',bar()))
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: tkoenig@gcc.gnu.org
                CC: pault@gcc.gnu.org


ig25@linux-fd1f:/tmp> cat foo.f90
module foo
  implicit none
contains
  function bar()
    integer bar
    integer :: i=9
    i = i + 1
    bar = i
  end function bar
end module foo

program note7_35
  use foo
  implicit none
  character(:), allocatable :: name
  character(:), allocatable :: src
  integer n
  n = 10
  allocate(name, SOURCE=repeat('x',bar()))
  if (name .ne. 'xxxxxxxxxx') call abort
  if (len (name) .ne. 10 ) call abort
  print *,bar()
end program note7_35
ig25@linux-fd1f:/tmp> gfortran foo.f90
ig25@linux-fd1f:/tmp> ./a.out
          12

The output should be 11.

Paul, the cleanest way to resolve this would be to replace the call
to gfc_copy_expr in your recent patch with a new function which
goes something like this (incomplete and untested...)

void gfc_freeze_expression (gfc_expr *oe, gfc_expr *ne, gfc_code **c)
{
  if (oe->expr_type == EXPR_CONSTANT || oe->expr_type == EXPR_VARIABLE)
    {
        ne = gfc_copy_expr (oe);
        return;
    }
    /* Create a new variable with the same type as oe,
       create an assignment where oe is assigned to that
       variable and insert it before the current statement,
       then replace oe with that variable.  */
}

This is a function that I've been meaning to write for the
front end passes for some time...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/47592] Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar()))
  2011-02-02 22:38 [Bug fortran/47592] New: Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar())) tkoenig at gcc dot gnu.org
@ 2011-02-03  7:43 ` paul.richard.thomas at gmail dot com
  2011-02-03 21:10 ` pault at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: paul.richard.thomas at gmail dot com @ 2011-02-03  7:43 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47592

--- Comment #1 from paul.richard.thomas at gmail dot com <paul.richard.thomas at gmail dot com> 2011-02-03 07:43:13 UTC ---
Hi Thomas,

I was aware of the potential for functions with side effects to screw
up ALLOCATE with source.  I believe that one can persuade the same
thing to happen with array constructors, although I have not tried it.


> ig25@linux-fd1f:/tmp> gfortran foo.f90
> ig25@linux-fd1f:/tmp> ./a.out
>          12
>
> The output should be 11.

Could you post a PR on this, please?

> Paul, the cleanest way to resolve this would be to replace the call
> to gfc_copy_expr in your recent patch with a new function which
> goes something like this (incomplete and untested...)
>
> void gfc_freeze_expression (gfc_expr *oe, gfc_expr *ne, gfc_code **c)
> {
>  if (oe->expr_type == EXPR_CONSTANT || oe->expr_type == EXPR_VARIABLE)
>    {
>        ne = gfc_copy_expr (oe);
>        return;
>    }
>    /* Create a new variable with the same type as oe,
>       create an assignment where oe is assigned to that
>       variable and insert it before the current statement,
>       then replace oe with that variable.  */
> }
>
> This is a function that I've been meaning to write for the
> front end passes for some time...

We have tended to use EXPR_PARENTHESES to achieve the same thing. It
will not work here, however.

This gets dangerously close to something that I have had in mind for a
long time - to do scalarization in the front end.  ***sigh*** I feel
like the White Rabbit in the opening scene in Alice in Wonderland!

Cheers

Paul


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/47592] Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar()))
  2011-02-02 22:38 [Bug fortran/47592] New: Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar())) tkoenig at gcc dot gnu.org
  2011-02-03  7:43 ` [Bug fortran/47592] " paul.richard.thomas at gmail dot com
@ 2011-02-03 21:10 ` pault at gcc dot gnu.org
  2011-02-06 14:25 ` pault at gcc dot gnu.org
  2011-02-06 15:07 ` pault at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pault at gcc dot gnu.org @ 2011-02-03 21:10 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47592

Paul Thomas <pault at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.02.03 21:09:44
         AssignedTo|unassigned at gcc dot       |pault at gcc dot gnu.org
                   |gnu.org                     |
     Ever Confirmed|0                           |1

--- Comment #2 from Paul Thomas <pault at gcc dot gnu.org> 2011-02-03 21:09:44 UTC ---
Created attachment 23240
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=23240
A mostly cooked patch

Thanks for posting this, Thomas.

The attached fixes the problem and regtests OK.  However, it uncovers some
oddities in gfc_trans_allocate that I want to take a look at before submitting
the patch.... just to be sure.

Paul


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/47592] Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar()))
  2011-02-02 22:38 [Bug fortran/47592] New: Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar())) tkoenig at gcc dot gnu.org
  2011-02-03  7:43 ` [Bug fortran/47592] " paul.richard.thomas at gmail dot com
  2011-02-03 21:10 ` pault at gcc dot gnu.org
@ 2011-02-06 14:25 ` pault at gcc dot gnu.org
  2011-02-06 15:07 ` pault at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pault at gcc dot gnu.org @ 2011-02-06 14:25 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47592

--- Comment #3 from Paul Thomas <pault at gcc dot gnu.org> 2011-02-06 14:22:51 UTC ---
Author: pault
Date: Sun Feb  6 14:22:48 2011
New Revision: 169862

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=169862
Log:
2011-02-06  Paul Thomas  <pault@gcc.gnu.org>

    PR fortran/47592
    * trans-stmt.c (gfc_trans_allocate): For deferred character
    length allocations with SOURCE, store to the values and string
    length to avoid calculating twice.  Replace gfc_start_block
    with gfc_init_block to avoid unnecessary contexts and to keep
    declarations of temporaries where they should be. Tidy up the
    code a bit.

2011-02-06  Paul Thomas  <pault@gcc.gnu.org>

    PR fortran/47592
    * gfortran.dg/allocate_with_source_1 : New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/allocate_with_source_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/testsuite/ChangeLog


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/47592] Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar()))
  2011-02-02 22:38 [Bug fortran/47592] New: Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar())) tkoenig at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2011-02-06 14:25 ` pault at gcc dot gnu.org
@ 2011-02-06 15:07 ` pault at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pault at gcc dot gnu.org @ 2011-02-06 15:07 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47592

Paul Thomas <pault at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED

--- Comment #4 from Paul Thomas <pault at gcc dot gnu.org> 2011-02-06 14:25:00 UTC ---
Fixed on trunk.

Thanks, Thomas.

Paul


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-02-06 14:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-02 22:38 [Bug fortran/47592] New: Multiple function invocation with ALLOCATE (SOURCE=REPEAT('x',bar())) tkoenig at gcc dot gnu.org
2011-02-03  7:43 ` [Bug fortran/47592] " paul.richard.thomas at gmail dot com
2011-02-03 21:10 ` pault at gcc dot gnu.org
2011-02-06 14:25 ` pault at gcc dot gnu.org
2011-02-06 15:07 ` pault at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).