diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp index 37207e6fde2f..c5ca4126f6be 100644 --- a/flang/lib/Semantics/expression.cpp +++ b/flang/lib/Semantics/expression.cpp @@ -1980,20 +1980,26 @@ const Symbol &ExpressionAnalyzer::AccessSpecific( } else if (const auto *used{ originalGeneric.detailsIf()}) { const auto &scope{originalGeneric.owner()}; - auto iter{scope.find(specific.name())}; - if (iter != scope.end() && iter->second->has() && - &iter->second->get().symbol() == &specific) { - return specific; - } else { - // Create a renaming USE of the specific procedure. - auto rename{context_.SaveTempName( - used->symbol().owner().GetName().value().ToString() + "$" + - specific.name().ToString())}; - return *const_cast(scope) - .try_emplace(rename, specific.attrs(), - semantics::UseDetails{rename, specific}) - .first->second; + if (auto iter{scope.find(specific.name())}; iter != scope.end()) { + if (const auto *useDetails{ + iter->second->detailsIf()}) { + const Symbol &usedSymbol{useDetails->symbol()}; + const auto *usedGeneric{ + usedSymbol.detailsIf()}; + if (&usedSymbol == &specific || + (usedGeneric && usedGeneric->specific() == &specific)) { + return specific; + } + } } + // Create a renaming USE of the specific procedure. + auto rename{context_.SaveTempName( + used->symbol().owner().GetName().value().ToString() + "$" + + specific.name().ToString())}; + return *const_cast(scope) + .try_emplace(rename, specific.attrs(), + semantics::UseDetails{rename, specific}) + .first->second; } else { return specific; }