diff --git a/flang/runtime/io-api.cpp b/flang/runtime/io-api.cpp --- a/flang/runtime/io-api.cpp +++ b/flang/runtime/io-api.cpp @@ -172,7 +172,7 @@ *child, sourceFile, sourceLine); } else { return &child->BeginIoStatement( - iostat, sourceFile, sourceLine); + iostat, nullptr /* no unit */, sourceFile, sourceLine); } } else { if (iostat == IostatOk && unit.access == Access::Direct) { @@ -186,7 +186,7 @@ std::forward(xs)..., unit, sourceFile, sourceLine); } else { return &unit.BeginIoStatement( - iostat, sourceFile, sourceLine); + iostat, &unit, sourceFile, sourceLine); } } } @@ -228,7 +228,7 @@ *child, format, formatLength, sourceFile, sourceLine); } else { return &child->BeginIoStatement( - iostat, sourceFile, sourceLine); + iostat, nullptr /* no unit */, sourceFile, sourceLine); } } else { if (iostat == IostatOk) { @@ -239,7 +239,7 @@ unit, format, formatLength, sourceFile, sourceLine); } else { return &unit.BeginIoStatement( - iostat, sourceFile, sourceLine); + iostat, &unit, sourceFile, sourceLine); } } } @@ -280,7 +280,7 @@ *child, sourceFile, sourceLine); } else { return &child->BeginIoStatement( - iostat, sourceFile, sourceLine); + iostat, nullptr /* no unit */, sourceFile, sourceLine); } } else { if (iostat == IostatOk) { @@ -301,7 +301,7 @@ return &io; } else { return &unit.BeginIoStatement( - iostat, sourceFile, sourceLine); + iostat, &unit, sourceFile, sourceLine); } } } diff --git a/flang/runtime/io-stmt.h b/flang/runtime/io-stmt.h --- a/flang/runtime/io-stmt.h +++ b/flang/runtime/io-stmt.h @@ -711,9 +711,10 @@ class ErroneousIoStatementState : public IoStatementBase { public: - explicit ErroneousIoStatementState( - Iostat iostat, const char *sourceFile = nullptr, int sourceLine = 0) - : IoStatementBase{sourceFile, sourceLine} { + explicit ErroneousIoStatementState(Iostat iostat, + ExternalFileUnit *unit = nullptr, const char *sourceFile = nullptr, + int sourceLine = 0) + : IoStatementBase{sourceFile, sourceLine}, unit_{unit} { SetPendingError(iostat); } int EndIoStatement(); @@ -722,6 +723,7 @@ private: ConnectionState connection_; + ExternalFileUnit *unit_{nullptr}; }; extern template bool IoStatementState::EmitEncoded( diff --git a/flang/runtime/io-stmt.cpp b/flang/runtime/io-stmt.cpp --- a/flang/runtime/io-stmt.cpp +++ b/flang/runtime/io-stmt.cpp @@ -1520,6 +1520,9 @@ int ErroneousIoStatementState::EndIoStatement() { SignalPendingError(); + if (unit_) { + unit_->EndIoStatement(); + } return IoStatementBase::EndIoStatement(); }