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

    <tr>
        <th>Summary</th>
        <td>
            [flang] The presence of `iostat=` specifier doesn't stop Flang runtime from crashing.
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          DanielCChen
      </td>
    </tr>
</table>

<pre>
    Consider the following code:
```
module m
    type base
        contains

 procedure :: writeBaseFmt
        generic :: write (formatted) => writeBaseFmt
    end type

    contains

    subroutine writeBaseFmt (dtv, unit, iotype, v_list, iostat, iomsg)
        class(base), intent(in) :: dtv
        integer, intent(in) :: unit
        character(*), intent(in) :: iotype
        integer, intent(in) :: v_list(:)
        integer, intent(out) :: iostat
        character(*), intent(inout) :: iomsg

 character(:), allocatable :: decMode

        allocate (character(20) :: decMode)
        inquire(unit, decimal=decMode, iostat=iostat, iomsg=iomsg) !! Should not crash with the same runtime error.

print*, iostat
print*, iomsg
        if (iostat == 0)  error stop 1

    end subroutine
end module

program dcmlChildWrite003
use m
    character(3) :: string1
 type(base) :: b1

    string1 = 'abc'

    write (string1, '(dc, DT)') b1

end
```

Flang failed at runtime with
```
> a.out
 256
 INQUIRE of unit created for defined derived type I/O of an internal unit


fatal Fortran runtime error(t.f:40): INQUIRE of unit created for defined derived type I/O of an internal unit
IOT/Abort trap(coredump)
```

While the `iostat=` specifier caught the runtime error that the test case intended to test, the runtime still crashes with the exact same error.

The expected behavior should be `error stop 1`.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVU2vozYU_TXO5moix7wQssiCSSbSW7Sjtq-aZWXsC7gyNrVNXuffV7ZJQtKZSiMVIQHmfp57js29V51BPJDtR7I9rfgUeusOJ24U6uOxR7NqrPx6OFrjlUQHoUdordb2XZkOhJVIiprQmpR0vmk9WDl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