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

            Bug ID: 100813
           Summary: Function of array of pointers to abstract class
                    returning an array
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mailling-lists-bd at posteo dot de
  Target Milestone: ---

Hello, 

I was trying to write a function that takes an array of pointers to an abstract
class as argument, and that returns an array with a shape determined by a
member of the mentioned abstract class.  The following example segfaults with
gfortran 10.3 on Opensuse: 

module baseClass
  implicit none

  type, abstract, public :: base
     integer, allocatable :: i(:)
   contains
     procedure(sub), deferred, pass :: mysub
  end type base

  abstract interface
     subroutine sub(bb) 
       import :: base
       class(base), intent(in) :: bb
     end subroutine sub
  end interface

  type, public :: baseWrap
     class(base), pointer :: p
  end type baseWrap

  public :: operate_on_baseWrap

contains

  function operate_on_baseWrap(wrapped) result(res)
    class(baseWrap), intent(in) :: wrapped(:)

    integer :: i, tmp
    integer, dimension(size(wrapped(1)%p%i)) :: res

    do i=1, size(wrapped)
       call wrapped(i)%p%mysub
    end do

    res = 0
  end function operate_on_baseWrap

end module baseClass


module childClass
  use baseClass
  implicit none

  type, extends(base), public :: child1
   contains
     procedure, pass :: mysub => sub_child1
  end type child1

contains

  subroutine sub_child1(bb)
    class(child1), intent(in) :: bb

    print *, 'Do something', bb%i
  end subroutine sub_child1

end module childClass


program test
  use baseClass
  use childClass
  implicit none

  type(baseWrap), allocatable :: wrapper(:)

  type(child1), allocatable, target :: c(:)

  integer :: i

  integer, allocatable :: tmp(:)

  allocate(wrapper(3))
  allocate(c(3))

  do i=1, 3
     allocate(c(i)%i(3))
     c(i)%i = i
     wrapper(i)%p => c(i)
  end do

  allocate(tmp(size(wrapper(1)%p%i)))
  tmp = operate_on_baseWrap(wrapper)
end program test

Reply via email to