public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/30003]  New: Expressions with side effects in array references
@ 2006-11-27 22:25 eedelman at gcc dot gnu dot org
  2006-11-29 21:21 ` [Bug fortran/30003] " tobi at gcc dot gnu dot org
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: eedelman at gcc dot gnu dot org @ 2006-11-27 22:25 UTC (permalink / raw)
  To: gcc-bugs

(This bug was discovered during a discussion on the mailinglist starting here:
http://gcc.gnu.org/ml/fortran/2006-11/msg00636.html) 

This short program

erik:~$ cat foo.f90
program foo

    implicit none
    integer :: a(5), b(3)

    b = [ 1, 2, 3 ]
    a(1:bar(3)) = b

contains
    integer function bar(n)
        integer, intent(in) :: n
        print *, 'Hello!'
        bar = n
    end function bar
end program foo

should, when compiled and run, call bar() once, and, hence, print 'hello!'
once.  But when compiled without -fbounds-check, it doesn't print anything:

erik:~$ gfortran foo.f90 
erik:~$ a.out

This is because gfortran will actually never evaluate the expression of the
size of the LHS; it will just assume the LHS has the same size as the RHS. 
That would be OK if the expression of the size didn't have side effects, but in
a case like this, it gives wrong results.

If we compile with -fbounds-check, it gets really strange:

erik:~$ gfortran foo.f90 -fbounds-check
erik:~$ a.out
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!
 Hello!


Here, bar() is called 13 times!  This is because bar() is called separately for
every check that the value of bar() is within bounds etc. Again, this would be
OK (albeit a bit inefficient) if bar() had no side effects, but with side
effects it gives wrong results.  

Also consider the case where bar():s result value depends on e.g. a SAVEd
variable, so that it returns different values every time it's called (even if
called with the same argument).  That could give _really_ wierd behaviour.


-- 
           Summary: Expressions with side effects in array references
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: eedelman at gcc dot gnu dot org
        ReportedBy: eedelman at gcc dot gnu dot org


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


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

end of thread, other threads:[~2006-12-09 17:48 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-27 22:25 [Bug fortran/30003] New: Expressions with side effects in array references eedelman at gcc dot gnu dot org
2006-11-29 21:21 ` [Bug fortran/30003] " tobi at gcc dot gnu dot org
2006-12-02 22:28 ` eedelman at gcc dot gnu dot org
2006-12-04 20:47 ` pault at gcc dot gnu dot org
2006-12-04 22:44 ` pault at gcc dot gnu dot org
2006-12-05  8:25 ` pault at gcc dot gnu dot org
2006-12-05  9:39 ` pault at gcc dot gnu dot org
2006-12-05 15:07 ` burnus at gcc dot gnu dot org
2006-12-05 15:57 ` tobi at gcc dot gnu dot org
2006-12-05 16:45 ` patchapp at dberlin dot org
2006-12-05 19:46 ` pault at gcc dot gnu dot org
2006-12-09 15:18 ` [Bug fortran/30003] [4.2 and 4.1 only] " pault at gcc dot gnu dot org
2006-12-09 15:22 ` [Bug fortran/30003] [4.1 " pault at gcc dot gnu dot org
2006-12-09 17:48 ` pault at gcc dot gnu dot 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).