Derived type finalization can be tied to the statement context in the
case of executable construct that references a non-pointer function.
Since the final subroutine can potentially do so IO, make sure the
current IO is ended before emitting finalization of the statement
context.
There might be other IO runtime that need the same update but it will be done
with proper examples.
Unfortunately it is not possible to move this StatementContext before. It is here because IO with error recovery (IOSTAT, ERRMSG...) will create conditional IO inside fir.if (see the makeNextConditionalOn just above).
For instance:
Should now fail with: "operand #0 does not dominate this use" (we really should have had this one has a regression test, sorry about that).
The previous IR for this code looked like:
Things can even get funnier if one use IO implied do:
In this case, the allocation/deallocation happens inside an if/then/else that is inside a fir.iterate_while before FortranAioEndIoStatement.
I do not have a brilliant idea to solve this. It seems we would want to propagate the temps through the iter_while (that by the way, can be a fir.do_loop is there is no IO error recoivery) and the if/then/else.
But if you have loops with temps created at each iteration, then piling the finalization/deallocation is far from ideal (you will need an array to store the descriptors...).
Maybe there can be a clever solution in the IO runtime that would somehow allow the finalizer to call IO.