Skip to content

[flang] DTIO failure with list-directed READ #154979

@DanielCChen

Description

@DanielCChen

Consider the following code:

module m
    type base
        real, allocatable :: data(:)
        complex(8), allocatable :: cx(:)

        contains

        procedure :: readBaseFmtd
        generic :: read(formatted) => readBaseFmtd
    end type

    contains

    !! read in the array size before allocating dtv's components
    subroutine readBaseFmtd (dtv, unit, iotype, v_list, iostat, iomsg)
        class(base), intent(inout) :: dtv
        integer, intent(in) :: unit
        character(*), intent(in) :: iotype
        integer, intent(in) :: v_list(:)
        integer, intent(out) :: iostat
        character(*), intent(inout) :: iomsg

        integer isize

        read (unit, *, iostat = iostat, iomsg=iomsg) isize

        if (allocated(dtv%data)) deallocate (dtv%data)

        allocate (dtv%data(isize))

        read (unit, *, iostat=iostat, iomsg=iomsg) dtv%data

        read (unit, *, iostat = iostat, iomsg=iomsg) isize

        if (allocated(dtv%cx)) deallocate (dtv%cx)

        allocate (dtv%cx(isize))

        read (unit, *, iostat=iostat, iomsg=iomsg) dtv%cx
    end subroutine
end module

program dcmlChildRead003a1
use m
    integer currentPos, ss

    type(base) :: b1(2)


    open (1, file='dcmlChildRead003.data', access='stream', form='formatted', &
            decimal='Comma')

    write (1, '(i4, 10(g15.7))', pos=1, decimal='Point') 10, (i*1.0, i=1, 10)

    inquire (1, pos=currentPos)

    write(1, *, pos=currentPos, decimal='point') 12, (cmplx(i*1.0, i*2.0, 8), i=-12, -1)

    write (1, *) 20, (i*1.22, i=1,20)
    write (1, *, sign='plus') 22, (cmplx(i*1.1, i*2.2,8), i=-10,11)

    allocate (b1(1)%cx(3), b1(2)%data(1000))
    read (1, '(dp, dt, dc, dt)', pos=1) b1

print*, size(b1(1)%data)
print*, size(b1(1)%cx)

end

The test case prepared an input file. Then resized the components of the input item based on the isize that is read from the input file.
Flang seems failed to update the isize at the 2nd READ inside of the DTIO procedure.

The expected output is

> a.out
 10
 12

Flang has

> a.out
 10
 10

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions