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 @@ -519,7 +519,7 @@ ConnectionState &connection{io.GetConnectionState()}; if (connection.access != Access::Stream) { io.GetIoErrorHandler().SignalError( - "REC= may not appear unless ACCESS='STREAM'"); + "POS= may not appear unless ACCESS='STREAM'"); return false; } if (pos < 1) { 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 @@ -577,7 +577,7 @@ private: bool wasExtant_; std::optional status_; - Position position_{Position::AsIs}; + std::optional position_; std::optional action_; Convert convert_{Convert::Native}; OwningPtr path_; 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 @@ -208,20 +208,30 @@ } int OpenStatementState::EndIoStatement() { + if (position_) { + if (access_ && *access_ == Access::Direct) { + SignalError("POSITION= may not be set with ACCESS='DIRECT'"); + position_.reset(); + } + } if (path_.get() || wasExtant_ || (status_ && *status_ == OpenStatus::Scratch)) { - unit().OpenUnit(status_, action_, position_, std::move(path_), pathLength_, - convert_, *this); + unit().OpenUnit(status_, action_, position_.value_or(Position::AsIs), + std::move(path_), pathLength_, convert_, *this); } else { - unit().OpenAnonymousUnit(status_, action_, position_, convert_, *this); + unit().OpenAnonymousUnit( + status_, action_, position_.value_or(Position::AsIs), convert_, *this); } if (access_) { if (*access_ != unit().access) { if (wasExtant_) { SignalError("ACCESS= may not be changed on an open unit"); + access_.reset(); } } - unit().access = *access_; + if (access_) { + unit().access = *access_; + } } if (!unit().isUnformatted) { unit().isUnformatted = isUnformatted_;