diff --git a/flang/runtime/transformational.cpp b/flang/runtime/transformational.cpp --- a/flang/runtime/transformational.cpp +++ b/flang/runtime/transformational.cpp @@ -521,7 +521,9 @@ } result.IncrementSubscripts(resultAt); mask.IncrementSubscripts(maskAt); - field.IncrementSubscripts(fieldAt); + if (field.rank() != 0) { // scalar "field" argument + field.IncrementSubscripts(fieldAt); + } } } diff --git a/flang/unittests/RuntimeGTest/Transformational.cpp b/flang/unittests/RuntimeGTest/Transformational.cpp --- a/flang/unittests/RuntimeGTest/Transformational.cpp +++ b/flang/unittests/RuntimeGTest/Transformational.cpp @@ -212,4 +212,20 @@ EXPECT_EQ(*result.ZeroBasedIndexedElement(j), expect[j]); } result.Destroy(); + + // Test for scalar value of the "field" argument + auto scalarField{MakeArray( + std::vector{}, std::vector{343})}; + RTNAME(Unpack)(result, *vector, *mask, *scalarField, __FILE__, __LINE__); + EXPECT_EQ(result.rank(), 2); + EXPECT_EQ(result.GetDimension(0).LowerBound(), 1); + EXPECT_EQ(result.GetDimension(0).Extent(), 2); + EXPECT_EQ(result.GetDimension(1).LowerBound(), 1); + EXPECT_EQ(result.GetDimension(1).Extent(), 3); + static std::int32_t scalarExpect[6]{343, 1, 2, 343, 343, 3}; + for (int j{0}; j < 6; ++j) { + EXPECT_EQ( + *result.ZeroBasedIndexedElement(j), scalarExpect[j]); + } + result.Destroy(); }