Skip to content

[flang] Fix compile-time infinite loop #66246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,8 @@ class DeclarationVisitor : public ArraySpecVisitor,
bool OkToAddComponent(const parser::Name &, const Symbol * = nullptr);
ParamValue GetParamValue(
const parser::TypeParamValue &, common::TypeParamAttr attr);
void CheckCommonBlockDerivedType(const SourceName &, const Symbol &);
void CheckCommonBlockDerivedType(
const SourceName &, const Symbol &, UnorderedSymbolSet &);
Attrs HandleSaveName(const SourceName &, Attrs);
void AddSaveName(std::set<SourceName> &, const SourceName &);
bool HandleUnrestrictedSpecificIntrinsicFunction(const parser::Name &);
Expand Down Expand Up @@ -6096,7 +6097,8 @@ void DeclarationVisitor::CheckCommonBlocks() {
"Derived type '%s' in COMMON block must have the BIND or"
" SEQUENCE attribute"_err_en_US);
}
CheckCommonBlockDerivedType(name, typeSymbol);
UnorderedSymbolSet typeSet;
CheckCommonBlockDerivedType(name, typeSymbol, typeSet);
}
}
}
Expand All @@ -6120,8 +6122,12 @@ bool DeclarationVisitor::NameIsKnownOrIntrinsic(const parser::Name &name) {
}

// Check if this derived type can be in a COMMON block.
void DeclarationVisitor::CheckCommonBlockDerivedType(
const SourceName &name, const Symbol &typeSymbol) {
void DeclarationVisitor::CheckCommonBlockDerivedType(const SourceName &name,
const Symbol &typeSymbol, UnorderedSymbolSet &typeSet) {
if (auto iter{typeSet.find(SymbolRef{typeSymbol})}; iter != typeSet.end()) {
return;
}
typeSet.emplace(typeSymbol);
if (const auto *scope{typeSymbol.scope()}) {
for (const auto &pair : *scope) {
const Symbol &component{*pair.second};
Expand All @@ -6144,13 +6150,7 @@ void DeclarationVisitor::CheckCommonBlockDerivedType(
if (const auto *type{details->type()}) {
if (const auto *derived{type->AsDerived()}) {
const Symbol &derivedTypeSymbol{derived->typeSymbol()};
// Don't call this member function recursively if the derived type
// symbol is the same symbol that is already being processed.
// This can happen when a component is a pointer of the same type
// as its parent component, for instance.
if (derivedTypeSymbol != typeSymbol) {
CheckCommonBlockDerivedType(name, derivedTypeSymbol);
}
CheckCommonBlockDerivedType(name, derivedTypeSymbol, typeSet);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions flang/test/Semantics/equivalence01.f90
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,17 @@ real function f17b()
equivalence (dupName, y)
end function f17b
end module m17

module m18
! Regression test: don't loop when checking mutually-referencing types
type t1
sequence
type (t2), pointer :: p
end type
type t2
sequence
type (t1), pointer :: p
end type
type(t1) x
common x
end