Issue 243200
Summary [flang-rt] T and TL ignore the left tab limit in a READ that continues a non-advancing READ
Labels flang:runtime
Assignees
Reporter eugeneepshteyn
    After a non-advancing READ, a READ that continues the same record should define its left tab limit as the current position (F2023 13.8.1.2 p1), and `Tn` and `TLn` position relative to that limit (13.8.1.2 p2, p3). flang-rt positions relative to the start of the record instead, so `T1` rereads characters the earlier READ already consumed, and `TL` can move left of where the statement started.

```fortran
program lefttab
  implicit none
  integer :: iu, i
  character(2) :: a, c
  character(1) :: d
  open (newunit=iu, status='scratch', form='formatted')
  write (iu, '(a)') 'AB12XY'
  rewind (iu)
  read (iu, '(a2)', advance='no') a
  read (iu, '(i2,t1,a2)') i, c
 print '(a,i0,a,a,a)', 'T1:  i=', i, ' c="', c, '"'
  rewind (iu)
  read (iu, '(a2)', advance='no') a
  read (iu, '(tl1,a1)') d
  print '(a,a,a)', 'TL1: d="', d, '"'
end program
```

flang (main at 20b943451383):

```
T1:  i=12 c="AB"
TL1: d="B"
```

Expected, and what gfortran prints:

```
T1:  i=12 c="12"
TL1: d="1"
```

The second READ begins at column 3, so its `T1` is column 3 and its `TL1` cannot go left of column 3.

Probable cause, from reading the source (not confirmed with a patch): for a non-advancing READ, `ExternalIoStatementState<Direction::Input>::CompleteOperation` sets `leftTabLimit = furthestPositionInRecord` ([io-stmt.cpp#L414-L418](https://github.com/llvm/llvm-project/blob/24580866f5d731e4962829e7b0f298092ceacb0f/flang-rt/lib/runtime/io-stmt.cpp#L414-L418)). Formatted input does not advance `furthestPositionInRecord`, as the comment in the `ExternalIoStatementState` constructor notes ([io-stmt.cpp#L399-L406](https://github.com/llvm/llvm-project/blob/24580866f5d731e4962829e7b0f298092ceacb0f/flang-rt/lib/runtime/io-stmt.cpp#L399-L406)), so the limit stays at 0. `positionInRecord` looks like the intended value.

Found while reviewing PR 238057, which does not change this.

(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

Reply via email to