Bug 35940

Summary: Array BACK ignored in INDEX intrinsic when other args scalar
Product: gcc Reporter: Dick Hendrickson <dick.hendrickson>
Component: fortranAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: dick.hendrickson, fxcoudert, gcc-bugs, tkoenig
Priority: P3 Keywords: wrong-code
Version: 4.4.0   
Target Milestone: 4.4.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2008-04-16 18:16:05

Description Dick Hendrickson 2008-04-14 21:46:43 UTC
The following gives the wrong answers.  It may have to do
with broadcasting things to arrays.  The all scalar case works
and an array first argument works.

      program FA1031

! fails on Windows XP
! gcc version 4.4.0 20080312 (experimental) [trunk revision 133139]

      INTEGER IDA1(10)
      LOGICAL GDA1(10)

      IDA1   = 0
      gda1 = (/ (i/2*2 .ne. I, i=1,10) /)
      print '(10L2)', gda1

      IDA1 = INDEX ( 'DEFDEF' , 'DEF', GDA1 )    !fails
      print '(10I2)', ida1

      IDA1 = INDEX ( (/ ('DEFDEF',i=1,10) /) , 'DEF', GDA1 )    !works
      print '(10I2)', ida1

      IDA = INDEX ( 'DEFDEF' , 'DEF', .true. )   !works
      print '(I2)', ida

      END
Comment 1 Dick Hendrickson 2008-04-14 21:49:33 UTC
I forgot to include the output

C:\gfortran>gfortran fa1031.f

C:\gfortran>a
 T F T F T F T F T F
 1 1 1 1 1 1 1 1 1 1
 4 1 4 1 4 1 4 1 4 1
 4
Comment 2 Thomas Koenig 2008-04-16 18:09:18 UTC
Confirmed, not a regression.
Comment 3 Bud Davis 2008-04-29 19:23:07 UTC
this looks promising:

Index: gcc/gcc/fortran/simplify.c
===================================================================
--- gcc/gcc/fortran/simplify.c  (revision 134801)
+++ gcc/gcc/fortran/simplify.c  (working copy)
@@ -1570,7 +1570,8 @@
   int back, len, lensub;
   int i, j, k, count, index = 0, start;
 
-  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT)
+  if (x->expr_type != EXPR_CONSTANT || y->expr_type != EXPR_CONSTANT 
+      || b->expr_type !=  EXPR_CONSTANT)
     return NULL;
 
   if (b != NULL && b->value.logical != 0)
Comment 4 Jerry DeLisle 2008-04-30 00:00:25 UTC
Note the last line of patch is checking for b != NULL before using it.  Probably want to do likewise for the added test condition above.
Comment 5 Bud Davis 2008-04-30 00:54:06 UTC
close.  here is a better patch.

http://gcc.gnu.org/ml/fortran/2008-04/msg00278.html
Comment 6 Bud Davis 2008-04-30 00:55:26 UTC
notice the 6 minutes between the above posts :)
jerry was correct, it needed a NULL check. 
Comment 7 Francois-Xavier Coudert 2008-05-12 18:02:04 UTC
Patch committed, fixed on mainline.