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

            Bug ID: 119579
           Summary: Derived type not initialized correctly with array
                    sections
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vivekrao4 at yahoo dot com
  Target Milestone: ---

For the code

module dataframe_mod
  implicit none
  private
  public :: DataFrame, display, subset_stride

  type :: DataFrame
     real(kind=4), allocatable :: values(:,:)
  end type DataFrame

contains

  subroutine display(self)
    class(DataFrame) :: self
    integer :: i
    do i = 1, UBOUND(self%values,dim=1)
       write(*,"(1x,A,I1,*(1x,f10.4))") "row: ", i, self%values(i,:)
    end do
  end subroutine display

  function subset_stride(df) result(df_new)
    type(DataFrame) :: df
    type(DataFrame) :: df_new
    !...suspect below ...
    df_new = DataFrame(values  = df%values(1:8:3, :))
    !...can fix this by explicitly setting result df_new
! uncomment_for_working_testcase 
    !allocate(df_new%values(3,2))
    !df_new%values(1,:) = [ 1.1, 1.2 ]
    !df_new%values(2,:) = [ 4.1, 4.2 ]
    !df_new%values(3,:) = [ 7.1, 7.2 ]
  end function subset_stride

end module dataframe_mod

program xxdataframe
  use dataframe_mod
  implicit none
  type(DataFrame) :: df, df_new
  allocate(df%values(8,2) )
   df%values(1,:) = [ 1.1, 1.2 ]
   df%values(2,:) = [ 2.1, 2.2 ]
   df%values(3,:) = [ 3.1, 3.2 ]
   df%values(4,:) = [ 4.1, 4.2 ]
   df%values(5,:) = [ 5.1, 5.2 ]
   df%values(6,:) = [ 6.1, 6.2 ]
   df%values(7,:) = [ 7.1, 7.2 ]
   df%values(8,:) = [ 8.1, 8.2 ]
  write(*,*) "display initial values of df"
   call display(df)
  write(*,*)

  df_new = subset_stride(df)
  write(*,*) "print df_new, should have rows 1, 4, 7 "
  write(*,*) "shape(df_new%values)", shape(df_new%values)
  write(*,*) "bounds of df_new%values, dim 1: ", LBOUND(df_new%values,1),
UBOUND(df_new%values,1)
  write(*,*) "bounds of df_new%values, dim 2: ", LBOUND(df_new%values,2),
UBOUND(df_new%values,2)
  call display(df_new)
end program xxdataframe

from
https://fortran-lang.discourse.group/t/derived-type-initialized-with-array-sections-giving-unexpected-results/9470/5?u=beliavsky

gfortran gives incorrect output

 display initial values of df
 row: 1     1.1000     1.2000
 row: 2     2.1000     2.2000
 row: 3     3.1000     3.2000
 row: 4     4.1000     4.2000
 row: 5     5.1000     5.2000
 row: 6     6.1000     6.2000
 row: 7     7.1000     7.2000
 row: 8     8.1000     8.2000

 print df_new, should have rows 1, 4, 7 
 shape(df_new%values)           3           2
 bounds of df_new%values, dim 1:            1           3
 bounds of df_new%values, dim 2:            1           2
 row: 1     1.1000     1.2000
 row: 2     2.1000     2.2000
 row: 3     3.1000     3.2000

Reply via email to