llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[flang] Signal end of file rather than error if namelist is not found in file

Open jonathonpenix opened this issue 4 years ago • 3 comments

Example program below:

subroutine test
  integer, parameter :: read_unit=10
  integer :: a
  namelist /my_namelist/ a
  namelist /missing_namelist/ a

  open(unit=read_unit, file="namelist.txt", form="FORMATTED", status="NEW")
  write(unit=read_unit, nml=my_namelist, err=1)
  close(unit=read_unit)

  open(unit=read_unit, file="namelist.txt", form="FORMATTED", status="OLD")
  read(unit=read_unit, nml=my_namelist, err=2, end=3)

  print *, "Read of my_namelist succeeded, as expected!"
  print *, "Trying to read non-present namelist..."

  read(unit=read_unit, nml=missing_namelist, err=2, end=3)

  return

1 print *, "Error while writing!"
  return

2 print *, "Error while reading!"
  return

3 print *, "End of file when reading!"
  return
end

program p
  call test
end

gfortran and ifort both print "End of file when reading!" when trying to read missing_namelist (please see https://godbolt.org/z/eT8KTE89o) but flang prints "Error while reading!" for me.

Would it be ok to change the behavior in flang to match that of gfortran/ifort? From what I understand, I don't think there is a set requirement one way or another for this, but 521.wrf_r in SPEC2017 seems to rely on end being taken in this situation and it seems that this is what gfortran and ifort do (although I am not sure about other compilers' behavior here).

jonathonpenix avatar Sep 30 '22 23:09 jonathonpenix

@llvm/issue-subscribers-flang-runtime

llvmbot avatar Sep 30 '22 23:09 llvmbot

https://reviews.llvm.org/D135212

klausler avatar Oct 04 '22 22:10 klausler

Fix was merged.

klausler avatar Oct 06 '22 18:10 klausler