Index: flang/runtime/io-api.cpp =================================================================== --- flang/runtime/io-api.cpp +++ flang/runtime/io-api.cpp @@ -345,7 +345,7 @@ } else { // CLOSE(UNIT=bad unit) is just a no-op Terminator oom{sourceFile, sourceLine}; - return &New{oom}(sourceFile, sourceLine) + return &New{oom}(sourceFile, sourceLine, unitNumber) .release() ->ioStatementState(); } @@ -359,7 +359,7 @@ } else { // FLUSH(UNIT=unknown) is a no-op Terminator oom{sourceFile, sourceLine}; - return &New{oom}(sourceFile, sourceLine) + return &New{oom}(sourceFile, sourceLine, unitNumber) .release() ->ioStatementState(); } @@ -405,7 +405,7 @@ } else { // INQUIRE(UNIT=unrecognized unit) Terminator oom{sourceFile, sourceLine}; - return &New{oom}(sourceFile, sourceLine) + return &New{oom}(sourceFile, sourceLine, unitNumber) .release() ->ioStatementState(); } Index: flang/runtime/io-stmt.h =================================================================== --- flang/runtime/io-stmt.h +++ flang/runtime/io-stmt.h @@ -588,23 +588,28 @@ IoStatementState &ioStatementState() { return ioStatementState_; } MutableModes &mutableModes() { return connection_.modes; } ConnectionState &GetConnectionState() { return connection_; } + int badUnitNumber() const { return badUnitNumber_; } void CompleteOperation(); int EndIoStatement(); protected: template - NoUnitIoStatementState(const char *sourceFile, int sourceLine, A &stmt) - : IoStatementBase{sourceFile, sourceLine}, ioStatementState_{stmt} {} + NoUnitIoStatementState(A &stmt, const char *sourceFile = nullptr, + int sourceLine = 0, int badUnitNumber = -1) + : IoStatementBase{sourceFile, sourceLine}, ioStatementState_{stmt}, + badUnitNumber_{badUnitNumber} {} private: IoStatementState ioStatementState_; // points to *this ConnectionState connection_; + int badUnitNumber_; }; class NoopStatementState : public NoUnitIoStatementState { public: - NoopStatementState(const char *sourceFile, int sourceLine) - : NoUnitIoStatementState{sourceFile, sourceLine, *this} {} + NoopStatementState( + const char *sourceFile = nullptr, int sourceLine = 0, int unitNumber = -1) + : NoUnitIoStatementState{*this, sourceFile, sourceLine, unitNumber} {} void set_status(CloseStatus) {} // discards }; @@ -656,7 +661,8 @@ class InquireNoUnitState : public NoUnitIoStatementState { public: - InquireNoUnitState(const char *sourceFile = nullptr, int sourceLine = 0); + InquireNoUnitState(const char *sourceFile = nullptr, int sourceLine = 0, + int badUnitNumber = -1); bool Inquire(InquiryKeywordHash, char *, std::size_t); bool Inquire(InquiryKeywordHash, bool &); bool Inquire(InquiryKeywordHash, std::int64_t, bool &); Index: flang/runtime/io-stmt.cpp =================================================================== --- flang/runtime/io-stmt.cpp +++ flang/runtime/io-stmt.cpp @@ -1270,7 +1270,7 @@ } return true; case HashInquiryKeyword("NUMBER"): - result = unit().IsConnected() ? unit().unitNumber() : -1; + result = unit().unitNumber(); return true; case HashInquiryKeyword("POS"): result = unit().InquirePos(); @@ -1300,8 +1300,9 @@ } } -InquireNoUnitState::InquireNoUnitState(const char *sourceFile, int sourceLine) - : NoUnitIoStatementState{sourceFile, sourceLine, *this} {} +InquireNoUnitState::InquireNoUnitState( + const char *sourceFile, int sourceLine, int badUnitNumber) + : NoUnitIoStatementState{*this, sourceFile, sourceLine, badUnitNumber} {} bool InquireNoUnitState::Inquire( InquiryKeywordHash inquiry, char *result, std::size_t length) { @@ -1370,8 +1371,10 @@ bool InquireNoUnitState::Inquire( InquiryKeywordHash inquiry, std::int64_t &result) { switch (inquiry) { - case HashInquiryKeyword("NEXTREC"): case HashInquiryKeyword("NUMBER"): + result = badUnitNumber(); + return true; + case HashInquiryKeyword("NEXTREC"): case HashInquiryKeyword("POS"): case HashInquiryKeyword("RECL"): case HashInquiryKeyword("SIZE"): @@ -1385,7 +1388,7 @@ InquireUnconnectedFileState::InquireUnconnectedFileState( OwningPtr &&path, const char *sourceFile, int sourceLine) - : NoUnitIoStatementState{sourceFile, sourceLine, *this}, path_{std::move( + : NoUnitIoStatementState{*this, sourceFile, sourceLine}, path_{std::move( path)} {} bool InquireUnconnectedFileState::Inquire( @@ -1491,7 +1494,7 @@ InquireIOLengthState::InquireIOLengthState( const char *sourceFile, int sourceLine) - : NoUnitIoStatementState{sourceFile, sourceLine, *this} {} + : NoUnitIoStatementState{*this, sourceFile, sourceLine} {} bool InquireIOLengthState::Emit(const char *, std::size_t n, std::size_t) { bytes_ += n;