| Issue |
244316
|
| Summary |
[flang-rt] Zero-size array of derived type ends the I/O list early when IOSTAT=, ERR=, END= or EOR= is present
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
When a data transfer statement has an IOSTAT=, ERR=, END= or EOR= specifier, a zero-size array of a derived type in its I/O list silently ends the transfer: the list items after it are never written or read, and IOSTAT= is 0. A zero-size array of an intrinsic type works correctly.
```fortran
program test
implicit none
type :: t
integer :: x
end type
type(t) :: a(0)
integer :: n, ios
character(10) :: buf
buf = 'old'
write(buf, '(I3)', iostat=ios) a, 42
print '(a,i0,3a)', 'formatted write: iostat=', ios, ' buf=[', buf, ']'
buf = ' 42'
n = -1
read(buf, '(I3)', iostat=ios) a, n
print '(a,i0,a,i0)', 'formatted read: iostat=', ios, ' n=', n
buf = 'old'
write(buf, *, iostat=ios) a, 42
print '(a,i0,3a)', 'list-directed write: iostat=', ios, ' buf=[', buf, ']'
end program
```
```console
$ flang test.f90 && ./a.out
formatted write: iostat=0 buf=[ ]
formatted read: iostat=0 n=-1
list-directed write: iostat=0 buf=[ ]
```
Expected:
```
formatted write: iostat=0 buf=[ 42 ]
formatted read: iostat=0 n=42
list-directed write: iostat=0 buf=[ 42 ]
```
Per F2023 12.6.3 p9, zero-sized arrays do not contribute to the list of effective items, so the statement should behave as if `a` were absent. Observed with flang built from main at 4b8b7dbb7c41:
- Formatted, list-directed and unformatted I/O are all affected. An unformatted `write(u, iostat=ios) a, 42` writes an empty record.
- An allocatable array allocated with size 0 behaves the same way, as does a scalar of a derived type whose only component is a zero-size array of a derived type.
- A zero-size array of an intrinsic type (`integer :: a(0)`), or a derived type whose only component is a zero-size intrinsic array, works correctly.
- Without IOSTAT=, ERR=, END= or EOR=, the output is correct.
Likely cause: `DescriptorIO` in `flang-rt/lib/runtime/descriptor-io.cpp` returns `anyIoTookPlace`, and that becomes the result of `OutputDescriptor`/`InputDescriptor`. For intrinsic types the per-type routines (`FormattedIntegerIO` and the like, or the unformatted transfer of `0` bytes) return true when there are no elements, so `anyIoTookPlace` is set. For a derived type, `DescriptorIoTicket::Begin` returns `StatContinue` and `Continue` loops over zero elements, so nothing sets `anyIoTookPlace` and the call returns false. When the statement has a condition specifier, lowering makes each subsequent I/O call conditional on the previous result (`makeNextConditionalOn` in `flang/lib/Lower/IO.cpp`), so the rest of the list is skipped while no error is recorded.
PR 239369 handles the zero-element case for enumeration types only; ordinary derived types are still affected.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs