Contained subroutine in main program

Hello,

A fortran code having contained subroutine inside main program does not seem not to work for me.

In the pasted below an example
when main code has the contained subroutine “l_print__” i,
the second call of
call index_all_woverlap(‘b2’, iv)

it return the size of array iv be 4 instead of 2

I tried with pgi version 18.7 to 19.9 ; all behave the same: had the wrong size of
iv2 after the 2nd call of index_all_woverlap

Deleting / commenting the contained subroutine “l_print__”
the code works OK.

Is this a bug or I am still missing some f90/08 rule somehow ?

ifort and gfortran worked OK.

sample code (simplified to underline the issue)

   module a_m

    public :: index_all_woverlap
    public :: iv2str

   contains

   subroutine index_all_woverlap( s_in, i_loc )
   implicit none
   integer, intent(out),allocatable :: i_loc(:)
   character(*), intent(in) :: s_in

   if(len(s_in)<1) then
       allocate(i_loc(0))
       return
   endif

   allocate(i_loc(len(s_in)))
   print*,'size(i_loc)=',size(i_loc)

   end subroutine index_all_woverlap

    function iv2str(iv) result ( u )
    implicit none
    integer, intent(in) :: iv(:)
    integer i
    character(:),allocatable :: u
    u=' ' ! same if return u=''
    end function iv2str

   end module a_m


  program main
  use a_m

  implicit none
  integer, allocatable :: iv(:), iv1(:)

   call  index_all_woverlap( ' +4 ',  iv)
   print*,'A>>',size(iv),'"',iv,'"'  ! returns 4 (OK)

   call  index_all_woverlap('b2',  iv)
   print*,'B>>',size(iv),'"',iv,'"'  ! not ok; returns 4 (but should be 2)


   stop

   print*,'siuze iv',size(iv)
   print*,'iv="'//iv2str(iv)//'"'   

  contains
  subroutine l_print__ !lset_keys_(str_)

! character(), intent(in) :: str_
print
,’??’
end subroutine l_print__ !lset_keys_

  end program main

Yes, this definitely looks like a bug. The size is okay in the subroutine, and not in the main program, which is odd. Even stranger, the behavior changes when an unused contained subprogram in main exists, or not. I will file a bug with the compiler group.

FYI, we are tracking this with FS#30876.

This has been fixed in our 22.5 release, now available