A wrong column is selected when addressing individual elements of unlimited polymorphic dummy argument

Viewed 156

Consider the following Fortran program:

program test_prg
  implicit none
  integer :: i
  integer, allocatable :: arr(:, :)

  arr = reshape([(i, i = 1, 100)], [10, 10])
  do i = 1, 3
    print *, 'Column', i
    call write_array(arr(1:2, i)) 
  end do

contains

  subroutine write_array(array)
    class(*), intent(in) :: array(:)
    integer :: i

    do i = 1, size(array)
      select type (elem => array(i))
        type is (integer)
          print '(I0)', elem
      end select
    end do
  end subroutine


  subroutine write_array_alt(array)
    class(*), intent(in) :: array(:)
    integer :: i

    select type (array)
      type is (integer)
        print '(I0)', array
    end select
  end subroutine

end program

Compiled with gfortran 9.3.0, the program prints:

 Column           1
1
2
 Column           2
21
22
 Column           3
41
42

As you can see, it skips every other column. If I replace the call to write_array with the call to write_array_alt, where I select type of the whole array and not individual elements, then every column is printed, as expected. Is this a gfortran bug or is this code illegal?

0 Answers
Related