Polymorphic type lost in allocation with mold option

It seems, that the polymorphic type information gets lost, when allocation is done with

allocate(a, mold=b)

Given the self containing example below, the code compiles, but delivers wrong results, as the wrong branch of the select type operator is executed.

Best regards, Bálint

module typedefs
  implicit none
  type :: Base
  end type Base

  type, extends(Base) :: Extended
    integer :: data = -1
  end type Extended
end module typedefs

program test
  use typedefs
  implicit none

  class(Base), pointer :: ptr
  type(Extended) :: myExt1

  myExt1%data = 42
  allocate(ptr, mold=myExt1)
  select type (ptr)
  type is (Extended)
    print *, "This should be executed"
  class default
    print *, "This should NOT be executed"
  end select
end program test

This problem has been replicated and logged as TPR 22427.


As a workaround, engineering has suggested you use sourced allocation,

For example,

allocate(ptr, source=myExt1) )

as a workaround for PGI.


TPR 22427 has been fixed in the current 16.5 release.

Thanks again for the report.