Skip to content

[flang] DTIO: stream access with pos= specifier not working properly #167569

@DanielCChen

Description

@DanielCChen

Consider the following code

module m
    type base
        integer(8), allocatable :: data(:)
    end type

    interface write(formatted)
        subroutine formattedWrite (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
end module


subroutine formattedWrite (dtv, unit, iotype, v_list, iostat, iomsg)
use m, only: 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

    flush (unit, iostat=iostat, iomsg=iomsg)

    if (iostat /= 0) return

    if (allocated (dtv%data)) then
        write (unit, *, iostat=iostat, iomsg=iomsg) dtv%data

        if (iostat /= 0) return
    end if

    flush (unit, iostat=iostat, iomsg=iomsg)

end subroutine

program fdtio057
use m
    integer stat
    class (base), allocatable :: b1, b2(:)

    integer(8) :: i1(8) = 9

    allocate (b1, source=base((/-1, -2/)))
    allocate (b2(0:1))

    allocate (b2(0)%data(1), source=100_8)
    allocate (b2(1)%data(0:1), source=(/10_8, 20_8/))

    open (10, file='fdtio057.data', access='stream', form='formatted', &
                status='new')

    write (10, *, pos=15) base ((/2,3,1/))

    write (10, *, pos=25) b1

    write (10, *, pos=1) b2

!    read (10, *, pos=1) i1(1:3)    !<-- 100, 10, 20
!    read (10, *, pos=15, iostat=stat) i1(4:6)   !<-- 2, 3, 1
!    read (10, *, pos=25, iostat=stat) i1(7:8)   !<-- -1, -2

!    print*, i1
!    if (any (i1 /= (/100, 10, 20, 2, 3, 1, -1, -2/))) ERROR STOP 1

!    close (10, status='delete')
end

The output file dtio057.data contains

 100 10 20

When the expected content is

 100 10 20
                2 3 1
               -1 -2

If I move the pos=1 write statement to be the first write statement, Flang produces the expected result.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions