<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/152527>152527</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang] DTIO: Polymorphic passed-object does not have the correct dynamic type in DTIO procedure.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang:runtime
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          DanielCChen
      </td>
    </tr>
</table>

<pre>
    Consider the following reducer:
```
module m
   type base
      character(3) :: c
      contains
         procedure, pass :: write
   end type

   type, extends(base) :: child
      integer(4) :: i
   end type

   interface write(formatted)
      subroutine writeformatted(dtv, unit, iotype, v_list, iostat, iomsg )
         import base
         class(base), intent(in) :: dtv
         integer,  intent(in) :: unit
         character(*), intent(in) :: iotype
         integer, intent(in)     :: v_list(:)
         integer, intent(out) :: iostat
         character(*),  intent(inout) :: iomsg
 end subroutine
   end interface

   integer :: unit = 1
   integer :: stat
   character(200) :: msg

contains

   class(base) function write(dtv)
      class(base), intent(in) :: dtv
      allocatable :: write
      namelist /W/ write

      allocate ( write, source = dtv )
 write (unit, W, iostat = stat, iomsg = msg )

   end function

end module

program funcRetrn001a
   use m

   class(base), allocatable :: b1
   type(child)              :: c1 = child ('FTN',123)

   open (1, file = 'funcRetrn001a.1', form='formatted', access='sequential' )

   b1 = c1%write()   !! The passed-object is of type Child.

end program


subroutine writeformatted (dtv, unit, iotype, v_list, iostat, iomsg)
   use m, only: base,child

   class(base), intent(in) :: dtv
 integer, intent(in) :: unit
   character(*), intent(in) :: iotype
 integer, intent(in)     :: v_list(:)
   integer, intent(out) :: iostat
 character(*), intent(inout) :: iomsg

   select type (dtv)
      type is (base)
print*, "base"
      type is (child)
print*, "child"
   end select
end subroutine
````

Flang outputs `base`, while the expected output is `child` (both gfortran and XLF).

The type of the passed-object of `c1%write()` call should be `child` instead of `base`.
</pre>
<img width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