diff --git a/flang/runtime/descriptor-io.h b/flang/runtime/descriptor-io.h index 840d73b8e857cfe..e20f31e9b443134 100644 --- a/flang/runtime/descriptor-io.h +++ b/flang/runtime/descriptor-io.h @@ -288,7 +288,11 @@ static bool DefaultComponentwiseIO(IoStatementState &io, *compArray.Element(at)}; if (!DefaultComponentIO( io, component, descriptor, subscripts, handler, table)) { - return false; + // Truncated nonempty namelist input sequence? + auto *listInput{ + io.get_if>()}; + return DIR == Direction::Input && (j > 0 || k > 0) && listInput && + listInput->inNamelistSequence(); } } } diff --git a/flang/runtime/io-stmt.h b/flang/runtime/io-stmt.h index fa432d07a680deb..d4ceb83265246bd 100644 --- a/flang/runtime/io-stmt.h +++ b/flang/runtime/io-stmt.h @@ -295,8 +295,7 @@ template <> class ListDirectedStatementState : public FormattedIoStatementState { public: - bool inNamelistArray() const { return inNamelistArray_; } - void set_inNamelistArray(bool yes = true) { inNamelistArray_ = yes; } + bool inNamelistSequence() const { return inNamelistSequence_; } // Skips value separators, handles repetition and null values. // Vacant when '/' appears; present with descriptor == ListDirectedNullValue @@ -308,11 +307,11 @@ class ListDirectedStatementState // input statement. This member function resets some state so that // repetition and null values work correctly for each successive // NAMELIST input item. - void ResetForNextNamelistItem(bool inNamelistArray) { + void ResetForNextNamelistItem(bool inNamelistSequence) { remaining_ = 0; eatComma_ = false; realPart_ = imaginaryPart_ = false; - inNamelistArray_ = inNamelistArray; + inNamelistSequence_ = inNamelistSequence; } private: @@ -322,7 +321,7 @@ class ListDirectedStatementState bool hitSlash_{false}; // once '/' is seen, nullify further items bool realPart_{false}; bool imaginaryPart_{false}; - bool inNamelistArray_{false}; + bool inNamelistSequence_{false}; }; template diff --git a/flang/runtime/namelist.cpp b/flang/runtime/namelist.cpp index 1b3207ef2f93212..61815a7cc8a4032 100644 --- a/flang/runtime/namelist.cpp +++ b/flang/runtime/namelist.cpp @@ -522,15 +522,18 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) { } io.HandleRelativePosition(byteCount); // Read the values into the descriptor. An array can be short. - listInput->ResetForNextNamelistItem(useDescriptor->rank() > 0); if (const auto *addendum{useDescriptor->Addendum()}; addendum && addendum->derivedType()) { const NonTbpDefinedIoTable *table{group.nonTbpDefinedIo}; + listInput->ResetForNextNamelistItem(/*inNamelistSequence=*/true); if (!IONAME(InputDerivedType)(cookie, *useDescriptor, table)) { return false; } - } else if (!descr::DescriptorIO(io, *useDescriptor)) { - return false; + } else { + listInput->ResetForNextNamelistItem(useDescriptor->rank() > 0); + if (!descr::DescriptorIO(io, *useDescriptor)) { + return false; + } } next = io.GetNextNonBlank(byteCount); if (next && *next == comma) { @@ -549,7 +552,7 @@ bool IONAME(InputNamelist)(Cookie cookie, const NamelistGroup &group) { bool IsNamelistNameOrSlash(IoStatementState &io) { if (auto *listInput{ io.get_if>()}) { - if (listInput->inNamelistArray()) { + if (listInput->inNamelistSequence()) { SavedPosition savedPosition{io}; std::size_t byteCount{0}; if (auto ch{io.GetNextNonBlank(byteCount)}) {