https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119540

--- Comment #1 from anlauf at gcc dot gnu.org ---
The following testcase - derived from reduce_1.f90 - shows the reason for
a bound-check violation:

program test_reduce
  implicit none
  integer, parameter :: n = 3
  integer, parameter :: vec(n) = [2, 5, 10]
  integer, parameter :: mat(n,2) = reshape([vec,2*vec],shape=[size(vec),2])
  integer, dimension(:), allocatable :: res1

  print *, "shape (mat) =", shape (mat)
  print *, "mat(:,1) =", mat(:,1)
  print *, "mat(:,2) =", mat(:,2)
  print *, "shape (reduce (mat, add, 1)):", shape (reduce (mat, add, 1))
  print *, "       reduce (mat, add, 1) :"
  print *,         reduce (mat, add, 1)
  print *, "shape (reduce (mat, add, 2)):", shape (reduce (mat, add, 2))
  print *, "       reduce (mat, add, 2) :"
  print *,         reduce (mat, add, 2)
  res1 = reduce (mat, add, 1)
  if (any (res1 /= [17, 34])) stop 3
contains
  pure function add(i,j) result(sum_ij)
    integer, intent(in) :: i, j
    integer             :: sum_ij
    sum_ij = i + j
  end function add
end

% gfc-15 reduce_x.f90 -fcheck=all -g -O && ./a.out

 shape (mat) =           3           2
 mat(:,1) =           2           5          10
 mat(:,2) =           4          10          20
 shape (reduce (mat, add, 1)):           3
        reduce (mat, add, 1) :
          17          34           0
 shape (reduce (mat, add, 2)):           3
        reduce (mat, add, 2) :
           6          15          30
At line 18 of file reduce_x.f90
Fortran runtime error: Array bound mismatch for dimension 1 of array 'res1'
(3/2)

Error termination. Backtrace:
#0  0x401437 in test_reduce
        at /home/anlauf/gcc-bugs/reduce_x.f90:18
#1  0x401463 in main
        at /home/anlauf/gcc-bugs/reduce_x.f90:18


Intel ifx gives:

 shape (mat) =           3           2
 mat(:,1) =           2           5          10
 mat(:,2) =           4          10          20
 shape (reduce (mat, add, 1)):           2
        reduce (mat, add, 1) :
          17          34
 shape (reduce (mat, add, 2)):           3
        reduce (mat, add, 2) :
           6          15          30

Note that gfortran has a wrong array shape for dim=1.

Reply via email to