diff --git a/flang/include/flang/Parser/message.h b/flang/include/flang/Parser/message.h index 224263e4be860..7c639eff1eeef 100644 --- a/flang/include/flang/Parser/message.h +++ b/flang/include/flang/Parser/message.h @@ -307,9 +307,9 @@ class Message : public common::ReferenceCounted { bool Merge(const Message &); bool operator==(const Message &that) const; bool operator!=(const Message &that) const { return !(*this == that); } + bool AtSameLocation(const Message &) const; private: - bool AtSameLocation(const Message &) const; std::variant location_; std::variant text_; diff --git a/flang/lib/Parser/message.cpp b/flang/lib/Parser/message.cpp index 2c4f930c0b088..cfcd08b0861ef 100644 --- a/flang/lib/Parser/message.cpp +++ b/flang/lib/Parser/message.cpp @@ -477,15 +477,31 @@ void Messages::Emit(llvm::raw_ostream &o, const AllCookedSources &allCooked, } std::stable_sort(sorted.begin(), sorted.end(), [](const Message *x, const Message *y) { return x->SortBefore(*y); }); - const Message *lastMsg{nullptr}; + std::vector msgsWithLastLocation; std::size_t errorsEmitted{0}; for (const Message *msg : sorted) { - if (lastMsg && *msg == *lastMsg) { - // Don't emit two identical messages for the same location + bool shouldSkipMsg{false}; + // Don't emit two identical messages for the same location. + // At the same location, messages are sorted by the order they were + // added to the Messages buffer, which is a decent proxy for the + // causality of the messages. + if (!msgsWithLastLocation.empty()) { + if (msgsWithLastLocation[0]->AtSameLocation(*msg)) { + for (const Message *msgAtThisLocation : msgsWithLastLocation) { + if (*msg == *msgAtThisLocation) { + shouldSkipMsg = true; // continue loop over sorted messages + break; + } + } + } else { + msgsWithLastLocation.clear(); + } + } + if (shouldSkipMsg) { continue; } + msgsWithLastLocation.push_back(msg); msg->Emit(o, allCooked, echoSourceLines, hintFlagPtr); - lastMsg = msg; if (warningsAreErrors || msg->IsFatal()) { ++errorsEmitted; } diff --git a/flang/test/Semantics/associated.f90 b/flang/test/Semantics/associated.f90 index 7cb6c240db226..731f2d828995f 100644 --- a/flang/test/Semantics/associated.f90 +++ b/flang/test/Semantics/associated.f90 @@ -253,8 +253,6 @@ subroutine test(assumedRank) lvar = associated(intPointerVar1, targetIntCoarray[1]) !ERROR: 'neverdeclared' is not a procedure !ERROR: Could not characterize intrinsic function actual argument 'badpointer' - !ERROR: 'neverdeclared' is not a procedure - !ERROR: Could not characterize intrinsic function actual argument 'badpointer' lvar = associated(badPointer) end subroutine test end subroutine assoc