Hallo,
I have implemented a code, where i open a file in the ‘constructor’
of an extended type as unformatted file.
When I try to write to this files in another method, both gfortran (11.2 Linux X64) and
aocc 3.1.0 (based on the “traditional” flang ) cause runtime error, whereas ifort 2021.4.0
runs successful, i.e., the unformatted file is written.
A minimal testcase is
module mod_testfile
use iso_fortran_env
implicit none
type testfile
integer :: unit
contains
procedure, pass(self) :: write_and_close
end type testfile
interface testfile
module procedure testfile_open
end interface testfile
contains
type(testfile) function testfile_open(filename) result(self)
implicit none
character(len=*), intent(in) :: filename
open (newunit = self % unit, file=filename, form='unformatted')
print*, 'output_unit :', self % unit
end function testfile_open
subroutine write_and_close(self, data)
implicit none
class(testfile), intent(out) :: self
integer :: data
write(self % unit) data
close(self % unit)
end subroutine write_and_close
end module mod_testfile
program file_test
use mod_testfile
implicit none
type(testfile) :: my_test
my_test = testfile('test.bin')
call my_test % write_and_close(42 )
end program file_test
Output Gfortran 11.2:
output_unit : -10
At line 28 of file write_minimal_file.f90
Fortran runtime error: Missing format for FORMATTED data transfer
Error termination. Backtrace:
#0 0x7f9bf9b34d5a
#1 0x7f9bf9b35869
#2 0x7f9bf9b3654f
#3 0x7f9bf9d7d1a0
#4 0x4012d9
#5 0x4014b0
#6 0x4014e7
#7 0x7f9bf97930b2
#8 0x4010ed
output aocc-3.1.0:
output_unit : -13
FIO-F-215/unformatted write/unit=0/formatted/unformatted file conflict.
File name = 'stderr ', formatted, sequential access record = 0
In source file write_minimal_file.f90, at line number 28
output Ifort:
output_unit : -129
The problem disappears when the logic for opening and writing the file
are put in the same subroutine.
I am not sure, whether my code causes undefined behavior, or whether the problem
is related to the compilers.
In the latter case I will a bug report to the gfortran mailing list.
Maybe further testcases are required to isolate the problem (e.g.-, non-OO code).
Cheers,
Johann