将 (1:n) 与继承的 类 一起使用在 gfortran 中运行异常

Using (1:n) with inherited classes works weirdly in gfortran

我一直在以 objective 的方式更新 Fortran90 中的旧程序,并且 我遇到了一个非常奇怪的继承对象行为: 例如:

module try_mod

implicit none

type, public :: Inner_t
  integer :: i
  real :: r
end type Inner_t

type, public, extends(Inner_t) :: InnerSpec_t
  integer :: j
end type InnerSpec_t

type, public :: Outer_t
  integer :: nelem
  class( Inner_t ), allocatable, dimension(:) :: elem

contains
  procedure :: init => initOuter

end type Outer_t


contains

 subroutine initOuter(this)
  class(Outer_t), intent(inout) :: this
  integer :: i, suma, k

  this%nelem = 4

  allocate( InnerSpec_t :: this%elem(1:this%nelem) )


  this%elem(1:2)%i = -100
  this%elem(3:4)%i = -200


  print*, '1st: ', this%elem(1:this%nelem)%i
  print*, '2nd: ', this%elem(1)%i, this%elem(2)%i, this%elem(3)%i, this%elem(4)%i

  this%elem(1)%i = 91 
  this%elem(2)%i = 92 
  this%elem(3)%i = 93
  this%elem(4)%i = 94 
  print*,'3rd: ', this%elem(1:4)%i
  print*,'4th: ', this%elem(1)%i, this%elem(2)%i, this%elem(3)%i, this%elem(4)%i



end subroutine initOuter

end module try_mod 

program adgo
  use try_mod

implicit none

  type( Outer_t ) :: outer

  call outer%init()
end program

如果我用 gfortran(4.8 或 4.9)编译这个程序,我得到

 1st:         -100        -100        -200        -200
 2nd:         -100       32751        -200  1852387182
 3rd:           91        -100        -200          93
 4th:           91          92          93          94

尽管第一行和第二行(分别为第三行和第四行)应该相同。看起来继承对象的内存分配存在一些问题,因为如果我注释变量 j 的声明(在 InnerSpec_t 中),这个问题就会消失。 如果用 intel fortran 编译,一切正常。

我是否使用了不恰当的结构?有没有办法让 gfortran 也能做到这一点?

这看起来很像 gfortran 中的错误。我已经为此提交了 PR 65359(PR 用于 "Problem Report")。