diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp index 9dd287e9d2495..b21e3cd757d6b 100644 --- a/flang/lib/Semantics/check-coarray.cpp +++ b/flang/lib/Semantics/check-coarray.cpp @@ -194,7 +194,6 @@ void CoarrayChecker::Leave(const parser::SyncAllStmt &x) { void CoarrayChecker::Leave(const parser::SyncImagesStmt &x) { CheckSyncStatList(context_, std::get>(x.t)); - const auto &imageSet{std::get(x.t)}; if (const auto *intExpr{std::get_if(&imageSet.u)}) { if (const auto *expr{GetExpr(context_, *intExpr)}) { @@ -202,6 +201,26 @@ void CoarrayChecker::Leave(const parser::SyncImagesStmt &x) { context_.Say(parser::FindSourceLocation(imageSet), // C1174 "An image-set that is an int-expr must be a scalar or a rank-one array"_err_en_US); } + if (const auto *someInt{ + std::get_if>(&expr->u)}; + someInt && evaluate::IsActuallyConstant(*someInt)) { + auto converted{evaluate::Fold(context_.foldingContext(), + evaluate::ConvertToType( + common::Clone(*someInt)))}; + if (const auto *cst{ + evaluate::UnwrapConstantValue( + converted)}) { + for (auto elt : cst->values()) { + auto n{elt.ToInt64()}; + if (n < 1) { + context_.Say(parser::FindSourceLocation(imageSet), + "Image number %jd in the image-set is not valid"_err_en_US, + std::intmax_t{n}); + break; + } + } + } + } } } } diff --git a/flang/test/Semantics/sync-images.f90 b/flang/test/Semantics/sync-images.f90 new file mode 100644 index 0000000000000..637fa263354d0 --- /dev/null +++ b/flang/test/Semantics/sync-images.f90 @@ -0,0 +1,12 @@ +!RUN: %python %S/test_errors.py %s %flang_fc1 +integer twod(1,1) +sync images (*) ! ok +!ERROR: An image-set that is an int-expr must be a scalar or a rank-one array +sync images (twod) +!ERROR: Must have INTEGER type, but is REAL(4) +sync images (3.14159) +!ERROR: Image number -1 in the image-set is not valid +sync images (-1) +!ERROR: Image number -1 in the image-set is not valid +sync images ([2, -1, 3]) +end