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 @@ -217,6 +217,16 @@ std::optional GetNextDataEdit( IoStatementState &, int maxRepeat = 1); + // Each NAMELIST input item is a distinct "list-directed" + // input statement. This member function resets this state + // so that repetition and null values work correctly for each + // successive NAMELIST input item. + void ResetForNextNamelistItem() { + remaining_ = 0; + isFirstItem_ = true; + realPart_ = imaginaryPart_ = false; + } + private: int remaining_{0}; // for "r*" repetition std::int64_t initialRecordNumber_; diff --git a/flang/runtime/namelist.cpp b/flang/runtime/namelist.cpp --- a/flang/runtime/namelist.cpp +++ b/flang/runtime/namelist.cpp @@ -260,6 +260,8 @@ ConnectionState &connection{io.GetConnectionState()}; connection.modes.inNamelist = true; IoErrorHandler &handler{io.GetIoErrorHandler()}; + auto *listInput{io.get_if>()}; + RUNTIME_CHECK(handler, listInput != nullptr); // Check the group header std::optional next{io.GetNextNonBlank()}; if (!next || *next != '&') { @@ -331,6 +333,7 @@ } io.HandleRelativePosition(1); // Read the values into the descriptor + listInput->ResetForNextNamelistItem(); if (!descr::DescriptorIO(io, *useDescriptor)) { return false; }