@@ -1183,13 +1183,23 @@ void AccAttributeVisitor::CheckAssociatedLoopIndex(
11831183 }
11841184
11851185 const auto getNextDoConstruct =
1186- [this ](const parser::Block &block) -> const parser::DoConstruct * {
1186+ [this ](const parser::Block &block,
1187+ std::int64_t &level) -> const parser::DoConstruct * {
11871188 for (const auto &entry : block) {
11881189 if (const auto *doConstruct = GetDoConstructIf (entry)) {
11891190 return doConstruct;
11901191 } else if (parser::Unwrap<parser::CompilerDirective>(entry)) {
11911192 // It is allowed to have a compiler directive associated with the loop.
11921193 continue ;
1194+ } else if (const auto &accLoop{
1195+ parser::Unwrap<parser::OpenACCLoopConstruct>(entry)}) {
1196+ if (level == 0 )
1197+ break ;
1198+ const auto &beginDir{
1199+ std::get<parser::AccBeginLoopDirective>(accLoop->t )};
1200+ context_.Say (beginDir.source ,
1201+ " LOOP directive not expected in COLLAPSE loop nest" _err_en_US);
1202+ level = 0 ;
11931203 } else {
11941204 break ;
11951205 }
@@ -1198,11 +1208,12 @@ void AccAttributeVisitor::CheckAssociatedLoopIndex(
11981208 };
11991209
12001210 const auto &outer{std::get<std::optional<parser::DoConstruct>>(x.t )};
1201- for (const parser::DoConstruct *loop{&*outer}; loop && level > 0 ; --level ) {
1211+ for (const parser::DoConstruct *loop{&*outer}; loop && level > 0 ;) {
12021212 // Go through all nested loops to ensure index variable exists.
12031213 GetLoopIndex (*loop);
12041214 const auto &block{std::get<parser::Block>(loop->t )};
1205- loop = getNextDoConstruct (block);
1215+ --level;
1216+ loop = getNextDoConstruct (block, level);
12061217 }
12071218 CHECK (level == 0 );
12081219}
0 commit comments