<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/108895>108895</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] write statement in subroutine specified in FINAL statement is not executed correctly
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang:runtime
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ohno-fj
      </td>
    </tr>
</table>

<pre>
    ```
Version of flang-new : 20.0.0(1cc4c87198ec0c6febca3f6ed03357e9e091cfd4)/AArch64
```

Execution error occurs because `subroutine` specified in `final-subroutine-name-list` of `FINAL` statement is not executed correctly.  
`Write` statement in the `subroutine` creates a file named `file.2` with no data.  
Then the empty file is read, resulting in a runtime error.

The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.

sngg632d_.f90:
```fortran
module m0
  type :: ty0
 integer :: ii
   contains
     FINAL::destructor
  end type ty0
  type   :: ty1
     integer :: jj
     integer :: kk
 type(ty0),allocatable :: ty0_obj
  end type ty1
contains
 subroutine destructor(dmy)
    IMPLICIT NONE
    TYPE(ty0), INTENT(INOUT):: dmy
    write(2,*)  dmy%ii
  end subroutine destructor
end module m0

program main
  use m0
  call sub
 rewind 2
  read(2,*) n
  if (n/=2) print *,701
  read(2,*,end=9) n
  if (n/=2) print *,702
9 print*, "pass"
contains
 subroutine s1(d1)
    type(ty1),INTENT(in)  :: d1
    integer :: v1, v2
    v1=d1%ty0_obj%ii
    v2=d1%jj
    v1=v1+v2
  end subroutine s1
  subroutine sub()
    type(ty0),save :: ty0_var
 ty0_var%ii=2
    call s1(ty1(10,10, ty0_var))
  end subroutine sub
end program main
```

```
$ flang-new sngg632d_.f90; ./a.out; ls -al fort.2; cat fort.2

fatal Fortran runtime error(/work/home/ohno/CT/test/fort/tp/reproducerJ/Metro/FFE_2019_generic_04/sngg632d_.f90:24): End of file during input
Aborted (core dumped)
-rw-------. 1 ohno ohno 0 Sep 10 13:39 fort.2
$
```

```
$ gfortran sngg632d_2.f90; ./a.out; ls -al fort.2; cat fort.2
 sngg632d : pass
-rw-r--r-- 1 ohno users 13 Sep 10  2024 fort.2
           2
$
```

```
$ ifort -diag-disable=10448 sngg632d_2.f90; ./a.out; ls -al fort.2; cat fort.2; cat fort.2
forrtl: No such file or directory
forrtl: severe (29): file not found, unit 2, file /home/ohno/CT/test/fort/tp/reproducerJ/Metro/FFE_2019_generic_04/fort.2
Image PC                Routine            Line Source
sngg632d_.f90.out  000000000040A08C  Unknown               Unknown Unknown
sngg632d_.f90.out  000000000040420D  Unknown               Unknown Unknown
sngg632d_.f90.out  000000000040416D  Unknown Unknown  Unknown
libc.so.6          00007C89BEA29D90  Unknown Unknown  Unknown
libc.so.6          00007C89BEA29E40  __libc_start_main Unknown  Unknown
sngg632d_.f90.out  0000000000404085  Unknown Unknown  Unknown
ls: cannot access 'fort.2': No such file or directory
cat: fort.2: No such file or directory
$
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0V1-P47gN_zTKC5FAlh3HfshDNhkXU-zNLXpzLfoUKDKdaNaRAkme6Xz7gv4XTy7b3d62RuBAFPUjKf4k0tJ7fTSIa7b8xJa7mWzCybq1PRk7r15mB1u-r1nK-x_fMb75OzqvrQFbQVVLc5wbfAMWb0DwBV9wJrJIqURlqyjPUHGVVnhQMq5SLHkcL1eYI88jVZUJEzkTxWbj1ClNOvAbW9374V-omkA20TnrwCrVOA8HVLLxCCzlvjk42wRtkKUc_AWVrjSWoA3NVtrIen7VmRt5xnmtfSBtW5FO8fi0-dwuDjLgGU0A7cHYANhaxxKUdQ5VqN8XAKO3_3A64M06A-F0xy3lUAb0IKHSNQI5UXbu1bgQpPGmwwmMhVIGORp5PmEHiOdLeO_Wag8OZcnEFhz6pg7aHMmuBNeYoM_Y7dRiuovPJ4TK1rV9I2XpsAUN6ANcnD06eSa4YkgpDf5SWRecNCBNCZoGoOz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