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

--- Comment #4 from federico <federico.perini at gmail dot com> ---
I've attached another program that perhaps highlights the problem better. 
Even just *accessing* a polymorphic array with an array causes wrong output
with gfortran 9.2.0: 

The attached program sends elements [3,6,9] in a polymorphic array to a
subroutine, which should then print some info: their ID in the original array. 
Instead of printing 3,6,9, it prints 3,4,5 when the input array was
polymorphic: 

module m
    implicit none

    type, public :: t
        integer :: i = 1
    end type t
    type, public, extends(t) :: tt
        integer :: j
    end type tt

    contains

    subroutine print_array(x,msg)
        class(t), intent(in) :: x(:)
        character(*), intent(in) :: msg
        integer :: j

        print *, msg//' :: size(x)=',size(x)
        select type (xx=>x(:))
           type is (t)
              do j=1,size(xx)
                  print *, 'x(',j,')%i = ',xx(j)%i
              end do
           type is (tt)
              do j=1,size(xx)
                  print *, 'x(',j,')%i = ',xx(j)%i,' %j=',xx(j)%j
              end do
        end select

    end subroutine print_array

end module m

program test_poly_access_array
    use m
    implicit none

    class(t), allocatable :: poly_t(:),poly_tt(:)
    type (t), allocatable :: nonpoly_t (:)
    type(tt), allocatable :: nonpoly_tt(:)
    integer :: i

    integer, dimension(3) :: chunk = [3,6,9]

    allocate(t  :: poly_t( 10))
    allocate(tt :: poly_tt(10))
    allocate(     nonpoly_t(10),nonpoly_tt(10))

    do i=1,10
       poly_t(i)%i = i
       poly_tt(i)%i = i
       select type (ptt=>poly_tt(:))
          type is (tt)
            ptt(i)%j = i
       end select
       nonpoly_t(i)%i = i
       nonpoly_tt(i)%i = i
       nonpoly_tt(i)%j = i
    end do

    call print_array(nonpoly_t(chunk),'nonpoly_t')
    call print_array(nonpoly_tt(chunk),'nonpoly_tt')
    call print_array(poly_t(chunk),'poly_t')
    call print_array(poly_tt(chunk),'poly_tt')


end program test_poly_access_array


Output is: 

 nonpoly_t :: size(x)=           3 ! Non-polymorphic, base type: OK
 x(           1 )%i =            3
 x(           2 )%i =            6
 x(           3 )%i =            9
 nonpoly_tt :: size(x)=           3 ! Non-polymorphic, extended type: OK
 x(           1 )%i =            3  %j=           3
 x(           2 )%i =            6  %j=           6
 x(           3 )%i =            9  %j=           9
 poly_t :: size(x)=           3     ! Polymorphic, base type: WRONG
 x(           1 )%i =            3
 x(           2 )%i =            4
 x(           3 )%i =            5
 poly_tt :: size(x)=           3    ! Polymorphic, extended type: WRONG  
 x(           1 )%i =            3  %j=           3
 x(           2 )%i =            4  %j=           4
 x(           3 )%i =            5  %j=           5

Reply via email to